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 client_warp_pointer_into(conn, current_client);
641 set_focus(conn, current_client, true);
646 * Jumps to the given window class / title.
647 * Title is matched using strstr, that is, matches if it appears anywhere
648 * in the string. Regular expressions seem to be a bit overkill here. However,
649 * if we need them for something else somewhen, we may introduce them here, too.
652 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
656 /* The first character is a quote, this was checked before */
657 classtitle = sstrdup(arguments+1);
658 /* The last character is a quote, we just set it to NULL */
659 classtitle[strlen(classtitle)-1] = '\0';
661 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
663 ELOG("No matching client found.\n");
668 set_focus(conn, client, true);
672 * Jump directly to the specified workspace, row and col.
673 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
676 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
680 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
681 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
683 /* No match? Either no arguments were specified, or no numbers */
685 ELOG("At least one valid argument required\n");
689 /* Move to the target workspace */
690 workspace_show(conn, ws);
695 DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
697 /* Move to row/col */
698 if (row >= c_ws->rows)
699 row = c_ws->rows - 1;
700 if (col >= c_ws->cols)
701 col = c_ws->cols - 1;
703 DLOG("Jumping to col %d, row %d\n", col, row);
704 if (c_ws->table[col][row]->currently_focused != NULL)
705 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
709 * Travels the focus stack by the given number of times (or once, if no argument
710 * was specified). That is, selects the window you were in before you focused
711 * the current window.
713 * The special values 'floating' (select the next floating window), 'tiling'
714 * (select the next tiling window), 'ft' (if the current window is floating,
715 * select the next tiling window and vice-versa) are also valid
718 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
719 /* Start count at -1 to always skip the first element */
720 int times, count = -1;
722 bool floating_criteria;
724 /* Either it’s one of the special values… */
725 if (strcasecmp(arguments, "floating") == 0) {
726 floating_criteria = true;
727 } else if (strcasecmp(arguments, "tiling") == 0) {
728 floating_criteria = false;
729 } else if (strcasecmp(arguments, "ft") == 0) {
730 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
731 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
732 ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
736 floating_criteria = !client_is_floating(last_focused);
738 /* …or a number was specified */
739 if (sscanf(arguments, "%u", ×) != 1) {
740 ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
744 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
745 if (++count < times) {
751 set_focus(conn, current, true);
757 /* Select the next client matching the criteria parsed above */
758 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
759 if (client_is_floating(current) == floating_criteria) {
760 set_focus(conn, current, true);
766 * Switch to next or previous existing workspace
769 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
770 Workspace *ws = c_ws;
772 if (direction == 'n') {
774 ws = TAILQ_NEXT(ws, workspaces);
776 if (ws == TAILQ_END(workspaces))
777 ws = TAILQ_FIRST(workspaces);
782 if (ws->screen == NULL)
785 workspace_show(conn, ws->num + 1);
788 } else if (direction == 'p') {
790 ws = TAILQ_PREV(ws, workspaces_head, workspaces);
792 if (ws == TAILQ_END(workspaces))
793 ws = TAILQ_LAST(workspaces, workspaces_head);
798 if (ws->screen == NULL)
801 workspace_show(conn, ws->num + 1);
807 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
809 resize_orientation_t orientation = O_VERTICAL;
810 Container *con = last_focused->container;
811 Workspace *ws = last_focused->workspace;
813 if (client_is_floating(last_focused)) {
814 DLOG("Resizing a floating client\n");
815 if (STARTS_WITH(command, "left")) {
816 command += strlen("left");
817 last_focused->rect.width += atoi(command);
818 last_focused->rect.x -= atoi(command);
819 } else if (STARTS_WITH(command, "right")) {
820 command += strlen("right");
821 last_focused->rect.width += atoi(command);
822 } else if (STARTS_WITH(command, "top")) {
823 command += strlen("top");
824 last_focused->rect.height += atoi(command);
825 last_focused->rect.y -= atoi(command);
826 } else if (STARTS_WITH(command, "bottom")) {
827 command += strlen("bottom");
828 last_focused->rect.height += atoi(command);
830 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
834 /* resize_client flushes */
835 resize_client(conn, last_focused);
840 if (STARTS_WITH(command, "left")) {
843 first = con->col - 1;
845 command += strlen("left");
846 } else if (STARTS_WITH(command, "right")) {
847 first = con->col + (con->colspan - 1);
848 DLOG("column %d\n", first);
850 if (!cell_exists(ws, first, con->row) ||
851 (first == (ws->cols-1)))
855 command += strlen("right");
856 } else if (STARTS_WITH(command, "top")) {
859 first = con->row - 1;
861 orientation = O_HORIZONTAL;
862 command += strlen("top");
863 } else if (STARTS_WITH(command, "bottom")) {
864 first = con->row + (con->rowspan - 1);
865 if (!cell_exists(ws, con->col, first) ||
866 (first == (ws->rows-1)))
870 orientation = O_HORIZONTAL;
871 command += strlen("bottom");
873 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
877 int pixels = atoi(command);
881 resize_container(conn, ws, first, second, orientation, pixels);
885 * Parses a command, see file CMDMODE for more information
888 void parse_command(xcb_connection_t *conn, const char *command) {
889 LOG("--- parsing command \"%s\" ---\n", command);
890 /* Get the first client from focus stack because floating clients are not
891 * in any container, therefore CUR_CELL is not appropriate. */
892 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
893 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
896 /* Hmm, just to be sure */
897 if (command[0] == '\0')
900 /* Is it an <exec>? Then execute the given command. */
901 if (STARTS_WITH(command, "exec ")) {
902 LOG("starting \"%s\"\n", command + strlen("exec "));
903 start_application(command+strlen("exec "));
907 if (STARTS_WITH(command, "mark")) {
908 if (last_focused == NULL) {
909 ELOG("There is no window to mark\n");
912 const char *rest = command + strlen("mark");
916 DLOG("interactive mark starting\n");
917 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
919 LOG("mark with \"%s\"\n", rest);
920 client_mark(conn, last_focused, rest);
925 if (STARTS_WITH(command, "goto")) {
926 const char *rest = command + strlen("goto");
930 DLOG("interactive go to mark starting\n");
931 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
933 LOG("go to \"%s\"\n", rest);
934 jump_to_mark(conn, rest);
939 if (STARTS_WITH(command, "stack-limit ")) {
940 if (last_focused == NULL || client_is_floating(last_focused)) {
941 ELOG("No container focused\n");
944 const char *rest = command + strlen("stack-limit ");
945 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
946 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
947 rest += strlen("rows ");
948 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
949 last_focused->container->stack_limit = STACK_LIMIT_COLS;
950 rest += strlen("cols ");
952 ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
956 last_focused->container->stack_limit_value = atoi(rest);
957 if (last_focused->container->stack_limit_value == 0)
958 last_focused->container->stack_limit = STACK_LIMIT_NONE;
963 if (STARTS_WITH(command, "resize ")) {
964 if (last_focused == NULL)
966 const char *rest = command + strlen("resize ");
967 parse_resize_command(conn, last_focused, rest);
971 if (STARTS_WITH(command, "mode ")) {
972 const char *rest = command + strlen("mode ");
973 switch_mode(conn, rest);
977 /* Is it an <exit>? */
978 if (STARTS_WITH(command, "exit")) {
979 LOG("User issued exit-command, exiting without error.\n");
983 /* Is it a <reload>? */
984 if (STARTS_WITH(command, "reload")) {
985 load_configuration(conn, NULL, true);
989 /* Is it <restart>? Then restart in place. */
990 if (STARTS_WITH(command, "restart")) {
994 if (STARTS_WITH(command, "kill")) {
995 if (last_focused == NULL) {
996 ELOG("There is no window to kill\n");
1000 LOG("Killing current window\n");
1001 client_kill(conn, last_focused);
1005 /* Is it a jump to a specified workspace, row, col? */
1006 if (STARTS_WITH(command, "jump ")) {
1007 const char *arguments = command + strlen("jump ");
1008 if (arguments[0] == '"')
1009 jump_to_window(conn, arguments);
1010 else jump_to_container(conn, arguments);
1014 /* Should we travel the focus stack? */
1015 if (STARTS_WITH(command, "focus")) {
1016 const char *arguments = command + strlen("focus ");
1017 travel_focus_stack(conn, arguments);
1021 /* Is it 'f' for fullscreen? */
1022 if (command[0] == 'f') {
1023 if (last_focused == NULL)
1025 client_toggle_fullscreen(conn, last_focused);
1029 /* Is it just 's' for stacking or 'd' for default? */
1030 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1031 if (last_focused != NULL && client_is_floating(last_focused)) {
1032 ELOG("not switching, this is a floating client\n");
1035 LOG("Switching mode for current container\n");
1036 int new_mode = MODE_DEFAULT;
1037 if (command[0] == 's')
1038 new_mode = MODE_STACK;
1039 if (command[0] == 'T')
1040 new_mode = MODE_TABBED;
1041 switch_layout_mode(conn, CUR_CELL, new_mode);
1045 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1046 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1047 if (command[0] == 'b') {
1048 if (last_focused == NULL) {
1049 ELOG("No window focused, cannot change border type\n");
1053 char com = command[1];
1054 if (command[1] == 't') {
1055 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1056 !last_focused->borderless)
1058 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1059 !last_focused->borderless)
1064 client_change_border(conn, last_focused, com);
1068 if (command[0] == 'H') {
1069 LOG("Hiding all floating windows\n");
1070 floating_toggle_hide(conn, c_ws);
1074 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1076 /* Is it a <with>? */
1077 if (command[0] == 'w') {
1079 /* TODO: implement */
1080 if (command[0] == 'c') {
1081 with = WITH_CONTAINER;
1083 } else if (command[0] == 'w') {
1084 with = WITH_WORKSPACE;
1086 } else if (command[0] == 's') {
1090 ELOG("not yet implemented.\n");
1095 /* Is it 't' for toggle tiling/floating? */
1096 if (command[0] == 't') {
1097 if (with == WITH_WORKSPACE) {
1098 c_ws->auto_float = !c_ws->auto_float;
1099 LOG("autofloat is now %d\n", c_ws->auto_float);
1102 if (last_focused == NULL) {
1103 ELOG("Cannot toggle tiling/floating: workspace empty\n");
1107 Workspace *ws = last_focused->workspace;
1109 toggle_floating_mode(conn, last_focused, false);
1110 /* delete all empty columns/rows */
1111 cleanup_table(conn, ws);
1113 /* Fix colspan/rowspan if it’d overlap */
1114 fix_colrowspan(conn, ws);
1116 render_workspace(conn, ws->screen, ws);
1118 /* Re-focus the client because cleanup_table sets the focus to the last
1119 * focused client inside a container only. */
1120 set_focus(conn, last_focused, true);
1125 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1126 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1127 next_previous_workspace(conn, command[0]);
1131 /* It’s a normal <cmd> */
1133 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1134 direction_t direction;
1135 int times = strtol(command, &rest, 10);
1137 ELOG("Invalid command (\"%s\")\n", command);
1141 if (*rest == '\0') {
1142 /* No rest? This was a workspace number, not a times specification */
1143 workspace_show(conn, times);
1147 if (*rest == 'm' || *rest == 's') {
1148 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1152 int workspace = strtol(rest, &rest, 10);
1155 ELOG("Invalid command (\"%s\")\n", command);
1159 if (*rest == '\0') {
1160 if (last_focused != NULL && client_is_floating(last_focused))
1161 move_floating_window_to_workspace(conn, last_focused, workspace);
1162 else move_current_window_to_workspace(conn, workspace);
1166 if (last_focused == NULL) {
1167 ELOG("Not performing (no window found)\n");
1171 if (client_is_floating(last_focused) &&
1172 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1173 ELOG("Not performing (floating)\n");
1177 /* Now perform action to <where> */
1178 while (*rest != '\0') {
1181 else if (*rest == 'j')
1183 else if (*rest == 'k')
1185 else if (*rest == 'l')
1186 direction = D_RIGHT;
1188 ELOG("unknown direction: %c\n", *rest);
1193 if (action == ACTION_FOCUS) {
1194 if (with == WITH_SCREEN) {
1195 focus_thing(conn, direction, THING_SCREEN);
1198 if (client_is_floating(last_focused)) {
1199 floating_focus_direction(conn, last_focused, direction);
1202 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1206 if (action == ACTION_MOVE) {
1207 if (with == WITH_SCREEN) {
1208 /* TODO: this should swap the screen’s contents
1209 * (e.g. all workspaces) with the next/previous/…
1211 ELOG("Not yet implemented\n");
1214 if (client_is_floating(last_focused)) {
1215 floating_move(conn, last_focused, direction);
1218 if (with == WITH_WINDOW)
1219 move_current_window(conn, direction);
1220 else move_current_container(conn, direction);
1224 if (action == ACTION_SNAP) {
1225 if (with == WITH_SCREEN) {
1226 ELOG("You cannot snap a screen (it makes no sense).\n");
1229 snap_current_container(conn, direction);