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 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
35 /* If this container is empty, we’re done */
36 if (container->currently_focused == NULL)
39 /* Get the previous/next client or wrap around */
40 Client *candidate = NULL;
41 if (direction == D_UP) {
42 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
43 candidate = CIRCLEQ_LAST(&(container->clients));
45 else if (direction == D_DOWN) {
46 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
47 candidate = CIRCLEQ_FIRST(&(container->clients));
48 } else LOG("Direction not implemented!\n");
50 /* If we could not switch, the container contains exactly one client. We return false */
51 if (candidate == container->currently_focused)
55 set_focus(conn, candidate, true);
60 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
62 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
64 LOG("Jumping to \"%s\"\n", mark);
67 TAILQ_FOREACH(ws, workspaces, workspaces)
68 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
69 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
72 workspace_show(conn, current->workspace->num + 1);
73 set_focus(conn, current, true);
77 LOG("No window with this mark found\n");
80 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
81 LOG("focusing direction %d\n", direction);
83 int new_row = current_row,
84 new_col = current_col;
85 Container *container = CUR_CELL;
86 Workspace *t_ws = c_ws;
88 /* Makes sure new_col and new_row are within bounds of the new workspace */
89 void check_colrow_boundaries() {
90 if (new_col >= t_ws->cols)
91 new_col = (t_ws->cols - 1);
92 if (new_row >= t_ws->rows)
93 new_row = (t_ws->rows - 1);
96 /* There always is a container. If not, current_col or current_row is wrong */
97 assert(container != NULL);
99 if (container->workspace->fullscreen_client != NULL) {
100 LOG("You're in fullscreen mode. Won't switch focus\n");
104 /* For focusing screens, situation is different: we get the rect
105 * of the current screen, then get the screen which is on its
106 * right/left/bottom/top and just switch to the workspace on
107 * the target screen. */
108 if (thing == THING_SCREEN) {
109 i3Screen *cs = c_ws->screen;
111 Rect bounds = cs->rect;
113 if (direction == D_RIGHT)
114 bounds.x += bounds.width;
115 else if (direction == D_LEFT)
116 bounds.x -= bounds.width;
117 else if (direction == D_UP)
118 bounds.y -= bounds.height;
119 else bounds.y += bounds.height;
121 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
122 if (target == NULL) {
123 LOG("Target screen NULL\n");
124 /* Wrap around if the target screen is out of bounds */
125 if (direction == D_RIGHT)
126 target = get_screen_most(D_LEFT, cs);
127 else if (direction == D_LEFT)
128 target = get_screen_most(D_RIGHT, cs);
129 else if (direction == D_UP)
130 target = get_screen_most(D_DOWN, cs);
131 else target = get_screen_most(D_UP, cs);
134 LOG("Switching to ws %d\n", target->current_workspace + 1);
135 workspace_show(conn, target->current_workspace->num + 1);
139 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
140 if (direction == D_UP || direction == D_DOWN) {
141 if (thing == THING_WINDOW)
142 /* Let’s see if we can perform up/down focus in the current container */
143 if (focus_window_in_container(conn, container, direction))
146 if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
147 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
148 else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
149 /* Set new_row as a sane default, but it may get overwritten in a second */
152 /* Search from the top to correctly handle rowspanned containers */
153 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
154 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
161 /* Let’s see if there is a screen down/up there to which we can switch */
162 LOG("container is at %d with height %d\n", container->y, container->height);
164 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
165 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
166 LOG("Wrapping screen around vertically\n");
167 /* No screen found? Then wrap */
168 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
170 t_ws = screen->current_workspace;
171 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
174 check_colrow_boundaries();
176 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
177 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
178 LOG("Cell empty, checking for colspanned client above...\n");
179 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
180 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
186 LOG("Fixed it to new col %d\n", new_col);
188 } else if (direction == D_LEFT || direction == D_RIGHT) {
189 if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
190 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
191 else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
192 /* Set new_col as a sane default, but it may get overwritten in a second */
195 /* Search from the left to correctly handle colspanned containers */
196 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
197 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
204 /* Let’s see if there is a screen left/right here to which we can switch */
205 LOG("container is at %d with width %d\n", container->x, container->width);
207 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
208 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
209 LOG("Wrapping screen around horizontally\n");
210 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
212 t_ws = screen->current_workspace;
213 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
216 check_colrow_boundaries();
218 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
219 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
220 LOG("Cell empty, checking for rowspanned client above...\n");
221 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
222 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
228 LOG("Fixed it to new row %d\n", new_row);
231 LOG("direction unhandled\n");
235 check_colrow_boundaries();
237 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
238 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
242 * Tries to move the window inside its current container.
244 * Returns true if the window could be moved, false otherwise.
247 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
248 direction_t direction) {
249 assert(client->container != NULL);
251 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
252 CIRCLEQ_NEXT(client, clients));
254 if (other == CIRCLEQ_END(&(client->container->clients)))
257 LOG("i can do that\n");
258 /* We can move the client inside its current container */
259 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
260 if (direction == D_UP)
261 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
262 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
268 * Moves the current window or whole container to the given direction, creating a column/row if
272 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
273 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
274 (direction == D_LEFT ? "left" : "right"))));
275 /* Get current window */
276 Container *container = CUR_CELL,
279 /* There has to be a container, see focus_window() */
280 assert(container != NULL);
282 /* If there is no window or the dock window is focused, we’re done */
283 if (container->currently_focused == NULL ||
284 container->currently_focused->dock)
287 /* As soon as the client is moved away, the last focused client in the old
288 * container needs to get focus, if any. Therefore, we save it here. */
289 Client *current_client = container->currently_focused;
290 Client *to_focus = get_last_focused_client(conn, container, current_client);
292 if (to_focus == NULL) {
293 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
294 if (to_focus == NULL)
295 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
300 /* If we’re at the left-most position, move the rest of the table right */
301 if (current_col == 0) {
302 expand_table_cols_at_head(c_ws);
305 new = CUR_TABLE[--current_col][current_row];
308 if (current_col == (c_ws->cols-1))
309 expand_table_cols(c_ws);
311 new = CUR_TABLE[++current_col][current_row];
314 if (move_current_window_in_container(conn, current_client, D_UP))
317 /* if we’re at the up-most position, move the rest of the table down */
318 if (current_row == 0) {
319 expand_table_rows_at_head(c_ws);
322 new = CUR_TABLE[current_col][--current_row];
325 if (move_current_window_in_container(conn, current_client, D_DOWN))
328 if (current_row == (c_ws->rows-1))
329 expand_table_rows(c_ws);
331 new = CUR_TABLE[current_col][++current_row];
333 /* To make static analyzers happy: */
338 /* Remove it from the old container and put it into the new one */
339 client_remove_from_container(conn, current_client, container, true);
341 if (new->currently_focused != NULL)
342 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
343 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
344 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
346 /* Update data structures */
347 current_client->container = new;
348 current_client->workspace = new->workspace;
349 container->currently_focused = to_focus;
350 new->currently_focused = current_client;
352 Workspace *workspace = container->workspace;
354 /* delete all empty columns/rows */
355 cleanup_table(conn, workspace);
357 /* Fix colspan/rowspan if it’d overlap */
358 fix_colrowspan(conn, workspace);
360 render_workspace(conn, workspace->screen, workspace);
363 set_focus(conn, current_client, true);
366 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
367 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
368 (direction == D_LEFT ? "left" : "right"))));
369 /* Get current window */
370 Container *container = CUR_CELL,
373 Container **old = &CUR_CELL;
375 /* There has to be a container, see focus_window() */
376 assert(container != NULL);
380 /* If we’re at the left-most position, move the rest of the table right */
381 if (current_col == 0) {
382 expand_table_cols_at_head(c_ws);
384 old = &CUR_TABLE[current_col+1][current_row];
386 new = CUR_TABLE[--current_col][current_row];
389 if (current_col == (c_ws->cols-1))
390 expand_table_cols(c_ws);
392 new = CUR_TABLE[++current_col][current_row];
395 /* if we’re at the up-most position, move the rest of the table down */
396 if (current_row == 0) {
397 expand_table_rows_at_head(c_ws);
399 old = &CUR_TABLE[current_col][current_row+1];
401 new = CUR_TABLE[current_col][--current_row];
404 if (current_row == (c_ws->rows-1))
405 expand_table_rows(c_ws);
407 new = CUR_TABLE[current_col][++current_row];
409 /* To make static analyzers happy: */
414 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
416 /* Swap the containers */
421 new->col = container->col;
422 new->row = container->row;
424 CUR_CELL = container;
425 container->col = col;
426 container->row = row;
428 Workspace *workspace = container->workspace;
430 /* delete all empty columns/rows */
431 cleanup_table(conn, workspace);
433 /* Fix colspan/rowspan if it’d overlap */
434 fix_colrowspan(conn, workspace);
440 * "Snaps" the current container (not possible for windows, because it works at table base)
441 * to the given direction, that is, adjusts cellspan/rowspan
444 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
445 LOG("snapping container to direction %d\n", direction);
447 Container *container = CUR_CELL;
449 assert(container != NULL);
453 /* Snap to the left is actually a move to the left and then a snap right */
454 if (!cell_exists(container->workspace, container->col - 1, container->row) ||
455 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
456 LOG("cannot snap to left - the cell is already used\n");
460 move_current_window(conn, D_LEFT);
461 snap_current_container(conn, D_RIGHT);
464 /* Check if the cell is used */
465 int new_col = container->col + container->colspan;
466 for (int i = 0; i < container->rowspan; i++)
467 if (!cell_exists(container->workspace, new_col, container->row + i) ||
468 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
469 LOG("cannot snap to right - the cell is already used\n");
473 /* Check if there are other cells with rowspan, which are in our way.
474 * If so, reduce their rowspan. */
475 for (int i = container->row-1; i >= 0; i--) {
476 LOG("we got cell %d, %d with rowspan %d\n",
477 new_col, i, CUR_TABLE[new_col][i]->rowspan);
478 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
479 CUR_TABLE[new_col][i]->rowspan--;
480 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
483 container->colspan++;
487 if (!cell_exists(container->workspace, container->col, container->row - 1) ||
488 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
489 LOG("cannot snap to top - the cell is already used\n");
493 move_current_window(conn, D_UP);
494 snap_current_container(conn, D_DOWN);
497 LOG("snapping down\n");
498 int new_row = container->row + container->rowspan;
499 for (int i = 0; i < container->colspan; i++)
500 if (!cell_exists(container->workspace, container->col + i, new_row) ||
501 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
502 LOG("cannot snap down - the cell is already used\n");
506 for (int i = container->col-1; i >= 0; i--) {
507 LOG("we got cell %d, %d with colspan %d\n",
508 i, new_row, CUR_TABLE[i][new_row]->colspan);
509 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
510 CUR_TABLE[i][new_row]->colspan--;
511 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
515 container->rowspan++;
518 /* To make static analyzers happy: */
526 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
527 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
528 Workspace *t_ws = workspace_get(workspace-1),
529 *old_ws = client->workspace;
531 LOG("moving floating\n");
533 workspace_initialize(t_ws, c_ws->screen);
535 /* Check if there is already a fullscreen client on the destination workspace and
536 * stop moving if so. */
537 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
538 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
542 floating_assign_to_workspace(client, t_ws);
544 /* If we’re moving it to an invisible screen, we need to unmap it */
545 if (!workspace_is_visible(t_ws)) {
546 LOG("This workspace is not visible, unmapping\n");
547 client_unmap(conn, client);
549 /* If this is not the case, we move the window to a workspace
550 * which is on another screen, so we also need to adjust its
552 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
553 uint32_t relative_x = client->rect.x - old_ws->rect.x,
554 relative_y = client->rect.y - old_ws->rect.y;
555 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
556 client->rect.x = t_ws->rect.x + relative_x;
557 client->rect.y = t_ws->rect.y + relative_y;
558 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
559 reposition_client(conn, client);
563 /* Configure the window above all tiling windows (or below a fullscreen
565 if (t_ws->fullscreen_client != NULL) {
566 uint32_t values[] = { t_ws->fullscreen_client->frame, XCB_STACK_MODE_BELOW };
567 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
570 SLIST_FOREACH(last_tiling, &(t_ws->focus_stack), focus_clients)
571 if (!client_is_floating(last_tiling))
573 if (last_tiling != SLIST_END(&(t_ws->focus_stack))) {
574 uint32_t values[] = { last_tiling->frame, XCB_STACK_MODE_ABOVE };
575 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
583 if (workspace_is_visible(t_ws))
584 set_focus(conn, client, true);
588 * Moves the currently selected window to the given workspace
591 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
592 LOG("Moving current window to workspace %d\n", workspace);
594 Container *container = CUR_CELL;
596 assert(container != NULL);
598 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
599 Workspace *t_ws = workspace_get(workspace-1);
601 Client *current_client = container->currently_focused;
602 if (current_client == NULL) {
603 LOG("No currently focused client in current container.\n");
606 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
607 if (to_focus == NULL)
608 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
610 workspace_initialize(t_ws, container->workspace->screen);
611 /* Check if there is already a fullscreen client on the destination workspace and
612 * stop moving if so. */
613 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
614 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
618 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
620 assert(to_container != NULL);
622 client_remove_from_container(conn, current_client, container, true);
623 if (container->workspace->fullscreen_client == current_client)
624 container->workspace->fullscreen_client = NULL;
626 /* TODO: insert it to the correct position */
627 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
629 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
632 current_client->container = to_container;
633 current_client->workspace = to_container->workspace;
634 container->currently_focused = to_focus;
635 to_container->currently_focused = current_client;
637 /* If we’re moving it to an invisible screen, we need to unmap it */
638 if (!workspace_is_visible(to_container->workspace)) {
639 LOG("This workspace is not visible, unmapping\n");
640 client_unmap(conn, current_client);
642 if (current_client->fullscreen) {
643 LOG("Calling client_enter_fullscreen again\n");
644 client_enter_fullscreen(conn, current_client);
648 /* delete all empty columns/rows */
649 cleanup_table(conn, container->workspace);
653 if (workspace_is_visible(to_container->workspace))
654 set_focus(conn, current_client, true);
658 * Jumps to the given window class / title.
659 * Title is matched using strstr, that is, matches if it appears anywhere
660 * in the string. Regular expressions seem to be a bit overkill here. However,
661 * if we need them for something else somewhen, we may introduce them here, too.
664 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
668 /* The first character is a quote, this was checked before */
669 classtitle = sstrdup(arguments+1);
670 /* The last character is a quote, we just set it to NULL */
671 classtitle[strlen(classtitle)-1] = '\0';
673 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
675 LOG("No matching client found.\n");
680 set_focus(conn, client, true);
684 * Jump directly to the specified workspace, row and col.
685 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
688 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
692 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
693 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
695 /* No match? Either no arguments were specified, or no numbers */
697 LOG("At least one valid argument required\n");
701 /* Move to the target workspace */
702 workspace_show(conn, ws);
707 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
709 /* Move to row/col */
710 if (row >= c_ws->rows)
711 row = c_ws->rows - 1;
712 if (col >= c_ws->cols)
713 col = c_ws->cols - 1;
715 LOG("Jumping to col %d, row %d\n", col, row);
716 if (c_ws->table[col][row]->currently_focused != NULL)
717 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
721 * Travels the focus stack by the given number of times (or once, if no argument
722 * was specified). That is, selects the window you were in before you focused
723 * the current window.
725 * The special values 'floating' (select the next floating window), 'tiling'
726 * (select the next tiling window), 'ft' (if the current window is floating,
727 * select the next tiling window and vice-versa) are also valid
730 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
731 /* Start count at -1 to always skip the first element */
732 int times, count = -1;
734 bool floating_criteria;
736 /* Either it’s one of the special values… */
737 if (strcasecmp(arguments, "floating") == 0) {
738 floating_criteria = true;
739 } else if (strcasecmp(arguments, "tiling") == 0) {
740 floating_criteria = false;
741 } else if (strcasecmp(arguments, "ft") == 0) {
742 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
743 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
744 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
748 floating_criteria = !client_is_floating(last_focused);
750 /* …or a number was specified */
751 if (sscanf(arguments, "%u", ×) != 1) {
752 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
756 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
757 if (++count < times) {
763 set_focus(conn, current, true);
769 /* Select the next client matching the criteria parsed above */
770 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
771 if (client_is_floating(current) == floating_criteria) {
772 set_focus(conn, current, true);
778 * Goes through the list of arguments (for exec()) and checks if the given argument
779 * is present. If not, it copies the arguments (because we cannot realloc it) and
780 * appends the given argument.
783 static char **append_argument(char **original, char *argument) {
785 for (num_args = 0; original[num_args] != NULL; num_args++) {
786 LOG("original argument: \"%s\"\n", original[num_args]);
787 /* If the argument is already present we return the original pointer */
788 if (strcmp(original[num_args], argument) == 0)
791 /* Copy the original array */
792 char **result = smalloc((num_args+2) * sizeof(char*));
793 memcpy(result, original, num_args * sizeof(char*));
794 result[num_args] = argument;
795 result[num_args+1] = NULL;
801 * Switch to next or previous existing workspace
804 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
805 Workspace *ws = c_ws;
807 if (direction == 'n') {
808 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
809 if (ws->screen == NULL)
812 workspace_show(conn, ws->num + 1);
815 } else if (direction == 'p') {
816 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
817 if (ws->screen == NULL)
820 workspace_show(conn, ws->num + 1);
826 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
828 resize_orientation_t orientation = O_VERTICAL;
829 Container *con = last_focused->container;
830 Workspace *ws = con->workspace;
832 if (STARTS_WITH(command, "left")) {
835 first = con->col - 1;
837 command += strlen("left");
838 } else if (STARTS_WITH(command, "right")) {
839 first = con->col + (con->colspan - 1);
840 LOG("column %d\n", first);
842 if (!cell_exists(ws, first, con->row) ||
843 (first == (ws->cols-1)))
847 command += strlen("right");
848 } else if (STARTS_WITH(command, "top")) {
851 first = con->row - 1;
853 orientation = O_HORIZONTAL;
854 command += strlen("top");
855 } else if (STARTS_WITH(command, "bottom")) {
856 first = con->row + (con->rowspan - 1);
857 if (!cell_exists(ws, con->col, first) ||
858 (first == (ws->rows-1)))
862 orientation = O_HORIZONTAL;
863 command += strlen("bottom");
865 LOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
869 int pixels = atoi(command);
873 resize_container(conn, ws, first, second, orientation, pixels);
877 * Parses a command, see file CMDMODE for more information
880 void parse_command(xcb_connection_t *conn, const char *command) {
881 LOG("--- parsing command \"%s\" ---\n", command);
882 /* Get the first client from focus stack because floating clients are not
883 * in any container, therefore CUR_CELL is not appropriate. */
884 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
885 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
888 /* Hmm, just to be sure */
889 if (command[0] == '\0')
892 /* Is it an <exec>? Then execute the given command. */
893 if (STARTS_WITH(command, "exec ")) {
894 LOG("starting \"%s\"\n", command + strlen("exec "));
895 start_application(command+strlen("exec "));
899 if (STARTS_WITH(command, "mark")) {
900 if (last_focused == NULL) {
901 LOG("There is no window to mark\n");
904 const char *rest = command + strlen("mark");
908 LOG("interactive mark starting\n");
909 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
911 LOG("mark with \"%s\"\n", rest);
912 client_mark(conn, last_focused, rest);
917 if (STARTS_WITH(command, "goto")) {
918 const char *rest = command + strlen("goto");
922 LOG("interactive go to mark starting\n");
923 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
925 LOG("go to \"%s\"\n", rest);
926 jump_to_mark(conn, rest);
931 if (STARTS_WITH(command, "stack-limit ")) {
932 if (last_focused == NULL || client_is_floating(last_focused)) {
933 LOG("No container focused\n");
936 const char *rest = command + strlen("stack-limit ");
937 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
938 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
939 rest += strlen("rows ");
940 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
941 last_focused->container->stack_limit = STACK_LIMIT_COLS;
942 rest += strlen("cols ");
944 LOG("Syntax: stack-limit <cols|rows> <limit>\n");
948 last_focused->container->stack_limit_value = atoi(rest);
949 if (last_focused->container->stack_limit_value == 0)
950 last_focused->container->stack_limit = STACK_LIMIT_NONE;
955 if (STARTS_WITH(command, "resize ")) {
956 if (last_focused == NULL)
958 const char *rest = command + strlen("resize ");
959 parse_resize_command(conn, last_focused, rest);
963 if (STARTS_WITH(command, "mode ")) {
964 const char *rest = command + strlen("mode ");
965 switch_mode(conn, rest);
969 /* Is it an <exit>? */
970 if (STARTS_WITH(command, "exit")) {
971 LOG("User issued exit-command, exiting without error.\n");
975 /* Is it a <reload>? */
976 if (STARTS_WITH(command, "reload")) {
977 load_configuration(conn, NULL, true);
981 /* Is it <restart>? Then restart in place. */
982 if (STARTS_WITH(command, "restart")) {
983 LOG("restarting \"%s\"...\n", start_argv[0]);
984 /* make sure -a is in the argument list or append it */
985 start_argv = append_argument(start_argv, "-a");
987 execvp(start_argv[0], start_argv);
991 if (STARTS_WITH(command, "kill")) {
992 if (last_focused == NULL) {
993 LOG("There is no window to kill\n");
997 LOG("Killing current window\n");
998 client_kill(conn, last_focused);
1002 /* Is it a jump to a specified workspace, row, col? */
1003 if (STARTS_WITH(command, "jump ")) {
1004 const char *arguments = command + strlen("jump ");
1005 if (arguments[0] == '"')
1006 jump_to_window(conn, arguments);
1007 else jump_to_container(conn, arguments);
1011 /* Should we travel the focus stack? */
1012 if (STARTS_WITH(command, "focus")) {
1013 const char *arguments = command + strlen("focus ");
1014 travel_focus_stack(conn, arguments);
1018 /* Is it 'f' for fullscreen? */
1019 if (command[0] == 'f') {
1020 if (last_focused == NULL)
1022 client_toggle_fullscreen(conn, last_focused);
1026 /* Is it just 's' for stacking or 'd' for default? */
1027 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1028 if (last_focused != NULL && client_is_floating(last_focused)) {
1029 LOG("not switching, this is a floating client\n");
1032 LOG("Switching mode for current container\n");
1033 int new_mode = MODE_DEFAULT;
1034 if (command[0] == 's')
1035 new_mode = MODE_STACK;
1036 if (command[0] == 'T')
1037 new_mode = MODE_TABBED;
1038 switch_layout_mode(conn, CUR_CELL, new_mode);
1042 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1043 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1044 if (command[0] == 'b') {
1045 if (last_focused == NULL) {
1046 LOG("No window focused, cannot change border type\n");
1050 char com = command[1];
1051 if (command[1] == 't') {
1052 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1053 !last_focused->borderless)
1055 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1056 !last_focused->borderless)
1061 client_change_border(conn, last_focused, com);
1065 if (command[0] == 'H') {
1066 LOG("Hiding all floating windows\n");
1067 floating_toggle_hide(conn, c_ws);
1071 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1073 /* Is it a <with>? */
1074 if (command[0] == 'w') {
1076 /* TODO: implement */
1077 if (command[0] == 'c') {
1078 with = WITH_CONTAINER;
1080 } else if (command[0] == 'w') {
1081 with = WITH_WORKSPACE;
1083 } else if (command[0] == 's') {
1087 LOG("not yet implemented.\n");
1092 /* Is it 't' for toggle tiling/floating? */
1093 if (command[0] == 't') {
1094 if (with == WITH_WORKSPACE) {
1095 c_ws->auto_float = !c_ws->auto_float;
1096 LOG("autofloat is now %d\n", c_ws->auto_float);
1099 if (last_focused == NULL) {
1100 LOG("Cannot toggle tiling/floating: workspace empty\n");
1104 Workspace *ws = last_focused->workspace;
1106 toggle_floating_mode(conn, last_focused, false);
1107 /* delete all empty columns/rows */
1108 cleanup_table(conn, ws);
1110 /* Fix colspan/rowspan if it’d overlap */
1111 fix_colrowspan(conn, ws);
1113 render_workspace(conn, ws->screen, ws);
1115 /* Re-focus the client because cleanup_table sets the focus to the last
1116 * focused client inside a container only. */
1117 set_focus(conn, last_focused, true);
1122 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1123 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1124 next_previous_workspace(conn, command[0]);
1128 /* It’s a normal <cmd> */
1130 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1131 direction_t direction;
1132 int times = strtol(command, &rest, 10);
1134 LOG("Invalid command (\"%s\")\n", command);
1138 if (*rest == '\0') {
1139 /* No rest? This was a workspace number, not a times specification */
1140 workspace_show(conn, times);
1144 if (*rest == 'm' || *rest == 's') {
1145 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1149 int workspace = strtol(rest, &rest, 10);
1152 LOG("Invalid command (\"%s\")\n", command);
1156 if (*rest == '\0') {
1157 if (last_focused != NULL && client_is_floating(last_focused))
1158 move_floating_window_to_workspace(conn, last_focused, workspace);
1159 else move_current_window_to_workspace(conn, workspace);
1163 if (last_focused == NULL) {
1164 LOG("Not performing (no window found)\n");
1168 if (client_is_floating(last_focused) &&
1169 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1170 LOG("Not performing (floating)\n");
1174 /* Now perform action to <where> */
1175 while (*rest != '\0') {
1178 else if (*rest == 'j')
1180 else if (*rest == 'k')
1182 else if (*rest == 'l')
1183 direction = D_RIGHT;
1185 LOG("unknown direction: %c\n", *rest);
1190 if (action == ACTION_FOCUS) {
1191 if (with == WITH_SCREEN) {
1192 focus_thing(conn, direction, THING_SCREEN);
1195 if (client_is_floating(last_focused)) {
1196 floating_focus_direction(conn, last_focused, direction);
1199 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1203 if (action == ACTION_MOVE) {
1204 if (with == WITH_SCREEN) {
1205 /* TODO: this should swap the screen’s contents
1206 * (e.g. all workspaces) with the next/previous/…
1208 LOG("Not yet implemented\n");
1211 if (client_is_floating(last_focused)) {
1212 floating_move(conn, last_focused, direction);
1215 if (with == WITH_WINDOW)
1216 move_current_window(conn, direction);
1217 else move_current_container(conn, direction);
1221 if (action == ACTION_SNAP) {
1222 if (with == WITH_SCREEN) {
1223 LOG("You cannot snap a screen (it makes no sense).\n");
1226 snap_current_container(conn, direction);