4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
29 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
30 /* If this container is empty, we’re done */
31 if (container->currently_focused == NULL)
34 /* Get the previous/next client or wrap around */
35 Client *candidate = NULL;
36 if (direction == D_UP) {
37 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
38 candidate = CIRCLEQ_LAST(&(container->clients));
40 else if (direction == D_DOWN) {
41 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
42 candidate = CIRCLEQ_FIRST(&(container->clients));
43 } else LOG("Direction not implemented!\n");
45 /* If we could not switch, the container contains exactly one client. We return false */
46 if (candidate == container->currently_focused)
50 set_focus(conn, candidate, true);
55 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
57 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
58 LOG("focusing direction %d\n", direction);
60 int new_row = current_row,
61 new_col = current_col;
63 Container *container = CUR_CELL;
64 Workspace *t_ws = c_ws;
66 /* There always is a container. If not, current_col or current_row is wrong */
67 assert(container != NULL);
69 if (container->workspace->fullscreen_client != NULL) {
70 LOG("You're in fullscreen mode. Won't switch focus\n");
74 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
75 if (direction == D_UP || direction == D_DOWN) {
76 if (thing == THING_WINDOW)
77 /* Let’s see if we can perform up/down focus in the current container */
78 if (focus_window_in_container(conn, container, direction))
81 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
82 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
83 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
84 /* Set new_row as a sane default, but it may get overwritten in a second */
87 /* Search from the top to correctly handle rowspanned containers */
88 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
89 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
96 /* Let’s see if there is a screen down/up there to which we can switch */
97 LOG("container is at %d with height %d\n", container->y, container->height);
99 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
100 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
101 LOG("Wrapping screen around vertically\n");
102 /* No screen found? Then wrap */
103 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
105 t_ws = &(workspaces[screen->current_workspace]);
106 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
108 } else if (direction == D_LEFT || direction == D_RIGHT) {
109 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
110 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
111 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
112 /* Set new_col as a sane default, but it may get overwritten in a second */
115 /* Search from the left to correctly handle colspanned containers */
116 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
117 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
124 /* Let’s see if there is a screen left/right here to which we can switch */
125 LOG("container is at %d with width %d\n", container->x, container->width);
127 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
128 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
129 LOG("Wrapping screen around horizontally\n");
130 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
132 t_ws = &(workspaces[screen->current_workspace]);
133 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
136 LOG("direction unhandled\n");
140 /* Bounds checking */
141 if (new_col >= t_ws->cols)
142 new_col = (t_ws->cols - 1);
143 if (new_row >= t_ws->rows)
144 new_row = (t_ws->rows - 1);
146 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
147 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
151 * Tries to move the window inside its current container.
153 * Returns true if the window could be moved, false otherwise.
156 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
157 direction_t direction) {
158 assert(client->container != NULL);
160 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
161 CIRCLEQ_NEXT(client, clients));
163 if (other == CIRCLEQ_END(&(client->container->clients)))
166 LOG("i can do that\n");
167 /* We can move the client inside its current container */
168 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
169 if (direction == D_UP)
170 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
171 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
177 * Moves the current window or whole container to the given direction, creating a column/row if
181 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
182 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
183 (direction == D_LEFT ? "left" : "right"))));
184 /* Get current window */
185 Container *container = CUR_CELL,
188 /* There has to be a container, see focus_window() */
189 assert(container != NULL);
191 /* If there is no window or the dock window is focused, we’re done */
192 if (container->currently_focused == NULL ||
193 container->currently_focused->dock)
196 /* As soon as the client is moved away, the last focused client in the old
197 * container needs to get focus, if any. Therefore, we save it here. */
198 Client *current_client = container->currently_focused;
199 Client *to_focus = get_last_focused_client(conn, container, current_client);
201 if (to_focus == NULL) {
202 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
203 if (to_focus == NULL)
204 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
209 /* If we’re at the left-most position, move the rest of the table right */
210 if (current_col == 0) {
211 expand_table_cols_at_head(c_ws);
214 new = CUR_TABLE[--current_col][current_row];
217 if (current_col == (c_ws->cols-1))
218 expand_table_cols(c_ws);
220 new = CUR_TABLE[++current_col][current_row];
223 if (move_current_window_in_container(conn, current_client, D_UP))
226 /* if we’re at the up-most position, move the rest of the table down */
227 if (current_row == 0) {
228 expand_table_rows_at_head(c_ws);
231 new = CUR_TABLE[current_col][--current_row];
234 if (move_current_window_in_container(conn, current_client, D_DOWN))
237 if (current_row == (c_ws->rows-1))
238 expand_table_rows(c_ws);
240 new = CUR_TABLE[current_col][++current_row];
244 /* Remove it from the old container and put it into the new one */
245 client_remove_from_container(conn, current_client, container);
247 if (new->currently_focused != NULL)
248 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
249 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
250 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
252 /* Update data structures */
253 current_client->container = new;
254 container->currently_focused = to_focus;
255 new->currently_focused = current_client;
257 Workspace *workspace = container->workspace;
259 /* delete all empty columns/rows */
260 cleanup_table(conn, workspace);
262 /* Fix colspan/rowspan if it’d overlap */
263 fix_colrowspan(conn, workspace);
265 render_workspace(conn, workspace->screen, workspace);
268 set_focus(conn, current_client, true);
271 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
272 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
273 (direction == D_LEFT ? "left" : "right"))));
274 /* Get current window */
275 Container *container = CUR_CELL,
278 Container **old = &CUR_CELL;
280 /* There has to be a container, see focus_window() */
281 assert(container != NULL);
285 /* If we’re at the left-most position, move the rest of the table right */
286 if (current_col == 0) {
287 expand_table_cols_at_head(c_ws);
289 old = &CUR_TABLE[current_col+1][current_row];
291 new = CUR_TABLE[--current_col][current_row];
294 if (current_col == (c_ws->cols-1))
295 expand_table_cols(c_ws);
297 new = CUR_TABLE[++current_col][current_row];
300 /* if we’re at the up-most position, move the rest of the table down */
301 if (current_row == 0) {
302 expand_table_rows_at_head(c_ws);
304 old = &CUR_TABLE[current_col][current_row+1];
306 new = CUR_TABLE[current_col][--current_row];
309 if (current_row == (c_ws->rows-1))
310 expand_table_rows(c_ws);
312 new = CUR_TABLE[current_col][++current_row];
316 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
318 /* Swap the containers */
323 new->col = container->col;
324 new->row = container->row;
326 CUR_CELL = container;
327 container->col = col;
328 container->row = row;
330 Workspace *workspace = container->workspace;
332 /* delete all empty columns/rows */
333 cleanup_table(conn, workspace);
335 /* Fix colspan/rowspan if it’d overlap */
336 fix_colrowspan(conn, workspace);
342 * "Snaps" the current container (not possible for windows, because it works at table base)
343 * to the given direction, that is, adjusts cellspan/rowspan
346 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
347 LOG("snapping container to direction %d\n", direction);
349 Container *container = CUR_CELL;
351 assert(container != NULL);
355 /* Snap to the left is actually a move to the left and then a snap right */
356 if (!cell_exists(container->col - 1, container->row) ||
357 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
358 LOG("cannot snap to left - the cell is already used\n");
362 move_current_window(conn, D_LEFT);
363 snap_current_container(conn, D_RIGHT);
366 /* Check if the cell is used */
367 int new_col = container->col + container->colspan;
368 if (!cell_exists(new_col, container->row) ||
369 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
370 LOG("cannot snap to right - the cell is already used\n");
374 /* Check if there are other cells with rowspan, which are in our way.
375 * If so, reduce their rowspan. */
376 for (int i = container->row-1; i >= 0; i--) {
377 LOG("we got cell %d, %d with rowspan %d\n",
378 new_col, i, CUR_TABLE[new_col][i]->rowspan);
379 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
380 CUR_TABLE[new_col][i]->rowspan--;
381 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
384 container->colspan++;
388 if (!cell_exists(container->col, container->row - 1) ||
389 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
390 LOG("cannot snap to top - the cell is already used\n");
394 move_current_window(conn, D_UP);
395 snap_current_container(conn, D_DOWN);
398 LOG("snapping down\n");
399 int new_row = container->row + container->rowspan;
400 if (!cell_exists(container->col, new_row) ||
401 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
402 LOG("cannot snap down - the cell is already used\n");
406 for (int i = container->col-1; i >= 0; i--) {
407 LOG("we got cell %d, %d with colspan %d\n",
408 i, new_row, CUR_TABLE[i][new_row]->colspan);
409 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
410 CUR_TABLE[i][new_row]->colspan--;
411 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
415 container->rowspan++;
423 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
424 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
425 Workspace *t_ws = &(workspaces[workspace-1]);
427 LOG("moving floating\n");
429 if (t_ws->screen == NULL) {
430 LOG("initializing new workspace, setting num to %d\n", workspace-1);
431 t_ws->screen = c_ws->screen;
432 /* Copy the dimensions from the virtual screen */
433 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
435 /* Check if there is already a fullscreen client on the destination workspace and
436 * stop moving if so. */
437 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
438 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
443 /* Remove from focus stack */
444 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
446 if (client->workspace->fullscreen_client == client)
447 client->workspace->fullscreen_client = NULL;
449 /* Insert into destination focus stack */
450 client->workspace = t_ws;
451 SLIST_INSERT_HEAD(&(t_ws->focus_stack), client, focus_clients);
452 if (client->fullscreen)
453 t_ws->fullscreen_client = client;
455 /* If we’re moving it to an invisible screen, we need to unmap it */
456 if (t_ws->screen->current_workspace != t_ws->num) {
457 LOG("This workspace is not visible, unmapping\n");
458 xcb_unmap_window(conn, client->frame);
467 * Moves the currently selected window to the given workspace
470 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
471 LOG("Moving current window to workspace %d\n", workspace);
473 Container *container = CUR_CELL;
475 assert(container != NULL);
477 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
478 Workspace *t_ws = &(workspaces[workspace-1]);
480 Client *current_client = container->currently_focused;
481 if (current_client == NULL) {
482 LOG("No currently focused client in current container.\n");
485 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
486 if (to_focus == NULL)
487 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
489 if (t_ws->screen == NULL) {
490 LOG("initializing new workspace, setting num to %d\n", workspace-1);
491 t_ws->screen = container->workspace->screen;
492 /* Copy the dimensions from the virtual screen */
493 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
495 /* Check if there is already a fullscreen client on the destination workspace and
496 * stop moving if so. */
497 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
498 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
503 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
505 assert(to_container != NULL);
507 client_remove_from_container(conn, current_client, container);
508 if (container->workspace->fullscreen_client == current_client)
509 container->workspace->fullscreen_client = NULL;
511 /* TODO: insert it to the correct position */
512 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
514 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
515 if (current_client->fullscreen)
516 t_ws->fullscreen_client = current_client;
519 current_client->container = to_container;
520 container->currently_focused = to_focus;
521 to_container->currently_focused = current_client;
523 /* If we’re moving it to an invisible screen, we need to unmap it */
524 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
525 LOG("This workspace is not visible, unmapping\n");
526 xcb_unmap_window(conn, current_client->frame);
529 /* delete all empty columns/rows */
530 cleanup_table(conn, container->workspace);
536 * Switches to the given workspace
539 void show_workspace(xcb_connection_t *conn, int workspace) {
541 bool need_warp = false;
542 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
543 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
544 Workspace *t_ws = &(workspaces[workspace-1]);
546 LOG("show_workspace(%d)\n", workspace);
548 /* Store current_row/current_col */
549 c_ws->current_row = current_row;
550 c_ws->current_col = current_col;
552 /* Check if the workspace has not been used yet */
553 if (t_ws->screen == NULL) {
554 LOG("initializing new workspace, setting num to %d\n", workspace);
555 t_ws->screen = c_ws->screen;
556 /* Copy the dimensions from the virtual screen */
557 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
560 if (c_ws->screen != t_ws->screen) {
561 /* We need to switch to the other screen first */
562 LOG("moving over to other screen.\n");
564 /* Store the old client */
565 Client *old_client = CUR_CELL->currently_focused;
567 c_ws = &(workspaces[t_ws->screen->current_workspace]);
568 current_col = c_ws->current_col;
569 current_row = c_ws->current_row;
570 if (CUR_CELL->currently_focused != NULL)
573 Rect *dims = &(c_ws->screen->rect);
574 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
575 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
578 /* Re-decorate the old client, it’s not focused anymore */
579 if ((old_client != NULL) && !old_client->dock)
580 redecorate_window(conn, old_client);
581 else xcb_flush(conn);
584 /* Check if we need to change something or if we’re already there */
585 if (c_ws->screen->current_workspace == (workspace-1)) {
586 if (CUR_CELL->currently_focused != NULL) {
587 set_focus(conn, CUR_CELL->currently_focused, true);
589 client_warp_pointer_into(conn, CUR_CELL->currently_focused);
596 t_ws->screen->current_workspace = workspace-1;
597 Workspace *old_workspace = c_ws;
598 c_ws = &workspaces[workspace-1];
600 /* Unmap all clients of the old workspace */
601 unmap_workspace(conn, old_workspace);
603 current_row = c_ws->current_row;
604 current_col = c_ws->current_col;
605 LOG("new current row = %d, current col = %d\n", current_row, current_col);
607 ignore_enter_notify_forall(conn, c_ws, true);
609 /* Map all clients on the new workspace */
611 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
612 xcb_map_window(conn, client->frame);
614 /* Map all floating clients */
615 SLIST_FOREACH(client, &(c_ws->focus_stack), focus_clients) {
616 if (!client->floating)
619 xcb_map_window(conn, client->frame);
622 /* Map all stack windows, if any */
623 struct Stack_Window *stack_win;
624 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
625 if (stack_win->container->workspace == c_ws)
626 xcb_map_window(conn, stack_win->window);
628 ignore_enter_notify_forall(conn, c_ws, false);
630 /* Restore focus on the new workspace */
631 if (CUR_CELL->currently_focused != NULL) {
632 set_focus(conn, CUR_CELL->currently_focused, true);
634 client_warp_pointer_into(conn, CUR_CELL->currently_focused);
637 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
643 * Jumps to the given window class / title.
644 * Title is matched using strstr, that is, matches if it appears anywhere
645 * in the string. Regular expressions seem to be a bit overkill here. However,
646 * if we need them for something else somewhen, we may introduce them here, too.
649 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
653 /* The first character is a quote, this was checked before */
654 classtitle = sstrdup(arguments+1);
655 /* The last character is a quote, we just set it to NULL */
656 classtitle[strlen(classtitle)-1] = '\0';
658 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
660 LOG("No matching client found.\n");
665 set_focus(conn, client, true);
669 * Jump directly to the specified workspace, row and col.
670 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
673 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
677 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
678 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
680 /* No match? Either no arguments were specified, or no numbers */
682 LOG("At least one valid argument required\n");
686 /* Move to the target workspace */
687 show_workspace(conn, ws);
692 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
694 /* Move to row/col */
695 if (row >= c_ws->rows)
696 row = c_ws->rows - 1;
697 if (col >= c_ws->cols)
698 col = c_ws->cols - 1;
700 LOG("Jumping to col %d, row %d\n", col, row);
701 if (c_ws->table[col][row]->currently_focused != NULL)
702 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
706 * Travels the focus stack by the given number of times (or once, if no argument
707 * was specified). That is, selects the window you were in before you focused
708 * the current window.
711 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
712 /* Start count at -1 to always skip the first element */
713 int times, count = -1;
716 if (sscanf(arguments, "%u", ×) != 1) {
717 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
721 Workspace *ws = CUR_CELL->workspace;
723 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
724 if (++count < times) {
730 set_focus(conn, current, true);
736 * Goes through the list of arguments (for exec()) and checks if the given argument
737 * is present. If not, it copies the arguments (because we cannot realloc it) and
738 * appends the given argument.
741 static char **append_argument(char **original, char *argument) {
743 for (num_args = 0; original[num_args] != NULL; num_args++) {
744 LOG("original argument: \"%s\"\n", original[num_args]);
745 /* If the argument is already present we return the original pointer */
746 if (strcmp(original[num_args], argument) == 0)
749 /* Copy the original array */
750 char **result = smalloc((num_args+2) * sizeof(char*));
751 memcpy(result, original, num_args * sizeof(char*));
752 result[num_args] = argument;
753 result[num_args+1] = NULL;
759 * Parses a command, see file CMDMODE for more information
762 void parse_command(xcb_connection_t *conn, const char *command) {
763 LOG("--- parsing command \"%s\" ---\n", command);
764 /* Get the first client from focus stack because floating clients are not
765 * in any container, therefore CUR_CELL is not appropriate. */
766 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
767 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
770 /* Hmm, just to be sure */
771 if (command[0] == '\0')
774 /* Is it an <exec>? Then execute the given command. */
775 if (STARTS_WITH(command, "exec ")) {
776 LOG("starting \"%s\"\n", command + strlen("exec "));
777 start_application(command+strlen("exec "));
781 /* Is it an <exit>? */
782 if (STARTS_WITH(command, "exit")) {
783 LOG("User issued exit-command, exiting without error.\n");
787 /* Is it <restart>? Then restart in place. */
788 if (STARTS_WITH(command, "restart")) {
789 LOG("restarting \"%s\"...\n", start_argv[0]);
790 /* make sure -a is in the argument list or append it */
791 start_argv = append_argument(start_argv, "-a");
793 execvp(start_argv[0], start_argv);
797 if (STARTS_WITH(command, "kill")) {
798 if (last_focused == NULL) {
799 LOG("There is no window to kill\n");
803 LOG("Killing current window\n");
804 client_kill(conn, last_focused);
808 /* Is it a jump to a specified workspace, row, col? */
809 if (STARTS_WITH(command, "jump ")) {
810 const char *arguments = command + strlen("jump ");
811 if (arguments[0] == '"')
812 jump_to_window(conn, arguments);
813 else jump_to_container(conn, arguments);
817 /* Should we travel the focus stack? */
818 if (STARTS_WITH(command, "focus")) {
819 const char *arguments = command + strlen("focus");
820 travel_focus_stack(conn, arguments);
824 /* Is it 'f' for fullscreen? */
825 if (command[0] == 'f') {
826 if (last_focused == NULL)
828 client_toggle_fullscreen(conn, last_focused);
832 /* Is it just 's' for stacking or 'd' for default? */
833 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
834 if (last_focused->floating) {
835 LOG("not switching, this is a floating client\n");
838 LOG("Switching mode for current container\n");
839 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
843 /* Is it 't' for toggle tiling/floating? */
844 if (command[0] == 't') {
845 if (last_focused == NULL) {
846 LOG("Cannot toggle tiling/floating: workspace empty\n");
850 toggle_floating_mode(conn, last_focused);
851 /* delete all empty columns/rows */
852 cleanup_table(conn, last_focused->workspace);
854 /* Fix colspan/rowspan if it’d overlap */
855 fix_colrowspan(conn, last_focused->workspace);
857 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
862 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
864 /* Is it a <with>? */
865 if (command[0] == 'w') {
867 /* TODO: implement */
868 if (command[0] == 'c') {
869 with = WITH_CONTAINER;
872 LOG("not yet implemented.\n");
877 /* It’s a normal <cmd> */
879 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
880 direction_t direction;
881 int times = strtol(command, &rest, 10);
883 LOG("Invalid command (\"%s\")\n", command);
888 /* No rest? This was a tag number, not a times specification */
889 show_workspace(conn, times);
893 if (*rest == 'm' || *rest == 's') {
894 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
898 int workspace = strtol(rest, &rest, 10);
901 LOG("Invalid command (\"%s\")\n", command);
906 if (last_focused->floating)
907 move_floating_window_to_workspace(conn, last_focused, workspace);
908 else move_current_window_to_workspace(conn, workspace);
912 if (last_focused->floating) {
913 LOG("Not performing because this is a floating window\n");
917 /* Now perform action to <where> */
918 while (*rest != '\0') {
921 else if (*rest == 'j')
923 else if (*rest == 'k')
925 else if (*rest == 'l')
928 LOG("unknown direction: %c\n", *rest);
932 if (action == ACTION_FOCUS)
933 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
934 else if (action == ACTION_MOVE) {
935 if (with == WITH_WINDOW)
936 move_current_window(conn, direction);
937 else move_current_container(conn, direction);
939 else if (action == ACTION_SNAP)
940 snap_current_container(conn, direction);
945 LOG("--- done ---\n");