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);
66 for (int c = 0; c < num_workspaces; c++)
67 SLIST_FOREACH(current, &(workspaces[c].focus_stack), focus_clients) {
68 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
71 set_focus(conn, current, true);
75 LOG("No window with this mark found\n");
78 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
79 LOG("focusing direction %d\n", direction);
81 int new_row = current_row,
82 new_col = current_col;
83 Container *container = CUR_CELL;
84 Workspace *t_ws = c_ws;
86 /* Makes sure new_col and new_row are within bounds of the new workspace */
87 void check_colrow_boundaries() {
88 if (new_col >= t_ws->cols)
89 new_col = (t_ws->cols - 1);
90 if (new_row >= t_ws->rows)
91 new_row = (t_ws->rows - 1);
94 /* There always is a container. If not, current_col or current_row is wrong */
95 assert(container != NULL);
97 if (container->workspace->fullscreen_client != NULL) {
98 LOG("You're in fullscreen mode. Won't switch focus\n");
102 /* For focusing screens, situation is different: we get the rect
103 * of the current screen, then get the screen which is on its
104 * right/left/bottom/top and just switch to the workspace on
105 * the target screen. */
106 if (thing == THING_SCREEN) {
107 i3Screen *cs = c_ws->screen;
109 Rect bounds = cs->rect;
111 if (direction == D_RIGHT)
112 bounds.x += bounds.width;
113 else if (direction == D_LEFT)
114 bounds.x -= bounds.width;
115 else if (direction == D_UP)
116 bounds.y -= bounds.height;
117 else bounds.y += bounds.height;
119 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
120 if (target == NULL) {
121 LOG("Target screen NULL\n");
122 /* Wrap around if the target screen is out of bounds */
123 if (direction == D_RIGHT)
124 target = get_screen_most(D_LEFT, cs);
125 else if (direction == D_LEFT)
126 target = get_screen_most(D_RIGHT, cs);
127 else if (direction == D_UP)
128 target = get_screen_most(D_DOWN, cs);
129 else target = get_screen_most(D_UP, cs);
132 LOG("Switching to ws %d\n", target->current_workspace + 1);
133 workspace_show(conn, target->current_workspace + 1);
137 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
138 if (direction == D_UP || direction == D_DOWN) {
139 if (thing == THING_WINDOW)
140 /* Let’s see if we can perform up/down focus in the current container */
141 if (focus_window_in_container(conn, container, direction))
144 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
145 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
146 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
147 /* Set new_row as a sane default, but it may get overwritten in a second */
150 /* Search from the top to correctly handle rowspanned containers */
151 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
152 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
159 /* Let’s see if there is a screen down/up there to which we can switch */
160 LOG("container is at %d with height %d\n", container->y, container->height);
162 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
163 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
164 LOG("Wrapping screen around vertically\n");
165 /* No screen found? Then wrap */
166 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
168 t_ws = workspace_get(screen->current_workspace);
169 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
172 check_colrow_boundaries();
174 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
175 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
176 LOG("Cell empty, checking for colspanned client above...\n");
177 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
178 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
184 LOG("Fixed it to new col %d\n", new_col);
186 } else if (direction == D_LEFT || direction == D_RIGHT) {
187 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
188 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
189 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
190 /* Set new_col as a sane default, but it may get overwritten in a second */
193 /* Search from the left to correctly handle colspanned containers */
194 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
195 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
202 /* Let’s see if there is a screen left/right here to which we can switch */
203 LOG("container is at %d with width %d\n", container->x, container->width);
205 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
206 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
207 LOG("Wrapping screen around horizontally\n");
208 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
210 t_ws = workspace_get(screen->current_workspace);
211 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
214 check_colrow_boundaries();
216 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
217 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
218 LOG("Cell empty, checking for rowspanned client above...\n");
219 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
220 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
226 LOG("Fixed it to new row %d\n", new_row);
229 LOG("direction unhandled\n");
233 check_colrow_boundaries();
235 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
236 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
240 * Tries to move the window inside its current container.
242 * Returns true if the window could be moved, false otherwise.
245 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
246 direction_t direction) {
247 assert(client->container != NULL);
249 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
250 CIRCLEQ_NEXT(client, clients));
252 if (other == CIRCLEQ_END(&(client->container->clients)))
255 LOG("i can do that\n");
256 /* We can move the client inside its current container */
257 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
258 if (direction == D_UP)
259 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
260 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
266 * Moves the current window or whole container to the given direction, creating a column/row if
270 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
271 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
272 (direction == D_LEFT ? "left" : "right"))));
273 /* Get current window */
274 Container *container = CUR_CELL,
277 /* There has to be a container, see focus_window() */
278 assert(container != NULL);
280 /* If there is no window or the dock window is focused, we’re done */
281 if (container->currently_focused == NULL ||
282 container->currently_focused->dock)
285 /* As soon as the client is moved away, the last focused client in the old
286 * container needs to get focus, if any. Therefore, we save it here. */
287 Client *current_client = container->currently_focused;
288 Client *to_focus = get_last_focused_client(conn, container, current_client);
290 if (to_focus == NULL) {
291 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
292 if (to_focus == NULL)
293 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
298 /* If we’re at the left-most position, move the rest of the table right */
299 if (current_col == 0) {
300 expand_table_cols_at_head(c_ws);
303 new = CUR_TABLE[--current_col][current_row];
306 if (current_col == (c_ws->cols-1))
307 expand_table_cols(c_ws);
309 new = CUR_TABLE[++current_col][current_row];
312 if (move_current_window_in_container(conn, current_client, D_UP))
315 /* if we’re at the up-most position, move the rest of the table down */
316 if (current_row == 0) {
317 expand_table_rows_at_head(c_ws);
320 new = CUR_TABLE[current_col][--current_row];
323 if (move_current_window_in_container(conn, current_client, D_DOWN))
326 if (current_row == (c_ws->rows-1))
327 expand_table_rows(c_ws);
329 new = CUR_TABLE[current_col][++current_row];
331 /* To make static analyzers happy: */
336 /* Remove it from the old container and put it into the new one */
337 client_remove_from_container(conn, current_client, container, true);
339 if (new->currently_focused != NULL)
340 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
341 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
342 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
344 /* Update data structures */
345 current_client->container = new;
346 current_client->workspace = new->workspace;
347 container->currently_focused = to_focus;
348 new->currently_focused = current_client;
350 Workspace *workspace = container->workspace;
352 /* delete all empty columns/rows */
353 cleanup_table(conn, workspace);
355 /* Fix colspan/rowspan if it’d overlap */
356 fix_colrowspan(conn, workspace);
358 render_workspace(conn, workspace->screen, workspace);
361 set_focus(conn, current_client, true);
364 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
365 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
366 (direction == D_LEFT ? "left" : "right"))));
367 /* Get current window */
368 Container *container = CUR_CELL,
371 Container **old = &CUR_CELL;
373 /* There has to be a container, see focus_window() */
374 assert(container != NULL);
378 /* If we’re at the left-most position, move the rest of the table right */
379 if (current_col == 0) {
380 expand_table_cols_at_head(c_ws);
382 old = &CUR_TABLE[current_col+1][current_row];
384 new = CUR_TABLE[--current_col][current_row];
387 if (current_col == (c_ws->cols-1))
388 expand_table_cols(c_ws);
390 new = CUR_TABLE[++current_col][current_row];
393 /* if we’re at the up-most position, move the rest of the table down */
394 if (current_row == 0) {
395 expand_table_rows_at_head(c_ws);
397 old = &CUR_TABLE[current_col][current_row+1];
399 new = CUR_TABLE[current_col][--current_row];
402 if (current_row == (c_ws->rows-1))
403 expand_table_rows(c_ws);
405 new = CUR_TABLE[current_col][++current_row];
407 /* To make static analyzers happy: */
412 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
414 /* Swap the containers */
419 new->col = container->col;
420 new->row = container->row;
422 CUR_CELL = container;
423 container->col = col;
424 container->row = row;
426 Workspace *workspace = container->workspace;
428 /* delete all empty columns/rows */
429 cleanup_table(conn, workspace);
431 /* Fix colspan/rowspan if it’d overlap */
432 fix_colrowspan(conn, workspace);
438 * "Snaps" the current container (not possible for windows, because it works at table base)
439 * to the given direction, that is, adjusts cellspan/rowspan
442 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
443 LOG("snapping container to direction %d\n", direction);
445 Container *container = CUR_CELL;
447 assert(container != NULL);
451 /* Snap to the left is actually a move to the left and then a snap right */
452 if (!cell_exists(container->col - 1, container->row) ||
453 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
454 LOG("cannot snap to left - the cell is already used\n");
458 move_current_window(conn, D_LEFT);
459 snap_current_container(conn, D_RIGHT);
462 /* Check if the cell is used */
463 int new_col = container->col + container->colspan;
464 for (int i = 0; i < container->rowspan; i++)
465 if (!cell_exists(new_col, container->row + i) ||
466 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
467 LOG("cannot snap to right - the cell is already used\n");
471 /* Check if there are other cells with rowspan, which are in our way.
472 * If so, reduce their rowspan. */
473 for (int i = container->row-1; i >= 0; i--) {
474 LOG("we got cell %d, %d with rowspan %d\n",
475 new_col, i, CUR_TABLE[new_col][i]->rowspan);
476 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
477 CUR_TABLE[new_col][i]->rowspan--;
478 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
481 container->colspan++;
485 if (!cell_exists(container->col, container->row - 1) ||
486 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
487 LOG("cannot snap to top - the cell is already used\n");
491 move_current_window(conn, D_UP);
492 snap_current_container(conn, D_DOWN);
495 LOG("snapping down\n");
496 int new_row = container->row + container->rowspan;
497 for (int i = 0; i < container->colspan; i++)
498 if (!cell_exists(container->col + i, new_row) ||
499 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
500 LOG("cannot snap down - the cell is already used\n");
504 for (int i = container->col-1; i >= 0; i--) {
505 LOG("we got cell %d, %d with colspan %d\n",
506 i, new_row, CUR_TABLE[i][new_row]->colspan);
507 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
508 CUR_TABLE[i][new_row]->colspan--;
509 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
513 container->rowspan++;
516 /* To make static analyzers happy: */
524 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
525 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
526 Workspace *t_ws = workspace_get(workspace-1),
527 *old_ws = client->workspace;
529 LOG("moving floating\n");
531 workspace_initialize(t_ws, c_ws->screen);
533 /* Check if there is already a fullscreen client on the destination workspace and
534 * stop moving if so. */
535 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
536 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
540 floating_assign_to_workspace(client, t_ws);
542 /* If we’re moving it to an invisible screen, we need to unmap it */
543 if (!workspace_is_visible(t_ws)) {
544 LOG("This workspace is not visible, unmapping\n");
545 client_unmap(conn, client);
547 /* If this is not the case, we move the window to a workspace
548 * which is on another screen, so we also need to adjust its
550 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
551 uint32_t relative_x = client->rect.x - old_ws->rect.x,
552 relative_y = client->rect.y - old_ws->rect.y;
553 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
554 client->rect.x = t_ws->rect.x + relative_x;
555 client->rect.y = t_ws->rect.y + relative_y;
556 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
557 reposition_client(conn, client);
565 if (workspace_is_visible(t_ws))
566 set_focus(conn, client, true);
570 * Moves the currently selected window to the given workspace
573 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
574 LOG("Moving current window to workspace %d\n", workspace);
576 Container *container = CUR_CELL;
578 assert(container != NULL);
580 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
581 Workspace *t_ws = workspace_get(workspace-1);
583 Client *current_client = container->currently_focused;
584 if (current_client == NULL) {
585 LOG("No currently focused client in current container.\n");
588 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
589 if (to_focus == NULL)
590 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
592 workspace_initialize(t_ws, container->workspace->screen);
593 /* Check if there is already a fullscreen client on the destination workspace and
594 * stop moving if so. */
595 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
596 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
600 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
602 assert(to_container != NULL);
604 client_remove_from_container(conn, current_client, container, true);
605 if (container->workspace->fullscreen_client == current_client)
606 container->workspace->fullscreen_client = NULL;
608 /* TODO: insert it to the correct position */
609 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
611 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
614 current_client->container = to_container;
615 current_client->workspace = to_container->workspace;
616 container->currently_focused = to_focus;
617 to_container->currently_focused = current_client;
619 /* If we’re moving it to an invisible screen, we need to unmap it */
620 if (!workspace_is_visible(to_container->workspace)) {
621 LOG("This workspace is not visible, unmapping\n");
622 client_unmap(conn, current_client);
624 if (current_client->fullscreen) {
625 LOG("Calling client_enter_fullscreen again\n");
626 client_enter_fullscreen(conn, current_client);
630 /* delete all empty columns/rows */
631 cleanup_table(conn, container->workspace);
635 if (workspace_is_visible(to_container->workspace))
636 set_focus(conn, current_client, true);
640 * Jumps to the given window class / title.
641 * Title is matched using strstr, that is, matches if it appears anywhere
642 * in the string. Regular expressions seem to be a bit overkill here. However,
643 * if we need them for something else somewhen, we may introduce them here, too.
646 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
650 /* The first character is a quote, this was checked before */
651 classtitle = sstrdup(arguments+1);
652 /* The last character is a quote, we just set it to NULL */
653 classtitle[strlen(classtitle)-1] = '\0';
655 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
657 LOG("No matching client found.\n");
662 set_focus(conn, client, true);
666 * Jump directly to the specified workspace, row and col.
667 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
670 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
674 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
675 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
677 /* No match? Either no arguments were specified, or no numbers */
679 LOG("At least one valid argument required\n");
683 /* Move to the target workspace */
684 workspace_show(conn, ws);
689 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
691 /* Move to row/col */
692 if (row >= c_ws->rows)
693 row = c_ws->rows - 1;
694 if (col >= c_ws->cols)
695 col = c_ws->cols - 1;
697 LOG("Jumping to col %d, row %d\n", col, row);
698 if (c_ws->table[col][row]->currently_focused != NULL)
699 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
703 * Travels the focus stack by the given number of times (or once, if no argument
704 * was specified). That is, selects the window you were in before you focused
705 * the current window.
707 * The special values 'floating' (select the next floating window), 'tiling'
708 * (select the next tiling window), 'ft' (if the current window is floating,
709 * select the next tiling window and vice-versa) are also valid
712 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
713 /* Start count at -1 to always skip the first element */
714 int times, count = -1;
716 bool floating_criteria;
718 /* Either it’s one of the special values… */
719 if (strcasecmp(arguments, "floating") == 0) {
720 floating_criteria = true;
721 } else if (strcasecmp(arguments, "tiling") == 0) {
722 floating_criteria = false;
723 } else if (strcasecmp(arguments, "ft") == 0) {
724 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
725 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
726 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
730 floating_criteria = !client_is_floating(last_focused);
732 /* …or a number was specified */
733 if (sscanf(arguments, "%u", ×) != 1) {
734 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
738 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
739 if (++count < times) {
745 set_focus(conn, current, true);
751 /* Select the next client matching the criteria parsed above */
752 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
753 if (client_is_floating(current) == floating_criteria) {
754 set_focus(conn, current, true);
760 * Goes through the list of arguments (for exec()) and checks if the given argument
761 * is present. If not, it copies the arguments (because we cannot realloc it) and
762 * appends the given argument.
765 static char **append_argument(char **original, char *argument) {
767 for (num_args = 0; original[num_args] != NULL; num_args++) {
768 LOG("original argument: \"%s\"\n", original[num_args]);
769 /* If the argument is already present we return the original pointer */
770 if (strcmp(original[num_args], argument) == 0)
773 /* Copy the original array */
774 char **result = smalloc((num_args+2) * sizeof(char*));
775 memcpy(result, original, num_args * sizeof(char*));
776 result[num_args] = argument;
777 result[num_args+1] = NULL;
783 * Switch to next or previous existing workspace
786 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
790 if (direction == 'n') {
791 /* If we are on the last workspace, we cannot go any further */
792 if (c_ws->num == (num_workspaces-1))
795 for (i = c_ws->num + 1; i < num_workspaces; i++) {
796 t_ws = &(workspaces[i]);
797 if (t_ws->screen != NULL)
800 } else if (direction == 'p') {
803 for (i = c_ws->num - 1; i >= 0 ; i--) {
804 t_ws = &(workspaces[i]);
805 if (t_ws->screen != NULL)
810 if (t_ws->screen != NULL)
811 workspace_show(conn, i+1);
814 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
816 resize_orientation_t orientation = O_VERTICAL;
817 Container *con = last_focused->container;
818 Workspace *ws = con->workspace;
820 if (STARTS_WITH(command, "left")) {
823 first = con->col - 1;
825 command += strlen("left");
826 } else if (STARTS_WITH(command, "right")) {
827 first = con->col + (con->colspan - 1);
828 LOG("column %d\n", first);
830 if (!cell_exists(first, con->row) ||
831 (first == (ws->cols-1)))
835 command += strlen("right");
836 } else if (STARTS_WITH(command, "top")) {
839 first = con->row - 1;
841 orientation = O_HORIZONTAL;
842 command += strlen("top");
843 } else if (STARTS_WITH(command, "bottom")) {
844 first = con->row + (con->rowspan - 1);
845 if (!cell_exists(con->col, first) ||
846 (first == (ws->rows-1)))
850 orientation = O_HORIZONTAL;
851 command += strlen("bottom");
853 LOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
857 int pixels = atoi(command);
861 resize_container(conn, ws, first, second, orientation, pixels);
865 * Parses a command, see file CMDMODE for more information
868 void parse_command(xcb_connection_t *conn, const char *command) {
869 LOG("--- parsing command \"%s\" ---\n", command);
870 /* Get the first client from focus stack because floating clients are not
871 * in any container, therefore CUR_CELL is not appropriate. */
872 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
873 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
876 /* Hmm, just to be sure */
877 if (command[0] == '\0')
880 /* Is it an <exec>? Then execute the given command. */
881 if (STARTS_WITH(command, "exec ")) {
882 LOG("starting \"%s\"\n", command + strlen("exec "));
883 start_application(command+strlen("exec "));
887 if (STARTS_WITH(command, "mark")) {
888 if (last_focused == NULL) {
889 LOG("There is no window to mark\n");
892 const char *rest = command + strlen("mark");
896 LOG("interactive mark starting\n");
897 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
899 LOG("mark with \"%s\"\n", rest);
900 client_mark(conn, last_focused, rest);
905 if (STARTS_WITH(command, "goto")) {
906 const char *rest = command + strlen("goto");
910 LOG("interactive go to mark starting\n");
911 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
913 LOG("go to \"%s\"\n", rest);
914 jump_to_mark(conn, rest);
919 if (STARTS_WITH(command, "stack-limit ")) {
920 if (last_focused == NULL || client_is_floating(last_focused)) {
921 LOG("No container focused\n");
924 const char *rest = command + strlen("stack-limit ");
925 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
926 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
927 rest += strlen("rows ");
928 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
929 last_focused->container->stack_limit = STACK_LIMIT_COLS;
930 rest += strlen("cols ");
932 LOG("Syntax: stack-limit <cols|rows> <limit>\n");
936 last_focused->container->stack_limit_value = atoi(rest);
937 if (last_focused->container->stack_limit_value == 0)
938 last_focused->container->stack_limit = STACK_LIMIT_NONE;
943 if (STARTS_WITH(command, "resize ")) {
944 if (last_focused == NULL)
946 const char *rest = command + strlen("resize ");
947 parse_resize_command(conn, last_focused, rest);
951 if (STARTS_WITH(command, "mode ")) {
952 const char *rest = command + strlen("mode ");
953 switch_mode(conn, rest);
957 /* Is it an <exit>? */
958 if (STARTS_WITH(command, "exit")) {
959 LOG("User issued exit-command, exiting without error.\n");
963 /* Is it a <reload>? */
964 if (STARTS_WITH(command, "reload")) {
965 load_configuration(conn, NULL, true);
969 /* Is it <restart>? Then restart in place. */
970 if (STARTS_WITH(command, "restart")) {
971 LOG("restarting \"%s\"...\n", start_argv[0]);
972 /* make sure -a is in the argument list or append it */
973 start_argv = append_argument(start_argv, "-a");
975 execvp(start_argv[0], start_argv);
979 if (STARTS_WITH(command, "kill")) {
980 if (last_focused == NULL) {
981 LOG("There is no window to kill\n");
985 LOG("Killing current window\n");
986 client_kill(conn, last_focused);
990 /* Is it a jump to a specified workspace, row, col? */
991 if (STARTS_WITH(command, "jump ")) {
992 const char *arguments = command + strlen("jump ");
993 if (arguments[0] == '"')
994 jump_to_window(conn, arguments);
995 else jump_to_container(conn, arguments);
999 /* Should we travel the focus stack? */
1000 if (STARTS_WITH(command, "focus")) {
1001 const char *arguments = command + strlen("focus ");
1002 travel_focus_stack(conn, arguments);
1006 /* Is it 'f' for fullscreen? */
1007 if (command[0] == 'f') {
1008 if (last_focused == NULL)
1010 client_toggle_fullscreen(conn, last_focused);
1014 /* Is it just 's' for stacking or 'd' for default? */
1015 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1016 if (last_focused != NULL && client_is_floating(last_focused)) {
1017 LOG("not switching, this is a floating client\n");
1020 LOG("Switching mode for current container\n");
1021 int new_mode = MODE_DEFAULT;
1022 if (command[0] == 's')
1023 new_mode = MODE_STACK;
1024 if (command[0] == 'T')
1025 new_mode = MODE_TABBED;
1026 switch_layout_mode(conn, CUR_CELL, new_mode);
1030 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1031 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1032 if (command[0] == 'b') {
1033 if (last_focused == NULL) {
1034 LOG("No window focused, cannot change border type\n");
1038 char com = command[1];
1039 if (command[1] == 't') {
1040 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1041 !last_focused->borderless)
1043 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1044 !last_focused->borderless)
1049 client_change_border(conn, last_focused, com);
1053 if (command[0] == 'H') {
1054 LOG("Hiding all floating windows\n");
1055 floating_toggle_hide(conn, c_ws);
1059 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1061 /* Is it a <with>? */
1062 if (command[0] == 'w') {
1064 /* TODO: implement */
1065 if (command[0] == 'c') {
1066 with = WITH_CONTAINER;
1068 } else if (command[0] == 'w') {
1069 with = WITH_WORKSPACE;
1071 } else if (command[0] == 's') {
1075 LOG("not yet implemented.\n");
1080 /* Is it 't' for toggle tiling/floating? */
1081 if (command[0] == 't') {
1082 if (with == WITH_WORKSPACE) {
1083 c_ws->auto_float = !c_ws->auto_float;
1084 LOG("autofloat is now %d\n", c_ws->auto_float);
1087 if (last_focused == NULL) {
1088 LOG("Cannot toggle tiling/floating: workspace empty\n");
1092 Workspace *ws = last_focused->workspace;
1094 toggle_floating_mode(conn, last_focused, false);
1095 /* delete all empty columns/rows */
1096 cleanup_table(conn, ws);
1098 /* Fix colspan/rowspan if it’d overlap */
1099 fix_colrowspan(conn, ws);
1101 render_workspace(conn, ws->screen, ws);
1103 /* Re-focus the client because cleanup_table sets the focus to the last
1104 * focused client inside a container only. */
1105 set_focus(conn, last_focused, true);
1110 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1111 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1112 next_previous_workspace(conn, command[0]);
1116 /* It’s a normal <cmd> */
1118 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1119 direction_t direction;
1120 int times = strtol(command, &rest, 10);
1122 LOG("Invalid command (\"%s\")\n", command);
1126 if (*rest == '\0') {
1127 /* No rest? This was a workspace number, not a times specification */
1128 workspace_show(conn, times);
1132 if (*rest == 'm' || *rest == 's') {
1133 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1137 int workspace = strtol(rest, &rest, 10);
1140 LOG("Invalid command (\"%s\")\n", command);
1144 if (*rest == '\0') {
1145 if (last_focused != NULL && client_is_floating(last_focused))
1146 move_floating_window_to_workspace(conn, last_focused, workspace);
1147 else move_current_window_to_workspace(conn, workspace);
1151 if (last_focused == NULL) {
1152 LOG("Not performing (no window found)\n");
1156 if (client_is_floating(last_focused) &&
1157 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1158 LOG("Not performing (floating)\n");
1162 /* Now perform action to <where> */
1163 while (*rest != '\0') {
1166 else if (*rest == 'j')
1168 else if (*rest == 'k')
1170 else if (*rest == 'l')
1171 direction = D_RIGHT;
1173 LOG("unknown direction: %c\n", *rest);
1178 if (action == ACTION_FOCUS) {
1179 if (with == WITH_SCREEN) {
1180 focus_thing(conn, direction, THING_SCREEN);
1183 if (client_is_floating(last_focused)) {
1184 floating_focus_direction(conn, last_focused, direction);
1187 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1191 if (action == ACTION_MOVE) {
1192 if (with == WITH_SCREEN) {
1193 /* TODO: this should swap the screen’s contents
1194 * (e.g. all workspaces) with the next/previous/…
1196 LOG("Not yet implemented\n");
1199 if (client_is_floating(last_focused)) {
1200 floating_move(conn, last_focused, direction);
1203 if (with == WITH_WINDOW)
1204 move_current_window(conn, direction);
1205 else move_current_container(conn, direction);
1209 if (action == ACTION_SNAP) {
1210 if (with == WITH_SCREEN) {
1211 LOG("You cannot snap a screen (it makes no sense).\n");
1214 snap_current_container(conn, direction);