4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
30 #include "workspace.h"
33 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
34 /* If this container is empty, we’re done */
35 if (container->currently_focused == NULL)
38 /* Get the previous/next client or wrap around */
39 Client *candidate = NULL;
40 if (direction == D_UP) {
41 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
42 candidate = CIRCLEQ_LAST(&(container->clients));
44 else if (direction == D_DOWN) {
45 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
46 candidate = CIRCLEQ_FIRST(&(container->clients));
47 } else LOG("Direction not implemented!\n");
49 /* If we could not switch, the container contains exactly one client. We return false */
50 if (candidate == container->currently_focused)
54 set_focus(conn, candidate, true);
59 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
61 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
63 LOG("Jumping to \"%s\"\n", mark);
65 for (int c = 0; c < 10; c++)
66 SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
67 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
70 set_focus(conn, current, true);
74 LOG("No window with this mark found\n");
77 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
78 LOG("focusing direction %d\n", direction);
80 int new_row = current_row,
81 new_col = current_col;
82 Container *container = CUR_CELL;
83 Workspace *t_ws = c_ws;
85 /* Makes sure new_col and new_row are within bounds of the new workspace */
86 void check_colrow_boundaries() {
87 if (new_col >= t_ws->cols)
88 new_col = (t_ws->cols - 1);
89 if (new_row >= t_ws->rows)
90 new_row = (t_ws->rows - 1);
93 /* There always is a container. If not, current_col or current_row is wrong */
94 assert(container != NULL);
96 if (container->workspace->fullscreen_client != NULL) {
97 LOG("You're in fullscreen mode. Won't switch focus\n");
101 /* For focusing screens, situation is different: we get the rect
102 * of the current screen, then get the screen which is on its
103 * right/left/bottom/top and just switch to the workspace on
104 * the target screen. */
105 if (thing == THING_SCREEN) {
106 i3Screen *cs = c_ws->screen;
108 Rect bounds = cs->rect;
110 if (direction == D_RIGHT)
111 bounds.x += bounds.width;
112 else if (direction == D_LEFT)
113 bounds.x -= bounds.width;
114 else if (direction == D_UP)
115 bounds.y -= bounds.height;
116 else bounds.y += bounds.height;
118 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
119 if (target == NULL) {
120 LOG("Target screen NULL\n");
121 /* Wrap around if the target screen is out of bounds */
122 if (direction == D_RIGHT)
123 target = get_screen_most(D_LEFT, cs);
124 else if (direction == D_LEFT)
125 target = get_screen_most(D_RIGHT, cs);
126 else if (direction == D_UP)
127 target = get_screen_most(D_DOWN, cs);
128 else target = get_screen_most(D_UP, cs);
131 LOG("Switching to ws %d\n", target->current_workspace + 1);
132 workspace_show(conn, target->current_workspace + 1);
136 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
137 if (direction == D_UP || direction == D_DOWN) {
138 if (thing == THING_WINDOW)
139 /* Let’s see if we can perform up/down focus in the current container */
140 if (focus_window_in_container(conn, container, direction))
143 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
144 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
145 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
146 /* Set new_row as a sane default, but it may get overwritten in a second */
149 /* Search from the top to correctly handle rowspanned containers */
150 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
151 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
158 /* Let’s see if there is a screen down/up there to which we can switch */
159 LOG("container is at %d with height %d\n", container->y, container->height);
161 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
162 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
163 LOG("Wrapping screen around vertically\n");
164 /* No screen found? Then wrap */
165 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
167 t_ws = &(workspaces[screen->current_workspace]);
168 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
171 check_colrow_boundaries();
173 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
174 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
175 LOG("Cell empty, checking for colspanned client above...\n");
176 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
177 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
183 LOG("Fixed it to new col %d\n", new_col);
185 } else if (direction == D_LEFT || direction == D_RIGHT) {
186 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
187 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
188 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
189 /* Set new_col as a sane default, but it may get overwritten in a second */
192 /* Search from the left to correctly handle colspanned containers */
193 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
194 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
201 /* Let’s see if there is a screen left/right here to which we can switch */
202 LOG("container is at %d with width %d\n", container->x, container->width);
204 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
205 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
206 LOG("Wrapping screen around horizontally\n");
207 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
209 t_ws = &(workspaces[screen->current_workspace]);
210 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
213 check_colrow_boundaries();
215 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
216 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
217 LOG("Cell empty, checking for rowspanned client above...\n");
218 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
219 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
225 LOG("Fixed it to new row %d\n", new_row);
228 LOG("direction unhandled\n");
232 check_colrow_boundaries();
234 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
235 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
239 * Tries to move the window inside its current container.
241 * Returns true if the window could be moved, false otherwise.
244 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
245 direction_t direction) {
246 assert(client->container != NULL);
248 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
249 CIRCLEQ_NEXT(client, clients));
251 if (other == CIRCLEQ_END(&(client->container->clients)))
254 LOG("i can do that\n");
255 /* We can move the client inside its current container */
256 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
257 if (direction == D_UP)
258 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
259 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
265 * Moves the current window or whole container to the given direction, creating a column/row if
269 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
270 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
271 (direction == D_LEFT ? "left" : "right"))));
272 /* Get current window */
273 Container *container = CUR_CELL,
276 /* There has to be a container, see focus_window() */
277 assert(container != NULL);
279 /* If there is no window or the dock window is focused, we’re done */
280 if (container->currently_focused == NULL ||
281 container->currently_focused->dock)
284 /* As soon as the client is moved away, the last focused client in the old
285 * container needs to get focus, if any. Therefore, we save it here. */
286 Client *current_client = container->currently_focused;
287 Client *to_focus = get_last_focused_client(conn, container, current_client);
289 if (to_focus == NULL) {
290 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
291 if (to_focus == NULL)
292 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
297 /* If we’re at the left-most position, move the rest of the table right */
298 if (current_col == 0) {
299 expand_table_cols_at_head(c_ws);
302 new = CUR_TABLE[--current_col][current_row];
305 if (current_col == (c_ws->cols-1))
306 expand_table_cols(c_ws);
308 new = CUR_TABLE[++current_col][current_row];
311 if (move_current_window_in_container(conn, current_client, D_UP))
314 /* if we’re at the up-most position, move the rest of the table down */
315 if (current_row == 0) {
316 expand_table_rows_at_head(c_ws);
319 new = CUR_TABLE[current_col][--current_row];
322 if (move_current_window_in_container(conn, current_client, D_DOWN))
325 if (current_row == (c_ws->rows-1))
326 expand_table_rows(c_ws);
328 new = CUR_TABLE[current_col][++current_row];
330 /* To make static analyzers happy: */
335 /* Remove it from the old container and put it into the new one */
336 client_remove_from_container(conn, current_client, container, true);
338 if (new->currently_focused != NULL)
339 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
340 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
341 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
343 /* Update data structures */
344 current_client->container = new;
345 current_client->workspace = new->workspace;
346 container->currently_focused = to_focus;
347 new->currently_focused = current_client;
349 Workspace *workspace = container->workspace;
351 /* delete all empty columns/rows */
352 cleanup_table(conn, workspace);
354 /* Fix colspan/rowspan if it’d overlap */
355 fix_colrowspan(conn, workspace);
357 render_workspace(conn, workspace->screen, workspace);
360 set_focus(conn, current_client, true);
363 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
364 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
365 (direction == D_LEFT ? "left" : "right"))));
366 /* Get current window */
367 Container *container = CUR_CELL,
370 Container **old = &CUR_CELL;
372 /* There has to be a container, see focus_window() */
373 assert(container != NULL);
377 /* If we’re at the left-most position, move the rest of the table right */
378 if (current_col == 0) {
379 expand_table_cols_at_head(c_ws);
381 old = &CUR_TABLE[current_col+1][current_row];
383 new = CUR_TABLE[--current_col][current_row];
386 if (current_col == (c_ws->cols-1))
387 expand_table_cols(c_ws);
389 new = CUR_TABLE[++current_col][current_row];
392 /* if we’re at the up-most position, move the rest of the table down */
393 if (current_row == 0) {
394 expand_table_rows_at_head(c_ws);
396 old = &CUR_TABLE[current_col][current_row+1];
398 new = CUR_TABLE[current_col][--current_row];
401 if (current_row == (c_ws->rows-1))
402 expand_table_rows(c_ws);
404 new = CUR_TABLE[current_col][++current_row];
406 /* To make static analyzers happy: */
411 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
413 /* Swap the containers */
418 new->col = container->col;
419 new->row = container->row;
421 CUR_CELL = container;
422 container->col = col;
423 container->row = row;
425 Workspace *workspace = container->workspace;
427 /* delete all empty columns/rows */
428 cleanup_table(conn, workspace);
430 /* Fix colspan/rowspan if it’d overlap */
431 fix_colrowspan(conn, workspace);
437 * "Snaps" the current container (not possible for windows, because it works at table base)
438 * to the given direction, that is, adjusts cellspan/rowspan
441 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
442 LOG("snapping container to direction %d\n", direction);
444 Container *container = CUR_CELL;
446 assert(container != NULL);
450 /* Snap to the left is actually a move to the left and then a snap right */
451 if (!cell_exists(container->col - 1, container->row) ||
452 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
453 LOG("cannot snap to left - the cell is already used\n");
457 move_current_window(conn, D_LEFT);
458 snap_current_container(conn, D_RIGHT);
461 /* Check if the cell is used */
462 int new_col = container->col + container->colspan;
463 for (int i = 0; i < container->rowspan; i++)
464 if (!cell_exists(new_col, container->row + i) ||
465 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
466 LOG("cannot snap to right - the cell is already used\n");
470 /* Check if there are other cells with rowspan, which are in our way.
471 * If so, reduce their rowspan. */
472 for (int i = container->row-1; i >= 0; i--) {
473 LOG("we got cell %d, %d with rowspan %d\n",
474 new_col, i, CUR_TABLE[new_col][i]->rowspan);
475 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
476 CUR_TABLE[new_col][i]->rowspan--;
477 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
480 container->colspan++;
484 if (!cell_exists(container->col, container->row - 1) ||
485 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
486 LOG("cannot snap to top - the cell is already used\n");
490 move_current_window(conn, D_UP);
491 snap_current_container(conn, D_DOWN);
494 LOG("snapping down\n");
495 int new_row = container->row + container->rowspan;
496 for (int i = 0; i < container->colspan; i++)
497 if (!cell_exists(container->col + i, new_row) ||
498 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
499 LOG("cannot snap down - the cell is already used\n");
503 for (int i = container->col-1; i >= 0; i--) {
504 LOG("we got cell %d, %d with colspan %d\n",
505 i, new_row, CUR_TABLE[i][new_row]->colspan);
506 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
507 CUR_TABLE[i][new_row]->colspan--;
508 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
512 container->rowspan++;
515 /* To make static analyzers happy: */
523 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
524 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
525 Workspace *t_ws = &(workspaces[workspace-1]),
526 *old_ws = client->workspace;
528 LOG("moving floating\n");
530 workspace_initialize(t_ws, c_ws->screen);
532 /* Check if there is already a fullscreen client on the destination workspace and
533 * stop moving if so. */
534 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
535 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
539 floating_assign_to_workspace(client, t_ws);
541 /* If we’re moving it to an invisible screen, we need to unmap it */
542 if (!workspace_is_visible(t_ws)) {
543 LOG("This workspace is not visible, unmapping\n");
544 client_unmap(conn, client);
546 /* If this is not the case, we move the window to a workspace
547 * which is on another screen, so we also need to adjust its
549 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
550 uint32_t relative_x = client->rect.x - old_ws->rect.x,
551 relative_y = client->rect.y - old_ws->rect.y;
552 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
553 client->rect.x = t_ws->rect.x + relative_x;
554 client->rect.y = t_ws->rect.y + relative_y;
555 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
556 reposition_client(conn, client);
564 if (workspace_is_visible(t_ws))
565 set_focus(conn, client, true);
569 * Moves the currently selected window to the given workspace
572 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
573 LOG("Moving current window to workspace %d\n", workspace);
575 Container *container = CUR_CELL;
577 assert(container != NULL);
579 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
580 Workspace *t_ws = &(workspaces[workspace-1]);
582 Client *current_client = container->currently_focused;
583 if (current_client == NULL) {
584 LOG("No currently focused client in current container.\n");
587 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
588 if (to_focus == NULL)
589 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
591 workspace_initialize(t_ws, container->workspace->screen);
592 /* Check if there is already a fullscreen client on the destination workspace and
593 * stop moving if so. */
594 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
595 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
599 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
601 assert(to_container != NULL);
603 client_remove_from_container(conn, current_client, container, true);
604 if (container->workspace->fullscreen_client == current_client)
605 container->workspace->fullscreen_client = NULL;
607 /* TODO: insert it to the correct position */
608 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
610 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
613 current_client->container = to_container;
614 current_client->workspace = to_container->workspace;
615 container->currently_focused = to_focus;
616 to_container->currently_focused = current_client;
618 /* If we’re moving it to an invisible screen, we need to unmap it */
619 if (!workspace_is_visible(to_container->workspace)) {
620 LOG("This workspace is not visible, unmapping\n");
621 client_unmap(conn, current_client);
623 if (current_client->fullscreen) {
624 LOG("Calling client_enter_fullscreen again\n");
625 client_enter_fullscreen(conn, current_client);
629 /* delete all empty columns/rows */
630 cleanup_table(conn, container->workspace);
634 if (workspace_is_visible(to_container->workspace))
635 set_focus(conn, current_client, true);
639 * Jumps to the given window class / title.
640 * Title is matched using strstr, that is, matches if it appears anywhere
641 * in the string. Regular expressions seem to be a bit overkill here. However,
642 * if we need them for something else somewhen, we may introduce them here, too.
645 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
649 /* The first character is a quote, this was checked before */
650 classtitle = sstrdup(arguments+1);
651 /* The last character is a quote, we just set it to NULL */
652 classtitle[strlen(classtitle)-1] = '\0';
654 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
656 LOG("No matching client found.\n");
661 set_focus(conn, client, true);
665 * Jump directly to the specified workspace, row and col.
666 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
669 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
673 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
674 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
676 /* No match? Either no arguments were specified, or no numbers */
678 LOG("At least one valid argument required\n");
682 /* Move to the target workspace */
683 workspace_show(conn, ws);
688 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
690 /* Move to row/col */
691 if (row >= c_ws->rows)
692 row = c_ws->rows - 1;
693 if (col >= c_ws->cols)
694 col = c_ws->cols - 1;
696 LOG("Jumping to col %d, row %d\n", col, row);
697 if (c_ws->table[col][row]->currently_focused != NULL)
698 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
702 * Travels the focus stack by the given number of times (or once, if no argument
703 * was specified). That is, selects the window you were in before you focused
704 * the current window.
706 * The special values 'floating' (select the next floating window), 'tiling'
707 * (select the next tiling window), 'ft' (if the current window is floating,
708 * select the next tiling window and vice-versa) are also valid
711 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
712 /* Start count at -1 to always skip the first element */
713 int times, count = -1;
715 bool floating_criteria;
717 /* Either it’s one of the special values… */
718 if (strcasecmp(arguments, "floating") == 0) {
719 floating_criteria = true;
720 } else if (strcasecmp(arguments, "tiling") == 0) {
721 floating_criteria = false;
722 } else if (strcasecmp(arguments, "ft") == 0) {
723 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
724 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
725 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
729 floating_criteria = !client_is_floating(last_focused);
731 /* …or a number was specified */
732 if (sscanf(arguments, "%u", ×) != 1) {
733 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
737 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
738 if (++count < times) {
744 set_focus(conn, current, true);
750 /* Select the next client matching the criteria parsed above */
751 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
752 if (client_is_floating(current) == floating_criteria) {
753 set_focus(conn, current, true);
759 * Goes through the list of arguments (for exec()) and checks if the given argument
760 * is present. If not, it copies the arguments (because we cannot realloc it) and
761 * appends the given argument.
764 static char **append_argument(char **original, char *argument) {
766 for (num_args = 0; original[num_args] != NULL; num_args++) {
767 LOG("original argument: \"%s\"\n", original[num_args]);
768 /* If the argument is already present we return the original pointer */
769 if (strcmp(original[num_args], argument) == 0)
772 /* Copy the original array */
773 char **result = smalloc((num_args+2) * sizeof(char*));
774 memcpy(result, original, num_args * sizeof(char*));
775 result[num_args] = argument;
776 result[num_args+1] = NULL;
782 * Switch to next or previous existing workspace
785 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
789 if (direction == 'n') {
790 /* If we are on the last workspace, we cannot go any further */
794 for (i = c_ws->num + 1; i <= 9; i++) {
795 t_ws = &(workspaces[i]);
796 if (t_ws->screen != NULL)
799 } else if (direction == 'p') {
802 for (i = c_ws->num - 1; i >= 0 ; i--) {
803 t_ws = &(workspaces[i]);
804 if (t_ws->screen != NULL)
809 if (t_ws->screen != NULL)
810 workspace_show(conn, i+1);
814 * Parses a command, see file CMDMODE for more information
817 void parse_command(xcb_connection_t *conn, const char *command) {
818 LOG("--- parsing command \"%s\" ---\n", command);
819 /* Get the first client from focus stack because floating clients are not
820 * in any container, therefore CUR_CELL is not appropriate. */
821 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
822 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
825 /* Hmm, just to be sure */
826 if (command[0] == '\0')
829 /* Is it an <exec>? Then execute the given command. */
830 if (STARTS_WITH(command, "exec ")) {
831 LOG("starting \"%s\"\n", command + strlen("exec "));
832 start_application(command+strlen("exec "));
836 if (STARTS_WITH(command, "mark")) {
837 if (last_focused == NULL) {
838 LOG("There is no window to mark\n");
841 const char *rest = command + strlen("mark");
845 LOG("interactive mark starting\n");
846 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
848 LOG("mark with \"%s\"\n", rest);
849 client_mark(conn, last_focused, rest);
854 if (STARTS_WITH(command, "goto")) {
855 const char *rest = command + strlen("goto");
859 LOG("interactive go to mark starting\n");
860 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
862 LOG("go to \"%s\"\n", rest);
863 jump_to_mark(conn, rest);
868 /* Is it an <exit>? */
869 if (STARTS_WITH(command, "exit")) {
870 LOG("User issued exit-command, exiting without error.\n");
874 /* Is it a <reload>? */
875 if (STARTS_WITH(command, "reload")) {
876 load_configuration(conn, NULL, true);
880 /* Is it <restart>? Then restart in place. */
881 if (STARTS_WITH(command, "restart")) {
882 LOG("restarting \"%s\"...\n", start_argv[0]);
883 /* make sure -a is in the argument list or append it */
884 start_argv = append_argument(start_argv, "-a");
886 execvp(start_argv[0], start_argv);
890 if (STARTS_WITH(command, "kill")) {
891 if (last_focused == NULL) {
892 LOG("There is no window to kill\n");
896 LOG("Killing current window\n");
897 client_kill(conn, last_focused);
901 /* Is it a jump to a specified workspace, row, col? */
902 if (STARTS_WITH(command, "jump ")) {
903 const char *arguments = command + strlen("jump ");
904 if (arguments[0] == '"')
905 jump_to_window(conn, arguments);
906 else jump_to_container(conn, arguments);
910 /* Should we travel the focus stack? */
911 if (STARTS_WITH(command, "focus")) {
912 const char *arguments = command + strlen("focus ");
913 travel_focus_stack(conn, arguments);
917 /* Is it 'f' for fullscreen? */
918 if (command[0] == 'f') {
919 if (last_focused == NULL)
921 client_toggle_fullscreen(conn, last_focused);
925 /* Is it just 's' for stacking or 'd' for default? */
926 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
927 if (last_focused != NULL && client_is_floating(last_focused)) {
928 LOG("not switching, this is a floating client\n");
931 LOG("Switching mode for current container\n");
932 int new_mode = MODE_DEFAULT;
933 if (command[0] == 's')
934 new_mode = MODE_STACK;
935 if (command[0] == 'T')
936 new_mode = MODE_TABBED;
937 switch_layout_mode(conn, CUR_CELL, new_mode);
941 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
942 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
943 if (command[0] == 'b') {
944 if (last_focused == NULL) {
945 LOG("No window focused, cannot change border type\n");
949 char com = command[1];
950 if (command[1] == 't') {
951 if (last_focused->titlebar_position == TITLEBAR_TOP &&
952 !last_focused->borderless)
954 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
955 !last_focused->borderless)
960 client_change_border(conn, last_focused, com);
964 if (command[0] == 'H') {
965 LOG("Hiding all floating windows\n");
966 floating_toggle_hide(conn, c_ws);
970 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
972 /* Is it a <with>? */
973 if (command[0] == 'w') {
975 /* TODO: implement */
976 if (command[0] == 'c') {
977 with = WITH_CONTAINER;
979 } else if (command[0] == 'w') {
980 with = WITH_WORKSPACE;
982 } else if (command[0] == 's') {
986 LOG("not yet implemented.\n");
991 /* Is it 't' for toggle tiling/floating? */
992 if (command[0] == 't') {
993 if (with == WITH_WORKSPACE) {
994 c_ws->auto_float = !c_ws->auto_float;
995 LOG("autofloat is now %d\n", c_ws->auto_float);
998 if (last_focused == NULL) {
999 LOG("Cannot toggle tiling/floating: workspace empty\n");
1003 toggle_floating_mode(conn, last_focused, false);
1004 /* delete all empty columns/rows */
1005 cleanup_table(conn, last_focused->workspace);
1007 /* Fix colspan/rowspan if it’d overlap */
1008 fix_colrowspan(conn, last_focused->workspace);
1010 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
1012 /* Re-focus the client because cleanup_table sets the focus to the last
1013 * focused client inside a container only. */
1014 set_focus(conn, last_focused, true);
1019 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1020 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1021 next_previous_workspace(conn, command[0]);
1025 /* It’s a normal <cmd> */
1027 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1028 direction_t direction;
1029 int times = strtol(command, &rest, 10);
1031 LOG("Invalid command (\"%s\")\n", command);
1035 if (*rest == '\0') {
1036 /* No rest? This was a workspace number, not a times specification */
1037 workspace_show(conn, times);
1041 if (*rest == 'm' || *rest == 's') {
1042 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1046 int workspace = strtol(rest, &rest, 10);
1049 LOG("Invalid command (\"%s\")\n", command);
1053 if (*rest == '\0') {
1054 if (last_focused != NULL && client_is_floating(last_focused))
1055 move_floating_window_to_workspace(conn, last_focused, workspace);
1056 else move_current_window_to_workspace(conn, workspace);
1060 if (last_focused == NULL) {
1061 LOG("Not performing (no window found)\n");
1065 if (client_is_floating(last_focused) &&
1066 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1067 LOG("Not performing (floating)\n");
1071 /* Now perform action to <where> */
1072 while (*rest != '\0') {
1075 else if (*rest == 'j')
1077 else if (*rest == 'k')
1079 else if (*rest == 'l')
1080 direction = D_RIGHT;
1082 LOG("unknown direction: %c\n", *rest);
1087 if (action == ACTION_FOCUS) {
1088 if (with == WITH_SCREEN) {
1089 focus_thing(conn, direction, THING_SCREEN);
1092 if (client_is_floating(last_focused)) {
1093 floating_focus_direction(conn, last_focused, direction);
1096 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1100 if (action == ACTION_MOVE) {
1101 if (with == WITH_SCREEN) {
1102 /* TODO: this should swap the screen’s contents
1103 * (e.g. all workspaces) with the next/previous/…
1105 LOG("Not yet implemented\n");
1108 if (client_is_floating(last_focused)) {
1109 floating_move(conn, last_focused, direction);
1112 if (with == WITH_WINDOW)
1113 move_current_window(conn, direction);
1114 else move_current_container(conn, direction);
1118 if (action == ACTION_SNAP) {
1119 if (with == WITH_SCREEN) {
1120 LOG("You cannot snap a screen (it makes no sense).\n");
1123 snap_current_container(conn, direction);