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 workspace_initialize(t_ws, c_ws->screen);
516 /* Check if there is already a fullscreen client on the destination workspace and
517 * stop moving if so. */
518 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
519 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
523 floating_assign_to_workspace(client, t_ws);
525 /* If we’re moving it to an invisible screen, we need to unmap it */
526 if (!workspace_is_visible(t_ws)) {
527 LOG("This workspace is not visible, unmapping\n");
528 client_unmap(conn, client);
530 /* If this is not the case, we move the window to a workspace
531 * which is on another screen, so we also need to adjust its
533 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
534 uint32_t relative_x = client->rect.x - old_ws->rect.x,
535 relative_y = client->rect.y - old_ws->rect.y;
536 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
537 client->rect.x = t_ws->rect.x + relative_x;
538 client->rect.y = t_ws->rect.y + relative_y;
539 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
540 reposition_client(conn, client);
548 if (workspace_is_visible(t_ws))
549 set_focus(conn, client, true);
553 * Moves the currently selected window to the given workspace
556 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
557 LOG("Moving current window to workspace %d\n", workspace);
559 Container *container = CUR_CELL;
561 assert(container != NULL);
563 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
564 Workspace *t_ws = &(workspaces[workspace-1]);
566 Client *current_client = container->currently_focused;
567 if (current_client == NULL) {
568 LOG("No currently focused client in current container.\n");
571 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
572 if (to_focus == NULL)
573 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
575 workspace_initialize(t_ws, container->workspace->screen);
576 /* Check if there is already a fullscreen client on the destination workspace and
577 * stop moving if so. */
578 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
579 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
583 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
585 assert(to_container != NULL);
587 client_remove_from_container(conn, current_client, container, true);
588 if (container->workspace->fullscreen_client == current_client)
589 container->workspace->fullscreen_client = NULL;
591 /* TODO: insert it to the correct position */
592 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
594 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
597 current_client->container = to_container;
598 current_client->workspace = to_container->workspace;
599 container->currently_focused = to_focus;
600 to_container->currently_focused = current_client;
602 /* If we’re moving it to an invisible screen, we need to unmap it */
603 if (!workspace_is_visible(to_container->workspace)) {
604 LOG("This workspace is not visible, unmapping\n");
605 client_unmap(conn, current_client);
607 if (current_client->fullscreen) {
608 LOG("Calling client_enter_fullscreen again\n");
609 client_enter_fullscreen(conn, current_client);
613 /* delete all empty columns/rows */
614 cleanup_table(conn, container->workspace);
618 if (workspace_is_visible(to_container->workspace))
619 set_focus(conn, current_client, true);
623 * Jumps to the given window class / title.
624 * Title is matched using strstr, that is, matches if it appears anywhere
625 * in the string. Regular expressions seem to be a bit overkill here. However,
626 * if we need them for something else somewhen, we may introduce them here, too.
629 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
633 /* The first character is a quote, this was checked before */
634 classtitle = sstrdup(arguments+1);
635 /* The last character is a quote, we just set it to NULL */
636 classtitle[strlen(classtitle)-1] = '\0';
638 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
640 LOG("No matching client found.\n");
645 set_focus(conn, client, true);
649 * Jump directly to the specified workspace, row and col.
650 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
653 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
657 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
658 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
660 /* No match? Either no arguments were specified, or no numbers */
662 LOG("At least one valid argument required\n");
666 /* Move to the target workspace */
667 workspace_show(conn, ws);
672 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
674 /* Move to row/col */
675 if (row >= c_ws->rows)
676 row = c_ws->rows - 1;
677 if (col >= c_ws->cols)
678 col = c_ws->cols - 1;
680 LOG("Jumping to col %d, row %d\n", col, row);
681 if (c_ws->table[col][row]->currently_focused != NULL)
682 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
686 * Travels the focus stack by the given number of times (or once, if no argument
687 * was specified). That is, selects the window you were in before you focused
688 * the current window.
690 * The special values 'floating' (select the next floating window), 'tiling'
691 * (select the next tiling window), 'ft' (if the current window is floating,
692 * select the next tiling window and vice-versa) are also valid
695 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
696 /* Start count at -1 to always skip the first element */
697 int times, count = -1;
699 bool floating_criteria;
701 /* Either it’s one of the special values… */
702 if (strcasecmp(arguments, "floating") == 0) {
703 floating_criteria = true;
704 } else if (strcasecmp(arguments, "tiling") == 0) {
705 floating_criteria = false;
706 } else if (strcasecmp(arguments, "ft") == 0) {
707 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
708 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
709 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
713 floating_criteria = !client_is_floating(last_focused);
715 /* …or a number was specified */
716 if (sscanf(arguments, "%u", ×) != 1) {
717 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
721 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
722 if (++count < times) {
728 set_focus(conn, current, true);
734 /* Select the next client matching the criteria parsed above */
735 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
736 if (client_is_floating(current) == floating_criteria) {
737 set_focus(conn, current, true);
743 * Goes through the list of arguments (for exec()) and checks if the given argument
744 * is present. If not, it copies the arguments (because we cannot realloc it) and
745 * appends the given argument.
748 static char **append_argument(char **original, char *argument) {
750 for (num_args = 0; original[num_args] != NULL; num_args++) {
751 LOG("original argument: \"%s\"\n", original[num_args]);
752 /* If the argument is already present we return the original pointer */
753 if (strcmp(original[num_args], argument) == 0)
756 /* Copy the original array */
757 char **result = smalloc((num_args+2) * sizeof(char*));
758 memcpy(result, original, num_args * sizeof(char*));
759 result[num_args] = argument;
760 result[num_args+1] = NULL;
766 * Switch to next or previous existing workspace
769 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
773 if (direction == 'n') {
774 /* If we are on the last workspace, we cannot go any further */
778 for (i = c_ws->num + 1; i <= 9; i++) {
779 t_ws = &(workspaces[i]);
780 if (t_ws->screen != NULL)
783 } else if (direction == 'p') {
786 for (i = c_ws->num - 1; i >= 0 ; i--) {
787 t_ws = &(workspaces[i]);
788 if (t_ws->screen != NULL)
793 if (t_ws->screen != NULL)
794 workspace_show(conn, i+1);
798 * Parses a command, see file CMDMODE for more information
801 void parse_command(xcb_connection_t *conn, const char *command) {
802 LOG("--- parsing command \"%s\" ---\n", command);
803 /* Get the first client from focus stack because floating clients are not
804 * in any container, therefore CUR_CELL is not appropriate. */
805 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
806 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
809 /* Hmm, just to be sure */
810 if (command[0] == '\0')
813 /* Is it an <exec>? Then execute the given command. */
814 if (STARTS_WITH(command, "exec ")) {
815 LOG("starting \"%s\"\n", command + strlen("exec "));
816 start_application(command+strlen("exec "));
820 /* Is it an <exit>? */
821 if (STARTS_WITH(command, "exit")) {
822 LOG("User issued exit-command, exiting without error.\n");
826 /* Is it a <reload>? */
827 if (STARTS_WITH(command, "reload")) {
828 load_configuration(conn, NULL, true);
832 /* Is it <restart>? Then restart in place. */
833 if (STARTS_WITH(command, "restart")) {
834 LOG("restarting \"%s\"...\n", start_argv[0]);
835 /* make sure -a is in the argument list or append it */
836 start_argv = append_argument(start_argv, "-a");
838 execvp(start_argv[0], start_argv);
842 if (STARTS_WITH(command, "kill")) {
843 if (last_focused == NULL) {
844 LOG("There is no window to kill\n");
848 LOG("Killing current window\n");
849 client_kill(conn, last_focused);
853 /* Is it a jump to a specified workspace, row, col? */
854 if (STARTS_WITH(command, "jump ")) {
855 const char *arguments = command + strlen("jump ");
856 if (arguments[0] == '"')
857 jump_to_window(conn, arguments);
858 else jump_to_container(conn, arguments);
862 /* Should we travel the focus stack? */
863 if (STARTS_WITH(command, "focus")) {
864 const char *arguments = command + strlen("focus ");
865 travel_focus_stack(conn, arguments);
869 /* Is it 'f' for fullscreen? */
870 if (command[0] == 'f') {
871 if (last_focused == NULL)
873 client_toggle_fullscreen(conn, last_focused);
877 /* Is it just 's' for stacking or 'd' for default? */
878 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
879 if (last_focused != NULL && client_is_floating(last_focused)) {
880 LOG("not switching, this is a floating client\n");
883 LOG("Switching mode for current container\n");
884 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
888 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
889 if (command[0] == 'b') {
890 if (last_focused == NULL) {
891 LOG("No window focused, cannot change border type\n");
894 client_change_border(conn, last_focused, command[1]);
898 if (command[0] == 'H') {
899 LOG("Hiding all floating windows\n");
900 floating_toggle_hide(conn, c_ws);
904 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
906 /* Is it a <with>? */
907 if (command[0] == 'w') {
909 /* TODO: implement */
910 if (command[0] == 'c') {
911 with = WITH_CONTAINER;
913 } else if (command[0] == 'w') {
914 with = WITH_WORKSPACE;
916 } else if (command[0] == 's') {
920 LOG("not yet implemented.\n");
925 /* Is it 't' for toggle tiling/floating? */
926 if (command[0] == 't') {
927 if (with == WITH_WORKSPACE) {
928 c_ws->auto_float = !c_ws->auto_float;
929 LOG("autofloat is now %d\n", c_ws->auto_float);
932 if (last_focused == NULL) {
933 LOG("Cannot toggle tiling/floating: workspace empty\n");
937 toggle_floating_mode(conn, last_focused, false);
938 /* delete all empty columns/rows */
939 cleanup_table(conn, last_focused->workspace);
941 /* Fix colspan/rowspan if it’d overlap */
942 fix_colrowspan(conn, last_focused->workspace);
944 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
946 /* Re-focus the client because cleanup_table sets the focus to the last
947 * focused client inside a container only. */
948 set_focus(conn, last_focused, true);
953 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
954 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
955 next_previous_workspace(conn, command[0]);
959 /* It’s a normal <cmd> */
961 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
962 direction_t direction;
963 int times = strtol(command, &rest, 10);
965 LOG("Invalid command (\"%s\")\n", command);
970 /* No rest? This was a workspace number, not a times specification */
971 workspace_show(conn, times);
975 if (*rest == 'm' || *rest == 's') {
976 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
980 int workspace = strtol(rest, &rest, 10);
983 LOG("Invalid command (\"%s\")\n", command);
988 if (last_focused != NULL && client_is_floating(last_focused))
989 move_floating_window_to_workspace(conn, last_focused, workspace);
990 else move_current_window_to_workspace(conn, workspace);
994 if (last_focused == NULL) {
995 LOG("Not performing (no window found)\n");
999 if (client_is_floating(last_focused) &&
1000 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1001 LOG("Not performing (floating)\n");
1005 /* Now perform action to <where> */
1006 while (*rest != '\0') {
1009 else if (*rest == 'j')
1011 else if (*rest == 'k')
1013 else if (*rest == 'l')
1014 direction = D_RIGHT;
1016 LOG("unknown direction: %c\n", *rest);
1021 if (action == ACTION_FOCUS) {
1022 if (with == WITH_SCREEN) {
1023 focus_thing(conn, direction, THING_SCREEN);
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 (with == WITH_SCREEN) {
1036 /* TODO: this should swap the screen’s contents
1037 * (e.g. all workspaces) with the next/previous/…
1039 LOG("Not yet implemented\n");
1042 if (client_is_floating(last_focused)) {
1043 floating_move(conn, last_focused, direction);
1046 if (with == WITH_WINDOW)
1047 move_current_window(conn, direction);
1048 else move_current_container(conn, direction);
1052 if (action == ACTION_SNAP) {
1053 if (with == WITH_SCREEN) {
1054 LOG("You cannot snap a screen (it makes no sense).\n");
1057 snap_current_container(conn, direction);