4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
29 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
30 /* If this container is empty, we’re done */
31 if (container->currently_focused == NULL)
34 /* Get the previous/next client or wrap around */
35 Client *candidate = NULL;
36 if (direction == D_UP) {
37 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
38 candidate = CIRCLEQ_LAST(&(container->clients));
40 else if (direction == D_DOWN) {
41 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
42 candidate = CIRCLEQ_FIRST(&(container->clients));
43 } else LOG("Direction not implemented!\n");
45 /* If we could not switch, the container contains exactly one client. We return false */
46 if (candidate == container->currently_focused)
50 set_focus(conn, candidate, true);
55 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
57 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
58 LOG("focusing direction %d\n", direction);
60 int new_row = current_row,
61 new_col = current_col;
63 Container *container = CUR_CELL;
64 Workspace *t_ws = c_ws;
66 /* There always is a container. If not, current_col or current_row is wrong */
67 assert(container != NULL);
69 if (container->workspace->fullscreen_client != NULL) {
70 LOG("You're in fullscreen mode. Won't switch focus\n");
74 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
75 if (direction == D_UP || direction == D_DOWN) {
76 if (thing == THING_WINDOW)
77 /* Let’s see if we can perform up/down focus in the current container */
78 if (focus_window_in_container(conn, container, direction))
81 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
82 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
83 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
84 /* Set new_row as a sane default, but it may get overwritten in a second */
87 /* Search from the top to correctly handle rowspanned containers */
88 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
89 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
96 /* Let’s see if there is a screen down/up there to which we can switch */
97 LOG("container is at %d with height %d\n", container->y, container->height);
99 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
100 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
101 LOG("Wrapping screen around vertically\n");
102 /* No screen found? Then wrap */
103 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
105 t_ws = &(workspaces[screen->current_workspace]);
106 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
109 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
110 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
111 LOG("Cell empty, checking for colspanned client above...\n");
112 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
113 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
119 LOG("Fixed it to new col %d\n", new_col);
121 } else if (direction == D_LEFT || direction == D_RIGHT) {
122 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
123 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
124 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
125 /* Set new_col as a sane default, but it may get overwritten in a second */
128 /* Search from the left to correctly handle colspanned containers */
129 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
130 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
137 /* Let’s see if there is a screen left/right here to which we can switch */
138 LOG("container is at %d with width %d\n", container->x, container->width);
140 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
141 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
142 LOG("Wrapping screen around horizontally\n");
143 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
145 t_ws = &(workspaces[screen->current_workspace]);
146 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
149 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
150 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
151 LOG("Cell empty, checking for rowspanned client above...\n");
152 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
153 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
159 LOG("Fixed it to new row %d\n", new_row);
162 LOG("direction unhandled\n");
166 /* Bounds checking */
167 if (new_col >= t_ws->cols)
168 new_col = (t_ws->cols - 1);
169 if (new_row >= t_ws->rows)
170 new_row = (t_ws->rows - 1);
172 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
173 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
177 * Tries to move the window inside its current container.
179 * Returns true if the window could be moved, false otherwise.
182 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
183 direction_t direction) {
184 assert(client->container != NULL);
186 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
187 CIRCLEQ_NEXT(client, clients));
189 if (other == CIRCLEQ_END(&(client->container->clients)))
192 LOG("i can do that\n");
193 /* We can move the client inside its current container */
194 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
195 if (direction == D_UP)
196 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
197 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
203 * Moves the current window or whole container to the given direction, creating a column/row if
207 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
208 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
209 (direction == D_LEFT ? "left" : "right"))));
210 /* Get current window */
211 Container *container = CUR_CELL,
214 /* There has to be a container, see focus_window() */
215 assert(container != NULL);
217 /* If there is no window or the dock window is focused, we’re done */
218 if (container->currently_focused == NULL ||
219 container->currently_focused->dock)
222 /* As soon as the client is moved away, the last focused client in the old
223 * container needs to get focus, if any. Therefore, we save it here. */
224 Client *current_client = container->currently_focused;
225 Client *to_focus = get_last_focused_client(conn, container, current_client);
227 if (to_focus == NULL) {
228 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
229 if (to_focus == NULL)
230 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
235 /* If we’re at the left-most position, move the rest of the table right */
236 if (current_col == 0) {
237 expand_table_cols_at_head(c_ws);
240 new = CUR_TABLE[--current_col][current_row];
243 if (current_col == (c_ws->cols-1))
244 expand_table_cols(c_ws);
246 new = CUR_TABLE[++current_col][current_row];
249 if (move_current_window_in_container(conn, current_client, D_UP))
252 /* if we’re at the up-most position, move the rest of the table down */
253 if (current_row == 0) {
254 expand_table_rows_at_head(c_ws);
257 new = CUR_TABLE[current_col][--current_row];
260 if (move_current_window_in_container(conn, current_client, D_DOWN))
263 if (current_row == (c_ws->rows-1))
264 expand_table_rows(c_ws);
266 new = CUR_TABLE[current_col][++current_row];
268 /* To make static analyzers happy: */
273 /* Remove it from the old container and put it into the new one */
274 client_remove_from_container(conn, current_client, container, true);
276 if (new->currently_focused != NULL)
277 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
278 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
279 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
281 /* Update data structures */
282 current_client->container = new;
283 current_client->workspace = new->workspace;
284 container->currently_focused = to_focus;
285 new->currently_focused = current_client;
287 Workspace *workspace = container->workspace;
289 /* delete all empty columns/rows */
290 cleanup_table(conn, workspace);
292 /* Fix colspan/rowspan if it’d overlap */
293 fix_colrowspan(conn, workspace);
295 render_workspace(conn, workspace->screen, workspace);
298 set_focus(conn, current_client, true);
301 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
302 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
303 (direction == D_LEFT ? "left" : "right"))));
304 /* Get current window */
305 Container *container = CUR_CELL,
308 Container **old = &CUR_CELL;
310 /* There has to be a container, see focus_window() */
311 assert(container != NULL);
315 /* If we’re at the left-most position, move the rest of the table right */
316 if (current_col == 0) {
317 expand_table_cols_at_head(c_ws);
319 old = &CUR_TABLE[current_col+1][current_row];
321 new = CUR_TABLE[--current_col][current_row];
324 if (current_col == (c_ws->cols-1))
325 expand_table_cols(c_ws);
327 new = CUR_TABLE[++current_col][current_row];
330 /* if we’re at the up-most position, move the rest of the table down */
331 if (current_row == 0) {
332 expand_table_rows_at_head(c_ws);
334 old = &CUR_TABLE[current_col][current_row+1];
336 new = CUR_TABLE[current_col][--current_row];
339 if (current_row == (c_ws->rows-1))
340 expand_table_rows(c_ws);
342 new = CUR_TABLE[current_col][++current_row];
344 /* To make static analyzers happy: */
349 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
351 /* Swap the containers */
356 new->col = container->col;
357 new->row = container->row;
359 CUR_CELL = container;
360 container->col = col;
361 container->row = row;
363 Workspace *workspace = container->workspace;
365 /* delete all empty columns/rows */
366 cleanup_table(conn, workspace);
368 /* Fix colspan/rowspan if it’d overlap */
369 fix_colrowspan(conn, workspace);
375 * "Snaps" the current container (not possible for windows, because it works at table base)
376 * to the given direction, that is, adjusts cellspan/rowspan
379 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
380 LOG("snapping container to direction %d\n", direction);
382 Container *container = CUR_CELL;
384 assert(container != NULL);
388 /* Snap to the left is actually a move to the left and then a snap right */
389 if (!cell_exists(container->col - 1, container->row) ||
390 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
391 LOG("cannot snap to left - the cell is already used\n");
395 move_current_window(conn, D_LEFT);
396 snap_current_container(conn, D_RIGHT);
399 /* Check if the cell is used */
400 int new_col = container->col + container->colspan;
401 for (int i = 0; i < container->rowspan; i++)
402 if (!cell_exists(new_col, container->row + i) ||
403 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
404 LOG("cannot snap to right - the cell is already used\n");
408 /* Check if there are other cells with rowspan, which are in our way.
409 * If so, reduce their rowspan. */
410 for (int i = container->row-1; i >= 0; i--) {
411 LOG("we got cell %d, %d with rowspan %d\n",
412 new_col, i, CUR_TABLE[new_col][i]->rowspan);
413 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
414 CUR_TABLE[new_col][i]->rowspan--;
415 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
418 container->colspan++;
422 if (!cell_exists(container->col, container->row - 1) ||
423 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
424 LOG("cannot snap to top - the cell is already used\n");
428 move_current_window(conn, D_UP);
429 snap_current_container(conn, D_DOWN);
432 LOG("snapping down\n");
433 int new_row = container->row + container->rowspan;
434 for (int i = 0; i < container->colspan; i++)
435 if (!cell_exists(container->col + i, new_row) ||
436 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
437 LOG("cannot snap down - the cell is already used\n");
441 for (int i = container->col-1; i >= 0; i--) {
442 LOG("we got cell %d, %d with colspan %d\n",
443 i, new_row, CUR_TABLE[i][new_row]->colspan);
444 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
445 CUR_TABLE[i][new_row]->colspan--;
446 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
450 container->rowspan++;
453 /* To make static analyzers happy: */
461 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
462 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
463 Workspace *t_ws = &(workspaces[workspace-1]),
464 *old_ws = client->workspace;
466 LOG("moving floating\n");
468 if (t_ws->screen == NULL) {
469 LOG("initializing new workspace, setting num to %d\n", workspace-1);
470 t_ws->screen = c_ws->screen;
471 /* Copy the dimensions from the virtual screen */
472 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
474 /* Check if there is already a fullscreen client on the destination workspace and
475 * stop moving if so. */
476 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
477 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
482 floating_assign_to_workspace(client, t_ws);
484 /* If we’re moving it to an invisible screen, we need to unmap it */
485 if (t_ws->screen->current_workspace != t_ws->num) {
486 LOG("This workspace is not visible, unmapping\n");
487 xcb_unmap_window(conn, client->frame);
489 /* If this is not the case, we move the window to a workspace
490 * which is on another screen, so we also need to adjust its
492 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
493 uint32_t relative_x = client->rect.x - old_ws->rect.x,
494 relative_y = client->rect.y - old_ws->rect.y;
495 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
496 client->rect.x = t_ws->rect.x + relative_x;
497 client->rect.y = t_ws->rect.y + relative_y;
498 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
499 reposition_client(conn, client);
507 set_focus(conn, client, true);
511 * Moves the currently selected window to the given workspace
514 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
515 LOG("Moving current window to workspace %d\n", workspace);
517 Container *container = CUR_CELL;
519 assert(container != NULL);
521 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
522 Workspace *t_ws = &(workspaces[workspace-1]);
524 Client *current_client = container->currently_focused;
525 if (current_client == NULL) {
526 LOG("No currently focused client in current container.\n");
529 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
530 if (to_focus == NULL)
531 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
533 if (t_ws->screen == NULL) {
534 LOG("initializing new workspace, setting num to %d\n", workspace-1);
535 t_ws->screen = container->workspace->screen;
536 /* Copy the dimensions from the virtual screen */
537 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
539 /* Check if there is already a fullscreen client on the destination workspace and
540 * stop moving if so. */
541 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
542 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
547 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
549 assert(to_container != NULL);
551 client_remove_from_container(conn, current_client, container, true);
552 if (container->workspace->fullscreen_client == current_client)
553 container->workspace->fullscreen_client = NULL;
555 /* TODO: insert it to the correct position */
556 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
558 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
559 if (current_client->fullscreen)
560 t_ws->fullscreen_client = current_client;
563 current_client->container = to_container;
564 current_client->workspace = to_container->workspace;
565 container->currently_focused = to_focus;
566 to_container->currently_focused = current_client;
568 /* If we’re moving it to an invisible screen, we need to unmap it */
569 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
570 LOG("This workspace is not visible, unmapping\n");
571 xcb_unmap_window(conn, current_client->frame);
574 /* delete all empty columns/rows */
575 cleanup_table(conn, container->workspace);
581 * Switches to the given workspace
584 void show_workspace(xcb_connection_t *conn, int workspace) {
586 bool need_warp = false;
587 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
588 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
589 Workspace *t_ws = &(workspaces[workspace-1]);
591 LOG("show_workspace(%d)\n", workspace);
593 /* Store current_row/current_col */
594 c_ws->current_row = current_row;
595 c_ws->current_col = current_col;
597 /* Check if the workspace has not been used yet */
598 if (t_ws->screen == NULL) {
599 LOG("initializing new workspace, setting num to %d\n", workspace);
600 t_ws->screen = c_ws->screen;
601 /* Copy the dimensions from the virtual screen */
602 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
605 if (c_ws->screen != t_ws->screen) {
606 /* We need to switch to the other screen first */
607 LOG("moving over to other screen.\n");
609 /* Store the old client */
610 Client *old_client = CUR_CELL->currently_focused;
612 c_ws = &(workspaces[t_ws->screen->current_workspace]);
613 current_col = c_ws->current_col;
614 current_row = c_ws->current_row;
615 if (CUR_CELL->currently_focused != NULL)
618 Rect *dims = &(c_ws->screen->rect);
619 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
620 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
623 /* Re-decorate the old client, it’s not focused anymore */
624 if ((old_client != NULL) && !old_client->dock)
625 redecorate_window(conn, old_client);
626 else xcb_flush(conn);
629 /* Check if we need to change something or if we’re already there */
630 if (c_ws->screen->current_workspace == (workspace-1)) {
631 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
632 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
633 set_focus(conn, last_focused, true);
635 client_warp_pointer_into(conn, last_focused);
643 t_ws->screen->current_workspace = workspace-1;
644 Workspace *old_workspace = c_ws;
645 c_ws = &workspaces[workspace-1];
647 /* Unmap all clients of the old workspace */
648 unmap_workspace(conn, old_workspace);
650 current_row = c_ws->current_row;
651 current_col = c_ws->current_col;
652 LOG("new current row = %d, current col = %d\n", current_row, current_col);
654 ignore_enter_notify_forall(conn, c_ws, true);
656 /* Map all clients on the new workspace */
658 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
659 xcb_map_window(conn, client->frame);
661 /* Map all floating clients */
662 if (!c_ws->floating_hidden)
663 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
664 xcb_map_window(conn, client->frame);
666 /* Map all stack windows, if any */
667 struct Stack_Window *stack_win;
668 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
669 if (stack_win->container->workspace == c_ws)
670 xcb_map_window(conn, stack_win->window);
672 ignore_enter_notify_forall(conn, c_ws, false);
674 /* Restore focus on the new workspace */
675 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
676 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
677 set_focus(conn, last_focused, true);
679 client_warp_pointer_into(conn, last_focused);
682 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
688 * Jumps to the given window class / title.
689 * Title is matched using strstr, that is, matches if it appears anywhere
690 * in the string. Regular expressions seem to be a bit overkill here. However,
691 * if we need them for something else somewhen, we may introduce them here, too.
694 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
698 /* The first character is a quote, this was checked before */
699 classtitle = sstrdup(arguments+1);
700 /* The last character is a quote, we just set it to NULL */
701 classtitle[strlen(classtitle)-1] = '\0';
703 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
705 LOG("No matching client found.\n");
710 set_focus(conn, client, true);
714 * Jump directly to the specified workspace, row and col.
715 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
718 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
722 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
723 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
725 /* No match? Either no arguments were specified, or no numbers */
727 LOG("At least one valid argument required\n");
731 /* Move to the target workspace */
732 show_workspace(conn, ws);
737 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
739 /* Move to row/col */
740 if (row >= c_ws->rows)
741 row = c_ws->rows - 1;
742 if (col >= c_ws->cols)
743 col = c_ws->cols - 1;
745 LOG("Jumping to col %d, row %d\n", col, row);
746 if (c_ws->table[col][row]->currently_focused != NULL)
747 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
751 * Travels the focus stack by the given number of times (or once, if no argument
752 * was specified). That is, selects the window you were in before you focused
753 * the current window.
755 * The special values 'floating' (select the next floating window), 'tiling'
756 * (select the next tiling window), 'ft' (if the current window is floating,
757 * select the next tiling window and vice-versa) are also valid
760 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
761 /* Start count at -1 to always skip the first element */
762 int times, count = -1;
764 bool floating_criteria;
766 /* Either it’s one of the special values… */
767 if (strcasecmp(arguments, "floating") == 0) {
768 floating_criteria = true;
769 } else if (strcasecmp(arguments, "tiling") == 0) {
770 floating_criteria = false;
771 } else if (strcasecmp(arguments, "ft") == 0) {
772 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
773 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
774 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
778 floating_criteria = !client_is_floating(last_focused);
780 /* …or a number was specified */
781 if (sscanf(arguments, "%u", ×) != 1) {
782 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
786 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
787 if (++count < times) {
793 set_focus(conn, current, true);
799 /* Select the next client matching the criteria parsed above */
800 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
801 if (client_is_floating(current) == floating_criteria) {
802 set_focus(conn, current, true);
808 * Goes through the list of arguments (for exec()) and checks if the given argument
809 * is present. If not, it copies the arguments (because we cannot realloc it) and
810 * appends the given argument.
813 static char **append_argument(char **original, char *argument) {
815 for (num_args = 0; original[num_args] != NULL; num_args++) {
816 LOG("original argument: \"%s\"\n", original[num_args]);
817 /* If the argument is already present we return the original pointer */
818 if (strcmp(original[num_args], argument) == 0)
821 /* Copy the original array */
822 char **result = smalloc((num_args+2) * sizeof(char*));
823 memcpy(result, original, num_args * sizeof(char*));
824 result[num_args] = argument;
825 result[num_args+1] = NULL;
831 * Parses a command, see file CMDMODE for more information
834 void parse_command(xcb_connection_t *conn, const char *command) {
835 LOG("--- parsing command \"%s\" ---\n", command);
836 /* Get the first client from focus stack because floating clients are not
837 * in any container, therefore CUR_CELL is not appropriate. */
838 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
839 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
842 /* Hmm, just to be sure */
843 if (command[0] == '\0')
846 /* Is it an <exec>? Then execute the given command. */
847 if (STARTS_WITH(command, "exec ")) {
848 LOG("starting \"%s\"\n", command + strlen("exec "));
849 start_application(command+strlen("exec "));
853 /* Is it an <exit>? */
854 if (STARTS_WITH(command, "exit")) {
855 LOG("User issued exit-command, exiting without error.\n");
859 /* Is it <restart>? Then restart in place. */
860 if (STARTS_WITH(command, "restart")) {
861 LOG("restarting \"%s\"...\n", start_argv[0]);
862 /* make sure -a is in the argument list or append it */
863 start_argv = append_argument(start_argv, "-a");
865 execvp(start_argv[0], start_argv);
869 if (STARTS_WITH(command, "kill")) {
870 if (last_focused == NULL) {
871 LOG("There is no window to kill\n");
875 LOG("Killing current window\n");
876 client_kill(conn, last_focused);
880 /* Is it a jump to a specified workspace, row, col? */
881 if (STARTS_WITH(command, "jump ")) {
882 const char *arguments = command + strlen("jump ");
883 if (arguments[0] == '"')
884 jump_to_window(conn, arguments);
885 else jump_to_container(conn, arguments);
889 /* Should we travel the focus stack? */
890 if (STARTS_WITH(command, "focus")) {
891 const char *arguments = command + strlen("focus ");
892 travel_focus_stack(conn, arguments);
896 /* Is it 'f' for fullscreen? */
897 if (command[0] == 'f') {
898 if (last_focused == NULL)
900 client_toggle_fullscreen(conn, last_focused);
904 /* Is it just 's' for stacking or 'd' for default? */
905 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
906 if (last_focused == NULL || client_is_floating(last_focused)) {
907 LOG("not switching, this is a floating client\n");
910 LOG("Switching mode for current container\n");
911 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
915 if (command[0] == 'H') {
916 LOG("Hiding all floating windows\n");
917 floating_toggle_hide(conn, c_ws);
921 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
923 /* Is it a <with>? */
924 if (command[0] == 'w') {
926 /* TODO: implement */
927 if (command[0] == 'c') {
928 with = WITH_CONTAINER;
930 } else if (command[0] == 'w') {
931 with = WITH_WORKSPACE;
934 LOG("not yet implemented.\n");
939 /* Is it 't' for toggle tiling/floating? */
940 if (command[0] == 't') {
941 if (with == WITH_WORKSPACE) {
942 c_ws->auto_float = !c_ws->auto_float;
943 LOG("autofloat is now %d\n", c_ws->auto_float);
946 if (last_focused == NULL) {
947 LOG("Cannot toggle tiling/floating: workspace empty\n");
951 toggle_floating_mode(conn, last_focused, false);
952 /* delete all empty columns/rows */
953 cleanup_table(conn, last_focused->workspace);
955 /* Fix colspan/rowspan if it’d overlap */
956 fix_colrowspan(conn, last_focused->workspace);
958 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
963 /* It’s a normal <cmd> */
965 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
966 direction_t direction;
967 int times = strtol(command, &rest, 10);
969 LOG("Invalid command (\"%s\")\n", command);
974 /* No rest? This was a workspace number, not a times specification */
975 show_workspace(conn, times);
979 if (*rest == 'm' || *rest == 's') {
980 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
984 int workspace = strtol(rest, &rest, 10);
987 LOG("Invalid command (\"%s\")\n", command);
992 if (last_focused != NULL && client_is_floating(last_focused))
993 move_floating_window_to_workspace(conn, last_focused, workspace);
994 else move_current_window_to_workspace(conn, workspace);
998 if (last_focused == NULL) {
999 LOG("Not performing (no window found)\n");
1003 if (client_is_floating(last_focused) &&
1004 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1005 LOG("Not performing (floating)\n");
1009 /* Now perform action to <where> */
1010 while (*rest != '\0') {
1013 else if (*rest == 'j')
1015 else if (*rest == 'k')
1017 else if (*rest == 'l')
1018 direction = D_RIGHT;
1020 LOG("unknown direction: %c\n", *rest);
1025 if (action == ACTION_FOCUS) {
1026 if (client_is_floating(last_focused)) {
1027 floating_focus_direction(conn, last_focused, direction);
1030 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1034 if (action == ACTION_MOVE) {
1035 if (client_is_floating(last_focused)) {
1036 floating_move(conn, last_focused, direction);
1039 if (with == WITH_WINDOW)
1040 move_current_window(conn, direction);
1041 else move_current_container(conn, direction);
1045 if (action == ACTION_SNAP) {
1046 snap_current_container(conn, direction);
1051 LOG("--- done ---\n");