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 set_focus(conn, current, true);
76 LOG("No window with this mark found\n");
79 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
80 LOG("focusing direction %d\n", direction);
82 int new_row = current_row,
83 new_col = current_col;
84 Container *container = CUR_CELL;
85 Workspace *t_ws = c_ws;
87 /* Makes sure new_col and new_row are within bounds of the new workspace */
88 void check_colrow_boundaries() {
89 if (new_col >= t_ws->cols)
90 new_col = (t_ws->cols - 1);
91 if (new_row >= t_ws->rows)
92 new_row = (t_ws->rows - 1);
95 /* There always is a container. If not, current_col or current_row is wrong */
96 assert(container != NULL);
98 if (container->workspace->fullscreen_client != NULL) {
99 LOG("You're in fullscreen mode. Won't switch focus\n");
103 /* For focusing screens, situation is different: we get the rect
104 * of the current screen, then get the screen which is on its
105 * right/left/bottom/top and just switch to the workspace on
106 * the target screen. */
107 if (thing == THING_SCREEN) {
108 i3Screen *cs = c_ws->screen;
110 Rect bounds = cs->rect;
112 if (direction == D_RIGHT)
113 bounds.x += bounds.width;
114 else if (direction == D_LEFT)
115 bounds.x -= bounds.width;
116 else if (direction == D_UP)
117 bounds.y -= bounds.height;
118 else bounds.y += bounds.height;
120 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
121 if (target == NULL) {
122 LOG("Target screen NULL\n");
123 /* Wrap around if the target screen is out of bounds */
124 if (direction == D_RIGHT)
125 target = get_screen_most(D_LEFT, cs);
126 else if (direction == D_LEFT)
127 target = get_screen_most(D_RIGHT, cs);
128 else if (direction == D_UP)
129 target = get_screen_most(D_DOWN, cs);
130 else target = get_screen_most(D_UP, cs);
133 LOG("Switching to ws %d\n", target->current_workspace + 1);
134 workspace_show(conn, target->current_workspace->num + 1);
138 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
139 if (direction == D_UP || direction == D_DOWN) {
140 if (thing == THING_WINDOW)
141 /* Let’s see if we can perform up/down focus in the current container */
142 if (focus_window_in_container(conn, container, direction))
145 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
146 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
147 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
148 /* Set new_row as a sane default, but it may get overwritten in a second */
151 /* Search from the top to correctly handle rowspanned containers */
152 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
153 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
160 /* Let’s see if there is a screen down/up there to which we can switch */
161 LOG("container is at %d with height %d\n", container->y, container->height);
163 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
164 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
165 LOG("Wrapping screen around vertically\n");
166 /* No screen found? Then wrap */
167 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
169 t_ws = screen->current_workspace;
170 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
173 check_colrow_boundaries();
175 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
176 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
177 LOG("Cell empty, checking for colspanned client above...\n");
178 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
179 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
185 LOG("Fixed it to new col %d\n", new_col);
187 } else if (direction == D_LEFT || direction == D_RIGHT) {
188 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
189 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
190 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
191 /* Set new_col as a sane default, but it may get overwritten in a second */
194 /* Search from the left to correctly handle colspanned containers */
195 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
196 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
203 /* Let’s see if there is a screen left/right here to which we can switch */
204 LOG("container is at %d with width %d\n", container->x, container->width);
206 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
207 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
208 LOG("Wrapping screen around horizontally\n");
209 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
211 t_ws = screen->current_workspace;
212 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
215 check_colrow_boundaries();
217 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
218 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
219 LOG("Cell empty, checking for rowspanned client above...\n");
220 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
221 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
227 LOG("Fixed it to new row %d\n", new_row);
230 LOG("direction unhandled\n");
234 check_colrow_boundaries();
236 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
237 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
241 * Tries to move the window inside its current container.
243 * Returns true if the window could be moved, false otherwise.
246 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
247 direction_t direction) {
248 assert(client->container != NULL);
250 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
251 CIRCLEQ_NEXT(client, clients));
253 if (other == CIRCLEQ_END(&(client->container->clients)))
256 LOG("i can do that\n");
257 /* We can move the client inside its current container */
258 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
259 if (direction == D_UP)
260 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
261 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
267 * Moves the current window or whole container to the given direction, creating a column/row if
271 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
272 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
273 (direction == D_LEFT ? "left" : "right"))));
274 /* Get current window */
275 Container *container = CUR_CELL,
278 /* There has to be a container, see focus_window() */
279 assert(container != NULL);
281 /* If there is no window or the dock window is focused, we’re done */
282 if (container->currently_focused == NULL ||
283 container->currently_focused->dock)
286 /* As soon as the client is moved away, the last focused client in the old
287 * container needs to get focus, if any. Therefore, we save it here. */
288 Client *current_client = container->currently_focused;
289 Client *to_focus = get_last_focused_client(conn, container, current_client);
291 if (to_focus == NULL) {
292 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
293 if (to_focus == NULL)
294 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
299 /* If we’re at the left-most position, move the rest of the table right */
300 if (current_col == 0) {
301 expand_table_cols_at_head(c_ws);
304 new = CUR_TABLE[--current_col][current_row];
307 if (current_col == (c_ws->cols-1))
308 expand_table_cols(c_ws);
310 new = CUR_TABLE[++current_col][current_row];
313 if (move_current_window_in_container(conn, current_client, D_UP))
316 /* if we’re at the up-most position, move the rest of the table down */
317 if (current_row == 0) {
318 expand_table_rows_at_head(c_ws);
321 new = CUR_TABLE[current_col][--current_row];
324 if (move_current_window_in_container(conn, current_client, D_DOWN))
327 if (current_row == (c_ws->rows-1))
328 expand_table_rows(c_ws);
330 new = CUR_TABLE[current_col][++current_row];
332 /* To make static analyzers happy: */
337 /* Remove it from the old container and put it into the new one */
338 client_remove_from_container(conn, current_client, container, true);
340 if (new->currently_focused != NULL)
341 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
342 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
343 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
345 /* Update data structures */
346 current_client->container = new;
347 current_client->workspace = new->workspace;
348 container->currently_focused = to_focus;
349 new->currently_focused = current_client;
351 Workspace *workspace = container->workspace;
353 /* delete all empty columns/rows */
354 cleanup_table(conn, workspace);
356 /* Fix colspan/rowspan if it’d overlap */
357 fix_colrowspan(conn, workspace);
359 render_workspace(conn, workspace->screen, workspace);
362 set_focus(conn, current_client, true);
365 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
366 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
367 (direction == D_LEFT ? "left" : "right"))));
368 /* Get current window */
369 Container *container = CUR_CELL,
372 Container **old = &CUR_CELL;
374 /* There has to be a container, see focus_window() */
375 assert(container != NULL);
379 /* If we’re at the left-most position, move the rest of the table right */
380 if (current_col == 0) {
381 expand_table_cols_at_head(c_ws);
383 old = &CUR_TABLE[current_col+1][current_row];
385 new = CUR_TABLE[--current_col][current_row];
388 if (current_col == (c_ws->cols-1))
389 expand_table_cols(c_ws);
391 new = CUR_TABLE[++current_col][current_row];
394 /* if we’re at the up-most position, move the rest of the table down */
395 if (current_row == 0) {
396 expand_table_rows_at_head(c_ws);
398 old = &CUR_TABLE[current_col][current_row+1];
400 new = CUR_TABLE[current_col][--current_row];
403 if (current_row == (c_ws->rows-1))
404 expand_table_rows(c_ws);
406 new = CUR_TABLE[current_col][++current_row];
408 /* To make static analyzers happy: */
413 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
415 /* Swap the containers */
420 new->col = container->col;
421 new->row = container->row;
423 CUR_CELL = container;
424 container->col = col;
425 container->row = row;
427 Workspace *workspace = container->workspace;
429 /* delete all empty columns/rows */
430 cleanup_table(conn, workspace);
432 /* Fix colspan/rowspan if it’d overlap */
433 fix_colrowspan(conn, workspace);
439 * "Snaps" the current container (not possible for windows, because it works at table base)
440 * to the given direction, that is, adjusts cellspan/rowspan
443 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
444 LOG("snapping container to direction %d\n", direction);
446 Container *container = CUR_CELL;
448 assert(container != NULL);
452 /* Snap to the left is actually a move to the left and then a snap right */
453 if (!cell_exists(container->col - 1, container->row) ||
454 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
455 LOG("cannot snap to left - the cell is already used\n");
459 move_current_window(conn, D_LEFT);
460 snap_current_container(conn, D_RIGHT);
463 /* Check if the cell is used */
464 int new_col = container->col + container->colspan;
465 for (int i = 0; i < container->rowspan; i++)
466 if (!cell_exists(new_col, container->row + i) ||
467 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
468 LOG("cannot snap to right - the cell is already used\n");
472 /* Check if there are other cells with rowspan, which are in our way.
473 * If so, reduce their rowspan. */
474 for (int i = container->row-1; i >= 0; i--) {
475 LOG("we got cell %d, %d with rowspan %d\n",
476 new_col, i, CUR_TABLE[new_col][i]->rowspan);
477 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
478 CUR_TABLE[new_col][i]->rowspan--;
479 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
482 container->colspan++;
486 if (!cell_exists(container->col, container->row - 1) ||
487 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
488 LOG("cannot snap to top - the cell is already used\n");
492 move_current_window(conn, D_UP);
493 snap_current_container(conn, D_DOWN);
496 LOG("snapping down\n");
497 int new_row = container->row + container->rowspan;
498 for (int i = 0; i < container->colspan; i++)
499 if (!cell_exists(container->col + i, new_row) ||
500 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
501 LOG("cannot snap down - the cell is already used\n");
505 for (int i = container->col-1; i >= 0; i--) {
506 LOG("we got cell %d, %d with colspan %d\n",
507 i, new_row, CUR_TABLE[i][new_row]->colspan);
508 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
509 CUR_TABLE[i][new_row]->colspan--;
510 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
514 container->rowspan++;
517 /* To make static analyzers happy: */
525 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
526 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
527 Workspace *t_ws = workspace_get(workspace-1),
528 *old_ws = client->workspace;
530 LOG("moving floating\n");
532 workspace_initialize(t_ws, c_ws->screen);
534 /* Check if there is already a fullscreen client on the destination workspace and
535 * stop moving if so. */
536 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
537 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
541 floating_assign_to_workspace(client, t_ws);
543 /* If we’re moving it to an invisible screen, we need to unmap it */
544 if (!workspace_is_visible(t_ws)) {
545 LOG("This workspace is not visible, unmapping\n");
546 client_unmap(conn, client);
548 /* If this is not the case, we move the window to a workspace
549 * which is on another screen, so we also need to adjust its
551 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
552 uint32_t relative_x = client->rect.x - old_ws->rect.x,
553 relative_y = client->rect.y - old_ws->rect.y;
554 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
555 client->rect.x = t_ws->rect.x + relative_x;
556 client->rect.y = t_ws->rect.y + relative_y;
557 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
558 reposition_client(conn, client);
566 if (workspace_is_visible(t_ws))
567 set_focus(conn, client, true);
571 * Moves the currently selected window to the given workspace
574 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
575 LOG("Moving current window to workspace %d\n", workspace);
577 Container *container = CUR_CELL;
579 assert(container != NULL);
581 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
582 Workspace *t_ws = workspace_get(workspace-1);
584 Client *current_client = container->currently_focused;
585 if (current_client == NULL) {
586 LOG("No currently focused client in current container.\n");
589 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
590 if (to_focus == NULL)
591 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
593 workspace_initialize(t_ws, container->workspace->screen);
594 /* Check if there is already a fullscreen client on the destination workspace and
595 * stop moving if so. */
596 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
597 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
601 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
603 assert(to_container != NULL);
605 client_remove_from_container(conn, current_client, container, true);
606 if (container->workspace->fullscreen_client == current_client)
607 container->workspace->fullscreen_client = NULL;
609 /* TODO: insert it to the correct position */
610 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
612 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
615 current_client->container = to_container;
616 current_client->workspace = to_container->workspace;
617 container->currently_focused = to_focus;
618 to_container->currently_focused = current_client;
620 /* If we’re moving it to an invisible screen, we need to unmap it */
621 if (!workspace_is_visible(to_container->workspace)) {
622 LOG("This workspace is not visible, unmapping\n");
623 client_unmap(conn, current_client);
625 if (current_client->fullscreen) {
626 LOG("Calling client_enter_fullscreen again\n");
627 client_enter_fullscreen(conn, current_client);
631 /* delete all empty columns/rows */
632 cleanup_table(conn, container->workspace);
636 if (workspace_is_visible(to_container->workspace))
637 set_focus(conn, current_client, true);
641 * Jumps to the given window class / title.
642 * Title is matched using strstr, that is, matches if it appears anywhere
643 * in the string. Regular expressions seem to be a bit overkill here. However,
644 * if we need them for something else somewhen, we may introduce them here, too.
647 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
651 /* The first character is a quote, this was checked before */
652 classtitle = sstrdup(arguments+1);
653 /* The last character is a quote, we just set it to NULL */
654 classtitle[strlen(classtitle)-1] = '\0';
656 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
658 LOG("No matching client found.\n");
663 set_focus(conn, client, true);
667 * Jump directly to the specified workspace, row and col.
668 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
671 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
675 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
676 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
678 /* No match? Either no arguments were specified, or no numbers */
680 LOG("At least one valid argument required\n");
684 /* Move to the target workspace */
685 workspace_show(conn, ws);
690 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
692 /* Move to row/col */
693 if (row >= c_ws->rows)
694 row = c_ws->rows - 1;
695 if (col >= c_ws->cols)
696 col = c_ws->cols - 1;
698 LOG("Jumping to col %d, row %d\n", col, row);
699 if (c_ws->table[col][row]->currently_focused != NULL)
700 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
704 * Travels the focus stack by the given number of times (or once, if no argument
705 * was specified). That is, selects the window you were in before you focused
706 * the current window.
708 * The special values 'floating' (select the next floating window), 'tiling'
709 * (select the next tiling window), 'ft' (if the current window is floating,
710 * select the next tiling window and vice-versa) are also valid
713 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
714 /* Start count at -1 to always skip the first element */
715 int times, count = -1;
717 bool floating_criteria;
719 /* Either it’s one of the special values… */
720 if (strcasecmp(arguments, "floating") == 0) {
721 floating_criteria = true;
722 } else if (strcasecmp(arguments, "tiling") == 0) {
723 floating_criteria = false;
724 } else if (strcasecmp(arguments, "ft") == 0) {
725 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
726 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
727 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
731 floating_criteria = !client_is_floating(last_focused);
733 /* …or a number was specified */
734 if (sscanf(arguments, "%u", ×) != 1) {
735 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
739 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
740 if (++count < times) {
746 set_focus(conn, current, true);
752 /* Select the next client matching the criteria parsed above */
753 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
754 if (client_is_floating(current) == floating_criteria) {
755 set_focus(conn, current, true);
761 * Goes through the list of arguments (for exec()) and checks if the given argument
762 * is present. If not, it copies the arguments (because we cannot realloc it) and
763 * appends the given argument.
766 static char **append_argument(char **original, char *argument) {
768 for (num_args = 0; original[num_args] != NULL; num_args++) {
769 LOG("original argument: \"%s\"\n", original[num_args]);
770 /* If the argument is already present we return the original pointer */
771 if (strcmp(original[num_args], argument) == 0)
774 /* Copy the original array */
775 char **result = smalloc((num_args+2) * sizeof(char*));
776 memcpy(result, original, num_args * sizeof(char*));
777 result[num_args] = argument;
778 result[num_args+1] = NULL;
784 * Switch to next or previous existing workspace
787 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
788 Workspace *ws = c_ws;
790 if (direction == 'n') {
791 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
792 if (ws->screen == NULL)
795 workspace_show(conn, ws->num + 1);
798 } else if (direction == 'p') {
799 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
800 if (ws->screen == NULL)
803 workspace_show(conn, ws->num + 1);
809 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
811 resize_orientation_t orientation = O_VERTICAL;
812 Container *con = last_focused->container;
813 Workspace *ws = con->workspace;
815 if (STARTS_WITH(command, "left")) {
818 first = con->col - 1;
820 command += strlen("left");
821 } else if (STARTS_WITH(command, "right")) {
822 first = con->col + (con->colspan - 1);
823 LOG("column %d\n", first);
825 if (!cell_exists(first, con->row) ||
826 (first == (ws->cols-1)))
830 command += strlen("right");
831 } else if (STARTS_WITH(command, "top")) {
834 first = con->row - 1;
836 orientation = O_HORIZONTAL;
837 command += strlen("top");
838 } else if (STARTS_WITH(command, "bottom")) {
839 first = con->row + (con->rowspan - 1);
840 if (!cell_exists(con->col, first) ||
841 (first == (ws->rows-1)))
845 orientation = O_HORIZONTAL;
846 command += strlen("bottom");
848 LOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
852 int pixels = atoi(command);
856 resize_container(conn, ws, first, second, orientation, pixels);
860 * Parses a command, see file CMDMODE for more information
863 void parse_command(xcb_connection_t *conn, const char *command) {
864 LOG("--- parsing command \"%s\" ---\n", command);
865 /* Get the first client from focus stack because floating clients are not
866 * in any container, therefore CUR_CELL is not appropriate. */
867 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
868 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
871 /* Hmm, just to be sure */
872 if (command[0] == '\0')
875 /* Is it an <exec>? Then execute the given command. */
876 if (STARTS_WITH(command, "exec ")) {
877 LOG("starting \"%s\"\n", command + strlen("exec "));
878 start_application(command+strlen("exec "));
882 if (STARTS_WITH(command, "mark")) {
883 if (last_focused == NULL) {
884 LOG("There is no window to mark\n");
887 const char *rest = command + strlen("mark");
891 LOG("interactive mark starting\n");
892 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
894 LOG("mark with \"%s\"\n", rest);
895 client_mark(conn, last_focused, rest);
900 if (STARTS_WITH(command, "goto")) {
901 const char *rest = command + strlen("goto");
905 LOG("interactive go to mark starting\n");
906 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
908 LOG("go to \"%s\"\n", rest);
909 jump_to_mark(conn, rest);
914 if (STARTS_WITH(command, "stack-limit ")) {
915 if (last_focused == NULL || client_is_floating(last_focused)) {
916 LOG("No container focused\n");
919 const char *rest = command + strlen("stack-limit ");
920 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
921 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
922 rest += strlen("rows ");
923 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
924 last_focused->container->stack_limit = STACK_LIMIT_COLS;
925 rest += strlen("cols ");
927 LOG("Syntax: stack-limit <cols|rows> <limit>\n");
931 last_focused->container->stack_limit_value = atoi(rest);
932 if (last_focused->container->stack_limit_value == 0)
933 last_focused->container->stack_limit = STACK_LIMIT_NONE;
938 if (STARTS_WITH(command, "resize ")) {
939 if (last_focused == NULL)
941 const char *rest = command + strlen("resize ");
942 parse_resize_command(conn, last_focused, rest);
946 if (STARTS_WITH(command, "mode ")) {
947 const char *rest = command + strlen("mode ");
948 switch_mode(conn, rest);
952 /* Is it an <exit>? */
953 if (STARTS_WITH(command, "exit")) {
954 LOG("User issued exit-command, exiting without error.\n");
958 /* Is it a <reload>? */
959 if (STARTS_WITH(command, "reload")) {
960 load_configuration(conn, NULL, true);
964 /* Is it <restart>? Then restart in place. */
965 if (STARTS_WITH(command, "restart")) {
966 LOG("restarting \"%s\"...\n", start_argv[0]);
967 /* make sure -a is in the argument list or append it */
968 start_argv = append_argument(start_argv, "-a");
970 execvp(start_argv[0], start_argv);
974 if (STARTS_WITH(command, "kill")) {
975 if (last_focused == NULL) {
976 LOG("There is no window to kill\n");
980 LOG("Killing current window\n");
981 client_kill(conn, last_focused);
985 /* Is it a jump to a specified workspace, row, col? */
986 if (STARTS_WITH(command, "jump ")) {
987 const char *arguments = command + strlen("jump ");
988 if (arguments[0] == '"')
989 jump_to_window(conn, arguments);
990 else jump_to_container(conn, arguments);
994 /* Should we travel the focus stack? */
995 if (STARTS_WITH(command, "focus")) {
996 const char *arguments = command + strlen("focus ");
997 travel_focus_stack(conn, arguments);
1001 /* Is it 'f' for fullscreen? */
1002 if (command[0] == 'f') {
1003 if (last_focused == NULL)
1005 client_toggle_fullscreen(conn, last_focused);
1009 /* Is it just 's' for stacking or 'd' for default? */
1010 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1011 if (last_focused != NULL && client_is_floating(last_focused)) {
1012 LOG("not switching, this is a floating client\n");
1015 LOG("Switching mode for current container\n");
1016 int new_mode = MODE_DEFAULT;
1017 if (command[0] == 's')
1018 new_mode = MODE_STACK;
1019 if (command[0] == 'T')
1020 new_mode = MODE_TABBED;
1021 switch_layout_mode(conn, CUR_CELL, new_mode);
1025 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1026 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1027 if (command[0] == 'b') {
1028 if (last_focused == NULL) {
1029 LOG("No window focused, cannot change border type\n");
1033 char com = command[1];
1034 if (command[1] == 't') {
1035 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1036 !last_focused->borderless)
1038 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1039 !last_focused->borderless)
1044 client_change_border(conn, last_focused, com);
1048 if (command[0] == 'H') {
1049 LOG("Hiding all floating windows\n");
1050 floating_toggle_hide(conn, c_ws);
1054 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1056 /* Is it a <with>? */
1057 if (command[0] == 'w') {
1059 /* TODO: implement */
1060 if (command[0] == 'c') {
1061 with = WITH_CONTAINER;
1063 } else if (command[0] == 'w') {
1064 with = WITH_WORKSPACE;
1066 } else if (command[0] == 's') {
1070 LOG("not yet implemented.\n");
1075 /* Is it 't' for toggle tiling/floating? */
1076 if (command[0] == 't') {
1077 if (with == WITH_WORKSPACE) {
1078 c_ws->auto_float = !c_ws->auto_float;
1079 LOG("autofloat is now %d\n", c_ws->auto_float);
1082 if (last_focused == NULL) {
1083 LOG("Cannot toggle tiling/floating: workspace empty\n");
1087 Workspace *ws = last_focused->workspace;
1089 toggle_floating_mode(conn, last_focused, false);
1090 /* delete all empty columns/rows */
1091 cleanup_table(conn, ws);
1093 /* Fix colspan/rowspan if it’d overlap */
1094 fix_colrowspan(conn, ws);
1096 render_workspace(conn, ws->screen, ws);
1098 /* Re-focus the client because cleanup_table sets the focus to the last
1099 * focused client inside a container only. */
1100 set_focus(conn, last_focused, true);
1105 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1106 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1107 next_previous_workspace(conn, command[0]);
1111 /* It’s a normal <cmd> */
1113 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1114 direction_t direction;
1115 int times = strtol(command, &rest, 10);
1117 LOG("Invalid command (\"%s\")\n", command);
1121 if (*rest == '\0') {
1122 /* No rest? This was a workspace number, not a times specification */
1123 workspace_show(conn, times);
1127 if (*rest == 'm' || *rest == 's') {
1128 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1132 int workspace = strtol(rest, &rest, 10);
1135 LOG("Invalid command (\"%s\")\n", command);
1139 if (*rest == '\0') {
1140 if (last_focused != NULL && client_is_floating(last_focused))
1141 move_floating_window_to_workspace(conn, last_focused, workspace);
1142 else move_current_window_to_workspace(conn, workspace);
1146 if (last_focused == NULL) {
1147 LOG("Not performing (no window found)\n");
1151 if (client_is_floating(last_focused) &&
1152 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1153 LOG("Not performing (floating)\n");
1157 /* Now perform action to <where> */
1158 while (*rest != '\0') {
1161 else if (*rest == 'j')
1163 else if (*rest == 'k')
1165 else if (*rest == 'l')
1166 direction = D_RIGHT;
1168 LOG("unknown direction: %c\n", *rest);
1173 if (action == ACTION_FOCUS) {
1174 if (with == WITH_SCREEN) {
1175 focus_thing(conn, direction, THING_SCREEN);
1178 if (client_is_floating(last_focused)) {
1179 floating_focus_direction(conn, last_focused, direction);
1182 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1186 if (action == ACTION_MOVE) {
1187 if (with == WITH_SCREEN) {
1188 /* TODO: this should swap the screen’s contents
1189 * (e.g. all workspaces) with the next/previous/…
1191 LOG("Not yet implemented\n");
1194 if (client_is_floating(last_focused)) {
1195 floating_move(conn, last_focused, direction);
1198 if (with == WITH_WINDOW)
1199 move_current_window(conn, direction);
1200 else move_current_container(conn, direction);
1204 if (action == ACTION_SNAP) {
1205 if (with == WITH_SCREEN) {
1206 LOG("You cannot snap a screen (it makes no sense).\n");
1209 snap_current_container(conn, direction);