4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
30 #include "workspace.h"
34 #include "sighandler.h"
37 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
38 /* If this container is empty, we’re done */
39 if (container->currently_focused == NULL)
42 /* Get the previous/next client or wrap around */
43 Client *candidate = NULL;
44 if (direction == D_UP) {
45 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
46 candidate = CIRCLEQ_LAST(&(container->clients));
48 else if (direction == D_DOWN) {
49 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
50 candidate = CIRCLEQ_FIRST(&(container->clients));
51 } else ELOG("Direction not implemented!\n");
53 /* If we could not switch, the container contains exactly one client. We return false */
54 if (candidate == container->currently_focused)
58 set_focus(conn, candidate, true);
63 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
65 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
67 LOG("Jumping to \"%s\"\n", mark);
70 TAILQ_FOREACH(ws, workspaces, workspaces)
71 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
72 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
75 workspace_show(conn, current->workspace->num + 1);
76 set_focus(conn, current, true);
80 ELOG("No window with this mark found\n");
83 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
84 DLOG("focusing direction %d\n", direction);
86 int new_row = current_row,
87 new_col = current_col;
88 Container *container = CUR_CELL;
89 Workspace *t_ws = c_ws;
91 /* Makes sure new_col and new_row are within bounds of the new workspace */
92 void check_colrow_boundaries() {
93 if (new_col >= t_ws->cols)
94 new_col = (t_ws->cols - 1);
95 if (new_row >= t_ws->rows)
96 new_row = (t_ws->rows - 1);
99 /* There always is a container. If not, current_col or current_row is wrong */
100 assert(container != NULL);
102 if (container->workspace->fullscreen_client != NULL) {
103 LOG("You're in fullscreen mode. Forcing focus to operate on whole screens\n");
104 thing = THING_SCREEN;
107 /* For focusing screens, situation is different: we get the rect
108 * of the current screen, then get the screen which is on its
109 * right/left/bottom/top and just switch to the workspace on
110 * the target screen. */
111 if (thing == THING_SCREEN) {
112 Output *cs = c_ws->output;
114 Rect bounds = cs->rect;
116 if (direction == D_RIGHT)
117 bounds.x += bounds.width;
118 else if (direction == D_LEFT)
119 bounds.x -= bounds.width;
120 else if (direction == D_UP)
121 bounds.y -= bounds.height;
122 else bounds.y += bounds.height;
124 Output *target = get_output_containing(bounds.x, bounds.y);
125 if (target == NULL) {
126 DLOG("Target output NULL\n");
127 /* Wrap around if the target screen is out of bounds */
128 if (direction == D_RIGHT)
129 target = get_output_most(D_LEFT, cs);
130 else if (direction == D_LEFT)
131 target = get_output_most(D_RIGHT, cs);
132 else if (direction == D_UP)
133 target = get_output_most(D_DOWN, cs);
134 else target = get_output_most(D_UP, cs);
137 DLOG("Switching to ws %d\n", target->current_workspace + 1);
138 workspace_show(conn, target->current_workspace->num + 1);
142 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
143 if (direction == D_UP || direction == D_DOWN) {
144 if (thing == THING_WINDOW)
145 /* Let’s see if we can perform up/down focus in the current container */
146 if (focus_window_in_container(conn, container, direction))
149 if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
150 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
151 else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
152 /* Set new_row as a sane default, but it may get overwritten in a second */
155 /* Search from the top to correctly handle rowspanned containers */
156 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
157 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
164 /* Let’s see if there is a screen down/up there to which we can switch */
165 DLOG("container is at %d with height %d\n", container->y, container->height);
167 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
168 if ((output = get_output_containing(container->x, destination_y)) == NULL) {
169 DLOG("Wrapping screen around vertically\n");
170 /* No screen found? Then wrap */
171 output = get_output_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->output);
173 t_ws = output->current_workspace;
174 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
177 check_colrow_boundaries();
179 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
180 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
181 DLOG("Cell empty, checking for colspanned client above...\n");
182 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
183 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
189 DLOG("Fixed it to new col %d\n", new_col);
191 } else if (direction == D_LEFT || direction == D_RIGHT) {
192 if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
193 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
194 else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
195 /* Set new_col as a sane default, but it may get overwritten in a second */
198 /* Search from the left to correctly handle colspanned containers */
199 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
200 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
207 /* Let’s see if there is a screen left/right here to which we can switch */
208 DLOG("container is at %d with width %d\n", container->x, container->width);
210 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
211 if ((output = get_output_containing(destination_x, container->y)) == NULL) {
212 DLOG("Wrapping screen around horizontally\n");
213 output = get_output_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->output);
215 t_ws = output->current_workspace;
216 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
219 check_colrow_boundaries();
221 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
222 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
223 DLOG("Cell empty, checking for rowspanned client above...\n");
224 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
225 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
231 DLOG("Fixed it to new row %d\n", new_row);
234 ELOG("direction unhandled\n");
238 check_colrow_boundaries();
240 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
241 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
245 * Tries to move the window inside its current container.
247 * Returns true if the window could be moved, false otherwise.
250 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
251 direction_t direction) {
252 assert(client->container != NULL);
254 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
255 CIRCLEQ_NEXT(client, clients));
257 if (other == CIRCLEQ_END(&(client->container->clients)))
260 DLOG("i can do that\n");
261 /* We can move the client inside its current container */
262 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
263 if (direction == D_UP)
264 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
265 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
271 * Moves the current window or whole container to the given direction, creating a column/row if
275 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
276 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
277 (direction == D_LEFT ? "left" : "right"))));
278 /* Get current window */
279 Container *container = CUR_CELL,
282 /* There has to be a container, see focus_window() */
283 assert(container != NULL);
285 /* If there is no window or the dock window is focused, we’re done */
286 if (container->currently_focused == NULL ||
287 container->currently_focused->dock)
290 /* As soon as the client is moved away, the last focused client in the old
291 * container needs to get focus, if any. Therefore, we save it here. */
292 Client *current_client = container->currently_focused;
293 Client *to_focus = get_last_focused_client(conn, container, current_client);
295 if (to_focus == NULL) {
296 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
297 if (to_focus == NULL)
298 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
303 /* If we’re at the left-most position, move the rest of the table right */
304 if (current_col == 0) {
305 expand_table_cols_at_head(c_ws);
308 new = CUR_TABLE[--current_col][current_row];
311 if (current_col == (c_ws->cols-1))
312 expand_table_cols(c_ws);
314 new = CUR_TABLE[++current_col][current_row];
317 if (move_current_window_in_container(conn, current_client, D_UP))
320 /* if we’re at the up-most position, move the rest of the table down */
321 if (current_row == 0) {
322 expand_table_rows_at_head(c_ws);
325 new = CUR_TABLE[current_col][--current_row];
328 if (move_current_window_in_container(conn, current_client, D_DOWN))
331 if (current_row == (c_ws->rows-1))
332 expand_table_rows(c_ws);
334 new = CUR_TABLE[current_col][++current_row];
336 /* To make static analyzers happy: */
341 /* Remove it from the old container and put it into the new one */
342 client_remove_from_container(conn, current_client, container, true);
344 if (new->currently_focused != NULL)
345 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
346 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
347 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
349 /* Update data structures */
350 current_client->container = new;
351 current_client->workspace = new->workspace;
352 container->currently_focused = to_focus;
353 new->currently_focused = current_client;
355 Workspace *workspace = container->workspace;
357 /* delete all empty columns/rows */
358 cleanup_table(conn, workspace);
360 /* Fix colspan/rowspan if it’d overlap */
361 fix_colrowspan(conn, workspace);
363 render_workspace(conn, workspace->output, workspace);
366 set_focus(conn, current_client, true);
369 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
370 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
371 (direction == D_LEFT ? "left" : "right"))));
372 /* Get current window */
373 Container *container = CUR_CELL,
376 Container **old = &CUR_CELL;
378 /* There has to be a container, see focus_window() */
379 assert(container != NULL);
383 /* If we’re at the left-most position, move the rest of the table right */
384 if (current_col == 0) {
385 expand_table_cols_at_head(c_ws);
387 old = &CUR_TABLE[current_col+1][current_row];
389 new = CUR_TABLE[--current_col][current_row];
392 if (current_col == (c_ws->cols-1))
393 expand_table_cols(c_ws);
395 new = CUR_TABLE[++current_col][current_row];
398 /* if we’re at the up-most position, move the rest of the table down */
399 if (current_row == 0) {
400 expand_table_rows_at_head(c_ws);
402 old = &CUR_TABLE[current_col][current_row+1];
404 new = CUR_TABLE[current_col][--current_row];
407 if (current_row == (c_ws->rows-1))
408 expand_table_rows(c_ws);
410 new = CUR_TABLE[current_col][++current_row];
412 /* To make static analyzers happy: */
417 DLOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
419 /* Swap the containers */
424 new->col = container->col;
425 new->row = container->row;
427 CUR_CELL = container;
428 container->col = col;
429 container->row = row;
431 Workspace *workspace = container->workspace;
433 /* delete all empty columns/rows */
434 cleanup_table(conn, workspace);
436 /* Fix colspan/rowspan if it’d overlap */
437 fix_colrowspan(conn, workspace);
443 * "Snaps" the current container (not possible for windows, because it works at table base)
444 * to the given direction, that is, adjusts cellspan/rowspan
447 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
448 LOG("snapping container to direction %d\n", direction);
450 Container *container = CUR_CELL;
452 assert(container != NULL);
456 /* Snap to the left is actually a move to the left and then a snap right */
457 if (!cell_exists(container->workspace, container->col - 1, container->row) ||
458 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
459 ELOG("cannot snap to left - the cell is already used\n");
463 move_current_window(conn, D_LEFT);
464 snap_current_container(conn, D_RIGHT);
467 /* Check if the cell is used */
468 int new_col = container->col + container->colspan;
469 for (int i = 0; i < container->rowspan; i++)
470 if (!cell_exists(container->workspace, new_col, container->row + i) ||
471 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
472 ELOG("cannot snap to right - the cell is already used\n");
476 /* Check if there are other cells with rowspan, which are in our way.
477 * If so, reduce their rowspan. */
478 for (int i = container->row-1; i >= 0; i--) {
479 DLOG("we got cell %d, %d with rowspan %d\n",
480 new_col, i, CUR_TABLE[new_col][i]->rowspan);
481 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
482 CUR_TABLE[new_col][i]->rowspan--;
483 DLOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
486 container->colspan++;
490 if (!cell_exists(container->workspace, container->col, container->row - 1) ||
491 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
492 ELOG("cannot snap to top - the cell is already used\n");
496 move_current_window(conn, D_UP);
497 snap_current_container(conn, D_DOWN);
500 DLOG("snapping down\n");
501 int new_row = container->row + container->rowspan;
502 for (int i = 0; i < container->colspan; i++)
503 if (!cell_exists(container->workspace, container->col + i, new_row) ||
504 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
505 ELOG("cannot snap down - the cell is already used\n");
509 for (int i = container->col-1; i >= 0; i--) {
510 DLOG("we got cell %d, %d with colspan %d\n",
511 i, new_row, CUR_TABLE[i][new_row]->colspan);
512 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
513 CUR_TABLE[i][new_row]->colspan--;
514 DLOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
518 container->rowspan++;
521 /* To make static analyzers happy: */
529 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
530 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
531 Workspace *t_ws = workspace_get(workspace-1),
532 *old_ws = client->workspace;
534 LOG("moving floating\n");
536 workspace_initialize(t_ws, c_ws->output, false);
538 /* Check if there is already a fullscreen client on the destination workspace and
539 * stop moving if so. */
540 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
541 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
545 floating_assign_to_workspace(client, t_ws);
547 /* If we’re moving it to an invisible screen, we need to unmap it */
548 if (!workspace_is_visible(t_ws)) {
549 DLOG("This workspace is not visible, unmapping\n");
550 client_unmap(conn, client);
552 /* If this is not the case, we move the window to a workspace
553 * which is on another screen, so we also need to adjust its
555 DLOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
556 uint32_t relative_x = client->rect.x - old_ws->rect.x,
557 relative_y = client->rect.y - old_ws->rect.y;
558 DLOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
559 client->rect.x = t_ws->rect.x + relative_x;
560 client->rect.y = t_ws->rect.y + relative_y;
561 DLOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
562 reposition_client(conn, client);
570 if (workspace_is_visible(t_ws))
571 set_focus(conn, client, true);
575 * Moves the currently selected window to the given workspace
578 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
579 LOG("Moving current window to workspace %d\n", workspace);
581 Container *container = CUR_CELL;
583 assert(container != NULL);
585 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
586 Workspace *t_ws = workspace_get(workspace-1);
588 Client *current_client = container->currently_focused;
589 if (current_client == NULL) {
590 ELOG("No currently focused client in current container.\n");
593 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
594 if (to_focus == NULL)
595 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
597 workspace_initialize(t_ws, container->workspace->output, false);
598 /* Check if there is already a fullscreen client on the destination workspace and
599 * stop moving if so. */
600 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
601 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
605 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
607 assert(to_container != NULL);
609 client_remove_from_container(conn, current_client, container, true);
610 if (container->workspace->fullscreen_client == current_client)
611 container->workspace->fullscreen_client = NULL;
613 /* TODO: insert it to the correct position */
614 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
616 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
619 current_client->container = to_container;
620 current_client->workspace = to_container->workspace;
621 container->currently_focused = to_focus;
622 to_container->currently_focused = current_client;
624 /* If we’re moving it to an invisible screen, we need to unmap it */
625 if (!workspace_is_visible(to_container->workspace)) {
626 DLOG("This workspace is not visible, unmapping\n");
627 client_unmap(conn, current_client);
629 if (current_client->fullscreen) {
630 DLOG("Calling client_enter_fullscreen again\n");
631 client_enter_fullscreen(conn, current_client);
635 /* delete all empty columns/rows */
636 cleanup_table(conn, container->workspace);
640 if (workspace_is_visible(to_container->workspace)) {
641 client_warp_pointer_into(conn, current_client);
642 set_focus(conn, current_client, true);
647 * Jumps to the given window class / title.
648 * Title is matched using strstr, that is, matches if it appears anywhere
649 * in the string. Regular expressions seem to be a bit overkill here. However,
650 * if we need them for something else somewhen, we may introduce them here, too.
653 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
657 /* The first character is a quote, this was checked before */
658 classtitle = sstrdup(arguments+1);
659 /* The last character is a quote, we just set it to NULL */
660 classtitle[strlen(classtitle)-1] = '\0';
662 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
664 ELOG("No matching client found.\n");
669 set_focus(conn, client, true);
673 * Jump directly to the specified workspace, row and col.
674 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
677 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
681 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
682 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
684 /* No match? Either no arguments were specified, or no numbers */
686 ELOG("At least one valid argument required\n");
690 /* Move to the target workspace */
691 workspace_show(conn, ws);
696 DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
698 /* Move to row/col */
699 if (row >= c_ws->rows)
700 row = c_ws->rows - 1;
701 if (col >= c_ws->cols)
702 col = c_ws->cols - 1;
704 DLOG("Jumping to col %d, row %d\n", col, row);
705 if (c_ws->table[col][row]->currently_focused != NULL)
706 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
710 * Travels the focus stack by the given number of times (or once, if no argument
711 * was specified). That is, selects the window you were in before you focused
712 * the current window.
714 * The special values 'floating' (select the next floating window), 'tiling'
715 * (select the next tiling window), 'ft' (if the current window is floating,
716 * select the next tiling window and vice-versa) are also valid
719 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
720 /* Start count at -1 to always skip the first element */
721 int times, count = -1;
723 bool floating_criteria;
725 /* Either it’s one of the special values… */
726 if (strcasecmp(arguments, "floating") == 0) {
727 floating_criteria = true;
728 } else if (strcasecmp(arguments, "tiling") == 0) {
729 floating_criteria = false;
730 } else if (strcasecmp(arguments, "ft") == 0) {
731 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
732 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
733 ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
737 floating_criteria = !client_is_floating(last_focused);
739 /* …or a number was specified */
740 if (sscanf(arguments, "%u", ×) != 1) {
741 ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
745 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
746 if (++count < times) {
752 set_focus(conn, current, true);
758 /* Select the next client matching the criteria parsed above */
759 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
760 if (client_is_floating(current) == floating_criteria) {
761 set_focus(conn, current, true);
767 * Switch to next or previous existing workspace
770 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
771 Workspace *ws = c_ws;
773 if (direction == 'n') {
775 ws = TAILQ_NEXT(ws, workspaces);
777 if (ws == TAILQ_END(workspaces))
778 ws = TAILQ_FIRST(workspaces);
783 if (ws->output == NULL)
786 workspace_show(conn, ws->num + 1);
789 } else if (direction == 'p') {
791 ws = TAILQ_PREV(ws, workspaces_head, workspaces);
793 if (ws == TAILQ_END(workspaces))
794 ws = TAILQ_LAST(workspaces, workspaces_head);
799 if (ws->output == NULL)
802 workspace_show(conn, ws->num + 1);
808 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
810 resize_orientation_t orientation = O_VERTICAL;
811 Container *con = last_focused->container;
812 Workspace *ws = last_focused->workspace;
814 if (client_is_floating(last_focused)) {
815 DLOG("Resizing a floating client\n");
816 if (STARTS_WITH(command, "left")) {
817 command += strlen("left");
818 last_focused->rect.width += atoi(command);
819 last_focused->rect.x -= atoi(command);
820 } else if (STARTS_WITH(command, "right")) {
821 command += strlen("right");
822 last_focused->rect.width += atoi(command);
823 } else if (STARTS_WITH(command, "top")) {
824 command += strlen("top");
825 last_focused->rect.height += atoi(command);
826 last_focused->rect.y -= atoi(command);
827 } else if (STARTS_WITH(command, "bottom")) {
828 command += strlen("bottom");
829 last_focused->rect.height += atoi(command);
831 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
835 /* resize_client flushes */
836 resize_client(conn, last_focused);
841 if (STARTS_WITH(command, "left")) {
844 first = con->col - 1;
846 command += strlen("left");
847 } else if (STARTS_WITH(command, "right")) {
848 first = con->col + (con->colspan - 1);
849 DLOG("column %d\n", first);
851 if (!cell_exists(ws, first, con->row) ||
852 (first == (ws->cols-1)))
856 command += strlen("right");
857 } else if (STARTS_WITH(command, "top")) {
860 first = con->row - 1;
862 orientation = O_HORIZONTAL;
863 command += strlen("top");
864 } else if (STARTS_WITH(command, "bottom")) {
865 first = con->row + (con->rowspan - 1);
866 if (!cell_exists(ws, con->col, first) ||
867 (first == (ws->rows-1)))
871 orientation = O_HORIZONTAL;
872 command += strlen("bottom");
874 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
878 int pixels = atoi(command);
882 resize_container(conn, ws, first, second, orientation, pixels);
886 * Parses a command, see file CMDMODE for more information
889 void parse_command(xcb_connection_t *conn, const char *command) {
890 LOG("--- parsing command \"%s\" ---\n", command);
891 /* Get the first client from focus stack because floating clients are not
892 * in any container, therefore CUR_CELL is not appropriate. */
893 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
894 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
897 /* Hmm, just to be sure */
898 if (command[0] == '\0')
901 /* Is it an <exec>? Then execute the given command. */
902 if (STARTS_WITH(command, "exec ")) {
903 LOG("starting \"%s\"\n", command + strlen("exec "));
904 start_application(command+strlen("exec "));
908 if (STARTS_WITH(command, "mark")) {
909 if (last_focused == NULL) {
910 ELOG("There is no window to mark\n");
913 const char *rest = command + strlen("mark");
917 DLOG("interactive mark starting\n");
918 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
920 LOG("mark with \"%s\"\n", rest);
921 client_mark(conn, last_focused, rest);
926 if (STARTS_WITH(command, "goto")) {
927 const char *rest = command + strlen("goto");
931 DLOG("interactive go to mark starting\n");
932 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
934 LOG("go to \"%s\"\n", rest);
935 jump_to_mark(conn, rest);
940 if (STARTS_WITH(command, "stack-limit ")) {
941 if (last_focused == NULL || client_is_floating(last_focused)) {
942 ELOG("No container focused\n");
945 const char *rest = command + strlen("stack-limit ");
946 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
947 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
948 rest += strlen("rows ");
949 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
950 last_focused->container->stack_limit = STACK_LIMIT_COLS;
951 rest += strlen("cols ");
953 ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
957 last_focused->container->stack_limit_value = atoi(rest);
958 if (last_focused->container->stack_limit_value == 0)
959 last_focused->container->stack_limit = STACK_LIMIT_NONE;
964 if (STARTS_WITH(command, "resize ")) {
965 if (last_focused == NULL)
967 const char *rest = command + strlen("resize ");
968 parse_resize_command(conn, last_focused, rest);
972 if (STARTS_WITH(command, "mode ")) {
973 const char *rest = command + strlen("mode ");
974 switch_mode(conn, rest);
978 /* Is it an <exit>? */
979 if (STARTS_WITH(command, "exit")) {
980 LOG("User issued exit-command, exiting without error.\n");
981 restore_geometry(global_conn);
985 /* Is it a <reload>? */
986 if (STARTS_WITH(command, "reload")) {
987 load_configuration(conn, NULL, true);
991 /* Is it <restart>? Then restart in place. */
992 if (STARTS_WITH(command, "restart")) {
996 if (STARTS_WITH(command, "kill")) {
997 if (last_focused == NULL) {
998 ELOG("There is no window to kill\n");
1002 LOG("Killing current window\n");
1003 client_kill(conn, last_focused);
1007 /* Is it a jump to a specified workspace, row, col? */
1008 if (STARTS_WITH(command, "jump ")) {
1009 const char *arguments = command + strlen("jump ");
1010 if (arguments[0] == '"')
1011 jump_to_window(conn, arguments);
1012 else jump_to_container(conn, arguments);
1016 /* Should we travel the focus stack? */
1017 if (STARTS_WITH(command, "focus")) {
1018 const char *arguments = command + strlen("focus ");
1019 travel_focus_stack(conn, arguments);
1023 /* Is it 'f' for fullscreen? */
1024 if (command[0] == 'f') {
1025 if (last_focused == NULL)
1027 client_toggle_fullscreen(conn, last_focused);
1031 /* Is it just 's' for stacking or 'd' for default? */
1032 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1033 if (last_focused != NULL && client_is_floating(last_focused)) {
1034 ELOG("not switching, this is a floating client\n");
1037 LOG("Switching mode for current container\n");
1038 int new_mode = MODE_DEFAULT;
1039 if (command[0] == 's')
1040 new_mode = MODE_STACK;
1041 if (command[0] == 'T')
1042 new_mode = MODE_TABBED;
1043 switch_layout_mode(conn, CUR_CELL, new_mode);
1047 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1048 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1049 if (command[0] == 'b') {
1050 if (last_focused == NULL) {
1051 ELOG("No window focused, cannot change border type\n");
1055 char com = command[1];
1056 if (command[1] == 't') {
1057 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1058 !last_focused->borderless)
1060 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1061 !last_focused->borderless)
1066 client_change_border(conn, last_focused, com);
1070 if (command[0] == 'H') {
1071 LOG("Hiding all floating windows\n");
1072 floating_toggle_hide(conn, c_ws);
1076 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1078 /* Is it a <with>? */
1079 if (command[0] == 'w') {
1081 /* TODO: implement */
1082 if (command[0] == 'c') {
1083 with = WITH_CONTAINER;
1085 } else if (command[0] == 'w') {
1086 with = WITH_WORKSPACE;
1088 } else if (command[0] == 's') {
1092 ELOG("not yet implemented.\n");
1097 /* Is it 't' for toggle tiling/floating? */
1098 if (command[0] == 't') {
1099 if (with == WITH_WORKSPACE) {
1100 c_ws->auto_float = !c_ws->auto_float;
1101 LOG("autofloat is now %d\n", c_ws->auto_float);
1104 if (last_focused == NULL) {
1105 ELOG("Cannot toggle tiling/floating: workspace empty\n");
1109 Workspace *ws = last_focused->workspace;
1111 toggle_floating_mode(conn, last_focused, false);
1112 /* delete all empty columns/rows */
1113 cleanup_table(conn, ws);
1115 /* Fix colspan/rowspan if it’d overlap */
1116 fix_colrowspan(conn, ws);
1118 render_workspace(conn, ws->output, ws);
1120 /* Re-focus the client because cleanup_table sets the focus to the last
1121 * focused client inside a container only. */
1122 set_focus(conn, last_focused, true);
1127 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1128 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1129 next_previous_workspace(conn, command[0]);
1133 /* It’s a normal <cmd> */
1135 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1136 direction_t direction;
1137 int times = strtol(command, &rest, 10);
1139 ELOG("Invalid command (\"%s\")\n", command);
1143 if (*rest == '\0') {
1144 /* No rest? This was a workspace number, not a times specification */
1145 workspace_show(conn, times);
1149 if (*rest == 'm' || *rest == 's') {
1150 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1154 int workspace = strtol(rest, &rest, 10);
1157 ELOG("Invalid command (\"%s\")\n", command);
1161 if (*rest == '\0') {
1162 if (last_focused != NULL && client_is_floating(last_focused))
1163 move_floating_window_to_workspace(conn, last_focused, workspace);
1164 else move_current_window_to_workspace(conn, workspace);
1168 if (last_focused == NULL) {
1169 ELOG("Not performing (no window found)\n");
1173 if (client_is_floating(last_focused) &&
1174 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1175 ELOG("Not performing (floating)\n");
1179 /* Now perform action to <where> */
1180 while (*rest != '\0') {
1183 else if (*rest == 'j')
1185 else if (*rest == 'k')
1187 else if (*rest == 'l')
1188 direction = D_RIGHT;
1190 ELOG("unknown direction: %c\n", *rest);
1195 if (action == ACTION_FOCUS) {
1196 if (with == WITH_SCREEN) {
1197 focus_thing(conn, direction, THING_SCREEN);
1200 if (client_is_floating(last_focused)) {
1201 floating_focus_direction(conn, last_focused, direction);
1204 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1208 if (action == ACTION_MOVE) {
1209 if (with == WITH_SCREEN) {
1210 /* TODO: this should swap the screen’s contents
1211 * (e.g. all workspaces) with the next/previous/…
1213 ELOG("Not yet implemented\n");
1216 if (client_is_floating(last_focused)) {
1217 floating_move(conn, last_focused, direction);
1220 if (with == WITH_WINDOW)
1221 move_current_window(conn, direction);
1222 else move_current_container(conn, direction);
1226 if (action == ACTION_SNAP) {
1227 if (with == WITH_SCREEN) {
1228 ELOG("You cannot snap a screen (it makes no sense).\n");
1231 snap_current_container(conn, direction);