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 focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
62 LOG("focusing direction %d\n", direction);
64 int new_row = current_row,
65 new_col = current_col;
66 Container *container = CUR_CELL;
67 Workspace *t_ws = c_ws;
69 /* Makes sure new_col and new_row are within bounds of the new workspace */
70 void check_colrow_boundaries() {
71 if (new_col >= t_ws->cols)
72 new_col = (t_ws->cols - 1);
73 if (new_row >= t_ws->rows)
74 new_row = (t_ws->rows - 1);
77 /* There always is a container. If not, current_col or current_row is wrong */
78 assert(container != NULL);
80 if (container->workspace->fullscreen_client != NULL) {
81 LOG("You're in fullscreen mode. Won't switch focus\n");
85 /* For focusing screens, situation is different: we get the rect
86 * of the current screen, then get the screen which is on its
87 * right/left/bottom/top and just switch to the workspace on
88 * the target screen. */
89 if (thing == THING_SCREEN) {
90 i3Screen *cs = c_ws->screen;
92 Rect bounds = cs->rect;
94 if (direction == D_RIGHT)
95 bounds.x += bounds.width;
96 else if (direction == D_LEFT)
97 bounds.x -= bounds.width;
98 else if (direction == D_UP)
99 bounds.y -= bounds.height;
100 else bounds.y += bounds.height;
102 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
103 if (target == NULL) {
104 LOG("Target screen NULL\n");
105 /* Wrap around if the target screen is out of bounds */
106 if (direction == D_RIGHT)
107 target = get_screen_most(D_LEFT, cs);
108 else if (direction == D_LEFT)
109 target = get_screen_most(D_RIGHT, cs);
110 else if (direction == D_UP)
111 target = get_screen_most(D_DOWN, cs);
112 else target = get_screen_most(D_UP, cs);
115 LOG("Switching to ws %d\n", target->current_workspace + 1);
116 workspace_show(conn, target->current_workspace + 1);
120 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
121 if (direction == D_UP || direction == D_DOWN) {
122 if (thing == THING_WINDOW)
123 /* Let’s see if we can perform up/down focus in the current container */
124 if (focus_window_in_container(conn, container, direction))
127 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
128 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
129 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
130 /* Set new_row as a sane default, but it may get overwritten in a second */
133 /* Search from the top to correctly handle rowspanned containers */
134 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
135 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
142 /* Let’s see if there is a screen down/up there to which we can switch */
143 LOG("container is at %d with height %d\n", container->y, container->height);
145 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
146 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
147 LOG("Wrapping screen around vertically\n");
148 /* No screen found? Then wrap */
149 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
151 t_ws = &(workspaces[screen->current_workspace]);
152 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
155 check_colrow_boundaries();
157 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
158 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
159 LOG("Cell empty, checking for colspanned client above...\n");
160 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
161 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
167 LOG("Fixed it to new col %d\n", new_col);
169 } else if (direction == D_LEFT || direction == D_RIGHT) {
170 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
171 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
172 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
173 /* Set new_col as a sane default, but it may get overwritten in a second */
176 /* Search from the left to correctly handle colspanned containers */
177 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
178 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
185 /* Let’s see if there is a screen left/right here to which we can switch */
186 LOG("container is at %d with width %d\n", container->x, container->width);
188 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
189 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
190 LOG("Wrapping screen around horizontally\n");
191 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
193 t_ws = &(workspaces[screen->current_workspace]);
194 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
197 check_colrow_boundaries();
199 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
200 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
201 LOG("Cell empty, checking for rowspanned client above...\n");
202 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
203 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
209 LOG("Fixed it to new row %d\n", new_row);
212 LOG("direction unhandled\n");
216 check_colrow_boundaries();
218 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
219 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
223 * Tries to move the window inside its current container.
225 * Returns true if the window could be moved, false otherwise.
228 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
229 direction_t direction) {
230 assert(client->container != NULL);
232 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
233 CIRCLEQ_NEXT(client, clients));
235 if (other == CIRCLEQ_END(&(client->container->clients)))
238 LOG("i can do that\n");
239 /* We can move the client inside its current container */
240 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
241 if (direction == D_UP)
242 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
243 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
249 * Moves the current window or whole container to the given direction, creating a column/row if
253 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
254 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
255 (direction == D_LEFT ? "left" : "right"))));
256 /* Get current window */
257 Container *container = CUR_CELL,
260 /* There has to be a container, see focus_window() */
261 assert(container != NULL);
263 /* If there is no window or the dock window is focused, we’re done */
264 if (container->currently_focused == NULL ||
265 container->currently_focused->dock)
268 /* As soon as the client is moved away, the last focused client in the old
269 * container needs to get focus, if any. Therefore, we save it here. */
270 Client *current_client = container->currently_focused;
271 Client *to_focus = get_last_focused_client(conn, container, current_client);
273 if (to_focus == NULL) {
274 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
275 if (to_focus == NULL)
276 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
281 /* If we’re at the left-most position, move the rest of the table right */
282 if (current_col == 0) {
283 expand_table_cols_at_head(c_ws);
286 new = CUR_TABLE[--current_col][current_row];
289 if (current_col == (c_ws->cols-1))
290 expand_table_cols(c_ws);
292 new = CUR_TABLE[++current_col][current_row];
295 if (move_current_window_in_container(conn, current_client, D_UP))
298 /* if we’re at the up-most position, move the rest of the table down */
299 if (current_row == 0) {
300 expand_table_rows_at_head(c_ws);
303 new = CUR_TABLE[current_col][--current_row];
306 if (move_current_window_in_container(conn, current_client, D_DOWN))
309 if (current_row == (c_ws->rows-1))
310 expand_table_rows(c_ws);
312 new = CUR_TABLE[current_col][++current_row];
314 /* To make static analyzers happy: */
319 /* Remove it from the old container and put it into the new one */
320 client_remove_from_container(conn, current_client, container, true);
322 if (new->currently_focused != NULL)
323 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
324 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
325 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
327 /* Update data structures */
328 current_client->container = new;
329 current_client->workspace = new->workspace;
330 container->currently_focused = to_focus;
331 new->currently_focused = current_client;
333 Workspace *workspace = container->workspace;
335 /* delete all empty columns/rows */
336 cleanup_table(conn, workspace);
338 /* Fix colspan/rowspan if it’d overlap */
339 fix_colrowspan(conn, workspace);
341 render_workspace(conn, workspace->screen, workspace);
344 set_focus(conn, current_client, true);
347 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
348 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
349 (direction == D_LEFT ? "left" : "right"))));
350 /* Get current window */
351 Container *container = CUR_CELL,
354 Container **old = &CUR_CELL;
356 /* There has to be a container, see focus_window() */
357 assert(container != NULL);
361 /* If we’re at the left-most position, move the rest of the table right */
362 if (current_col == 0) {
363 expand_table_cols_at_head(c_ws);
365 old = &CUR_TABLE[current_col+1][current_row];
367 new = CUR_TABLE[--current_col][current_row];
370 if (current_col == (c_ws->cols-1))
371 expand_table_cols(c_ws);
373 new = CUR_TABLE[++current_col][current_row];
376 /* if we’re at the up-most position, move the rest of the table down */
377 if (current_row == 0) {
378 expand_table_rows_at_head(c_ws);
380 old = &CUR_TABLE[current_col][current_row+1];
382 new = CUR_TABLE[current_col][--current_row];
385 if (current_row == (c_ws->rows-1))
386 expand_table_rows(c_ws);
388 new = CUR_TABLE[current_col][++current_row];
390 /* To make static analyzers happy: */
395 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
397 /* Swap the containers */
402 new->col = container->col;
403 new->row = container->row;
405 CUR_CELL = container;
406 container->col = col;
407 container->row = row;
409 Workspace *workspace = container->workspace;
411 /* delete all empty columns/rows */
412 cleanup_table(conn, workspace);
414 /* Fix colspan/rowspan if it’d overlap */
415 fix_colrowspan(conn, workspace);
421 * "Snaps" the current container (not possible for windows, because it works at table base)
422 * to the given direction, that is, adjusts cellspan/rowspan
425 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
426 LOG("snapping container to direction %d\n", direction);
428 Container *container = CUR_CELL;
430 assert(container != NULL);
434 /* Snap to the left is actually a move to the left and then a snap right */
435 if (!cell_exists(container->col - 1, container->row) ||
436 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
437 LOG("cannot snap to left - the cell is already used\n");
441 move_current_window(conn, D_LEFT);
442 snap_current_container(conn, D_RIGHT);
445 /* Check if the cell is used */
446 int new_col = container->col + container->colspan;
447 for (int i = 0; i < container->rowspan; i++)
448 if (!cell_exists(new_col, container->row + i) ||
449 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
450 LOG("cannot snap to right - the cell is already used\n");
454 /* Check if there are other cells with rowspan, which are in our way.
455 * If so, reduce their rowspan. */
456 for (int i = container->row-1; i >= 0; i--) {
457 LOG("we got cell %d, %d with rowspan %d\n",
458 new_col, i, CUR_TABLE[new_col][i]->rowspan);
459 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
460 CUR_TABLE[new_col][i]->rowspan--;
461 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
464 container->colspan++;
468 if (!cell_exists(container->col, container->row - 1) ||
469 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
470 LOG("cannot snap to top - the cell is already used\n");
474 move_current_window(conn, D_UP);
475 snap_current_container(conn, D_DOWN);
478 LOG("snapping down\n");
479 int new_row = container->row + container->rowspan;
480 for (int i = 0; i < container->colspan; i++)
481 if (!cell_exists(container->col + i, new_row) ||
482 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
483 LOG("cannot snap down - the cell is already used\n");
487 for (int i = container->col-1; i >= 0; i--) {
488 LOG("we got cell %d, %d with colspan %d\n",
489 i, new_row, CUR_TABLE[i][new_row]->colspan);
490 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
491 CUR_TABLE[i][new_row]->colspan--;
492 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
496 container->rowspan++;
499 /* To make static analyzers happy: */
507 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
508 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
509 Workspace *t_ws = &(workspaces[workspace-1]),
510 *old_ws = client->workspace;
512 LOG("moving floating\n");
514 if (t_ws->screen == NULL) {
515 LOG("initializing new workspace, setting num to %d\n", workspace-1);
516 t_ws->screen = c_ws->screen;
517 /* Copy the dimensions from the virtual screen */
518 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
520 /* Check if there is already a fullscreen client on the destination workspace and
521 * stop moving if so. */
522 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
523 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
528 floating_assign_to_workspace(client, t_ws);
530 /* If we’re moving it to an invisible screen, we need to unmap it */
531 if (!workspace_is_visible(t_ws)) {
532 LOG("This workspace is not visible, unmapping\n");
533 client_unmap(conn, client);
535 /* If this is not the case, we move the window to a workspace
536 * which is on another screen, so we also need to adjust its
538 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
539 uint32_t relative_x = client->rect.x - old_ws->rect.x,
540 relative_y = client->rect.y - old_ws->rect.y;
541 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
542 client->rect.x = t_ws->rect.x + relative_x;
543 client->rect.y = t_ws->rect.y + relative_y;
544 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
545 reposition_client(conn, client);
553 if (workspace_is_visible(t_ws))
554 set_focus(conn, client, true);
558 * Moves the currently selected window to the given workspace
561 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
562 LOG("Moving current window to workspace %d\n", workspace);
564 Container *container = CUR_CELL;
566 assert(container != NULL);
568 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
569 Workspace *t_ws = &(workspaces[workspace-1]);
571 Client *current_client = container->currently_focused;
572 if (current_client == NULL) {
573 LOG("No currently focused client in current container.\n");
576 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
577 if (to_focus == NULL)
578 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
580 if (t_ws->screen == NULL) {
581 LOG("initializing new workspace, setting num to %d\n", workspace-1);
582 t_ws->screen = container->workspace->screen;
583 /* Copy the dimensions from the virtual screen */
584 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
586 /* Check if there is already a fullscreen client on the destination workspace and
587 * stop moving if so. */
588 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
589 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
594 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
596 assert(to_container != NULL);
598 client_remove_from_container(conn, current_client, container, true);
599 if (container->workspace->fullscreen_client == current_client)
600 container->workspace->fullscreen_client = NULL;
602 /* TODO: insert it to the correct position */
603 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
605 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
608 current_client->container = to_container;
609 current_client->workspace = to_container->workspace;
610 container->currently_focused = to_focus;
611 to_container->currently_focused = current_client;
613 /* If we’re moving it to an invisible screen, we need to unmap it */
614 if (!workspace_is_visible(to_container->workspace)) {
615 LOG("This workspace is not visible, unmapping\n");
616 client_unmap(conn, current_client);
618 if (current_client->fullscreen) {
619 LOG("Calling client_enter_fullscreen again\n");
620 client_enter_fullscreen(conn, current_client);
624 /* delete all empty columns/rows */
625 cleanup_table(conn, container->workspace);
629 if (workspace_is_visible(to_container->workspace))
630 set_focus(conn, current_client, true);
634 * Jumps to the given window class / title.
635 * Title is matched using strstr, that is, matches if it appears anywhere
636 * in the string. Regular expressions seem to be a bit overkill here. However,
637 * if we need them for something else somewhen, we may introduce them here, too.
640 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
644 /* The first character is a quote, this was checked before */
645 classtitle = sstrdup(arguments+1);
646 /* The last character is a quote, we just set it to NULL */
647 classtitle[strlen(classtitle)-1] = '\0';
649 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
651 LOG("No matching client found.\n");
656 set_focus(conn, client, true);
660 * Jump directly to the specified workspace, row and col.
661 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
664 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
668 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
669 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
671 /* No match? Either no arguments were specified, or no numbers */
673 LOG("At least one valid argument required\n");
677 /* Move to the target workspace */
678 workspace_show(conn, ws);
683 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
685 /* Move to row/col */
686 if (row >= c_ws->rows)
687 row = c_ws->rows - 1;
688 if (col >= c_ws->cols)
689 col = c_ws->cols - 1;
691 LOG("Jumping to col %d, row %d\n", col, row);
692 if (c_ws->table[col][row]->currently_focused != NULL)
693 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
697 * Travels the focus stack by the given number of times (or once, if no argument
698 * was specified). That is, selects the window you were in before you focused
699 * the current window.
701 * The special values 'floating' (select the next floating window), 'tiling'
702 * (select the next tiling window), 'ft' (if the current window is floating,
703 * select the next tiling window and vice-versa) are also valid
706 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
707 /* Start count at -1 to always skip the first element */
708 int times, count = -1;
710 bool floating_criteria;
712 /* Either it’s one of the special values… */
713 if (strcasecmp(arguments, "floating") == 0) {
714 floating_criteria = true;
715 } else if (strcasecmp(arguments, "tiling") == 0) {
716 floating_criteria = false;
717 } else if (strcasecmp(arguments, "ft") == 0) {
718 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
719 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
720 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
724 floating_criteria = !client_is_floating(last_focused);
726 /* …or a number was specified */
727 if (sscanf(arguments, "%u", ×) != 1) {
728 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
732 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
733 if (++count < times) {
739 set_focus(conn, current, true);
745 /* Select the next client matching the criteria parsed above */
746 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
747 if (client_is_floating(current) == floating_criteria) {
748 set_focus(conn, current, true);
754 * Goes through the list of arguments (for exec()) and checks if the given argument
755 * is present. If not, it copies the arguments (because we cannot realloc it) and
756 * appends the given argument.
759 static char **append_argument(char **original, char *argument) {
761 for (num_args = 0; original[num_args] != NULL; num_args++) {
762 LOG("original argument: \"%s\"\n", original[num_args]);
763 /* If the argument is already present we return the original pointer */
764 if (strcmp(original[num_args], argument) == 0)
767 /* Copy the original array */
768 char **result = smalloc((num_args+2) * sizeof(char*));
769 memcpy(result, original, num_args * sizeof(char*));
770 result[num_args] = argument;
771 result[num_args+1] = NULL;
777 * Switch to next or previous existing workspace
780 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
784 if (direction == 'n') {
785 /* If we are on the last workspace, we cannot go any further */
789 for (i = c_ws->num + 1; i <= 9; i++) {
790 t_ws = &(workspaces[i]);
791 if (t_ws->screen != NULL)
794 } else if (direction == 'p') {
797 for (i = c_ws->num - 1; i >= 0 ; i--) {
798 t_ws = &(workspaces[i]);
799 if (t_ws->screen != NULL)
804 if (t_ws->screen != NULL)
805 workspace_show(conn, i+1);
809 * Parses a command, see file CMDMODE for more information
812 void parse_command(xcb_connection_t *conn, const char *command) {
813 LOG("--- parsing command \"%s\" ---\n", command);
814 /* Get the first client from focus stack because floating clients are not
815 * in any container, therefore CUR_CELL is not appropriate. */
816 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
817 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
820 /* Hmm, just to be sure */
821 if (command[0] == '\0')
824 /* Is it an <exec>? Then execute the given command. */
825 if (STARTS_WITH(command, "exec ")) {
826 LOG("starting \"%s\"\n", command + strlen("exec "));
827 start_application(command+strlen("exec "));
831 /* Is it an <exit>? */
832 if (STARTS_WITH(command, "exit")) {
833 LOG("User issued exit-command, exiting without error.\n");
837 /* Is it a <reload>? */
838 if (STARTS_WITH(command, "reload")) {
839 load_configuration(conn, NULL, true);
843 /* Is it <restart>? Then restart in place. */
844 if (STARTS_WITH(command, "restart")) {
845 LOG("restarting \"%s\"...\n", start_argv[0]);
846 /* make sure -a is in the argument list or append it */
847 start_argv = append_argument(start_argv, "-a");
849 execvp(start_argv[0], start_argv);
853 if (STARTS_WITH(command, "kill")) {
854 if (last_focused == NULL) {
855 LOG("There is no window to kill\n");
859 LOG("Killing current window\n");
860 client_kill(conn, last_focused);
864 /* Is it a jump to a specified workspace, row, col? */
865 if (STARTS_WITH(command, "jump ")) {
866 const char *arguments = command + strlen("jump ");
867 if (arguments[0] == '"')
868 jump_to_window(conn, arguments);
869 else jump_to_container(conn, arguments);
873 /* Should we travel the focus stack? */
874 if (STARTS_WITH(command, "focus")) {
875 const char *arguments = command + strlen("focus ");
876 travel_focus_stack(conn, arguments);
880 /* Is it 'f' for fullscreen? */
881 if (command[0] == 'f') {
882 if (last_focused == NULL)
884 client_toggle_fullscreen(conn, last_focused);
888 /* Is it just 's' for stacking or 'd' for default? */
889 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
890 if (last_focused != NULL && client_is_floating(last_focused)) {
891 LOG("not switching, this is a floating client\n");
894 LOG("Switching mode for current container\n");
895 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
899 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
900 if (command[0] == 'b') {
901 if (last_focused == NULL) {
902 LOG("No window focused, cannot change border type\n");
905 client_change_border(conn, last_focused, command[1]);
909 if (command[0] == 'H') {
910 LOG("Hiding all floating windows\n");
911 floating_toggle_hide(conn, c_ws);
915 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
917 /* Is it a <with>? */
918 if (command[0] == 'w') {
920 /* TODO: implement */
921 if (command[0] == 'c') {
922 with = WITH_CONTAINER;
924 } else if (command[0] == 'w') {
925 with = WITH_WORKSPACE;
927 } else if (command[0] == 's') {
931 LOG("not yet implemented.\n");
936 /* Is it 't' for toggle tiling/floating? */
937 if (command[0] == 't') {
938 if (with == WITH_WORKSPACE) {
939 c_ws->auto_float = !c_ws->auto_float;
940 LOG("autofloat is now %d\n", c_ws->auto_float);
943 if (last_focused == NULL) {
944 LOG("Cannot toggle tiling/floating: workspace empty\n");
948 toggle_floating_mode(conn, last_focused, false);
949 /* delete all empty columns/rows */
950 cleanup_table(conn, last_focused->workspace);
952 /* Fix colspan/rowspan if it’d overlap */
953 fix_colrowspan(conn, last_focused->workspace);
955 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
957 /* Re-focus the client because cleanup_table sets the focus to the last
958 * focused client inside a container only. */
959 set_focus(conn, last_focused, true);
964 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
965 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
966 next_previous_workspace(conn, command[0]);
970 /* It’s a normal <cmd> */
972 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
973 direction_t direction;
974 int times = strtol(command, &rest, 10);
976 LOG("Invalid command (\"%s\")\n", command);
981 /* No rest? This was a workspace number, not a times specification */
982 workspace_show(conn, times);
986 if (*rest == 'm' || *rest == 's') {
987 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
991 int workspace = strtol(rest, &rest, 10);
994 LOG("Invalid command (\"%s\")\n", command);
999 if (last_focused != NULL && client_is_floating(last_focused))
1000 move_floating_window_to_workspace(conn, last_focused, workspace);
1001 else move_current_window_to_workspace(conn, workspace);
1005 if (last_focused == NULL) {
1006 LOG("Not performing (no window found)\n");
1010 if (client_is_floating(last_focused) &&
1011 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1012 LOG("Not performing (floating)\n");
1016 /* Now perform action to <where> */
1017 while (*rest != '\0') {
1020 else if (*rest == 'j')
1022 else if (*rest == 'k')
1024 else if (*rest == 'l')
1025 direction = D_RIGHT;
1027 LOG("unknown direction: %c\n", *rest);
1032 if (action == ACTION_FOCUS) {
1033 if (with == WITH_SCREEN) {
1034 focus_thing(conn, direction, THING_SCREEN);
1037 if (client_is_floating(last_focused)) {
1038 floating_focus_direction(conn, last_focused, direction);
1041 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1045 if (action == ACTION_MOVE) {
1046 if (with == WITH_SCREEN) {
1047 /* TODO: this should swap the screen’s contents
1048 * (e.g. all workspaces) with the next/previous/…
1050 LOG("Not yet implemented\n");
1053 if (client_is_floating(last_focused)) {
1054 floating_move(conn, last_focused, direction);
1057 if (with == WITH_WINDOW)
1058 move_current_window(conn, direction);
1059 else move_current_container(conn, direction);
1063 if (action == ACTION_SNAP) {
1064 if (with == WITH_SCREEN) {
1065 LOG("You cannot snap a screen (it makes no sense).\n");
1068 snap_current_container(conn, direction);