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 #define CHECK_COLROW_BOUNDARIES \
94 if (new_col >= t_ws->cols) \
95 new_col = (t_ws->cols - 1); \
96 if (new_row >= t_ws->rows) \
97 new_row = (t_ws->rows - 1); \
100 /* There always is a container. If not, current_col or current_row is wrong */
101 assert(container != NULL);
103 if (container->workspace->fullscreen_client != NULL) {
104 LOG("You're in fullscreen mode. Forcing focus to operate on whole screens\n");
105 thing = THING_SCREEN;
108 /* For focusing screens, situation is different: we get the rect
109 * of the current screen, then get the screen which is on its
110 * right/left/bottom/top and just switch to the workspace on
111 * the target screen. */
112 if (thing == THING_SCREEN) {
113 Output *cs = c_ws->output;
115 Rect bounds = cs->rect;
117 if (direction == D_RIGHT)
118 bounds.x += bounds.width;
119 else if (direction == D_LEFT)
120 bounds.x -= bounds.width;
121 else if (direction == D_UP)
122 bounds.y -= bounds.height;
123 else bounds.y += bounds.height;
125 Output *target = get_output_containing(bounds.x, bounds.y);
126 if (target == NULL) {
127 DLOG("Target output NULL\n");
128 /* Wrap around if the target screen is out of bounds */
129 if (direction == D_RIGHT)
130 target = get_output_most(D_LEFT, cs);
131 else if (direction == D_LEFT)
132 target = get_output_most(D_RIGHT, cs);
133 else if (direction == D_UP)
134 target = get_output_most(D_DOWN, cs);
135 else target = get_output_most(D_UP, cs);
138 DLOG("Switching to ws %d\n", target->current_workspace + 1);
139 workspace_show(conn, target->current_workspace->num + 1);
143 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
144 if (direction == D_UP || direction == D_DOWN) {
145 if (thing == THING_WINDOW)
146 /* Let’s see if we can perform up/down focus in the current container */
147 if (focus_window_in_container(conn, container, direction))
150 if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
151 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
152 else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
153 /* Set new_row as a sane default, but it may get overwritten in a second */
156 /* Search from the top to correctly handle rowspanned containers */
157 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
158 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
165 /* Let’s see if there is a screen down/up there to which we can switch */
166 DLOG("container is at %d with height %d\n", container->y, container->height);
168 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
169 if ((output = get_output_containing(container->x, destination_y)) == NULL) {
170 DLOG("Wrapping screen around vertically\n");
171 /* No screen found? Then wrap */
172 output = get_output_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->output);
174 t_ws = output->current_workspace;
175 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
178 CHECK_COLROW_BOUNDARIES;
180 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
181 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
182 DLOG("Cell empty, checking for colspanned client above...\n");
183 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
184 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
190 DLOG("Fixed it to new col %d\n", new_col);
192 } else if (direction == D_LEFT || direction == D_RIGHT) {
193 if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
194 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
195 else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
196 /* Set new_col as a sane default, but it may get overwritten in a second */
199 /* Search from the left to correctly handle colspanned containers */
200 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
201 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
208 /* Let’s see if there is a screen left/right here to which we can switch */
209 DLOG("container is at %d with width %d\n", container->x, container->width);
211 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
212 if ((output = get_output_containing(destination_x, container->y)) == NULL) {
213 DLOG("Wrapping screen around horizontally\n");
214 output = get_output_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->output);
216 t_ws = output->current_workspace;
217 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
220 CHECK_COLROW_BOUNDARIES;
222 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
223 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
224 DLOG("Cell empty, checking for rowspanned client above...\n");
225 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
226 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
232 DLOG("Fixed it to new row %d\n", new_row);
235 ELOG("direction unhandled\n");
239 CHECK_COLROW_BOUNDARIES;
241 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
242 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
246 * Tries to move the window inside its current container.
248 * Returns true if the window could be moved, false otherwise.
251 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
252 direction_t direction) {
253 assert(client->container != NULL);
255 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
256 CIRCLEQ_NEXT(client, clients));
258 if (other == CIRCLEQ_END(&(client->container->clients)))
261 DLOG("i can do that\n");
262 /* We can move the client inside its current container */
263 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
264 if (direction == D_UP)
265 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
266 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
272 * Moves the current window or whole container to the given direction, creating a column/row if
276 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
277 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
278 (direction == D_LEFT ? "left" : "right"))));
279 /* Get current window */
280 Container *container = CUR_CELL,
283 /* There has to be a container, see focus_window() */
284 assert(container != NULL);
286 /* If there is no window or the dock window is focused, we’re done */
287 if (container->currently_focused == NULL ||
288 container->currently_focused->dock)
291 /* As soon as the client is moved away, the last focused client in the old
292 * container needs to get focus, if any. Therefore, we save it here. */
293 Client *current_client = container->currently_focused;
294 Client *to_focus = get_last_focused_client(conn, container, current_client);
296 if (to_focus == NULL) {
297 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
298 if (to_focus == NULL)
299 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
304 /* If we’re at the left-most position, move the rest of the table right */
305 if (current_col == 0) {
306 expand_table_cols_at_head(c_ws);
309 new = CUR_TABLE[--current_col][current_row];
312 if (current_col == (c_ws->cols-1))
313 expand_table_cols(c_ws);
315 new = CUR_TABLE[++current_col][current_row];
318 if (move_current_window_in_container(conn, current_client, D_UP))
321 /* if we’re at the up-most position, move the rest of the table down */
322 if (current_row == 0) {
323 expand_table_rows_at_head(c_ws);
326 new = CUR_TABLE[current_col][--current_row];
329 if (move_current_window_in_container(conn, current_client, D_DOWN))
332 if (current_row == (c_ws->rows-1))
333 expand_table_rows(c_ws);
335 new = CUR_TABLE[current_col][++current_row];
337 /* To make static analyzers happy: */
342 /* Remove it from the old container and put it into the new one */
343 client_remove_from_container(conn, current_client, container, true);
345 if (new->currently_focused != NULL)
346 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
347 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
348 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
350 /* Update data structures */
351 current_client->container = new;
352 current_client->workspace = new->workspace;
353 container->currently_focused = to_focus;
354 new->currently_focused = current_client;
356 Workspace *workspace = container->workspace;
358 /* delete all empty columns/rows */
359 cleanup_table(conn, workspace);
361 /* Fix colspan/rowspan if it’d overlap */
362 fix_colrowspan(conn, workspace);
364 render_workspace(conn, workspace->output, workspace);
367 set_focus(conn, current_client, true);
370 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
371 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
372 (direction == D_LEFT ? "left" : "right"))));
373 /* Get current window */
374 Container *container = CUR_CELL,
377 Container **old = &CUR_CELL;
379 /* There has to be a container, see focus_window() */
380 assert(container != NULL);
384 /* If we’re at the left-most position, move the rest of the table right */
385 if (current_col == 0) {
386 expand_table_cols_at_head(c_ws);
388 old = &CUR_TABLE[current_col+1][current_row];
390 new = CUR_TABLE[--current_col][current_row];
393 if (current_col == (c_ws->cols-1))
394 expand_table_cols(c_ws);
396 new = CUR_TABLE[++current_col][current_row];
399 /* if we’re at the up-most position, move the rest of the table down */
400 if (current_row == 0) {
401 expand_table_rows_at_head(c_ws);
403 old = &CUR_TABLE[current_col][current_row+1];
405 new = CUR_TABLE[current_col][--current_row];
408 if (current_row == (c_ws->rows-1))
409 expand_table_rows(c_ws);
411 new = CUR_TABLE[current_col][++current_row];
413 /* To make static analyzers happy: */
418 DLOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
420 /* Swap the containers */
425 new->col = container->col;
426 new->row = container->row;
428 CUR_CELL = container;
429 container->col = col;
430 container->row = row;
432 Workspace *workspace = container->workspace;
434 /* delete all empty columns/rows */
435 cleanup_table(conn, workspace);
437 /* Fix colspan/rowspan if it’d overlap */
438 fix_colrowspan(conn, workspace);
444 * "Snaps" the current container (not possible for windows, because it works at table base)
445 * to the given direction, that is, adjusts cellspan/rowspan
448 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
449 LOG("snapping container to direction %d\n", direction);
451 Container *container = CUR_CELL;
453 assert(container != NULL);
457 /* Snap to the left is actually a move to the left and then a snap right */
458 if (!cell_exists(container->workspace, container->col - 1, container->row) ||
459 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
460 ELOG("cannot snap to left - the cell is already used\n");
464 move_current_window(conn, D_LEFT);
465 snap_current_container(conn, D_RIGHT);
468 /* Check if the cell is used */
469 int new_col = container->col + container->colspan;
470 for (int i = 0; i < container->rowspan; i++)
471 if (!cell_exists(container->workspace, new_col, container->row + i) ||
472 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
473 ELOG("cannot snap to right - the cell is already used\n");
477 /* Check if there are other cells with rowspan, which are in our way.
478 * If so, reduce their rowspan. */
479 for (int i = container->row-1; i >= 0; i--) {
480 DLOG("we got cell %d, %d with rowspan %d\n",
481 new_col, i, CUR_TABLE[new_col][i]->rowspan);
482 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
483 CUR_TABLE[new_col][i]->rowspan--;
484 DLOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
487 container->colspan++;
491 if (!cell_exists(container->workspace, container->col, container->row - 1) ||
492 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
493 ELOG("cannot snap to top - the cell is already used\n");
497 move_current_window(conn, D_UP);
498 snap_current_container(conn, D_DOWN);
501 DLOG("snapping down\n");
502 int new_row = container->row + container->rowspan;
503 for (int i = 0; i < container->colspan; i++)
504 if (!cell_exists(container->workspace, container->col + i, new_row) ||
505 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
506 ELOG("cannot snap down - the cell is already used\n");
510 for (int i = container->col-1; i >= 0; i--) {
511 DLOG("we got cell %d, %d with colspan %d\n",
512 i, new_row, CUR_TABLE[i][new_row]->colspan);
513 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
514 CUR_TABLE[i][new_row]->colspan--;
515 DLOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
519 container->rowspan++;
522 /* To make static analyzers happy: */
530 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
531 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
532 Workspace *t_ws = workspace_get(workspace-1),
533 *old_ws = client->workspace;
535 LOG("moving floating\n");
537 workspace_initialize(t_ws, c_ws->output, false);
539 /* Check if there is already a fullscreen client on the destination workspace and
540 * stop moving if so. */
541 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
542 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
546 floating_assign_to_workspace(client, t_ws);
548 /* If we’re moving it to an invisible screen, we need to unmap it */
549 if (!workspace_is_visible(t_ws)) {
550 DLOG("This workspace is not visible, unmapping\n");
551 client_unmap(conn, client);
553 /* If this is not the case, we move the window to a workspace
554 * which is on another screen, so we also need to adjust its
556 DLOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
557 uint32_t relative_x = client->rect.x - old_ws->rect.x,
558 relative_y = client->rect.y - old_ws->rect.y;
559 DLOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
560 client->rect.x = t_ws->rect.x + relative_x;
561 client->rect.y = t_ws->rect.y + relative_y;
562 DLOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
563 reposition_client(conn, client);
571 if (workspace_is_visible(t_ws))
572 set_focus(conn, client, true);
576 * Moves the currently selected window to the given workspace
579 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
580 LOG("Moving current window to workspace %d\n", workspace);
582 Container *container = CUR_CELL;
584 assert(container != NULL);
586 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
587 Workspace *t_ws = workspace_get(workspace-1);
589 Client *current_client = container->currently_focused;
590 if (current_client == NULL) {
591 ELOG("No currently focused client in current container.\n");
594 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
595 if (to_focus == NULL)
596 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
598 workspace_initialize(t_ws, container->workspace->output, false);
599 /* Check if there is already a fullscreen client on the destination workspace and
600 * stop moving if so. */
601 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
602 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
606 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
608 assert(to_container != NULL);
610 client_remove_from_container(conn, current_client, container, true);
611 if (container->workspace->fullscreen_client == current_client)
612 container->workspace->fullscreen_client = NULL;
614 /* TODO: insert it to the correct position */
615 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
617 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
620 current_client->container = to_container;
621 current_client->workspace = to_container->workspace;
622 container->currently_focused = to_focus;
623 to_container->currently_focused = current_client;
625 /* If we’re moving it to an invisible screen, we need to unmap it */
626 if (!workspace_is_visible(to_container->workspace)) {
627 DLOG("This workspace is not visible, unmapping\n");
628 client_unmap(conn, current_client);
630 if (current_client->fullscreen) {
631 DLOG("Calling client_enter_fullscreen again\n");
632 client_enter_fullscreen(conn, current_client, false);
636 /* delete all empty columns/rows */
637 cleanup_table(conn, container->workspace);
641 if (workspace_is_visible(to_container->workspace)) {
642 client_warp_pointer_into(conn, current_client);
643 set_focus(conn, current_client, true);
648 * Jumps to the given window class / title.
649 * Title is matched using strstr, that is, matches if it appears anywhere
650 * in the string. Regular expressions seem to be a bit overkill here. However,
651 * if we need them for something else somewhen, we may introduce them here, too.
654 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
658 /* The first character is a quote, this was checked before */
659 classtitle = sstrdup(arguments+1);
660 /* The last character is a quote, we just set it to NULL */
661 classtitle[strlen(classtitle)-1] = '\0';
663 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
665 ELOG("No matching client found.\n");
670 set_focus(conn, client, true);
674 * Jump directly to the specified workspace, row and col.
675 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
678 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
682 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
683 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
685 /* No match? Either no arguments were specified, or no numbers */
687 ELOG("At least one valid argument required\n");
691 /* Move to the target workspace */
692 workspace_show(conn, ws);
697 DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
699 /* Move to row/col */
700 if (row >= c_ws->rows)
701 row = c_ws->rows - 1;
702 if (col >= c_ws->cols)
703 col = c_ws->cols - 1;
705 DLOG("Jumping to col %d, row %d\n", col, row);
706 if (c_ws->table[col][row]->currently_focused != NULL)
707 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
711 * Travels the focus stack by the given number of times (or once, if no argument
712 * was specified). That is, selects the window you were in before you focused
713 * the current window.
715 * The special values 'floating' (select the next floating window), 'tiling'
716 * (select the next tiling window), 'ft' (if the current window is floating,
717 * select the next tiling window and vice-versa) are also valid
720 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
721 /* Start count at -1 to always skip the first element */
722 int times, count = -1;
724 bool floating_criteria;
726 /* Either it’s one of the special values… */
727 if (strcasecmp(arguments, "floating") == 0) {
728 floating_criteria = true;
729 } else if (strcasecmp(arguments, "tiling") == 0) {
730 floating_criteria = false;
731 } else if (strcasecmp(arguments, "ft") == 0) {
732 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
733 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
734 ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
738 floating_criteria = !client_is_floating(last_focused);
740 /* …or a number was specified */
741 if (sscanf(arguments, "%u", ×) != 1) {
742 ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
746 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
747 if (++count < times) {
753 set_focus(conn, current, true);
759 /* Select the next client matching the criteria parsed above */
760 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
761 if (client_is_floating(current) == floating_criteria) {
762 set_focus(conn, current, true);
768 * Switch to next or previous existing workspace
771 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
772 Workspace *ws = c_ws;
774 if (direction == 'n') {
776 ws = TAILQ_NEXT(ws, workspaces);
778 if (ws == TAILQ_END(workspaces))
779 ws = TAILQ_FIRST(workspaces);
784 if (ws->output == NULL)
787 workspace_show(conn, ws->num + 1);
790 } else if (direction == 'p') {
792 ws = TAILQ_PREV(ws, workspaces_head, workspaces);
794 if (ws == TAILQ_END(workspaces))
795 ws = TAILQ_LAST(workspaces, workspaces_head);
800 if (ws->output == NULL)
803 workspace_show(conn, ws->num + 1);
809 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
811 resize_orientation_t orientation = O_VERTICAL;
812 Container *con = last_focused->container;
813 Workspace *ws = last_focused->workspace;
815 if (client_is_floating(last_focused)) {
816 DLOG("Resizing a floating client\n");
817 if (STARTS_WITH(command, "left")) {
818 command += strlen("left");
819 last_focused->rect.width += atoi(command);
820 last_focused->rect.x -= atoi(command);
821 } else if (STARTS_WITH(command, "right")) {
822 command += strlen("right");
823 last_focused->rect.width += atoi(command);
824 } else if (STARTS_WITH(command, "top")) {
825 command += strlen("top");
826 last_focused->rect.height += atoi(command);
827 last_focused->rect.y -= atoi(command);
828 } else if (STARTS_WITH(command, "bottom")) {
829 command += strlen("bottom");
830 last_focused->rect.height += atoi(command);
832 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
836 /* resize_client flushes */
837 resize_client(conn, last_focused);
842 if (STARTS_WITH(command, "left")) {
845 first = con->col - 1;
847 command += strlen("left");
848 } else if (STARTS_WITH(command, "right")) {
849 first = con->col + (con->colspan - 1);
850 DLOG("column %d\n", first);
852 if (!cell_exists(ws, first, con->row) ||
853 (first == (ws->cols-1)))
857 command += strlen("right");
858 } else if (STARTS_WITH(command, "top")) {
861 first = con->row - 1;
863 orientation = O_HORIZONTAL;
864 command += strlen("top");
865 } else if (STARTS_WITH(command, "bottom")) {
866 first = con->row + (con->rowspan - 1);
867 if (!cell_exists(ws, con->col, first) ||
868 (first == (ws->rows-1)))
872 orientation = O_HORIZONTAL;
873 command += strlen("bottom");
875 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
879 int pixels = atoi(command);
883 resize_container(conn, ws, first, second, orientation, pixels);
887 * Parses a command, see file CMDMODE for more information
890 void parse_command(xcb_connection_t *conn, const char *command) {
891 LOG("--- parsing command \"%s\" ---\n", command);
892 /* Get the first client from focus stack because floating clients are not
893 * in any container, therefore CUR_CELL is not appropriate. */
894 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
895 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
898 /* Hmm, just to be sure */
899 if (command[0] == '\0')
902 /* Is it an <exec>? Then execute the given command. */
903 if (STARTS_WITH(command, "exec ")) {
904 LOG("starting \"%s\"\n", command + strlen("exec "));
905 start_application(command+strlen("exec "));
909 if (STARTS_WITH(command, "mark")) {
910 if (last_focused == NULL) {
911 ELOG("There is no window to mark\n");
914 const char *rest = command + strlen("mark");
918 DLOG("interactive mark starting\n");
919 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
921 LOG("mark with \"%s\"\n", rest);
922 client_mark(conn, last_focused, rest);
927 if (STARTS_WITH(command, "goto")) {
928 const char *rest = command + strlen("goto");
932 DLOG("interactive go to mark starting\n");
933 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
935 LOG("go to \"%s\"\n", rest);
936 jump_to_mark(conn, rest);
941 if (STARTS_WITH(command, "stack-limit ")) {
942 if (last_focused == NULL || client_is_floating(last_focused)) {
943 ELOG("No container focused\n");
946 const char *rest = command + strlen("stack-limit ");
947 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
948 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
949 rest += strlen("rows ");
950 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
951 last_focused->container->stack_limit = STACK_LIMIT_COLS;
952 rest += strlen("cols ");
954 ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
958 last_focused->container->stack_limit_value = atoi(rest);
959 if (last_focused->container->stack_limit_value == 0)
960 last_focused->container->stack_limit = STACK_LIMIT_NONE;
965 if (STARTS_WITH(command, "resize ")) {
966 if (last_focused == NULL)
968 const char *rest = command + strlen("resize ");
969 parse_resize_command(conn, last_focused, rest);
973 if (STARTS_WITH(command, "mode ")) {
974 const char *rest = command + strlen("mode ");
975 switch_mode(conn, rest);
979 /* Is it an <exit>? */
980 if (STARTS_WITH(command, "exit")) {
981 LOG("User issued exit-command, exiting without error.\n");
982 restore_geometry(global_conn);
986 /* Is it a <reload>? */
987 if (STARTS_WITH(command, "reload")) {
988 load_configuration(conn, NULL, true);
992 /* Is it <restart>? Then restart in place. */
993 if (STARTS_WITH(command, "restart")) {
997 if (STARTS_WITH(command, "kill")) {
998 if (last_focused == NULL) {
999 ELOG("There is no window to kill\n");
1003 LOG("Killing current window\n");
1004 client_kill(conn, last_focused);
1008 /* Is it a jump to a specified workspace, row, col? */
1009 if (STARTS_WITH(command, "jump ")) {
1010 const char *arguments = command + strlen("jump ");
1011 if (arguments[0] == '"')
1012 jump_to_window(conn, arguments);
1013 else jump_to_container(conn, arguments);
1017 /* Should we travel the focus stack? */
1018 if (STARTS_WITH(command, "focus")) {
1019 const char *arguments = command + strlen("focus ");
1020 travel_focus_stack(conn, arguments);
1024 /* Is it 'f' for fullscreen, or 'fg' for fullscreen_global? */
1025 if (command[0] == 'f') {
1026 if (last_focused == NULL)
1028 if (command[1] == 'g')
1029 client_toggle_fullscreen_global(conn, last_focused);
1031 client_toggle_fullscreen(conn, last_focused);
1035 /* Is it just 's' for stacking or 'd' for default? */
1036 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1037 if (last_focused != NULL && client_is_floating(last_focused)) {
1038 ELOG("not switching, this is a floating client\n");
1041 LOG("Switching mode for current container\n");
1042 int new_mode = MODE_DEFAULT;
1043 if (command[0] == 's' && CUR_CELL->mode != MODE_STACK)
1044 new_mode = MODE_STACK;
1045 if (command[0] == 'T' && CUR_CELL->mode != MODE_TABBED)
1046 new_mode = MODE_TABBED;
1047 switch_layout_mode(conn, CUR_CELL, new_mode);
1051 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1052 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1053 if (command[0] == 'b') {
1054 if (last_focused == NULL) {
1055 ELOG("No window focused, cannot change border type\n");
1059 char com = command[1];
1060 if (command[1] == 't') {
1061 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1062 !last_focused->borderless)
1064 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1065 !last_focused->borderless)
1070 client_change_border(conn, last_focused, com);
1074 if (command[0] == 'H') {
1075 LOG("Hiding all floating windows\n");
1076 floating_toggle_hide(conn, c_ws);
1080 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1082 /* Is it a <with>? */
1083 if (command[0] == 'w') {
1085 /* TODO: implement */
1086 if (command[0] == 'c') {
1087 with = WITH_CONTAINER;
1089 } else if (command[0] == 'w') {
1090 with = WITH_WORKSPACE;
1092 } else if (command[0] == 's') {
1096 ELOG("not yet implemented.\n");
1101 /* Is it 't' for toggle tiling/floating? */
1102 if (command[0] == 't') {
1103 if (with == WITH_WORKSPACE) {
1104 c_ws->auto_float = !c_ws->auto_float;
1105 LOG("autofloat is now %d\n", c_ws->auto_float);
1108 if (last_focused == NULL) {
1109 ELOG("Cannot toggle tiling/floating: workspace empty\n");
1113 Workspace *ws = last_focused->workspace;
1115 toggle_floating_mode(conn, last_focused, false);
1116 /* delete all empty columns/rows */
1117 cleanup_table(conn, ws);
1119 /* Fix colspan/rowspan if it’d overlap */
1120 fix_colrowspan(conn, ws);
1122 render_workspace(conn, ws->output, ws);
1124 /* Re-focus the client because cleanup_table sets the focus to the last
1125 * focused client inside a container only. */
1126 set_focus(conn, last_focused, true);
1131 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1132 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1133 next_previous_workspace(conn, command[0]);
1137 /* It’s a normal <cmd> */
1139 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1140 direction_t direction;
1141 int times = strtol(command, &rest, 10);
1143 ELOG("Invalid command (\"%s\")\n", command);
1147 if (*rest == '\0') {
1148 /* No rest? This was a workspace number, not a times specification */
1149 workspace_show(conn, times);
1153 if (*rest == 'm' || *rest == 's') {
1154 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1158 int workspace = strtol(rest, &rest, 10);
1161 ELOG("Invalid command (\"%s\")\n", command);
1165 if (*rest == '\0') {
1166 if (last_focused != NULL && client_is_floating(last_focused))
1167 move_floating_window_to_workspace(conn, last_focused, workspace);
1168 else move_current_window_to_workspace(conn, workspace);
1172 if (last_focused == NULL) {
1173 ELOG("Not performing (no window found)\n");
1177 if (client_is_floating(last_focused) &&
1178 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1179 ELOG("Not performing (floating)\n");
1183 /* Now perform action to <where> */
1184 while (*rest != '\0') {
1187 else if (*rest == 'j')
1189 else if (*rest == 'k')
1191 else if (*rest == 'l')
1192 direction = D_RIGHT;
1194 ELOG("unknown direction: %c\n", *rest);
1199 if (action == ACTION_FOCUS) {
1200 if (with == WITH_SCREEN) {
1201 focus_thing(conn, direction, THING_SCREEN);
1204 if (client_is_floating(last_focused)) {
1205 floating_focus_direction(conn, last_focused, direction);
1208 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1212 if (action == ACTION_MOVE) {
1213 if (with == WITH_SCREEN) {
1214 /* TODO: this should swap the screen’s contents
1215 * (e.g. all workspaces) with the next/previous/…
1217 ELOG("Not yet implemented\n");
1220 if (client_is_floating(last_focused)) {
1221 floating_move(conn, last_focused, direction);
1224 if (with == WITH_WINDOW)
1225 move_current_window(conn, direction);
1226 else move_current_container(conn, direction);
1230 if (action == ACTION_SNAP) {
1231 if (with == WITH_SCREEN) {
1232 ELOG("You cannot snap a screen (it makes no sense).\n");
1235 snap_current_container(conn, direction);