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 #include "sighandler.h"
36 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
37 /* If this container is empty, we’re done */
38 if (container->currently_focused == NULL)
41 /* Get the previous/next client or wrap around */
42 Client *candidate = NULL;
43 if (direction == D_UP) {
44 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
45 candidate = CIRCLEQ_LAST(&(container->clients));
47 else if (direction == D_DOWN) {
48 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
49 candidate = CIRCLEQ_FIRST(&(container->clients));
50 } else ELOG("Direction not implemented!\n");
52 /* If we could not switch, the container contains exactly one client. We return false */
53 if (candidate == container->currently_focused)
57 set_focus(conn, candidate, true);
62 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
64 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
66 LOG("Jumping to \"%s\"\n", mark);
69 TAILQ_FOREACH(ws, workspaces, workspaces)
70 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
71 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
74 workspace_show(conn, current->workspace->num + 1);
75 set_focus(conn, current, true);
79 ELOG("No window with this mark found\n");
82 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
83 DLOG("focusing direction %d\n", direction);
85 int new_row = current_row,
86 new_col = current_col;
87 Container *container = CUR_CELL;
88 Workspace *t_ws = c_ws;
90 /* Makes sure new_col and new_row are within bounds of the new workspace */
91 void check_colrow_boundaries() {
92 if (new_col >= t_ws->cols)
93 new_col = (t_ws->cols - 1);
94 if (new_row >= t_ws->rows)
95 new_row = (t_ws->rows - 1);
98 /* There always is a container. If not, current_col or current_row is wrong */
99 assert(container != NULL);
101 if (container->workspace->fullscreen_client != NULL) {
102 LOG("You're in fullscreen mode. Won't switch focus\n");
106 /* For focusing screens, situation is different: we get the rect
107 * of the current screen, then get the screen which is on its
108 * right/left/bottom/top and just switch to the workspace on
109 * the target screen. */
110 if (thing == THING_SCREEN) {
111 i3Screen *cs = c_ws->screen;
113 Rect bounds = cs->rect;
115 if (direction == D_RIGHT)
116 bounds.x += bounds.width;
117 else if (direction == D_LEFT)
118 bounds.x -= bounds.width;
119 else if (direction == D_UP)
120 bounds.y -= bounds.height;
121 else bounds.y += bounds.height;
123 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
124 if (target == NULL) {
125 DLOG("Target screen NULL\n");
126 /* Wrap around if the target screen is out of bounds */
127 if (direction == D_RIGHT)
128 target = get_screen_most(D_LEFT, cs);
129 else if (direction == D_LEFT)
130 target = get_screen_most(D_RIGHT, cs);
131 else if (direction == D_UP)
132 target = get_screen_most(D_DOWN, cs);
133 else target = get_screen_most(D_UP, cs);
136 DLOG("Switching to ws %d\n", target->current_workspace + 1);
137 workspace_show(conn, target->current_workspace->num + 1);
141 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
142 if (direction == D_UP || direction == D_DOWN) {
143 if (thing == THING_WINDOW)
144 /* Let’s see if we can perform up/down focus in the current container */
145 if (focus_window_in_container(conn, container, direction))
148 if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
149 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
150 else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
151 /* Set new_row as a sane default, but it may get overwritten in a second */
154 /* Search from the top to correctly handle rowspanned containers */
155 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
156 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
163 /* Let’s see if there is a screen down/up there to which we can switch */
164 DLOG("container is at %d with height %d\n", container->y, container->height);
166 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
167 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
168 DLOG("Wrapping screen around vertically\n");
169 /* No screen found? Then wrap */
170 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
172 t_ws = screen->current_workspace;
173 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
176 check_colrow_boundaries();
178 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
179 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
180 DLOG("Cell empty, checking for colspanned client above...\n");
181 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
182 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
188 DLOG("Fixed it to new col %d\n", new_col);
190 } else if (direction == D_LEFT || direction == D_RIGHT) {
191 if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
192 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
193 else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
194 /* Set new_col as a sane default, but it may get overwritten in a second */
197 /* Search from the left to correctly handle colspanned containers */
198 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
199 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
206 /* Let’s see if there is a screen left/right here to which we can switch */
207 DLOG("container is at %d with width %d\n", container->x, container->width);
209 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
210 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
211 DLOG("Wrapping screen around horizontally\n");
212 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
214 t_ws = screen->current_workspace;
215 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
218 check_colrow_boundaries();
220 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
221 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
222 DLOG("Cell empty, checking for rowspanned client above...\n");
223 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
224 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
230 DLOG("Fixed it to new row %d\n", new_row);
233 ELOG("direction unhandled\n");
237 check_colrow_boundaries();
239 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
240 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
244 * Tries to move the window inside its current container.
246 * Returns true if the window could be moved, false otherwise.
249 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
250 direction_t direction) {
251 assert(client->container != NULL);
253 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
254 CIRCLEQ_NEXT(client, clients));
256 if (other == CIRCLEQ_END(&(client->container->clients)))
259 DLOG("i can do that\n");
260 /* We can move the client inside its current container */
261 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
262 if (direction == D_UP)
263 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
264 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
270 * Moves the current window or whole container to the given direction, creating a column/row if
274 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
275 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
276 (direction == D_LEFT ? "left" : "right"))));
277 /* Get current window */
278 Container *container = CUR_CELL,
281 /* There has to be a container, see focus_window() */
282 assert(container != NULL);
284 /* If there is no window or the dock window is focused, we’re done */
285 if (container->currently_focused == NULL ||
286 container->currently_focused->dock)
289 /* As soon as the client is moved away, the last focused client in the old
290 * container needs to get focus, if any. Therefore, we save it here. */
291 Client *current_client = container->currently_focused;
292 Client *to_focus = get_last_focused_client(conn, container, current_client);
294 if (to_focus == NULL) {
295 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
296 if (to_focus == NULL)
297 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
302 /* If we’re at the left-most position, move the rest of the table right */
303 if (current_col == 0) {
304 expand_table_cols_at_head(c_ws);
307 new = CUR_TABLE[--current_col][current_row];
310 if (current_col == (c_ws->cols-1))
311 expand_table_cols(c_ws);
313 new = CUR_TABLE[++current_col][current_row];
316 if (move_current_window_in_container(conn, current_client, D_UP))
319 /* if we’re at the up-most position, move the rest of the table down */
320 if (current_row == 0) {
321 expand_table_rows_at_head(c_ws);
324 new = CUR_TABLE[current_col][--current_row];
327 if (move_current_window_in_container(conn, current_client, D_DOWN))
330 if (current_row == (c_ws->rows-1))
331 expand_table_rows(c_ws);
333 new = CUR_TABLE[current_col][++current_row];
335 /* To make static analyzers happy: */
340 /* Remove it from the old container and put it into the new one */
341 client_remove_from_container(conn, current_client, container, true);
343 if (new->currently_focused != NULL)
344 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
345 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
346 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
348 /* Update data structures */
349 current_client->container = new;
350 current_client->workspace = new->workspace;
351 container->currently_focused = to_focus;
352 new->currently_focused = current_client;
354 Workspace *workspace = container->workspace;
356 /* delete all empty columns/rows */
357 cleanup_table(conn, workspace);
359 /* Fix colspan/rowspan if it’d overlap */
360 fix_colrowspan(conn, workspace);
362 render_workspace(conn, workspace->screen, workspace);
365 set_focus(conn, current_client, true);
368 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
369 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
370 (direction == D_LEFT ? "left" : "right"))));
371 /* Get current window */
372 Container *container = CUR_CELL,
375 Container **old = &CUR_CELL;
377 /* There has to be a container, see focus_window() */
378 assert(container != NULL);
382 /* If we’re at the left-most position, move the rest of the table right */
383 if (current_col == 0) {
384 expand_table_cols_at_head(c_ws);
386 old = &CUR_TABLE[current_col+1][current_row];
388 new = CUR_TABLE[--current_col][current_row];
391 if (current_col == (c_ws->cols-1))
392 expand_table_cols(c_ws);
394 new = CUR_TABLE[++current_col][current_row];
397 /* if we’re at the up-most position, move the rest of the table down */
398 if (current_row == 0) {
399 expand_table_rows_at_head(c_ws);
401 old = &CUR_TABLE[current_col][current_row+1];
403 new = CUR_TABLE[current_col][--current_row];
406 if (current_row == (c_ws->rows-1))
407 expand_table_rows(c_ws);
409 new = CUR_TABLE[current_col][++current_row];
411 /* To make static analyzers happy: */
416 DLOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
418 /* Swap the containers */
423 new->col = container->col;
424 new->row = container->row;
426 CUR_CELL = container;
427 container->col = col;
428 container->row = row;
430 Workspace *workspace = container->workspace;
432 /* delete all empty columns/rows */
433 cleanup_table(conn, workspace);
435 /* Fix colspan/rowspan if it’d overlap */
436 fix_colrowspan(conn, workspace);
442 * "Snaps" the current container (not possible for windows, because it works at table base)
443 * to the given direction, that is, adjusts cellspan/rowspan
446 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
447 LOG("snapping container to direction %d\n", direction);
449 Container *container = CUR_CELL;
451 assert(container != NULL);
455 /* Snap to the left is actually a move to the left and then a snap right */
456 if (!cell_exists(container->workspace, container->col - 1, container->row) ||
457 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
458 ELOG("cannot snap to left - the cell is already used\n");
462 move_current_window(conn, D_LEFT);
463 snap_current_container(conn, D_RIGHT);
466 /* Check if the cell is used */
467 int new_col = container->col + container->colspan;
468 for (int i = 0; i < container->rowspan; i++)
469 if (!cell_exists(container->workspace, new_col, container->row + i) ||
470 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
471 ELOG("cannot snap to right - the cell is already used\n");
475 /* Check if there are other cells with rowspan, which are in our way.
476 * If so, reduce their rowspan. */
477 for (int i = container->row-1; i >= 0; i--) {
478 DLOG("we got cell %d, %d with rowspan %d\n",
479 new_col, i, CUR_TABLE[new_col][i]->rowspan);
480 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
481 CUR_TABLE[new_col][i]->rowspan--;
482 DLOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
485 container->colspan++;
489 if (!cell_exists(container->workspace, container->col, container->row - 1) ||
490 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
491 ELOG("cannot snap to top - the cell is already used\n");
495 move_current_window(conn, D_UP);
496 snap_current_container(conn, D_DOWN);
499 DLOG("snapping down\n");
500 int new_row = container->row + container->rowspan;
501 for (int i = 0; i < container->colspan; i++)
502 if (!cell_exists(container->workspace, container->col + i, new_row) ||
503 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
504 ELOG("cannot snap down - the cell is already used\n");
508 for (int i = container->col-1; i >= 0; i--) {
509 DLOG("we got cell %d, %d with colspan %d\n",
510 i, new_row, CUR_TABLE[i][new_row]->colspan);
511 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
512 CUR_TABLE[i][new_row]->colspan--;
513 DLOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
517 container->rowspan++;
520 /* To make static analyzers happy: */
528 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
529 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
530 Workspace *t_ws = workspace_get(workspace-1),
531 *old_ws = client->workspace;
533 LOG("moving floating\n");
535 workspace_initialize(t_ws, c_ws->screen, false);
537 /* Check if there is already a fullscreen client on the destination workspace and
538 * stop moving if so. */
539 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
540 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
544 floating_assign_to_workspace(client, t_ws);
546 /* If we’re moving it to an invisible screen, we need to unmap it */
547 if (!workspace_is_visible(t_ws)) {
548 DLOG("This workspace is not visible, unmapping\n");
549 client_unmap(conn, client);
551 /* If this is not the case, we move the window to a workspace
552 * which is on another screen, so we also need to adjust its
554 DLOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
555 uint32_t relative_x = client->rect.x - old_ws->rect.x,
556 relative_y = client->rect.y - old_ws->rect.y;
557 DLOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
558 client->rect.x = t_ws->rect.x + relative_x;
559 client->rect.y = t_ws->rect.y + relative_y;
560 DLOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
561 reposition_client(conn, client);
569 if (workspace_is_visible(t_ws))
570 set_focus(conn, client, true);
574 * Moves the currently selected window to the given workspace
577 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
578 LOG("Moving current window to workspace %d\n", workspace);
580 Container *container = CUR_CELL;
582 assert(container != NULL);
584 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
585 Workspace *t_ws = workspace_get(workspace-1);
587 Client *current_client = container->currently_focused;
588 if (current_client == NULL) {
589 ELOG("No currently focused client in current container.\n");
592 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
593 if (to_focus == NULL)
594 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
596 workspace_initialize(t_ws, container->workspace->screen, false);
597 /* Check if there is already a fullscreen client on the destination workspace and
598 * stop moving if so. */
599 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
600 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
604 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
606 assert(to_container != NULL);
608 client_remove_from_container(conn, current_client, container, true);
609 if (container->workspace->fullscreen_client == current_client)
610 container->workspace->fullscreen_client = NULL;
612 /* TODO: insert it to the correct position */
613 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
615 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
618 current_client->container = to_container;
619 current_client->workspace = to_container->workspace;
620 container->currently_focused = to_focus;
621 to_container->currently_focused = current_client;
623 /* If we’re moving it to an invisible screen, we need to unmap it */
624 if (!workspace_is_visible(to_container->workspace)) {
625 DLOG("This workspace is not visible, unmapping\n");
626 client_unmap(conn, current_client);
628 if (current_client->fullscreen) {
629 DLOG("Calling client_enter_fullscreen again\n");
630 client_enter_fullscreen(conn, current_client);
634 /* delete all empty columns/rows */
635 cleanup_table(conn, container->workspace);
639 if (workspace_is_visible(to_container->workspace))
640 set_focus(conn, current_client, true);
644 * Jumps to the given window class / title.
645 * Title is matched using strstr, that is, matches if it appears anywhere
646 * in the string. Regular expressions seem to be a bit overkill here. However,
647 * if we need them for something else somewhen, we may introduce them here, too.
650 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
654 /* The first character is a quote, this was checked before */
655 classtitle = sstrdup(arguments+1);
656 /* The last character is a quote, we just set it to NULL */
657 classtitle[strlen(classtitle)-1] = '\0';
659 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
661 ELOG("No matching client found.\n");
666 set_focus(conn, client, true);
670 * Jump directly to the specified workspace, row and col.
671 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
674 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
678 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
679 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
681 /* No match? Either no arguments were specified, or no numbers */
683 ELOG("At least one valid argument required\n");
687 /* Move to the target workspace */
688 workspace_show(conn, ws);
693 DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
695 /* Move to row/col */
696 if (row >= c_ws->rows)
697 row = c_ws->rows - 1;
698 if (col >= c_ws->cols)
699 col = c_ws->cols - 1;
701 DLOG("Jumping to col %d, row %d\n", col, row);
702 if (c_ws->table[col][row]->currently_focused != NULL)
703 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
707 * Travels the focus stack by the given number of times (or once, if no argument
708 * was specified). That is, selects the window you were in before you focused
709 * the current window.
711 * The special values 'floating' (select the next floating window), 'tiling'
712 * (select the next tiling window), 'ft' (if the current window is floating,
713 * select the next tiling window and vice-versa) are also valid
716 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
717 /* Start count at -1 to always skip the first element */
718 int times, count = -1;
720 bool floating_criteria;
722 /* Either it’s one of the special values… */
723 if (strcasecmp(arguments, "floating") == 0) {
724 floating_criteria = true;
725 } else if (strcasecmp(arguments, "tiling") == 0) {
726 floating_criteria = false;
727 } else if (strcasecmp(arguments, "ft") == 0) {
728 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
729 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
730 ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
734 floating_criteria = !client_is_floating(last_focused);
736 /* …or a number was specified */
737 if (sscanf(arguments, "%u", ×) != 1) {
738 ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
742 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
743 if (++count < times) {
749 set_focus(conn, current, true);
755 /* Select the next client matching the criteria parsed above */
756 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
757 if (client_is_floating(current) == floating_criteria) {
758 set_focus(conn, current, true);
764 * Switch to next or previous existing workspace
767 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
768 Workspace *ws = c_ws;
770 if (direction == 'n') {
771 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
772 if (ws->screen == NULL)
775 workspace_show(conn, ws->num + 1);
778 } else if (direction == 'p') {
779 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
780 if (ws->screen == NULL)
783 workspace_show(conn, ws->num + 1);
789 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
791 resize_orientation_t orientation = O_VERTICAL;
792 Container *con = last_focused->container;
793 Workspace *ws = con->workspace;
795 if (STARTS_WITH(command, "left")) {
798 first = con->col - 1;
800 command += strlen("left");
801 } else if (STARTS_WITH(command, "right")) {
802 first = con->col + (con->colspan - 1);
803 DLOG("column %d\n", first);
805 if (!cell_exists(ws, first, con->row) ||
806 (first == (ws->cols-1)))
810 command += strlen("right");
811 } else if (STARTS_WITH(command, "top")) {
814 first = con->row - 1;
816 orientation = O_HORIZONTAL;
817 command += strlen("top");
818 } else if (STARTS_WITH(command, "bottom")) {
819 first = con->row + (con->rowspan - 1);
820 if (!cell_exists(ws, con->col, first) ||
821 (first == (ws->rows-1)))
825 orientation = O_HORIZONTAL;
826 command += strlen("bottom");
828 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
832 int pixels = atoi(command);
836 resize_container(conn, ws, first, second, orientation, pixels);
840 * Parses a command, see file CMDMODE for more information
843 void parse_command(xcb_connection_t *conn, const char *command) {
844 LOG("--- parsing command \"%s\" ---\n", command);
845 /* Get the first client from focus stack because floating clients are not
846 * in any container, therefore CUR_CELL is not appropriate. */
847 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
848 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
851 /* Hmm, just to be sure */
852 if (command[0] == '\0')
855 /* Is it an <exec>? Then execute the given command. */
856 if (STARTS_WITH(command, "exec ")) {
857 LOG("starting \"%s\"\n", command + strlen("exec "));
858 start_application(command+strlen("exec "));
862 if (STARTS_WITH(command, "mark")) {
863 if (last_focused == NULL) {
864 ELOG("There is no window to mark\n");
867 const char *rest = command + strlen("mark");
871 DLOG("interactive mark starting\n");
872 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
874 LOG("mark with \"%s\"\n", rest);
875 client_mark(conn, last_focused, rest);
880 if (STARTS_WITH(command, "goto")) {
881 const char *rest = command + strlen("goto");
885 DLOG("interactive go to mark starting\n");
886 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
888 LOG("go to \"%s\"\n", rest);
889 jump_to_mark(conn, rest);
894 if (STARTS_WITH(command, "stack-limit ")) {
895 if (last_focused == NULL || client_is_floating(last_focused)) {
896 ELOG("No container focused\n");
899 const char *rest = command + strlen("stack-limit ");
900 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
901 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
902 rest += strlen("rows ");
903 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
904 last_focused->container->stack_limit = STACK_LIMIT_COLS;
905 rest += strlen("cols ");
907 ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
911 last_focused->container->stack_limit_value = atoi(rest);
912 if (last_focused->container->stack_limit_value == 0)
913 last_focused->container->stack_limit = STACK_LIMIT_NONE;
918 if (STARTS_WITH(command, "resize ")) {
919 if (last_focused == NULL)
921 const char *rest = command + strlen("resize ");
922 parse_resize_command(conn, last_focused, rest);
926 if (STARTS_WITH(command, "mode ")) {
927 const char *rest = command + strlen("mode ");
928 switch_mode(conn, rest);
932 /* Is it an <exit>? */
933 if (STARTS_WITH(command, "exit")) {
934 LOG("User issued exit-command, exiting without error.\n");
938 /* Is it a <reload>? */
939 if (STARTS_WITH(command, "reload")) {
940 load_configuration(conn, NULL, true);
944 /* Is it <restart>? Then restart in place. */
945 if (STARTS_WITH(command, "restart")) {
949 if (STARTS_WITH(command, "kill")) {
950 if (last_focused == NULL) {
951 ELOG("There is no window to kill\n");
955 LOG("Killing current window\n");
956 client_kill(conn, last_focused);
960 /* Is it a jump to a specified workspace, row, col? */
961 if (STARTS_WITH(command, "jump ")) {
962 const char *arguments = command + strlen("jump ");
963 if (arguments[0] == '"')
964 jump_to_window(conn, arguments);
965 else jump_to_container(conn, arguments);
969 /* Should we travel the focus stack? */
970 if (STARTS_WITH(command, "focus")) {
971 const char *arguments = command + strlen("focus ");
972 travel_focus_stack(conn, arguments);
976 /* Is it 'f' for fullscreen? */
977 if (command[0] == 'f') {
978 if (last_focused == NULL)
980 client_toggle_fullscreen(conn, last_focused);
984 /* Is it just 's' for stacking or 'd' for default? */
985 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
986 if (last_focused != NULL && client_is_floating(last_focused)) {
987 ELOG("not switching, this is a floating client\n");
990 LOG("Switching mode for current container\n");
991 int new_mode = MODE_DEFAULT;
992 if (command[0] == 's')
993 new_mode = MODE_STACK;
994 if (command[0] == 'T')
995 new_mode = MODE_TABBED;
996 switch_layout_mode(conn, CUR_CELL, new_mode);
1000 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1001 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1002 if (command[0] == 'b') {
1003 if (last_focused == NULL) {
1004 ELOG("No window focused, cannot change border type\n");
1008 char com = command[1];
1009 if (command[1] == 't') {
1010 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1011 !last_focused->borderless)
1013 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1014 !last_focused->borderless)
1019 client_change_border(conn, last_focused, com);
1023 if (command[0] == 'H') {
1024 LOG("Hiding all floating windows\n");
1025 floating_toggle_hide(conn, c_ws);
1029 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1031 /* Is it a <with>? */
1032 if (command[0] == 'w') {
1034 /* TODO: implement */
1035 if (command[0] == 'c') {
1036 with = WITH_CONTAINER;
1038 } else if (command[0] == 'w') {
1039 with = WITH_WORKSPACE;
1041 } else if (command[0] == 's') {
1045 ELOG("not yet implemented.\n");
1050 /* Is it 't' for toggle tiling/floating? */
1051 if (command[0] == 't') {
1052 if (with == WITH_WORKSPACE) {
1053 c_ws->auto_float = !c_ws->auto_float;
1054 LOG("autofloat is now %d\n", c_ws->auto_float);
1057 if (last_focused == NULL) {
1058 ELOG("Cannot toggle tiling/floating: workspace empty\n");
1062 Workspace *ws = last_focused->workspace;
1064 toggle_floating_mode(conn, last_focused, false);
1065 /* delete all empty columns/rows */
1066 cleanup_table(conn, ws);
1068 /* Fix colspan/rowspan if it’d overlap */
1069 fix_colrowspan(conn, ws);
1071 render_workspace(conn, ws->screen, ws);
1073 /* Re-focus the client because cleanup_table sets the focus to the last
1074 * focused client inside a container only. */
1075 set_focus(conn, last_focused, true);
1080 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1081 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1082 next_previous_workspace(conn, command[0]);
1086 /* It’s a normal <cmd> */
1088 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1089 direction_t direction;
1090 int times = strtol(command, &rest, 10);
1092 ELOG("Invalid command (\"%s\")\n", command);
1096 if (*rest == '\0') {
1097 /* No rest? This was a workspace number, not a times specification */
1098 workspace_show(conn, times);
1102 if (*rest == 'm' || *rest == 's') {
1103 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1107 int workspace = strtol(rest, &rest, 10);
1110 ELOG("Invalid command (\"%s\")\n", command);
1114 if (*rest == '\0') {
1115 if (last_focused != NULL && client_is_floating(last_focused))
1116 move_floating_window_to_workspace(conn, last_focused, workspace);
1117 else move_current_window_to_workspace(conn, workspace);
1121 if (last_focused == NULL) {
1122 ELOG("Not performing (no window found)\n");
1126 if (client_is_floating(last_focused) &&
1127 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1128 ELOG("Not performing (floating)\n");
1132 /* Now perform action to <where> */
1133 while (*rest != '\0') {
1136 else if (*rest == 'j')
1138 else if (*rest == 'k')
1140 else if (*rest == 'l')
1141 direction = D_RIGHT;
1143 ELOG("unknown direction: %c\n", *rest);
1148 if (action == ACTION_FOCUS) {
1149 if (with == WITH_SCREEN) {
1150 focus_thing(conn, direction, THING_SCREEN);
1153 if (client_is_floating(last_focused)) {
1154 floating_focus_direction(conn, last_focused, direction);
1157 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1161 if (action == ACTION_MOVE) {
1162 if (with == WITH_SCREEN) {
1163 /* TODO: this should swap the screen’s contents
1164 * (e.g. all workspaces) with the next/previous/…
1166 ELOG("Not yet implemented\n");
1169 if (client_is_floating(last_focused)) {
1170 floating_move(conn, last_focused, direction);
1173 if (with == WITH_WINDOW)
1174 move_current_window(conn, direction);
1175 else move_current_container(conn, direction);
1179 if (action == ACTION_SNAP) {
1180 if (with == WITH_SCREEN) {
1181 ELOG("You cannot snap a screen (it makes no sense).\n");
1184 snap_current_container(conn, direction);