4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
31 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
32 /* If this container is empty, we’re done */
33 if (container->currently_focused == NULL)
36 /* Get the previous/next client or wrap around */
37 Client *candidate = NULL;
38 if (direction == D_UP) {
39 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
40 candidate = CIRCLEQ_LAST(&(container->clients));
42 else if (direction == D_DOWN) {
43 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
44 candidate = CIRCLEQ_FIRST(&(container->clients));
45 } else LOG("Direction not implemented!\n");
47 /* If we could not switch, the container contains exactly one client. We return false */
48 if (candidate == container->currently_focused)
52 set_focus(conn, candidate, true);
57 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
59 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
60 LOG("focusing direction %d\n", direction);
62 int new_row = current_row,
63 new_col = current_col;
64 Container *container = CUR_CELL;
65 Workspace *t_ws = c_ws;
67 /* Makes sure new_col and new_row are within bounds of the new workspace */
68 void check_colrow_boundaries() {
69 if (new_col >= t_ws->cols)
70 new_col = (t_ws->cols - 1);
71 if (new_row >= t_ws->rows)
72 new_row = (t_ws->rows - 1);
75 /* There always is a container. If not, current_col or current_row is wrong */
76 assert(container != NULL);
78 if (container->workspace->fullscreen_client != NULL) {
79 LOG("You're in fullscreen mode. Won't switch focus\n");
83 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
84 if (direction == D_UP || direction == D_DOWN) {
85 if (thing == THING_WINDOW)
86 /* Let’s see if we can perform up/down focus in the current container */
87 if (focus_window_in_container(conn, container, direction))
90 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
91 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
92 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
93 /* Set new_row as a sane default, but it may get overwritten in a second */
96 /* Search from the top to correctly handle rowspanned containers */
97 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
98 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
105 /* Let’s see if there is a screen down/up there to which we can switch */
106 LOG("container is at %d with height %d\n", container->y, container->height);
108 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
109 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
110 LOG("Wrapping screen around vertically\n");
111 /* No screen found? Then wrap */
112 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
114 t_ws = &(workspaces[screen->current_workspace]);
115 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
118 check_colrow_boundaries();
120 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
121 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
122 LOG("Cell empty, checking for colspanned client above...\n");
123 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
124 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
130 LOG("Fixed it to new col %d\n", new_col);
132 } else if (direction == D_LEFT || direction == D_RIGHT) {
133 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
134 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
135 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
136 /* Set new_col as a sane default, but it may get overwritten in a second */
139 /* Search from the left to correctly handle colspanned containers */
140 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
141 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
148 /* Let’s see if there is a screen left/right here to which we can switch */
149 LOG("container is at %d with width %d\n", container->x, container->width);
151 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
152 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
153 LOG("Wrapping screen around horizontally\n");
154 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
156 t_ws = &(workspaces[screen->current_workspace]);
157 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
160 check_colrow_boundaries();
162 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
163 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
164 LOG("Cell empty, checking for rowspanned client above...\n");
165 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
166 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
172 LOG("Fixed it to new row %d\n", new_row);
175 LOG("direction unhandled\n");
179 check_colrow_boundaries();
181 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
182 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
186 * Tries to move the window inside its current container.
188 * Returns true if the window could be moved, false otherwise.
191 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
192 direction_t direction) {
193 assert(client->container != NULL);
195 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
196 CIRCLEQ_NEXT(client, clients));
198 if (other == CIRCLEQ_END(&(client->container->clients)))
201 LOG("i can do that\n");
202 /* We can move the client inside its current container */
203 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
204 if (direction == D_UP)
205 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
206 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
212 * Moves the current window or whole container to the given direction, creating a column/row if
216 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
217 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
218 (direction == D_LEFT ? "left" : "right"))));
219 /* Get current window */
220 Container *container = CUR_CELL,
223 /* There has to be a container, see focus_window() */
224 assert(container != NULL);
226 /* If there is no window or the dock window is focused, we’re done */
227 if (container->currently_focused == NULL ||
228 container->currently_focused->dock)
231 /* As soon as the client is moved away, the last focused client in the old
232 * container needs to get focus, if any. Therefore, we save it here. */
233 Client *current_client = container->currently_focused;
234 Client *to_focus = get_last_focused_client(conn, container, current_client);
236 if (to_focus == NULL) {
237 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
238 if (to_focus == NULL)
239 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
244 /* If we’re at the left-most position, move the rest of the table right */
245 if (current_col == 0) {
246 expand_table_cols_at_head(c_ws);
249 new = CUR_TABLE[--current_col][current_row];
252 if (current_col == (c_ws->cols-1))
253 expand_table_cols(c_ws);
255 new = CUR_TABLE[++current_col][current_row];
258 if (move_current_window_in_container(conn, current_client, D_UP))
261 /* if we’re at the up-most position, move the rest of the table down */
262 if (current_row == 0) {
263 expand_table_rows_at_head(c_ws);
266 new = CUR_TABLE[current_col][--current_row];
269 if (move_current_window_in_container(conn, current_client, D_DOWN))
272 if (current_row == (c_ws->rows-1))
273 expand_table_rows(c_ws);
275 new = CUR_TABLE[current_col][++current_row];
277 /* To make static analyzers happy: */
282 /* Remove it from the old container and put it into the new one */
283 client_remove_from_container(conn, current_client, container, true);
285 if (new->currently_focused != NULL)
286 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
287 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
288 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
290 /* Update data structures */
291 current_client->container = new;
292 current_client->workspace = new->workspace;
293 container->currently_focused = to_focus;
294 new->currently_focused = current_client;
296 Workspace *workspace = container->workspace;
298 /* delete all empty columns/rows */
299 cleanup_table(conn, workspace);
301 /* Fix colspan/rowspan if it’d overlap */
302 fix_colrowspan(conn, workspace);
304 render_workspace(conn, workspace->screen, workspace);
307 set_focus(conn, current_client, true);
310 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
311 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
312 (direction == D_LEFT ? "left" : "right"))));
313 /* Get current window */
314 Container *container = CUR_CELL,
317 Container **old = &CUR_CELL;
319 /* There has to be a container, see focus_window() */
320 assert(container != NULL);
324 /* If we’re at the left-most position, move the rest of the table right */
325 if (current_col == 0) {
326 expand_table_cols_at_head(c_ws);
328 old = &CUR_TABLE[current_col+1][current_row];
330 new = CUR_TABLE[--current_col][current_row];
333 if (current_col == (c_ws->cols-1))
334 expand_table_cols(c_ws);
336 new = CUR_TABLE[++current_col][current_row];
339 /* if we’re at the up-most position, move the rest of the table down */
340 if (current_row == 0) {
341 expand_table_rows_at_head(c_ws);
343 old = &CUR_TABLE[current_col][current_row+1];
345 new = CUR_TABLE[current_col][--current_row];
348 if (current_row == (c_ws->rows-1))
349 expand_table_rows(c_ws);
351 new = CUR_TABLE[current_col][++current_row];
353 /* To make static analyzers happy: */
358 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
360 /* Swap the containers */
365 new->col = container->col;
366 new->row = container->row;
368 CUR_CELL = container;
369 container->col = col;
370 container->row = row;
372 Workspace *workspace = container->workspace;
374 /* delete all empty columns/rows */
375 cleanup_table(conn, workspace);
377 /* Fix colspan/rowspan if it’d overlap */
378 fix_colrowspan(conn, workspace);
384 * "Snaps" the current container (not possible for windows, because it works at table base)
385 * to the given direction, that is, adjusts cellspan/rowspan
388 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
389 LOG("snapping container to direction %d\n", direction);
391 Container *container = CUR_CELL;
393 assert(container != NULL);
397 /* Snap to the left is actually a move to the left and then a snap right */
398 if (!cell_exists(container->col - 1, container->row) ||
399 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
400 LOG("cannot snap to left - the cell is already used\n");
404 move_current_window(conn, D_LEFT);
405 snap_current_container(conn, D_RIGHT);
408 /* Check if the cell is used */
409 int new_col = container->col + container->colspan;
410 for (int i = 0; i < container->rowspan; i++)
411 if (!cell_exists(new_col, container->row + i) ||
412 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
413 LOG("cannot snap to right - the cell is already used\n");
417 /* Check if there are other cells with rowspan, which are in our way.
418 * If so, reduce their rowspan. */
419 for (int i = container->row-1; i >= 0; i--) {
420 LOG("we got cell %d, %d with rowspan %d\n",
421 new_col, i, CUR_TABLE[new_col][i]->rowspan);
422 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
423 CUR_TABLE[new_col][i]->rowspan--;
424 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
427 container->colspan++;
431 if (!cell_exists(container->col, container->row - 1) ||
432 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
433 LOG("cannot snap to top - the cell is already used\n");
437 move_current_window(conn, D_UP);
438 snap_current_container(conn, D_DOWN);
441 LOG("snapping down\n");
442 int new_row = container->row + container->rowspan;
443 for (int i = 0; i < container->colspan; i++)
444 if (!cell_exists(container->col + i, new_row) ||
445 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
446 LOG("cannot snap down - the cell is already used\n");
450 for (int i = container->col-1; i >= 0; i--) {
451 LOG("we got cell %d, %d with colspan %d\n",
452 i, new_row, CUR_TABLE[i][new_row]->colspan);
453 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
454 CUR_TABLE[i][new_row]->colspan--;
455 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
459 container->rowspan++;
462 /* To make static analyzers happy: */
470 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
471 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
472 Workspace *t_ws = &(workspaces[workspace-1]),
473 *old_ws = client->workspace;
475 LOG("moving floating\n");
477 if (t_ws->screen == NULL) {
478 LOG("initializing new workspace, setting num to %d\n", workspace-1);
479 t_ws->screen = c_ws->screen;
480 /* Copy the dimensions from the virtual screen */
481 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
483 /* Check if there is already a fullscreen client on the destination workspace and
484 * stop moving if so. */
485 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
486 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
491 floating_assign_to_workspace(client, t_ws);
493 bool target_invisible = t_ws->screen->current_workspace != t_ws->num;
495 /* If we’re moving it to an invisible screen, we need to unmap it */
496 if (target_invisible) {
497 LOG("This workspace is not visible, unmapping\n");
498 xcb_unmap_window(conn, client->frame);
500 /* If this is not the case, we move the window to a workspace
501 * which is on another screen, so we also need to adjust its
503 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
504 uint32_t relative_x = client->rect.x - old_ws->rect.x,
505 relative_y = client->rect.y - old_ws->rect.y;
506 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
507 client->rect.x = t_ws->rect.x + relative_x;
508 client->rect.y = t_ws->rect.y + relative_y;
509 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
510 reposition_client(conn, client);
518 if (!target_invisible)
519 set_focus(conn, client, true);
523 * Moves the currently selected window to the given workspace
526 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
527 LOG("Moving current window to workspace %d\n", workspace);
529 Container *container = CUR_CELL;
531 assert(container != NULL);
533 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
534 Workspace *t_ws = &(workspaces[workspace-1]);
536 Client *current_client = container->currently_focused;
537 if (current_client == NULL) {
538 LOG("No currently focused client in current container.\n");
541 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
542 if (to_focus == NULL)
543 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
545 if (t_ws->screen == NULL) {
546 LOG("initializing new workspace, setting num to %d\n", workspace-1);
547 t_ws->screen = container->workspace->screen;
548 /* Copy the dimensions from the virtual screen */
549 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
551 /* Check if there is already a fullscreen client on the destination workspace and
552 * stop moving if so. */
553 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
554 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
559 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
561 assert(to_container != NULL);
563 client_remove_from_container(conn, current_client, container, true);
564 if (container->workspace->fullscreen_client == current_client)
565 container->workspace->fullscreen_client = NULL;
567 /* TODO: insert it to the correct position */
568 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
570 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
573 current_client->container = to_container;
574 current_client->workspace = to_container->workspace;
575 container->currently_focused = to_focus;
576 to_container->currently_focused = current_client;
578 bool target_invisible = (to_container->workspace->screen->current_workspace != to_container->workspace->num);
580 /* If we’re moving it to an invisible screen, we need to unmap it */
581 if (target_invisible) {
582 LOG("This workspace is not visible, unmapping\n");
583 xcb_unmap_window(conn, current_client->frame);
585 if (current_client->fullscreen) {
586 LOG("Calling client_enter_fullscreen again\n");
587 client_enter_fullscreen(conn, current_client);
591 /* delete all empty columns/rows */
592 cleanup_table(conn, container->workspace);
596 if (!target_invisible)
597 set_focus(conn, current_client, true);
601 * Switches to the given workspace
604 void show_workspace(xcb_connection_t *conn, int workspace) {
606 bool need_warp = false;
607 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
608 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
609 Workspace *t_ws = &(workspaces[workspace-1]);
611 LOG("show_workspace(%d)\n", workspace);
613 /* Store current_row/current_col */
614 c_ws->current_row = current_row;
615 c_ws->current_col = current_col;
617 /* Check if the workspace has not been used yet */
618 if (t_ws->screen == NULL) {
619 LOG("initializing new workspace, setting num to %d\n", workspace);
620 t_ws->screen = c_ws->screen;
621 /* Copy the dimensions from the virtual screen */
622 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
625 if (c_ws->screen != t_ws->screen) {
626 /* We need to switch to the other screen first */
627 LOG("moving over to other screen.\n");
629 /* Store the old client */
630 Client *old_client = CUR_CELL->currently_focused;
632 c_ws = &(workspaces[t_ws->screen->current_workspace]);
633 current_col = c_ws->current_col;
634 current_row = c_ws->current_row;
635 if (CUR_CELL->currently_focused != NULL)
638 Rect *dims = &(c_ws->screen->rect);
639 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
640 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
643 /* Re-decorate the old client, it’s not focused anymore */
644 if ((old_client != NULL) && !old_client->dock)
645 redecorate_window(conn, old_client);
646 else xcb_flush(conn);
649 /* Check if we need to change something or if we’re already there */
650 if (c_ws->screen->current_workspace == (workspace-1)) {
651 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
652 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
653 set_focus(conn, last_focused, true);
655 client_warp_pointer_into(conn, last_focused);
663 t_ws->screen->current_workspace = workspace-1;
664 Workspace *old_workspace = c_ws;
665 c_ws = &workspaces[workspace-1];
667 /* Unmap all clients of the old workspace */
668 unmap_workspace(conn, old_workspace);
670 current_row = c_ws->current_row;
671 current_col = c_ws->current_col;
672 LOG("new current row = %d, current col = %d\n", current_row, current_col);
674 ignore_enter_notify_forall(conn, c_ws, true);
676 /* Map all clients on the new workspace */
678 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
679 xcb_map_window(conn, client->frame);
681 /* Map all floating clients */
682 if (!c_ws->floating_hidden)
683 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
684 xcb_map_window(conn, client->frame);
686 /* Map all stack windows, if any */
687 struct Stack_Window *stack_win;
688 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
689 if (stack_win->container->workspace == c_ws)
690 xcb_map_window(conn, stack_win->window);
692 ignore_enter_notify_forall(conn, c_ws, false);
694 /* Restore focus on the new workspace */
695 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
696 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
697 set_focus(conn, last_focused, true);
699 client_warp_pointer_into(conn, last_focused);
702 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
708 * Jumps to the given window class / title.
709 * Title is matched using strstr, that is, matches if it appears anywhere
710 * in the string. Regular expressions seem to be a bit overkill here. However,
711 * if we need them for something else somewhen, we may introduce them here, too.
714 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
718 /* The first character is a quote, this was checked before */
719 classtitle = sstrdup(arguments+1);
720 /* The last character is a quote, we just set it to NULL */
721 classtitle[strlen(classtitle)-1] = '\0';
723 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
725 LOG("No matching client found.\n");
730 set_focus(conn, client, true);
734 * Jump directly to the specified workspace, row and col.
735 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
738 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
742 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
743 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
745 /* No match? Either no arguments were specified, or no numbers */
747 LOG("At least one valid argument required\n");
751 /* Move to the target workspace */
752 show_workspace(conn, ws);
757 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
759 /* Move to row/col */
760 if (row >= c_ws->rows)
761 row = c_ws->rows - 1;
762 if (col >= c_ws->cols)
763 col = c_ws->cols - 1;
765 LOG("Jumping to col %d, row %d\n", col, row);
766 if (c_ws->table[col][row]->currently_focused != NULL)
767 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
771 * Travels the focus stack by the given number of times (or once, if no argument
772 * was specified). That is, selects the window you were in before you focused
773 * the current window.
775 * The special values 'floating' (select the next floating window), 'tiling'
776 * (select the next tiling window), 'ft' (if the current window is floating,
777 * select the next tiling window and vice-versa) are also valid
780 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
781 /* Start count at -1 to always skip the first element */
782 int times, count = -1;
784 bool floating_criteria;
786 /* Either it’s one of the special values… */
787 if (strcasecmp(arguments, "floating") == 0) {
788 floating_criteria = true;
789 } else if (strcasecmp(arguments, "tiling") == 0) {
790 floating_criteria = false;
791 } else if (strcasecmp(arguments, "ft") == 0) {
792 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
793 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
794 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
798 floating_criteria = !client_is_floating(last_focused);
800 /* …or a number was specified */
801 if (sscanf(arguments, "%u", ×) != 1) {
802 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
806 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
807 if (++count < times) {
813 set_focus(conn, current, true);
819 /* Select the next client matching the criteria parsed above */
820 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
821 if (client_is_floating(current) == floating_criteria) {
822 set_focus(conn, current, true);
828 * Goes through the list of arguments (for exec()) and checks if the given argument
829 * is present. If not, it copies the arguments (because we cannot realloc it) and
830 * appends the given argument.
833 static char **append_argument(char **original, char *argument) {
835 for (num_args = 0; original[num_args] != NULL; num_args++) {
836 LOG("original argument: \"%s\"\n", original[num_args]);
837 /* If the argument is already present we return the original pointer */
838 if (strcmp(original[num_args], argument) == 0)
841 /* Copy the original array */
842 char **result = smalloc((num_args+2) * sizeof(char*));
843 memcpy(result, original, num_args * sizeof(char*));
844 result[num_args] = argument;
845 result[num_args+1] = NULL;
851 * Switch to next or previous existing workspace
854 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
858 if (direction == 'n') {
859 /* If we are on the last workspace, we cannot go any further */
863 for (i = c_ws->num + 1; i <= 9; i++) {
864 t_ws = &(workspaces[i]);
865 if (t_ws->screen != NULL)
868 } else if (direction == 'p') {
871 for (i = c_ws->num - 1; i >= 0 ; i--) {
872 t_ws = &(workspaces[i]);
873 if (t_ws->screen != NULL)
878 if (t_ws->screen != NULL)
879 show_workspace(conn, i+1);
883 * Parses a command, see file CMDMODE for more information
886 void parse_command(xcb_connection_t *conn, const char *command) {
887 LOG("--- parsing command \"%s\" ---\n", command);
888 /* Get the first client from focus stack because floating clients are not
889 * in any container, therefore CUR_CELL is not appropriate. */
890 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
891 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
894 /* Hmm, just to be sure */
895 if (command[0] == '\0')
898 /* Is it an <exec>? Then execute the given command. */
899 if (STARTS_WITH(command, "exec ")) {
900 LOG("starting \"%s\"\n", command + strlen("exec "));
901 start_application(command+strlen("exec "));
905 /* Is it an <exit>? */
906 if (STARTS_WITH(command, "exit")) {
907 LOG("User issued exit-command, exiting without error.\n");
911 /* Is it a <reload>? */
912 if (STARTS_WITH(command, "reload")) {
913 load_configuration(conn, NULL, true);
917 /* Is it <restart>? Then restart in place. */
918 if (STARTS_WITH(command, "restart")) {
919 LOG("restarting \"%s\"...\n", start_argv[0]);
920 /* make sure -a is in the argument list or append it */
921 start_argv = append_argument(start_argv, "-a");
923 execvp(start_argv[0], start_argv);
927 if (STARTS_WITH(command, "kill")) {
928 if (last_focused == NULL) {
929 LOG("There is no window to kill\n");
933 LOG("Killing current window\n");
934 client_kill(conn, last_focused);
938 /* Is it a jump to a specified workspace, row, col? */
939 if (STARTS_WITH(command, "jump ")) {
940 const char *arguments = command + strlen("jump ");
941 if (arguments[0] == '"')
942 jump_to_window(conn, arguments);
943 else jump_to_container(conn, arguments);
947 /* Should we travel the focus stack? */
948 if (STARTS_WITH(command, "focus")) {
949 const char *arguments = command + strlen("focus ");
950 travel_focus_stack(conn, arguments);
954 /* Is it 'f' for fullscreen? */
955 if (command[0] == 'f') {
956 if (last_focused == NULL)
958 client_toggle_fullscreen(conn, last_focused);
962 /* Is it just 's' for stacking or 'd' for default? */
963 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
964 if (last_focused == NULL || client_is_floating(last_focused)) {
965 LOG("not switching, this is a floating client\n");
968 LOG("Switching mode for current container\n");
969 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
973 if (command[0] == 'H') {
974 LOG("Hiding all floating windows\n");
975 floating_toggle_hide(conn, c_ws);
979 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
981 /* Is it a <with>? */
982 if (command[0] == 'w') {
984 /* TODO: implement */
985 if (command[0] == 'c') {
986 with = WITH_CONTAINER;
988 } else if (command[0] == 'w') {
989 with = WITH_WORKSPACE;
992 LOG("not yet implemented.\n");
997 /* Is it 't' for toggle tiling/floating? */
998 if (command[0] == 't') {
999 if (with == WITH_WORKSPACE) {
1000 c_ws->auto_float = !c_ws->auto_float;
1001 LOG("autofloat is now %d\n", c_ws->auto_float);
1004 if (last_focused == NULL) {
1005 LOG("Cannot toggle tiling/floating: workspace empty\n");
1009 toggle_floating_mode(conn, last_focused, false);
1010 /* delete all empty columns/rows */
1011 cleanup_table(conn, last_focused->workspace);
1013 /* Fix colspan/rowspan if it’d overlap */
1014 fix_colrowspan(conn, last_focused->workspace);
1016 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
1018 /* Re-focus the client because cleanup_table sets the focus to the last
1019 * focused client inside a container only. */
1020 set_focus(conn, last_focused, true);
1025 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1026 if (command[0] == 'n' && command[1] == 'w') {
1027 next_previous_workspace(conn, command[0]);
1031 if (command[0] == 'p' && command[1] == 'w') {
1032 next_previous_workspace(conn, command[0]);
1036 /* It’s a normal <cmd> */
1038 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1039 direction_t direction;
1040 int times = strtol(command, &rest, 10);
1042 LOG("Invalid command (\"%s\")\n", command);
1046 if (*rest == '\0') {
1047 /* No rest? This was a workspace number, not a times specification */
1048 show_workspace(conn, times);
1052 if (*rest == 'm' || *rest == 's') {
1053 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1057 int workspace = strtol(rest, &rest, 10);
1060 LOG("Invalid command (\"%s\")\n", command);
1064 if (*rest == '\0') {
1065 if (last_focused != NULL && client_is_floating(last_focused))
1066 move_floating_window_to_workspace(conn, last_focused, workspace);
1067 else move_current_window_to_workspace(conn, workspace);
1071 if (last_focused == NULL) {
1072 LOG("Not performing (no window found)\n");
1076 if (client_is_floating(last_focused) &&
1077 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1078 LOG("Not performing (floating)\n");
1082 /* Now perform action to <where> */
1083 while (*rest != '\0') {
1086 else if (*rest == 'j')
1088 else if (*rest == 'k')
1090 else if (*rest == 'l')
1091 direction = D_RIGHT;
1093 LOG("unknown direction: %c\n", *rest);
1098 if (action == ACTION_FOCUS) {
1099 if (client_is_floating(last_focused)) {
1100 floating_focus_direction(conn, last_focused, direction);
1103 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1107 if (action == ACTION_MOVE) {
1108 if (client_is_floating(last_focused)) {
1109 floating_move(conn, last_focused, direction);
1112 if (with == WITH_WINDOW)
1113 move_current_window(conn, direction);
1114 else move_current_container(conn, direction);
1118 if (action == ACTION_SNAP) {
1119 snap_current_container(conn, direction);
1124 LOG("--- done ---\n");