4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
30 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
31 /* If this container is empty, we’re done */
32 if (container->currently_focused == NULL)
35 /* Get the previous/next client or wrap around */
36 Client *candidate = NULL;
37 if (direction == D_UP) {
38 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
39 candidate = CIRCLEQ_LAST(&(container->clients));
41 else if (direction == D_DOWN) {
42 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
43 candidate = CIRCLEQ_FIRST(&(container->clients));
44 } else LOG("Direction not implemented!\n");
46 /* If we could not switch, the container contains exactly one client. We return false */
47 if (candidate == container->currently_focused)
51 set_focus(conn, candidate, true);
56 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
58 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
59 LOG("focusing direction %d\n", direction);
61 int new_row = current_row,
62 new_col = current_col;
64 Container *container = CUR_CELL;
65 Workspace *t_ws = c_ws;
67 /* There always is a container. If not, current_col or current_row is wrong */
68 assert(container != NULL);
70 if (container->workspace->fullscreen_client != NULL) {
71 LOG("You're in fullscreen mode. Won't switch focus\n");
75 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
76 if (direction == D_UP || direction == D_DOWN) {
77 if (thing == THING_WINDOW)
78 /* Let’s see if we can perform up/down focus in the current container */
79 if (focus_window_in_container(conn, container, direction))
82 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
83 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
84 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
85 /* Set new_row as a sane default, but it may get overwritten in a second */
88 /* Search from the top to correctly handle rowspanned containers */
89 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
90 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
97 /* Let’s see if there is a screen down/up there to which we can switch */
98 LOG("container is at %d with height %d\n", container->y, container->height);
100 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
101 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
102 LOG("Wrapping screen around vertically\n");
103 /* No screen found? Then wrap */
104 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
106 t_ws = &(workspaces[screen->current_workspace]);
107 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
110 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
111 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
112 LOG("Cell empty, checking for colspanned client above...\n");
113 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
114 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
120 LOG("Fixed it to new col %d\n", new_col);
122 } else if (direction == D_LEFT || direction == D_RIGHT) {
123 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
124 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
125 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
126 /* Set new_col as a sane default, but it may get overwritten in a second */
129 /* Search from the left to correctly handle colspanned containers */
130 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
131 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
138 /* Let’s see if there is a screen left/right here to which we can switch */
139 LOG("container is at %d with width %d\n", container->x, container->width);
141 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
142 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
143 LOG("Wrapping screen around horizontally\n");
144 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
146 t_ws = &(workspaces[screen->current_workspace]);
147 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
150 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
151 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
152 LOG("Cell empty, checking for rowspanned client above...\n");
153 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
154 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
160 LOG("Fixed it to new row %d\n", new_row);
163 LOG("direction unhandled\n");
167 /* Bounds checking */
168 if (new_col >= t_ws->cols)
169 new_col = (t_ws->cols - 1);
170 if (new_row >= t_ws->rows)
171 new_row = (t_ws->rows - 1);
173 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
174 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
178 * Tries to move the window inside its current container.
180 * Returns true if the window could be moved, false otherwise.
183 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
184 direction_t direction) {
185 assert(client->container != NULL);
187 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
188 CIRCLEQ_NEXT(client, clients));
190 if (other == CIRCLEQ_END(&(client->container->clients)))
193 LOG("i can do that\n");
194 /* We can move the client inside its current container */
195 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
196 if (direction == D_UP)
197 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
198 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
204 * Moves the current window or whole container to the given direction, creating a column/row if
208 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
209 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
210 (direction == D_LEFT ? "left" : "right"))));
211 /* Get current window */
212 Container *container = CUR_CELL,
215 /* There has to be a container, see focus_window() */
216 assert(container != NULL);
218 /* If there is no window or the dock window is focused, we’re done */
219 if (container->currently_focused == NULL ||
220 container->currently_focused->dock)
223 /* As soon as the client is moved away, the last focused client in the old
224 * container needs to get focus, if any. Therefore, we save it here. */
225 Client *current_client = container->currently_focused;
226 Client *to_focus = get_last_focused_client(conn, container, current_client);
228 if (to_focus == NULL) {
229 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
230 if (to_focus == NULL)
231 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
236 /* If we’re at the left-most position, move the rest of the table right */
237 if (current_col == 0) {
238 expand_table_cols_at_head(c_ws);
241 new = CUR_TABLE[--current_col][current_row];
244 if (current_col == (c_ws->cols-1))
245 expand_table_cols(c_ws);
247 new = CUR_TABLE[++current_col][current_row];
250 if (move_current_window_in_container(conn, current_client, D_UP))
253 /* if we’re at the up-most position, move the rest of the table down */
254 if (current_row == 0) {
255 expand_table_rows_at_head(c_ws);
258 new = CUR_TABLE[current_col][--current_row];
261 if (move_current_window_in_container(conn, current_client, D_DOWN))
264 if (current_row == (c_ws->rows-1))
265 expand_table_rows(c_ws);
267 new = CUR_TABLE[current_col][++current_row];
269 /* To make static analyzers happy: */
274 /* Remove it from the old container and put it into the new one */
275 client_remove_from_container(conn, current_client, container, true);
277 if (new->currently_focused != NULL)
278 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
279 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
280 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
282 /* Update data structures */
283 current_client->container = new;
284 current_client->workspace = new->workspace;
285 container->currently_focused = to_focus;
286 new->currently_focused = current_client;
288 Workspace *workspace = container->workspace;
290 /* delete all empty columns/rows */
291 cleanup_table(conn, workspace);
293 /* Fix colspan/rowspan if it’d overlap */
294 fix_colrowspan(conn, workspace);
296 render_workspace(conn, workspace->screen, workspace);
299 set_focus(conn, current_client, true);
302 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
303 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
304 (direction == D_LEFT ? "left" : "right"))));
305 /* Get current window */
306 Container *container = CUR_CELL,
309 Container **old = &CUR_CELL;
311 /* There has to be a container, see focus_window() */
312 assert(container != NULL);
316 /* If we’re at the left-most position, move the rest of the table right */
317 if (current_col == 0) {
318 expand_table_cols_at_head(c_ws);
320 old = &CUR_TABLE[current_col+1][current_row];
322 new = CUR_TABLE[--current_col][current_row];
325 if (current_col == (c_ws->cols-1))
326 expand_table_cols(c_ws);
328 new = CUR_TABLE[++current_col][current_row];
331 /* if we’re at the up-most position, move the rest of the table down */
332 if (current_row == 0) {
333 expand_table_rows_at_head(c_ws);
335 old = &CUR_TABLE[current_col][current_row+1];
337 new = CUR_TABLE[current_col][--current_row];
340 if (current_row == (c_ws->rows-1))
341 expand_table_rows(c_ws);
343 new = CUR_TABLE[current_col][++current_row];
345 /* To make static analyzers happy: */
350 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
352 /* Swap the containers */
357 new->col = container->col;
358 new->row = container->row;
360 CUR_CELL = container;
361 container->col = col;
362 container->row = row;
364 Workspace *workspace = container->workspace;
366 /* delete all empty columns/rows */
367 cleanup_table(conn, workspace);
369 /* Fix colspan/rowspan if it’d overlap */
370 fix_colrowspan(conn, workspace);
376 * "Snaps" the current container (not possible for windows, because it works at table base)
377 * to the given direction, that is, adjusts cellspan/rowspan
380 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
381 LOG("snapping container to direction %d\n", direction);
383 Container *container = CUR_CELL;
385 assert(container != NULL);
389 /* Snap to the left is actually a move to the left and then a snap right */
390 if (!cell_exists(container->col - 1, container->row) ||
391 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
392 LOG("cannot snap to left - the cell is already used\n");
396 move_current_window(conn, D_LEFT);
397 snap_current_container(conn, D_RIGHT);
400 /* Check if the cell is used */
401 int new_col = container->col + container->colspan;
402 for (int i = 0; i < container->rowspan; i++)
403 if (!cell_exists(new_col, container->row + i) ||
404 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
405 LOG("cannot snap to right - the cell is already used\n");
409 /* Check if there are other cells with rowspan, which are in our way.
410 * If so, reduce their rowspan. */
411 for (int i = container->row-1; i >= 0; i--) {
412 LOG("we got cell %d, %d with rowspan %d\n",
413 new_col, i, CUR_TABLE[new_col][i]->rowspan);
414 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
415 CUR_TABLE[new_col][i]->rowspan--;
416 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
419 container->colspan++;
423 if (!cell_exists(container->col, container->row - 1) ||
424 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
425 LOG("cannot snap to top - the cell is already used\n");
429 move_current_window(conn, D_UP);
430 snap_current_container(conn, D_DOWN);
433 LOG("snapping down\n");
434 int new_row = container->row + container->rowspan;
435 for (int i = 0; i < container->colspan; i++)
436 if (!cell_exists(container->col + i, new_row) ||
437 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
438 LOG("cannot snap down - the cell is already used\n");
442 for (int i = container->col-1; i >= 0; i--) {
443 LOG("we got cell %d, %d with colspan %d\n",
444 i, new_row, CUR_TABLE[i][new_row]->colspan);
445 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
446 CUR_TABLE[i][new_row]->colspan--;
447 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
451 container->rowspan++;
454 /* To make static analyzers happy: */
462 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
463 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
464 Workspace *t_ws = &(workspaces[workspace-1]),
465 *old_ws = client->workspace;
467 LOG("moving floating\n");
469 if (t_ws->screen == NULL) {
470 LOG("initializing new workspace, setting num to %d\n", workspace-1);
471 t_ws->screen = c_ws->screen;
472 /* Copy the dimensions from the virtual screen */
473 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
475 /* Check if there is already a fullscreen client on the destination workspace and
476 * stop moving if so. */
477 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
478 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
483 floating_assign_to_workspace(client, t_ws);
485 bool target_invisible = t_ws->screen->current_workspace != t_ws->num;
487 /* If we’re moving it to an invisible screen, we need to unmap it */
488 if (target_invisible) {
489 LOG("This workspace is not visible, unmapping\n");
490 xcb_unmap_window(conn, client->frame);
492 /* If this is not the case, we move the window to a workspace
493 * which is on another screen, so we also need to adjust its
495 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
496 uint32_t relative_x = client->rect.x - old_ws->rect.x,
497 relative_y = client->rect.y - old_ws->rect.y;
498 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
499 client->rect.x = t_ws->rect.x + relative_x;
500 client->rect.y = t_ws->rect.y + relative_y;
501 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
502 reposition_client(conn, client);
510 if (!target_invisible)
511 set_focus(conn, client, true);
515 * Moves the currently selected window to the given workspace
518 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
519 LOG("Moving current window to workspace %d\n", workspace);
521 Container *container = CUR_CELL;
523 assert(container != NULL);
525 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
526 Workspace *t_ws = &(workspaces[workspace-1]);
528 Client *current_client = container->currently_focused;
529 if (current_client == NULL) {
530 LOG("No currently focused client in current container.\n");
533 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
534 if (to_focus == NULL)
535 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
537 if (t_ws->screen == NULL) {
538 LOG("initializing new workspace, setting num to %d\n", workspace-1);
539 t_ws->screen = container->workspace->screen;
540 /* Copy the dimensions from the virtual screen */
541 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
543 /* Check if there is already a fullscreen client on the destination workspace and
544 * stop moving if so. */
545 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
546 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
551 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
553 assert(to_container != NULL);
555 client_remove_from_container(conn, current_client, container, true);
556 if (container->workspace->fullscreen_client == current_client)
557 container->workspace->fullscreen_client = NULL;
559 /* TODO: insert it to the correct position */
560 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
562 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
565 current_client->container = to_container;
566 current_client->workspace = to_container->workspace;
567 container->currently_focused = to_focus;
568 to_container->currently_focused = current_client;
570 bool target_invisible = (to_container->workspace->screen->current_workspace != to_container->workspace->num);
572 /* If we’re moving it to an invisible screen, we need to unmap it */
573 if (target_invisible) {
574 LOG("This workspace is not visible, unmapping\n");
575 xcb_unmap_window(conn, current_client->frame);
577 if (current_client->fullscreen) {
578 LOG("Calling client_enter_fullscreen again\n");
579 client_enter_fullscreen(conn, current_client);
583 /* delete all empty columns/rows */
584 cleanup_table(conn, container->workspace);
588 if (!target_invisible)
589 set_focus(conn, current_client, true);
593 * Switches to the given workspace
596 void show_workspace(xcb_connection_t *conn, int workspace) {
598 bool need_warp = false;
599 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
600 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
601 Workspace *t_ws = &(workspaces[workspace-1]);
603 LOG("show_workspace(%d)\n", workspace);
605 /* Store current_row/current_col */
606 c_ws->current_row = current_row;
607 c_ws->current_col = current_col;
609 /* Check if the workspace has not been used yet */
610 if (t_ws->screen == NULL) {
611 LOG("initializing new workspace, setting num to %d\n", workspace);
612 t_ws->screen = c_ws->screen;
613 /* Copy the dimensions from the virtual screen */
614 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
617 if (c_ws->screen != t_ws->screen) {
618 /* We need to switch to the other screen first */
619 LOG("moving over to other screen.\n");
621 /* Store the old client */
622 Client *old_client = CUR_CELL->currently_focused;
624 c_ws = &(workspaces[t_ws->screen->current_workspace]);
625 current_col = c_ws->current_col;
626 current_row = c_ws->current_row;
627 if (CUR_CELL->currently_focused != NULL)
630 Rect *dims = &(c_ws->screen->rect);
631 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
632 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
635 /* Re-decorate the old client, it’s not focused anymore */
636 if ((old_client != NULL) && !old_client->dock)
637 redecorate_window(conn, old_client);
638 else xcb_flush(conn);
641 /* Check if we need to change something or if we’re already there */
642 if (c_ws->screen->current_workspace == (workspace-1)) {
643 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
644 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
645 set_focus(conn, last_focused, true);
647 client_warp_pointer_into(conn, last_focused);
655 t_ws->screen->current_workspace = workspace-1;
656 Workspace *old_workspace = c_ws;
657 c_ws = &workspaces[workspace-1];
659 /* Unmap all clients of the old workspace */
660 unmap_workspace(conn, old_workspace);
662 current_row = c_ws->current_row;
663 current_col = c_ws->current_col;
664 LOG("new current row = %d, current col = %d\n", current_row, current_col);
666 ignore_enter_notify_forall(conn, c_ws, true);
668 /* Map all clients on the new workspace */
670 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
671 xcb_map_window(conn, client->frame);
673 /* Map all floating clients */
674 if (!c_ws->floating_hidden)
675 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
676 xcb_map_window(conn, client->frame);
678 /* Map all stack windows, if any */
679 struct Stack_Window *stack_win;
680 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
681 if (stack_win->container->workspace == c_ws)
682 xcb_map_window(conn, stack_win->window);
684 ignore_enter_notify_forall(conn, c_ws, false);
686 /* Restore focus on the new workspace */
687 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
688 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
689 set_focus(conn, last_focused, true);
691 client_warp_pointer_into(conn, last_focused);
694 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
700 * Jumps to the given window class / title.
701 * Title is matched using strstr, that is, matches if it appears anywhere
702 * in the string. Regular expressions seem to be a bit overkill here. However,
703 * if we need them for something else somewhen, we may introduce them here, too.
706 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
710 /* The first character is a quote, this was checked before */
711 classtitle = sstrdup(arguments+1);
712 /* The last character is a quote, we just set it to NULL */
713 classtitle[strlen(classtitle)-1] = '\0';
715 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
717 LOG("No matching client found.\n");
722 set_focus(conn, client, true);
726 * Jump directly to the specified workspace, row and col.
727 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
730 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
734 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
735 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
737 /* No match? Either no arguments were specified, or no numbers */
739 LOG("At least one valid argument required\n");
743 /* Move to the target workspace */
744 show_workspace(conn, ws);
749 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
751 /* Move to row/col */
752 if (row >= c_ws->rows)
753 row = c_ws->rows - 1;
754 if (col >= c_ws->cols)
755 col = c_ws->cols - 1;
757 LOG("Jumping to col %d, row %d\n", col, row);
758 if (c_ws->table[col][row]->currently_focused != NULL)
759 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
763 * Travels the focus stack by the given number of times (or once, if no argument
764 * was specified). That is, selects the window you were in before you focused
765 * the current window.
767 * The special values 'floating' (select the next floating window), 'tiling'
768 * (select the next tiling window), 'ft' (if the current window is floating,
769 * select the next tiling window and vice-versa) are also valid
772 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
773 /* Start count at -1 to always skip the first element */
774 int times, count = -1;
776 bool floating_criteria;
778 /* Either it’s one of the special values… */
779 if (strcasecmp(arguments, "floating") == 0) {
780 floating_criteria = true;
781 } else if (strcasecmp(arguments, "tiling") == 0) {
782 floating_criteria = false;
783 } else if (strcasecmp(arguments, "ft") == 0) {
784 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
785 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
786 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
790 floating_criteria = !client_is_floating(last_focused);
792 /* …or a number was specified */
793 if (sscanf(arguments, "%u", ×) != 1) {
794 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
798 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
799 if (++count < times) {
805 set_focus(conn, current, true);
811 /* Select the next client matching the criteria parsed above */
812 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
813 if (client_is_floating(current) == floating_criteria) {
814 set_focus(conn, current, true);
820 * Goes through the list of arguments (for exec()) and checks if the given argument
821 * is present. If not, it copies the arguments (because we cannot realloc it) and
822 * appends the given argument.
825 static char **append_argument(char **original, char *argument) {
827 for (num_args = 0; original[num_args] != NULL; num_args++) {
828 LOG("original argument: \"%s\"\n", original[num_args]);
829 /* If the argument is already present we return the original pointer */
830 if (strcmp(original[num_args], argument) == 0)
833 /* Copy the original array */
834 char **result = smalloc((num_args+2) * sizeof(char*));
835 memcpy(result, original, num_args * sizeof(char*));
836 result[num_args] = argument;
837 result[num_args+1] = NULL;
843 * Parses a command, see file CMDMODE for more information
846 void parse_command(xcb_connection_t *conn, const char *command) {
847 LOG("--- parsing command \"%s\" ---\n", command);
848 /* Get the first client from focus stack because floating clients are not
849 * in any container, therefore CUR_CELL is not appropriate. */
850 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
851 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
854 /* Hmm, just to be sure */
855 if (command[0] == '\0')
858 /* Is it an <exec>? Then execute the given command. */
859 if (STARTS_WITH(command, "exec ")) {
860 LOG("starting \"%s\"\n", command + strlen("exec "));
861 start_application(command+strlen("exec "));
865 /* Is it an <exit>? */
866 if (STARTS_WITH(command, "exit")) {
867 LOG("User issued exit-command, exiting without error.\n");
871 /* Is it <restart>? Then restart in place. */
872 if (STARTS_WITH(command, "restart")) {
873 LOG("restarting \"%s\"...\n", start_argv[0]);
874 /* make sure -a is in the argument list or append it */
875 start_argv = append_argument(start_argv, "-a");
877 execvp(start_argv[0], start_argv);
881 if (STARTS_WITH(command, "kill")) {
882 if (last_focused == NULL) {
883 LOG("There is no window to kill\n");
887 LOG("Killing current window\n");
888 client_kill(conn, last_focused);
892 /* Is it a jump to a specified workspace, row, col? */
893 if (STARTS_WITH(command, "jump ")) {
894 const char *arguments = command + strlen("jump ");
895 if (arguments[0] == '"')
896 jump_to_window(conn, arguments);
897 else jump_to_container(conn, arguments);
901 /* Should we travel the focus stack? */
902 if (STARTS_WITH(command, "focus")) {
903 const char *arguments = command + strlen("focus ");
904 travel_focus_stack(conn, arguments);
908 /* Is it 'f' for fullscreen? */
909 if (command[0] == 'f') {
910 if (last_focused == NULL)
912 client_toggle_fullscreen(conn, last_focused);
916 /* Is it just 's' for stacking or 'd' for default? */
917 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
918 if (last_focused == NULL || client_is_floating(last_focused)) {
919 LOG("not switching, this is a floating client\n");
922 LOG("Switching mode for current container\n");
923 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
927 if (command[0] == 'H') {
928 LOG("Hiding all floating windows\n");
929 floating_toggle_hide(conn, c_ws);
933 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
935 /* Is it a <with>? */
936 if (command[0] == 'w') {
938 /* TODO: implement */
939 if (command[0] == 'c') {
940 with = WITH_CONTAINER;
942 } else if (command[0] == 'w') {
943 with = WITH_WORKSPACE;
946 LOG("not yet implemented.\n");
951 /* Is it 't' for toggle tiling/floating? */
952 if (command[0] == 't') {
953 if (with == WITH_WORKSPACE) {
954 c_ws->auto_float = !c_ws->auto_float;
955 LOG("autofloat is now %d\n", c_ws->auto_float);
958 if (last_focused == NULL) {
959 LOG("Cannot toggle tiling/floating: workspace empty\n");
963 toggle_floating_mode(conn, last_focused, false);
964 /* delete all empty columns/rows */
965 cleanup_table(conn, last_focused->workspace);
967 /* Fix colspan/rowspan if it’d overlap */
968 fix_colrowspan(conn, last_focused->workspace);
970 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
975 /* It’s a normal <cmd> */
977 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
978 direction_t direction;
979 int times = strtol(command, &rest, 10);
981 LOG("Invalid command (\"%s\")\n", command);
986 /* No rest? This was a workspace number, not a times specification */
987 show_workspace(conn, times);
991 if (*rest == 'm' || *rest == 's') {
992 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
996 int workspace = strtol(rest, &rest, 10);
999 LOG("Invalid command (\"%s\")\n", command);
1003 if (*rest == '\0') {
1004 if (last_focused != NULL && client_is_floating(last_focused))
1005 move_floating_window_to_workspace(conn, last_focused, workspace);
1006 else move_current_window_to_workspace(conn, workspace);
1010 if (last_focused == NULL) {
1011 LOG("Not performing (no window found)\n");
1015 if (client_is_floating(last_focused) &&
1016 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1017 LOG("Not performing (floating)\n");
1021 /* Now perform action to <where> */
1022 while (*rest != '\0') {
1025 else if (*rest == 'j')
1027 else if (*rest == 'k')
1029 else if (*rest == 'l')
1030 direction = D_RIGHT;
1032 LOG("unknown direction: %c\n", *rest);
1037 if (action == ACTION_FOCUS) {
1038 if (client_is_floating(last_focused)) {
1039 floating_focus_direction(conn, last_focused, direction);
1042 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1046 if (action == ACTION_MOVE) {
1047 if (client_is_floating(last_focused)) {
1048 floating_move(conn, last_focused, direction);
1051 if (with == WITH_WINDOW)
1052 move_current_window(conn, direction);
1053 else move_current_container(conn, direction);
1057 if (action == ACTION_SNAP) {
1058 snap_current_container(conn, direction);
1063 LOG("--- done ---\n");