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"
34 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
35 /* If this container is empty, we’re done */
36 if (container->currently_focused == NULL)
39 /* Get the previous/next client or wrap around */
40 Client *candidate = NULL;
41 if (direction == D_UP) {
42 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
43 candidate = CIRCLEQ_LAST(&(container->clients));
45 else if (direction == D_DOWN) {
46 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
47 candidate = CIRCLEQ_FIRST(&(container->clients));
48 } else LOG("Direction not implemented!\n");
50 /* If we could not switch, the container contains exactly one client. We return false */
51 if (candidate == container->currently_focused)
55 set_focus(conn, candidate, true);
60 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
62 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
64 LOG("Jumping to \"%s\"\n", mark);
67 TAILQ_FOREACH(ws, workspaces, workspaces)
68 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
69 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
72 workspace_show(conn, current->workspace->num + 1);
73 set_focus(conn, current, true);
77 LOG("No window with this mark found\n");
80 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
81 LOG("focusing direction %d\n", direction);
83 int new_row = current_row,
84 new_col = current_col;
85 Container *container = CUR_CELL;
86 Workspace *t_ws = c_ws;
88 /* Makes sure new_col and new_row are within bounds of the new workspace */
89 void check_colrow_boundaries() {
90 if (new_col >= t_ws->cols)
91 new_col = (t_ws->cols - 1);
92 if (new_row >= t_ws->rows)
93 new_row = (t_ws->rows - 1);
96 /* There always is a container. If not, current_col or current_row is wrong */
97 assert(container != NULL);
99 if (container->workspace->fullscreen_client != NULL) {
100 LOG("You're in fullscreen mode. Won't switch focus\n");
104 /* For focusing screens, situation is different: we get the rect
105 * of the current screen, then get the screen which is on its
106 * right/left/bottom/top and just switch to the workspace on
107 * the target screen. */
108 if (thing == THING_SCREEN) {
109 i3Screen *cs = c_ws->screen;
111 Rect bounds = cs->rect;
113 if (direction == D_RIGHT)
114 bounds.x += bounds.width;
115 else if (direction == D_LEFT)
116 bounds.x -= bounds.width;
117 else if (direction == D_UP)
118 bounds.y -= bounds.height;
119 else bounds.y += bounds.height;
121 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
122 if (target == NULL) {
123 LOG("Target screen NULL\n");
124 /* Wrap around if the target screen is out of bounds */
125 if (direction == D_RIGHT)
126 target = get_screen_most(D_LEFT, cs);
127 else if (direction == D_LEFT)
128 target = get_screen_most(D_RIGHT, cs);
129 else if (direction == D_UP)
130 target = get_screen_most(D_DOWN, cs);
131 else target = get_screen_most(D_UP, cs);
134 LOG("Switching to ws %d\n", target->current_workspace + 1);
135 workspace_show(conn, target->current_workspace->num + 1);
139 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
140 if (direction == D_UP || direction == D_DOWN) {
141 if (thing == THING_WINDOW)
142 /* Let’s see if we can perform up/down focus in the current container */
143 if (focus_window_in_container(conn, container, direction))
146 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
147 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
148 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
149 /* Set new_row as a sane default, but it may get overwritten in a second */
152 /* Search from the top to correctly handle rowspanned containers */
153 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
154 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
161 /* Let’s see if there is a screen down/up there to which we can switch */
162 LOG("container is at %d with height %d\n", container->y, container->height);
164 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
165 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
166 LOG("Wrapping screen around vertically\n");
167 /* No screen found? Then wrap */
168 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
170 t_ws = screen->current_workspace;
171 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
174 check_colrow_boundaries();
176 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
177 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
178 LOG("Cell empty, checking for colspanned client above...\n");
179 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
180 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
186 LOG("Fixed it to new col %d\n", new_col);
188 } else if (direction == D_LEFT || direction == D_RIGHT) {
189 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
190 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
191 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
192 /* Set new_col as a sane default, but it may get overwritten in a second */
195 /* Search from the left to correctly handle colspanned containers */
196 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
197 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
204 /* Let’s see if there is a screen left/right here to which we can switch */
205 LOG("container is at %d with width %d\n", container->x, container->width);
207 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
208 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
209 LOG("Wrapping screen around horizontally\n");
210 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
212 t_ws = screen->current_workspace;
213 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
216 check_colrow_boundaries();
218 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
219 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
220 LOG("Cell empty, checking for rowspanned client above...\n");
221 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
222 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
228 LOG("Fixed it to new row %d\n", new_row);
231 LOG("direction unhandled\n");
235 check_colrow_boundaries();
237 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
238 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
242 * Tries to move the window inside its current container.
244 * Returns true if the window could be moved, false otherwise.
247 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
248 direction_t direction) {
249 assert(client->container != NULL);
251 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
252 CIRCLEQ_NEXT(client, clients));
254 if (other == CIRCLEQ_END(&(client->container->clients)))
257 LOG("i can do that\n");
258 /* We can move the client inside its current container */
259 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
260 if (direction == D_UP)
261 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
262 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
268 * Moves the current window or whole container to the given direction, creating a column/row if
272 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
273 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
274 (direction == D_LEFT ? "left" : "right"))));
275 /* Get current window */
276 Container *container = CUR_CELL,
279 /* There has to be a container, see focus_window() */
280 assert(container != NULL);
282 /* If there is no window or the dock window is focused, we’re done */
283 if (container->currently_focused == NULL ||
284 container->currently_focused->dock)
287 /* As soon as the client is moved away, the last focused client in the old
288 * container needs to get focus, if any. Therefore, we save it here. */
289 Client *current_client = container->currently_focused;
290 Client *to_focus = get_last_focused_client(conn, container, current_client);
292 if (to_focus == NULL) {
293 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
294 if (to_focus == NULL)
295 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
300 /* If we’re at the left-most position, move the rest of the table right */
301 if (current_col == 0) {
302 expand_table_cols_at_head(c_ws);
305 new = CUR_TABLE[--current_col][current_row];
308 if (current_col == (c_ws->cols-1))
309 expand_table_cols(c_ws);
311 new = CUR_TABLE[++current_col][current_row];
314 if (move_current_window_in_container(conn, current_client, D_UP))
317 /* if we’re at the up-most position, move the rest of the table down */
318 if (current_row == 0) {
319 expand_table_rows_at_head(c_ws);
322 new = CUR_TABLE[current_col][--current_row];
325 if (move_current_window_in_container(conn, current_client, D_DOWN))
328 if (current_row == (c_ws->rows-1))
329 expand_table_rows(c_ws);
331 new = CUR_TABLE[current_col][++current_row];
333 /* To make static analyzers happy: */
338 /* Remove it from the old container and put it into the new one */
339 client_remove_from_container(conn, current_client, container, true);
341 if (new->currently_focused != NULL)
342 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
343 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
344 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
346 /* Update data structures */
347 current_client->container = new;
348 current_client->workspace = new->workspace;
349 container->currently_focused = to_focus;
350 new->currently_focused = current_client;
352 Workspace *workspace = container->workspace;
354 /* delete all empty columns/rows */
355 cleanup_table(conn, workspace);
357 /* Fix colspan/rowspan if it’d overlap */
358 fix_colrowspan(conn, workspace);
360 render_workspace(conn, workspace->screen, workspace);
363 set_focus(conn, current_client, true);
366 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
367 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
368 (direction == D_LEFT ? "left" : "right"))));
369 /* Get current window */
370 Container *container = CUR_CELL,
373 Container **old = &CUR_CELL;
375 /* There has to be a container, see focus_window() */
376 assert(container != NULL);
380 /* If we’re at the left-most position, move the rest of the table right */
381 if (current_col == 0) {
382 expand_table_cols_at_head(c_ws);
384 old = &CUR_TABLE[current_col+1][current_row];
386 new = CUR_TABLE[--current_col][current_row];
389 if (current_col == (c_ws->cols-1))
390 expand_table_cols(c_ws);
392 new = CUR_TABLE[++current_col][current_row];
395 /* if we’re at the up-most position, move the rest of the table down */
396 if (current_row == 0) {
397 expand_table_rows_at_head(c_ws);
399 old = &CUR_TABLE[current_col][current_row+1];
401 new = CUR_TABLE[current_col][--current_row];
404 if (current_row == (c_ws->rows-1))
405 expand_table_rows(c_ws);
407 new = CUR_TABLE[current_col][++current_row];
409 /* To make static analyzers happy: */
414 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
416 /* Swap the containers */
421 new->col = container->col;
422 new->row = container->row;
424 CUR_CELL = container;
425 container->col = col;
426 container->row = row;
428 Workspace *workspace = container->workspace;
430 /* delete all empty columns/rows */
431 cleanup_table(conn, workspace);
433 /* Fix colspan/rowspan if it’d overlap */
434 fix_colrowspan(conn, workspace);
440 * "Snaps" the current container (not possible for windows, because it works at table base)
441 * to the given direction, that is, adjusts cellspan/rowspan
444 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
445 LOG("snapping container to direction %d\n", direction);
447 Container *container = CUR_CELL;
449 assert(container != NULL);
453 /* Snap to the left is actually a move to the left and then a snap right */
454 if (!cell_exists(container->col - 1, container->row) ||
455 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
456 LOG("cannot snap to left - the cell is already used\n");
460 move_current_window(conn, D_LEFT);
461 snap_current_container(conn, D_RIGHT);
464 /* Check if the cell is used */
465 int new_col = container->col + container->colspan;
466 for (int i = 0; i < container->rowspan; i++)
467 if (!cell_exists(new_col, container->row + i) ||
468 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
469 LOG("cannot snap to right - the cell is already used\n");
473 /* Check if there are other cells with rowspan, which are in our way.
474 * If so, reduce their rowspan. */
475 for (int i = container->row-1; i >= 0; i--) {
476 LOG("we got cell %d, %d with rowspan %d\n",
477 new_col, i, CUR_TABLE[new_col][i]->rowspan);
478 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
479 CUR_TABLE[new_col][i]->rowspan--;
480 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
483 container->colspan++;
487 if (!cell_exists(container->col, container->row - 1) ||
488 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
489 LOG("cannot snap to top - the cell is already used\n");
493 move_current_window(conn, D_UP);
494 snap_current_container(conn, D_DOWN);
497 LOG("snapping down\n");
498 int new_row = container->row + container->rowspan;
499 for (int i = 0; i < container->colspan; i++)
500 if (!cell_exists(container->col + i, new_row) ||
501 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
502 LOG("cannot snap down - the cell is already used\n");
506 for (int i = container->col-1; i >= 0; i--) {
507 LOG("we got cell %d, %d with colspan %d\n",
508 i, new_row, CUR_TABLE[i][new_row]->colspan);
509 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
510 CUR_TABLE[i][new_row]->colspan--;
511 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
515 container->rowspan++;
518 /* To make static analyzers happy: */
526 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
527 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
528 Workspace *t_ws = workspace_get(workspace-1),
529 *old_ws = client->workspace;
531 LOG("moving floating\n");
533 workspace_initialize(t_ws, c_ws->screen);
535 /* Check if there is already a fullscreen client on the destination workspace and
536 * stop moving if so. */
537 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
538 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
542 floating_assign_to_workspace(client, t_ws);
544 /* If we’re moving it to an invisible screen, we need to unmap it */
545 if (!workspace_is_visible(t_ws)) {
546 LOG("This workspace is not visible, unmapping\n");
547 client_unmap(conn, client);
549 /* If this is not the case, we move the window to a workspace
550 * which is on another screen, so we also need to adjust its
552 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
553 uint32_t relative_x = client->rect.x - old_ws->rect.x,
554 relative_y = client->rect.y - old_ws->rect.y;
555 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
556 client->rect.x = t_ws->rect.x + relative_x;
557 client->rect.y = t_ws->rect.y + relative_y;
558 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
559 reposition_client(conn, client);
567 if (workspace_is_visible(t_ws))
568 set_focus(conn, client, true);
572 * Moves the currently selected window to the given workspace
575 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
576 LOG("Moving current window to workspace %d\n", workspace);
578 Container *container = CUR_CELL;
580 assert(container != NULL);
582 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
583 Workspace *t_ws = workspace_get(workspace-1);
585 Client *current_client = container->currently_focused;
586 if (current_client == NULL) {
587 LOG("No currently focused client in current container.\n");
590 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
591 if (to_focus == NULL)
592 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
594 workspace_initialize(t_ws, container->workspace->screen);
595 /* Check if there is already a fullscreen client on the destination workspace and
596 * stop moving if so. */
597 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
598 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
602 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
604 assert(to_container != NULL);
606 client_remove_from_container(conn, current_client, container, true);
607 if (container->workspace->fullscreen_client == current_client)
608 container->workspace->fullscreen_client = NULL;
610 /* TODO: insert it to the correct position */
611 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
613 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
616 current_client->container = to_container;
617 current_client->workspace = to_container->workspace;
618 container->currently_focused = to_focus;
619 to_container->currently_focused = current_client;
621 /* If we’re moving it to an invisible screen, we need to unmap it */
622 if (!workspace_is_visible(to_container->workspace)) {
623 LOG("This workspace is not visible, unmapping\n");
624 client_unmap(conn, current_client);
626 if (current_client->fullscreen) {
627 LOG("Calling client_enter_fullscreen again\n");
628 client_enter_fullscreen(conn, current_client);
632 /* delete all empty columns/rows */
633 cleanup_table(conn, container->workspace);
637 if (workspace_is_visible(to_container->workspace))
638 set_focus(conn, current_client, true);
642 * Jumps to the given window class / title.
643 * Title is matched using strstr, that is, matches if it appears anywhere
644 * in the string. Regular expressions seem to be a bit overkill here. However,
645 * if we need them for something else somewhen, we may introduce them here, too.
648 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
652 /* The first character is a quote, this was checked before */
653 classtitle = sstrdup(arguments+1);
654 /* The last character is a quote, we just set it to NULL */
655 classtitle[strlen(classtitle)-1] = '\0';
657 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
659 LOG("No matching client found.\n");
664 set_focus(conn, client, true);
668 * Jump directly to the specified workspace, row and col.
669 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
672 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
676 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
677 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
679 /* No match? Either no arguments were specified, or no numbers */
681 LOG("At least one valid argument required\n");
685 /* Move to the target workspace */
686 workspace_show(conn, ws);
691 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
693 /* Move to row/col */
694 if (row >= c_ws->rows)
695 row = c_ws->rows - 1;
696 if (col >= c_ws->cols)
697 col = c_ws->cols - 1;
699 LOG("Jumping to col %d, row %d\n", col, row);
700 if (c_ws->table[col][row]->currently_focused != NULL)
701 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
705 * Travels the focus stack by the given number of times (or once, if no argument
706 * was specified). That is, selects the window you were in before you focused
707 * the current window.
709 * The special values 'floating' (select the next floating window), 'tiling'
710 * (select the next tiling window), 'ft' (if the current window is floating,
711 * select the next tiling window and vice-versa) are also valid
714 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
715 /* Start count at -1 to always skip the first element */
716 int times, count = -1;
718 bool floating_criteria;
720 /* Either it’s one of the special values… */
721 if (strcasecmp(arguments, "floating") == 0) {
722 floating_criteria = true;
723 } else if (strcasecmp(arguments, "tiling") == 0) {
724 floating_criteria = false;
725 } else if (strcasecmp(arguments, "ft") == 0) {
726 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
727 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
728 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
732 floating_criteria = !client_is_floating(last_focused);
734 /* …or a number was specified */
735 if (sscanf(arguments, "%u", ×) != 1) {
736 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
740 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
741 if (++count < times) {
747 set_focus(conn, current, true);
753 /* Select the next client matching the criteria parsed above */
754 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
755 if (client_is_floating(current) == floating_criteria) {
756 set_focus(conn, current, true);
762 * Goes through the list of arguments (for exec()) and checks if the given argument
763 * is present. If not, it copies the arguments (because we cannot realloc it) and
764 * appends the given argument.
767 static char **append_argument(char **original, char *argument) {
769 for (num_args = 0; original[num_args] != NULL; num_args++) {
770 LOG("original argument: \"%s\"\n", original[num_args]);
771 /* If the argument is already present we return the original pointer */
772 if (strcmp(original[num_args], argument) == 0)
775 /* Copy the original array */
776 char **result = smalloc((num_args+2) * sizeof(char*));
777 memcpy(result, original, num_args * sizeof(char*));
778 result[num_args] = argument;
779 result[num_args+1] = NULL;
785 * Switch to next or previous existing workspace
788 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
789 Workspace *ws = c_ws;
791 if (direction == 'n') {
792 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
793 if (ws->screen == NULL)
796 workspace_show(conn, ws->num + 1);
799 } else if (direction == 'p') {
800 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
801 if (ws->screen == NULL)
804 workspace_show(conn, ws->num + 1);
810 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
812 resize_orientation_t orientation = O_VERTICAL;
813 Container *con = last_focused->container;
814 Workspace *ws = con->workspace;
816 if (STARTS_WITH(command, "left")) {
819 first = con->col - 1;
821 command += strlen("left");
822 } else if (STARTS_WITH(command, "right")) {
823 first = con->col + (con->colspan - 1);
824 LOG("column %d\n", first);
826 if (!cell_exists(first, con->row) ||
827 (first == (ws->cols-1)))
831 command += strlen("right");
832 } else if (STARTS_WITH(command, "top")) {
835 first = con->row - 1;
837 orientation = O_HORIZONTAL;
838 command += strlen("top");
839 } else if (STARTS_WITH(command, "bottom")) {
840 first = con->row + (con->rowspan - 1);
841 if (!cell_exists(con->col, first) ||
842 (first == (ws->rows-1)))
846 orientation = O_HORIZONTAL;
847 command += strlen("bottom");
849 LOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
853 int pixels = atoi(command);
857 resize_container(conn, ws, first, second, orientation, pixels);
861 * Parses a command, see file CMDMODE for more information
864 void parse_command(xcb_connection_t *conn, const char *command) {
865 LOG("--- parsing command \"%s\" ---\n", command);
866 /* Get the first client from focus stack because floating clients are not
867 * in any container, therefore CUR_CELL is not appropriate. */
868 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
869 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
872 /* Hmm, just to be sure */
873 if (command[0] == '\0')
876 /* Is it an <exec>? Then execute the given command. */
877 if (STARTS_WITH(command, "exec ")) {
878 LOG("starting \"%s\"\n", command + strlen("exec "));
879 start_application(command+strlen("exec "));
883 if (STARTS_WITH(command, "mark")) {
884 if (last_focused == NULL) {
885 LOG("There is no window to mark\n");
888 const char *rest = command + strlen("mark");
892 LOG("interactive mark starting\n");
893 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
895 LOG("mark with \"%s\"\n", rest);
896 client_mark(conn, last_focused, rest);
901 if (STARTS_WITH(command, "goto")) {
902 const char *rest = command + strlen("goto");
906 LOG("interactive go to mark starting\n");
907 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
909 LOG("go to \"%s\"\n", rest);
910 jump_to_mark(conn, rest);
915 if (STARTS_WITH(command, "stack-limit ")) {
916 if (last_focused == NULL || client_is_floating(last_focused)) {
917 LOG("No container focused\n");
920 const char *rest = command + strlen("stack-limit ");
921 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
922 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
923 rest += strlen("rows ");
924 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
925 last_focused->container->stack_limit = STACK_LIMIT_COLS;
926 rest += strlen("cols ");
928 LOG("Syntax: stack-limit <cols|rows> <limit>\n");
932 last_focused->container->stack_limit_value = atoi(rest);
933 if (last_focused->container->stack_limit_value == 0)
934 last_focused->container->stack_limit = STACK_LIMIT_NONE;
939 if (STARTS_WITH(command, "resize ")) {
940 if (last_focused == NULL)
942 const char *rest = command + strlen("resize ");
943 parse_resize_command(conn, last_focused, rest);
947 if (STARTS_WITH(command, "mode ")) {
948 const char *rest = command + strlen("mode ");
949 switch_mode(conn, rest);
953 /* Is it an <exit>? */
954 if (STARTS_WITH(command, "exit")) {
955 LOG("User issued exit-command, exiting without error.\n");
959 /* Is it a <reload>? */
960 if (STARTS_WITH(command, "reload")) {
961 load_configuration(conn, NULL, true);
965 /* Is it <restart>? Then restart in place. */
966 if (STARTS_WITH(command, "restart")) {
967 LOG("restarting \"%s\"...\n", start_argv[0]);
968 /* make sure -a is in the argument list or append it */
969 start_argv = append_argument(start_argv, "-a");
971 execvp(start_argv[0], start_argv);
975 if (STARTS_WITH(command, "kill")) {
976 if (last_focused == NULL) {
977 LOG("There is no window to kill\n");
981 LOG("Killing current window\n");
982 client_kill(conn, last_focused);
986 /* Is it a jump to a specified workspace, row, col? */
987 if (STARTS_WITH(command, "jump ")) {
988 const char *arguments = command + strlen("jump ");
989 if (arguments[0] == '"')
990 jump_to_window(conn, arguments);
991 else jump_to_container(conn, arguments);
995 /* Should we travel the focus stack? */
996 if (STARTS_WITH(command, "focus")) {
997 const char *arguments = command + strlen("focus ");
998 travel_focus_stack(conn, arguments);
1002 /* Is it 'f' for fullscreen? */
1003 if (command[0] == 'f') {
1004 if (last_focused == NULL)
1006 client_toggle_fullscreen(conn, last_focused);
1010 /* Is it just 's' for stacking or 'd' for default? */
1011 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1012 if (last_focused != NULL && client_is_floating(last_focused)) {
1013 LOG("not switching, this is a floating client\n");
1016 LOG("Switching mode for current container\n");
1017 int new_mode = MODE_DEFAULT;
1018 if (command[0] == 's')
1019 new_mode = MODE_STACK;
1020 if (command[0] == 'T')
1021 new_mode = MODE_TABBED;
1022 switch_layout_mode(conn, CUR_CELL, new_mode);
1026 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1027 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1028 if (command[0] == 'b') {
1029 if (last_focused == NULL) {
1030 LOG("No window focused, cannot change border type\n");
1034 char com = command[1];
1035 if (command[1] == 't') {
1036 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1037 !last_focused->borderless)
1039 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1040 !last_focused->borderless)
1045 client_change_border(conn, last_focused, com);
1049 if (command[0] == 'H') {
1050 LOG("Hiding all floating windows\n");
1051 floating_toggle_hide(conn, c_ws);
1055 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1057 /* Is it a <with>? */
1058 if (command[0] == 'w') {
1060 /* TODO: implement */
1061 if (command[0] == 'c') {
1062 with = WITH_CONTAINER;
1064 } else if (command[0] == 'w') {
1065 with = WITH_WORKSPACE;
1067 } else if (command[0] == 's') {
1071 LOG("not yet implemented.\n");
1076 /* Is it 't' for toggle tiling/floating? */
1077 if (command[0] == 't') {
1078 if (with == WITH_WORKSPACE) {
1079 c_ws->auto_float = !c_ws->auto_float;
1080 LOG("autofloat is now %d\n", c_ws->auto_float);
1083 if (last_focused == NULL) {
1084 LOG("Cannot toggle tiling/floating: workspace empty\n");
1088 Workspace *ws = last_focused->workspace;
1090 toggle_floating_mode(conn, last_focused, false);
1091 /* delete all empty columns/rows */
1092 cleanup_table(conn, ws);
1094 /* Fix colspan/rowspan if it’d overlap */
1095 fix_colrowspan(conn, ws);
1097 render_workspace(conn, ws->screen, ws);
1099 /* Re-focus the client because cleanup_table sets the focus to the last
1100 * focused client inside a container only. */
1101 set_focus(conn, last_focused, true);
1106 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1107 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1108 next_previous_workspace(conn, command[0]);
1112 /* It’s a normal <cmd> */
1114 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1115 direction_t direction;
1116 int times = strtol(command, &rest, 10);
1118 LOG("Invalid command (\"%s\")\n", command);
1122 if (*rest == '\0') {
1123 /* No rest? This was a workspace number, not a times specification */
1124 workspace_show(conn, times);
1128 if (*rest == 'm' || *rest == 's') {
1129 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1133 int workspace = strtol(rest, &rest, 10);
1136 LOG("Invalid command (\"%s\")\n", command);
1140 if (*rest == '\0') {
1141 if (last_focused != NULL && client_is_floating(last_focused))
1142 move_floating_window_to_workspace(conn, last_focused, workspace);
1143 else move_current_window_to_workspace(conn, workspace);
1147 if (last_focused == NULL) {
1148 LOG("Not performing (no window found)\n");
1152 if (client_is_floating(last_focused) &&
1153 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1154 LOG("Not performing (floating)\n");
1158 /* Now perform action to <where> */
1159 while (*rest != '\0') {
1162 else if (*rest == 'j')
1164 else if (*rest == 'k')
1166 else if (*rest == 'l')
1167 direction = D_RIGHT;
1169 LOG("unknown direction: %c\n", *rest);
1174 if (action == ACTION_FOCUS) {
1175 if (with == WITH_SCREEN) {
1176 focus_thing(conn, direction, THING_SCREEN);
1179 if (client_is_floating(last_focused)) {
1180 floating_focus_direction(conn, last_focused, direction);
1183 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1187 if (action == ACTION_MOVE) {
1188 if (with == WITH_SCREEN) {
1189 /* TODO: this should swap the screen’s contents
1190 * (e.g. all workspaces) with the next/previous/…
1192 LOG("Not yet implemented\n");
1195 if (client_is_floating(last_focused)) {
1196 floating_move(conn, last_focused, direction);
1199 if (with == WITH_WINDOW)
1200 move_current_window(conn, direction);
1201 else move_current_container(conn, direction);
1205 if (action == ACTION_SNAP) {
1206 if (with == WITH_SCREEN) {
1207 LOG("You cannot snap a screen (it makes no sense).\n");
1210 snap_current_container(conn, direction);