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"
35 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
36 /* If this container is empty, we’re done */
37 if (container->currently_focused == NULL)
40 /* Get the previous/next client or wrap around */
41 Client *candidate = NULL;
42 if (direction == D_UP) {
43 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
44 candidate = CIRCLEQ_LAST(&(container->clients));
46 else if (direction == D_DOWN) {
47 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
48 candidate = CIRCLEQ_FIRST(&(container->clients));
49 } else ELOG("Direction not implemented!\n");
51 /* If we could not switch, the container contains exactly one client. We return false */
52 if (candidate == container->currently_focused)
56 set_focus(conn, candidate, true);
61 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
63 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
65 LOG("Jumping to \"%s\"\n", mark);
68 TAILQ_FOREACH(ws, workspaces, workspaces)
69 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
70 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
73 workspace_show(conn, current->workspace->num + 1);
74 set_focus(conn, current, true);
78 ELOG("No window with this mark found\n");
81 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
82 DLOG("focusing direction %d\n", direction);
84 int new_row = current_row,
85 new_col = current_col;
86 Container *container = CUR_CELL;
87 Workspace *t_ws = c_ws;
89 /* Makes sure new_col and new_row are within bounds of the new workspace */
90 void check_colrow_boundaries() {
91 if (new_col >= t_ws->cols)
92 new_col = (t_ws->cols - 1);
93 if (new_row >= t_ws->rows)
94 new_row = (t_ws->rows - 1);
97 /* There always is a container. If not, current_col or current_row is wrong */
98 assert(container != NULL);
100 if (container->workspace->fullscreen_client != NULL) {
101 LOG("You're in fullscreen mode. Won't switch focus\n");
105 /* For focusing screens, situation is different: we get the rect
106 * of the current screen, then get the screen which is on its
107 * right/left/bottom/top and just switch to the workspace on
108 * the target screen. */
109 if (thing == THING_SCREEN) {
110 i3Screen *cs = c_ws->screen;
112 Rect bounds = cs->rect;
114 if (direction == D_RIGHT)
115 bounds.x += bounds.width;
116 else if (direction == D_LEFT)
117 bounds.x -= bounds.width;
118 else if (direction == D_UP)
119 bounds.y -= bounds.height;
120 else bounds.y += bounds.height;
122 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
123 if (target == NULL) {
124 DLOG("Target screen NULL\n");
125 /* Wrap around if the target screen is out of bounds */
126 if (direction == D_RIGHT)
127 target = get_screen_most(D_LEFT, cs);
128 else if (direction == D_LEFT)
129 target = get_screen_most(D_RIGHT, cs);
130 else if (direction == D_UP)
131 target = get_screen_most(D_DOWN, cs);
132 else target = get_screen_most(D_UP, cs);
135 DLOG("Switching to ws %d\n", target->current_workspace + 1);
136 workspace_show(conn, target->current_workspace->num + 1);
140 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
141 if (direction == D_UP || direction == D_DOWN) {
142 if (thing == THING_WINDOW)
143 /* Let’s see if we can perform up/down focus in the current container */
144 if (focus_window_in_container(conn, container, direction))
147 if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
148 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
149 else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
150 /* Set new_row as a sane default, but it may get overwritten in a second */
153 /* Search from the top to correctly handle rowspanned containers */
154 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
155 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
162 /* Let’s see if there is a screen down/up there to which we can switch */
163 DLOG("container is at %d with height %d\n", container->y, container->height);
165 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
166 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
167 DLOG("Wrapping screen around vertically\n");
168 /* No screen found? Then wrap */
169 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
171 t_ws = screen->current_workspace;
172 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
175 check_colrow_boundaries();
177 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
178 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
179 DLOG("Cell empty, checking for colspanned client above...\n");
180 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
181 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
187 DLOG("Fixed it to new col %d\n", new_col);
189 } else if (direction == D_LEFT || direction == D_RIGHT) {
190 if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
191 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
192 else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
193 /* Set new_col as a sane default, but it may get overwritten in a second */
196 /* Search from the left to correctly handle colspanned containers */
197 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
198 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
205 /* Let’s see if there is a screen left/right here to which we can switch */
206 DLOG("container is at %d with width %d\n", container->x, container->width);
208 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
209 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
210 DLOG("Wrapping screen around horizontally\n");
211 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
213 t_ws = screen->current_workspace;
214 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
217 check_colrow_boundaries();
219 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
220 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
221 DLOG("Cell empty, checking for rowspanned client above...\n");
222 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
223 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
229 DLOG("Fixed it to new row %d\n", new_row);
232 ELOG("direction unhandled\n");
236 check_colrow_boundaries();
238 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
239 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
243 * Tries to move the window inside its current container.
245 * Returns true if the window could be moved, false otherwise.
248 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
249 direction_t direction) {
250 assert(client->container != NULL);
252 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
253 CIRCLEQ_NEXT(client, clients));
255 if (other == CIRCLEQ_END(&(client->container->clients)))
258 DLOG("i can do that\n");
259 /* We can move the client inside its current container */
260 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
261 if (direction == D_UP)
262 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
263 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
269 * Moves the current window or whole container to the given direction, creating a column/row if
273 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
274 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
275 (direction == D_LEFT ? "left" : "right"))));
276 /* Get current window */
277 Container *container = CUR_CELL,
280 /* There has to be a container, see focus_window() */
281 assert(container != NULL);
283 /* If there is no window or the dock window is focused, we’re done */
284 if (container->currently_focused == NULL ||
285 container->currently_focused->dock)
288 /* As soon as the client is moved away, the last focused client in the old
289 * container needs to get focus, if any. Therefore, we save it here. */
290 Client *current_client = container->currently_focused;
291 Client *to_focus = get_last_focused_client(conn, container, current_client);
293 if (to_focus == NULL) {
294 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
295 if (to_focus == NULL)
296 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
301 /* If we’re at the left-most position, move the rest of the table right */
302 if (current_col == 0) {
303 expand_table_cols_at_head(c_ws);
306 new = CUR_TABLE[--current_col][current_row];
309 if (current_col == (c_ws->cols-1))
310 expand_table_cols(c_ws);
312 new = CUR_TABLE[++current_col][current_row];
315 if (move_current_window_in_container(conn, current_client, D_UP))
318 /* if we’re at the up-most position, move the rest of the table down */
319 if (current_row == 0) {
320 expand_table_rows_at_head(c_ws);
323 new = CUR_TABLE[current_col][--current_row];
326 if (move_current_window_in_container(conn, current_client, D_DOWN))
329 if (current_row == (c_ws->rows-1))
330 expand_table_rows(c_ws);
332 new = CUR_TABLE[current_col][++current_row];
334 /* To make static analyzers happy: */
339 /* Remove it from the old container and put it into the new one */
340 client_remove_from_container(conn, current_client, container, true);
342 if (new->currently_focused != NULL)
343 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
344 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
345 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
347 /* Update data structures */
348 current_client->container = new;
349 current_client->workspace = new->workspace;
350 container->currently_focused = to_focus;
351 new->currently_focused = current_client;
353 Workspace *workspace = container->workspace;
355 /* delete all empty columns/rows */
356 cleanup_table(conn, workspace);
358 /* Fix colspan/rowspan if it’d overlap */
359 fix_colrowspan(conn, workspace);
361 render_workspace(conn, workspace->screen, workspace);
364 set_focus(conn, current_client, true);
367 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
368 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
369 (direction == D_LEFT ? "left" : "right"))));
370 /* Get current window */
371 Container *container = CUR_CELL,
374 Container **old = &CUR_CELL;
376 /* There has to be a container, see focus_window() */
377 assert(container != NULL);
381 /* If we’re at the left-most position, move the rest of the table right */
382 if (current_col == 0) {
383 expand_table_cols_at_head(c_ws);
385 old = &CUR_TABLE[current_col+1][current_row];
387 new = CUR_TABLE[--current_col][current_row];
390 if (current_col == (c_ws->cols-1))
391 expand_table_cols(c_ws);
393 new = CUR_TABLE[++current_col][current_row];
396 /* if we’re at the up-most position, move the rest of the table down */
397 if (current_row == 0) {
398 expand_table_rows_at_head(c_ws);
400 old = &CUR_TABLE[current_col][current_row+1];
402 new = CUR_TABLE[current_col][--current_row];
405 if (current_row == (c_ws->rows-1))
406 expand_table_rows(c_ws);
408 new = CUR_TABLE[current_col][++current_row];
410 /* To make static analyzers happy: */
415 DLOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
417 /* Swap the containers */
422 new->col = container->col;
423 new->row = container->row;
425 CUR_CELL = container;
426 container->col = col;
427 container->row = row;
429 Workspace *workspace = container->workspace;
431 /* delete all empty columns/rows */
432 cleanup_table(conn, workspace);
434 /* Fix colspan/rowspan if it’d overlap */
435 fix_colrowspan(conn, workspace);
441 * "Snaps" the current container (not possible for windows, because it works at table base)
442 * to the given direction, that is, adjusts cellspan/rowspan
445 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
446 LOG("snapping container to direction %d\n", direction);
448 Container *container = CUR_CELL;
450 assert(container != NULL);
454 /* Snap to the left is actually a move to the left and then a snap right */
455 if (!cell_exists(container->workspace, container->col - 1, container->row) ||
456 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
457 ELOG("cannot snap to left - the cell is already used\n");
461 move_current_window(conn, D_LEFT);
462 snap_current_container(conn, D_RIGHT);
465 /* Check if the cell is used */
466 int new_col = container->col + container->colspan;
467 for (int i = 0; i < container->rowspan; i++)
468 if (!cell_exists(container->workspace, new_col, container->row + i) ||
469 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
470 ELOG("cannot snap to right - the cell is already used\n");
474 /* Check if there are other cells with rowspan, which are in our way.
475 * If so, reduce their rowspan. */
476 for (int i = container->row-1; i >= 0; i--) {
477 DLOG("we got cell %d, %d with rowspan %d\n",
478 new_col, i, CUR_TABLE[new_col][i]->rowspan);
479 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
480 CUR_TABLE[new_col][i]->rowspan--;
481 DLOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
484 container->colspan++;
488 if (!cell_exists(container->workspace, container->col, container->row - 1) ||
489 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
490 ELOG("cannot snap to top - the cell is already used\n");
494 move_current_window(conn, D_UP);
495 snap_current_container(conn, D_DOWN);
498 DLOG("snapping down\n");
499 int new_row = container->row + container->rowspan;
500 for (int i = 0; i < container->colspan; i++)
501 if (!cell_exists(container->workspace, container->col + i, new_row) ||
502 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
503 ELOG("cannot snap down - the cell is already used\n");
507 for (int i = container->col-1; i >= 0; i--) {
508 DLOG("we got cell %d, %d with colspan %d\n",
509 i, new_row, CUR_TABLE[i][new_row]->colspan);
510 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
511 CUR_TABLE[i][new_row]->colspan--;
512 DLOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
516 container->rowspan++;
519 /* To make static analyzers happy: */
527 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
528 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
529 Workspace *t_ws = workspace_get(workspace-1),
530 *old_ws = client->workspace;
532 LOG("moving floating\n");
534 workspace_initialize(t_ws, c_ws->screen, false);
536 /* Check if there is already a fullscreen client on the destination workspace and
537 * stop moving if so. */
538 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
539 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
543 floating_assign_to_workspace(client, t_ws);
545 /* If we’re moving it to an invisible screen, we need to unmap it */
546 if (!workspace_is_visible(t_ws)) {
547 DLOG("This workspace is not visible, unmapping\n");
548 client_unmap(conn, client);
550 /* If this is not the case, we move the window to a workspace
551 * which is on another screen, so we also need to adjust its
553 DLOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
554 uint32_t relative_x = client->rect.x - old_ws->rect.x,
555 relative_y = client->rect.y - old_ws->rect.y;
556 DLOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
557 client->rect.x = t_ws->rect.x + relative_x;
558 client->rect.y = t_ws->rect.y + relative_y;
559 DLOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
560 reposition_client(conn, client);
568 if (workspace_is_visible(t_ws))
569 set_focus(conn, client, true);
573 * Moves the currently selected window to the given workspace
576 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
577 LOG("Moving current window to workspace %d\n", workspace);
579 Container *container = CUR_CELL;
581 assert(container != NULL);
583 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
584 Workspace *t_ws = workspace_get(workspace-1);
586 Client *current_client = container->currently_focused;
587 if (current_client == NULL) {
588 ELOG("No currently focused client in current container.\n");
591 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
592 if (to_focus == NULL)
593 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
595 workspace_initialize(t_ws, container->workspace->screen, false);
596 /* Check if there is already a fullscreen client on the destination workspace and
597 * stop moving if so. */
598 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
599 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
603 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
605 assert(to_container != NULL);
607 client_remove_from_container(conn, current_client, container, true);
608 if (container->workspace->fullscreen_client == current_client)
609 container->workspace->fullscreen_client = NULL;
611 /* TODO: insert it to the correct position */
612 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
614 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
617 current_client->container = to_container;
618 current_client->workspace = to_container->workspace;
619 container->currently_focused = to_focus;
620 to_container->currently_focused = current_client;
622 /* If we’re moving it to an invisible screen, we need to unmap it */
623 if (!workspace_is_visible(to_container->workspace)) {
624 DLOG("This workspace is not visible, unmapping\n");
625 client_unmap(conn, current_client);
627 if (current_client->fullscreen) {
628 DLOG("Calling client_enter_fullscreen again\n");
629 client_enter_fullscreen(conn, current_client);
633 /* delete all empty columns/rows */
634 cleanup_table(conn, container->workspace);
638 if (workspace_is_visible(to_container->workspace))
639 set_focus(conn, current_client, true);
643 * Jumps to the given window class / title.
644 * Title is matched using strstr, that is, matches if it appears anywhere
645 * in the string. Regular expressions seem to be a bit overkill here. However,
646 * if we need them for something else somewhen, we may introduce them here, too.
649 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
653 /* The first character is a quote, this was checked before */
654 classtitle = sstrdup(arguments+1);
655 /* The last character is a quote, we just set it to NULL */
656 classtitle[strlen(classtitle)-1] = '\0';
658 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
660 ELOG("No matching client found.\n");
665 set_focus(conn, client, true);
669 * Jump directly to the specified workspace, row and col.
670 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
673 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
677 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
678 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
680 /* No match? Either no arguments were specified, or no numbers */
682 ELOG("At least one valid argument required\n");
686 /* Move to the target workspace */
687 workspace_show(conn, ws);
692 DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
694 /* Move to row/col */
695 if (row >= c_ws->rows)
696 row = c_ws->rows - 1;
697 if (col >= c_ws->cols)
698 col = c_ws->cols - 1;
700 DLOG("Jumping to col %d, row %d\n", col, row);
701 if (c_ws->table[col][row]->currently_focused != NULL)
702 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
706 * Travels the focus stack by the given number of times (or once, if no argument
707 * was specified). That is, selects the window you were in before you focused
708 * the current window.
710 * The special values 'floating' (select the next floating window), 'tiling'
711 * (select the next tiling window), 'ft' (if the current window is floating,
712 * select the next tiling window and vice-versa) are also valid
715 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
716 /* Start count at -1 to always skip the first element */
717 int times, count = -1;
719 bool floating_criteria;
721 /* Either it’s one of the special values… */
722 if (strcasecmp(arguments, "floating") == 0) {
723 floating_criteria = true;
724 } else if (strcasecmp(arguments, "tiling") == 0) {
725 floating_criteria = false;
726 } else if (strcasecmp(arguments, "ft") == 0) {
727 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
728 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
729 ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
733 floating_criteria = !client_is_floating(last_focused);
735 /* …or a number was specified */
736 if (sscanf(arguments, "%u", ×) != 1) {
737 ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
741 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
742 if (++count < times) {
748 set_focus(conn, current, true);
754 /* Select the next client matching the criteria parsed above */
755 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
756 if (client_is_floating(current) == floating_criteria) {
757 set_focus(conn, current, true);
763 * Goes through the list of arguments (for exec()) and checks if the given argument
764 * is present. If not, it copies the arguments (because we cannot realloc it) and
765 * appends the given argument.
768 static char **append_argument(char **original, char *argument) {
770 for (num_args = 0; original[num_args] != NULL; num_args++) {
771 DLOG("original argument: \"%s\"\n", original[num_args]);
772 /* If the argument is already present we return the original pointer */
773 if (strcmp(original[num_args], argument) == 0)
776 /* Copy the original array */
777 char **result = smalloc((num_args+2) * sizeof(char*));
778 memcpy(result, original, num_args * sizeof(char*));
779 result[num_args] = argument;
780 result[num_args+1] = NULL;
786 * Switch to next or previous existing workspace
789 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
790 Workspace *ws = c_ws;
792 if (direction == 'n') {
793 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
794 if (ws->screen == NULL)
797 workspace_show(conn, ws->num + 1);
800 } else if (direction == 'p') {
801 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
802 if (ws->screen == NULL)
805 workspace_show(conn, ws->num + 1);
811 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
813 resize_orientation_t orientation = O_VERTICAL;
814 Container *con = last_focused->container;
815 Workspace *ws = con->workspace;
817 if (STARTS_WITH(command, "left")) {
820 first = con->col - 1;
822 command += strlen("left");
823 } else if (STARTS_WITH(command, "right")) {
824 first = con->col + (con->colspan - 1);
825 DLOG("column %d\n", first);
827 if (!cell_exists(ws, first, con->row) ||
828 (first == (ws->cols-1)))
832 command += strlen("right");
833 } else if (STARTS_WITH(command, "top")) {
836 first = con->row - 1;
838 orientation = O_HORIZONTAL;
839 command += strlen("top");
840 } else if (STARTS_WITH(command, "bottom")) {
841 first = con->row + (con->rowspan - 1);
842 if (!cell_exists(ws, con->col, first) ||
843 (first == (ws->rows-1)))
847 orientation = O_HORIZONTAL;
848 command += strlen("bottom");
850 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
854 int pixels = atoi(command);
858 resize_container(conn, ws, first, second, orientation, pixels);
862 * Parses a command, see file CMDMODE for more information
865 void parse_command(xcb_connection_t *conn, const char *command) {
866 LOG("--- parsing command \"%s\" ---\n", command);
867 /* Get the first client from focus stack because floating clients are not
868 * in any container, therefore CUR_CELL is not appropriate. */
869 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
870 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
873 /* Hmm, just to be sure */
874 if (command[0] == '\0')
877 /* Is it an <exec>? Then execute the given command. */
878 if (STARTS_WITH(command, "exec ")) {
879 LOG("starting \"%s\"\n", command + strlen("exec "));
880 start_application(command+strlen("exec "));
884 if (STARTS_WITH(command, "mark")) {
885 if (last_focused == NULL) {
886 ELOG("There is no window to mark\n");
889 const char *rest = command + strlen("mark");
893 DLOG("interactive mark starting\n");
894 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
896 LOG("mark with \"%s\"\n", rest);
897 client_mark(conn, last_focused, rest);
902 if (STARTS_WITH(command, "goto")) {
903 const char *rest = command + strlen("goto");
907 DLOG("interactive go to mark starting\n");
908 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
910 LOG("go to \"%s\"\n", rest);
911 jump_to_mark(conn, rest);
916 if (STARTS_WITH(command, "stack-limit ")) {
917 if (last_focused == NULL || client_is_floating(last_focused)) {
918 ELOG("No container focused\n");
921 const char *rest = command + strlen("stack-limit ");
922 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
923 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
924 rest += strlen("rows ");
925 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
926 last_focused->container->stack_limit = STACK_LIMIT_COLS;
927 rest += strlen("cols ");
929 ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
933 last_focused->container->stack_limit_value = atoi(rest);
934 if (last_focused->container->stack_limit_value == 0)
935 last_focused->container->stack_limit = STACK_LIMIT_NONE;
940 if (STARTS_WITH(command, "resize ")) {
941 if (last_focused == NULL)
943 const char *rest = command + strlen("resize ");
944 parse_resize_command(conn, last_focused, rest);
948 if (STARTS_WITH(command, "mode ")) {
949 const char *rest = command + strlen("mode ");
950 switch_mode(conn, rest);
954 /* Is it an <exit>? */
955 if (STARTS_WITH(command, "exit")) {
956 LOG("User issued exit-command, exiting without error.\n");
960 /* Is it a <reload>? */
961 if (STARTS_WITH(command, "reload")) {
962 load_configuration(conn, NULL, true);
966 /* Is it <restart>? Then restart in place. */
967 if (STARTS_WITH(command, "restart")) {
968 LOG("restarting \"%s\"...\n", start_argv[0]);
969 /* make sure -a is in the argument list or append it */
970 start_argv = append_argument(start_argv, "-a");
972 execvp(start_argv[0], start_argv);
976 if (STARTS_WITH(command, "kill")) {
977 if (last_focused == NULL) {
978 ELOG("There is no window to kill\n");
982 LOG("Killing current window\n");
983 client_kill(conn, last_focused);
987 /* Is it a jump to a specified workspace, row, col? */
988 if (STARTS_WITH(command, "jump ")) {
989 const char *arguments = command + strlen("jump ");
990 if (arguments[0] == '"')
991 jump_to_window(conn, arguments);
992 else jump_to_container(conn, arguments);
996 /* Should we travel the focus stack? */
997 if (STARTS_WITH(command, "focus")) {
998 const char *arguments = command + strlen("focus ");
999 travel_focus_stack(conn, arguments);
1003 /* Is it 'f' for fullscreen? */
1004 if (command[0] == 'f') {
1005 if (last_focused == NULL)
1007 client_toggle_fullscreen(conn, last_focused);
1011 /* Is it just 's' for stacking or 'd' for default? */
1012 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1013 if (last_focused != NULL && client_is_floating(last_focused)) {
1014 ELOG("not switching, this is a floating client\n");
1017 LOG("Switching mode for current container\n");
1018 int new_mode = MODE_DEFAULT;
1019 if (command[0] == 's')
1020 new_mode = MODE_STACK;
1021 if (command[0] == 'T')
1022 new_mode = MODE_TABBED;
1023 switch_layout_mode(conn, CUR_CELL, new_mode);
1027 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1028 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1029 if (command[0] == 'b') {
1030 if (last_focused == NULL) {
1031 ELOG("No window focused, cannot change border type\n");
1035 char com = command[1];
1036 if (command[1] == 't') {
1037 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1038 !last_focused->borderless)
1040 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1041 !last_focused->borderless)
1046 client_change_border(conn, last_focused, com);
1050 if (command[0] == 'H') {
1051 LOG("Hiding all floating windows\n");
1052 floating_toggle_hide(conn, c_ws);
1056 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1058 /* Is it a <with>? */
1059 if (command[0] == 'w') {
1061 /* TODO: implement */
1062 if (command[0] == 'c') {
1063 with = WITH_CONTAINER;
1065 } else if (command[0] == 'w') {
1066 with = WITH_WORKSPACE;
1068 } else if (command[0] == 's') {
1072 ELOG("not yet implemented.\n");
1077 /* Is it 't' for toggle tiling/floating? */
1078 if (command[0] == 't') {
1079 if (with == WITH_WORKSPACE) {
1080 c_ws->auto_float = !c_ws->auto_float;
1081 LOG("autofloat is now %d\n", c_ws->auto_float);
1084 if (last_focused == NULL) {
1085 ELOG("Cannot toggle tiling/floating: workspace empty\n");
1089 Workspace *ws = last_focused->workspace;
1091 toggle_floating_mode(conn, last_focused, false);
1092 /* delete all empty columns/rows */
1093 cleanup_table(conn, ws);
1095 /* Fix colspan/rowspan if it’d overlap */
1096 fix_colrowspan(conn, ws);
1098 render_workspace(conn, ws->screen, ws);
1100 /* Re-focus the client because cleanup_table sets the focus to the last
1101 * focused client inside a container only. */
1102 set_focus(conn, last_focused, true);
1107 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1108 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1109 next_previous_workspace(conn, command[0]);
1113 /* It’s a normal <cmd> */
1115 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1116 direction_t direction;
1117 int times = strtol(command, &rest, 10);
1119 ELOG("Invalid command (\"%s\")\n", command);
1123 if (*rest == '\0') {
1124 /* No rest? This was a workspace number, not a times specification */
1125 workspace_show(conn, times);
1129 if (*rest == 'm' || *rest == 's') {
1130 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1134 int workspace = strtol(rest, &rest, 10);
1137 ELOG("Invalid command (\"%s\")\n", command);
1141 if (*rest == '\0') {
1142 if (last_focused != NULL && client_is_floating(last_focused))
1143 move_floating_window_to_workspace(conn, last_focused, workspace);
1144 else move_current_window_to_workspace(conn, workspace);
1148 if (last_focused == NULL) {
1149 ELOG("Not performing (no window found)\n");
1153 if (client_is_floating(last_focused) &&
1154 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1155 ELOG("Not performing (floating)\n");
1159 /* Now perform action to <where> */
1160 while (*rest != '\0') {
1163 else if (*rest == 'j')
1165 else if (*rest == 'k')
1167 else if (*rest == 'l')
1168 direction = D_RIGHT;
1170 ELOG("unknown direction: %c\n", *rest);
1175 if (action == ACTION_FOCUS) {
1176 if (with == WITH_SCREEN) {
1177 focus_thing(conn, direction, THING_SCREEN);
1180 if (client_is_floating(last_focused)) {
1181 floating_focus_direction(conn, last_focused, direction);
1184 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1188 if (action == ACTION_MOVE) {
1189 if (with == WITH_SCREEN) {
1190 /* TODO: this should swap the screen’s contents
1191 * (e.g. all workspaces) with the next/previous/…
1193 ELOG("Not yet implemented\n");
1196 if (client_is_floating(last_focused)) {
1197 floating_move(conn, last_focused, direction);
1200 if (with == WITH_WINDOW)
1201 move_current_window(conn, direction);
1202 else move_current_container(conn, direction);
1206 if (action == ACTION_SNAP) {
1207 if (with == WITH_SCREEN) {
1208 ELOG("You cannot snap a screen (it makes no sense).\n");
1211 snap_current_container(conn, direction);