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];
242 /* To make static analyzers happy: */
247 /* Remove it from the old container and put it into the new one */
248 client_remove_from_container(conn, current_client, container, true);
250 if (new->currently_focused != NULL)
251 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
252 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
253 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
255 /* Update data structures */
256 current_client->container = new;
257 current_client->workspace = new->workspace;
258 container->currently_focused = to_focus;
259 new->currently_focused = current_client;
261 Workspace *workspace = container->workspace;
263 /* delete all empty columns/rows */
264 cleanup_table(conn, workspace);
266 /* Fix colspan/rowspan if it’d overlap */
267 fix_colrowspan(conn, workspace);
269 render_workspace(conn, workspace->screen, workspace);
272 set_focus(conn, current_client, true);
275 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
276 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
277 (direction == D_LEFT ? "left" : "right"))));
278 /* Get current window */
279 Container *container = CUR_CELL,
282 Container **old = &CUR_CELL;
284 /* There has to be a container, see focus_window() */
285 assert(container != NULL);
289 /* If we’re at the left-most position, move the rest of the table right */
290 if (current_col == 0) {
291 expand_table_cols_at_head(c_ws);
293 old = &CUR_TABLE[current_col+1][current_row];
295 new = CUR_TABLE[--current_col][current_row];
298 if (current_col == (c_ws->cols-1))
299 expand_table_cols(c_ws);
301 new = CUR_TABLE[++current_col][current_row];
304 /* if we’re at the up-most position, move the rest of the table down */
305 if (current_row == 0) {
306 expand_table_rows_at_head(c_ws);
308 old = &CUR_TABLE[current_col][current_row+1];
310 new = CUR_TABLE[current_col][--current_row];
313 if (current_row == (c_ws->rows-1))
314 expand_table_rows(c_ws);
316 new = CUR_TABLE[current_col][++current_row];
318 /* To make static analyzers happy: */
323 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
325 /* Swap the containers */
330 new->col = container->col;
331 new->row = container->row;
333 CUR_CELL = container;
334 container->col = col;
335 container->row = row;
337 Workspace *workspace = container->workspace;
339 /* delete all empty columns/rows */
340 cleanup_table(conn, workspace);
342 /* Fix colspan/rowspan if it’d overlap */
343 fix_colrowspan(conn, workspace);
349 * "Snaps" the current container (not possible for windows, because it works at table base)
350 * to the given direction, that is, adjusts cellspan/rowspan
353 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
354 LOG("snapping container to direction %d\n", direction);
356 Container *container = CUR_CELL;
358 assert(container != NULL);
362 /* Snap to the left is actually a move to the left and then a snap right */
363 if (!cell_exists(container->col - 1, container->row) ||
364 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
365 LOG("cannot snap to left - the cell is already used\n");
369 move_current_window(conn, D_LEFT);
370 snap_current_container(conn, D_RIGHT);
373 /* Check if the cell is used */
374 int new_col = container->col + container->colspan;
375 for (int i = 0; i < container->rowspan; i++)
376 if (!cell_exists(new_col, container->row + i) ||
377 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
378 LOG("cannot snap to right - the cell is already used\n");
382 /* Check if there are other cells with rowspan, which are in our way.
383 * If so, reduce their rowspan. */
384 for (int i = container->row-1; i >= 0; i--) {
385 LOG("we got cell %d, %d with rowspan %d\n",
386 new_col, i, CUR_TABLE[new_col][i]->rowspan);
387 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
388 CUR_TABLE[new_col][i]->rowspan--;
389 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
392 container->colspan++;
396 if (!cell_exists(container->col, container->row - 1) ||
397 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
398 LOG("cannot snap to top - the cell is already used\n");
402 move_current_window(conn, D_UP);
403 snap_current_container(conn, D_DOWN);
406 LOG("snapping down\n");
407 int new_row = container->row + container->rowspan;
408 for (int i = 0; i < container->colspan; i++)
409 if (!cell_exists(container->col + i, new_row) ||
410 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
411 LOG("cannot snap down - the cell is already used\n");
415 for (int i = container->col-1; i >= 0; i--) {
416 LOG("we got cell %d, %d with colspan %d\n",
417 i, new_row, CUR_TABLE[i][new_row]->colspan);
418 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
419 CUR_TABLE[i][new_row]->colspan--;
420 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
424 container->rowspan++;
427 /* To make static analyzers happy: */
435 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
436 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
437 Workspace *t_ws = &(workspaces[workspace-1]),
438 *old_ws = client->workspace;
440 LOG("moving floating\n");
442 if (t_ws->screen == NULL) {
443 LOG("initializing new workspace, setting num to %d\n", workspace-1);
444 t_ws->screen = c_ws->screen;
445 /* Copy the dimensions from the virtual screen */
446 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
448 /* Check if there is already a fullscreen client on the destination workspace and
449 * stop moving if so. */
450 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
451 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
456 /* Remove from focus stack and list of floating clients */
457 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
458 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
460 if (client->workspace->fullscreen_client == client)
461 client->workspace->fullscreen_client = NULL;
463 /* Insert into destination focus stack and list of floating clients */
464 client->workspace = t_ws;
465 SLIST_INSERT_HEAD(&(t_ws->focus_stack), client, focus_clients);
466 TAILQ_INSERT_TAIL(&(t_ws->floating_clients), client, floating_clients);
467 if (client->fullscreen)
468 t_ws->fullscreen_client = client;
470 /* If we’re moving it to an invisible screen, we need to unmap it */
471 if (t_ws->screen->current_workspace != t_ws->num) {
472 LOG("This workspace is not visible, unmapping\n");
473 xcb_unmap_window(conn, client->frame);
475 /* If this is not the case, we move the window to a workspace
476 * which is on another screen, so we also need to adjust its
478 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
479 uint32_t relative_x = client->rect.x - old_ws->rect.x,
480 relative_y = client->rect.y - old_ws->rect.y;
481 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
482 client->rect.x = t_ws->rect.x + relative_x;
483 client->rect.y = t_ws->rect.y + relative_y;
484 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
485 reposition_client(conn, client);
493 set_focus(conn, client, true);
497 * Moves the currently selected window to the given workspace
500 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
501 LOG("Moving current window to workspace %d\n", workspace);
503 Container *container = CUR_CELL;
505 assert(container != NULL);
507 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
508 Workspace *t_ws = &(workspaces[workspace-1]);
510 Client *current_client = container->currently_focused;
511 if (current_client == NULL) {
512 LOG("No currently focused client in current container.\n");
515 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
516 if (to_focus == NULL)
517 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
519 if (t_ws->screen == NULL) {
520 LOG("initializing new workspace, setting num to %d\n", workspace-1);
521 t_ws->screen = container->workspace->screen;
522 /* Copy the dimensions from the virtual screen */
523 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
525 /* Check if there is already a fullscreen client on the destination workspace and
526 * stop moving if so. */
527 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
528 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
533 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
535 assert(to_container != NULL);
537 client_remove_from_container(conn, current_client, container, true);
538 if (container->workspace->fullscreen_client == current_client)
539 container->workspace->fullscreen_client = NULL;
541 /* TODO: insert it to the correct position */
542 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
544 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
545 if (current_client->fullscreen)
546 t_ws->fullscreen_client = current_client;
549 current_client->container = to_container;
550 current_client->workspace = to_container->workspace;
551 container->currently_focused = to_focus;
552 to_container->currently_focused = current_client;
554 /* If we’re moving it to an invisible screen, we need to unmap it */
555 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
556 LOG("This workspace is not visible, unmapping\n");
557 xcb_unmap_window(conn, current_client->frame);
560 /* delete all empty columns/rows */
561 cleanup_table(conn, container->workspace);
567 * Switches to the given workspace
570 void show_workspace(xcb_connection_t *conn, int workspace) {
572 bool need_warp = false;
573 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
574 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
575 Workspace *t_ws = &(workspaces[workspace-1]);
577 LOG("show_workspace(%d)\n", workspace);
579 /* Store current_row/current_col */
580 c_ws->current_row = current_row;
581 c_ws->current_col = current_col;
583 /* Check if the workspace has not been used yet */
584 if (t_ws->screen == NULL) {
585 LOG("initializing new workspace, setting num to %d\n", workspace);
586 t_ws->screen = c_ws->screen;
587 /* Copy the dimensions from the virtual screen */
588 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
591 if (c_ws->screen != t_ws->screen) {
592 /* We need to switch to the other screen first */
593 LOG("moving over to other screen.\n");
595 /* Store the old client */
596 Client *old_client = CUR_CELL->currently_focused;
598 c_ws = &(workspaces[t_ws->screen->current_workspace]);
599 current_col = c_ws->current_col;
600 current_row = c_ws->current_row;
601 if (CUR_CELL->currently_focused != NULL)
604 Rect *dims = &(c_ws->screen->rect);
605 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
606 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
609 /* Re-decorate the old client, it’s not focused anymore */
610 if ((old_client != NULL) && !old_client->dock)
611 redecorate_window(conn, old_client);
612 else xcb_flush(conn);
615 /* Check if we need to change something or if we’re already there */
616 if (c_ws->screen->current_workspace == (workspace-1)) {
617 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
618 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
619 set_focus(conn, last_focused, true);
621 client_warp_pointer_into(conn, last_focused);
629 t_ws->screen->current_workspace = workspace-1;
630 Workspace *old_workspace = c_ws;
631 c_ws = &workspaces[workspace-1];
633 /* Unmap all clients of the old workspace */
634 unmap_workspace(conn, old_workspace);
636 current_row = c_ws->current_row;
637 current_col = c_ws->current_col;
638 LOG("new current row = %d, current col = %d\n", current_row, current_col);
640 ignore_enter_notify_forall(conn, c_ws, true);
642 /* Map all clients on the new workspace */
644 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
645 xcb_map_window(conn, client->frame);
647 /* Map all floating clients */
648 if (!c_ws->floating_hidden)
649 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
650 xcb_map_window(conn, client->frame);
652 /* Map all stack windows, if any */
653 struct Stack_Window *stack_win;
654 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
655 if (stack_win->container->workspace == c_ws)
656 xcb_map_window(conn, stack_win->window);
658 ignore_enter_notify_forall(conn, c_ws, false);
660 /* Restore focus on the new workspace */
661 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
662 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
663 set_focus(conn, last_focused, true);
665 client_warp_pointer_into(conn, last_focused);
668 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
674 * Jumps to the given window class / title.
675 * Title is matched using strstr, that is, matches if it appears anywhere
676 * in the string. Regular expressions seem to be a bit overkill here. However,
677 * if we need them for something else somewhen, we may introduce them here, too.
680 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
684 /* The first character is a quote, this was checked before */
685 classtitle = sstrdup(arguments+1);
686 /* The last character is a quote, we just set it to NULL */
687 classtitle[strlen(classtitle)-1] = '\0';
689 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
691 LOG("No matching client found.\n");
696 set_focus(conn, client, true);
700 * Jump directly to the specified workspace, row and col.
701 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
704 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
708 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
709 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
711 /* No match? Either no arguments were specified, or no numbers */
713 LOG("At least one valid argument required\n");
717 /* Move to the target workspace */
718 show_workspace(conn, ws);
723 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
725 /* Move to row/col */
726 if (row >= c_ws->rows)
727 row = c_ws->rows - 1;
728 if (col >= c_ws->cols)
729 col = c_ws->cols - 1;
731 LOG("Jumping to col %d, row %d\n", col, row);
732 if (c_ws->table[col][row]->currently_focused != NULL)
733 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
737 * Travels the focus stack by the given number of times (or once, if no argument
738 * was specified). That is, selects the window you were in before you focused
739 * the current window.
741 * The special values 'floating' (select the next floating window), 'tiling'
742 * (select the next tiling window), 'ft' (if the current window is floating,
743 * select the next tiling window and vice-versa) are also valid
746 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
747 /* Start count at -1 to always skip the first element */
748 int times, count = -1;
750 bool floating_criteria;
752 /* Either it’s one of the special values… */
753 if (strcasecmp(arguments, "floating") == 0) {
754 floating_criteria = true;
755 } else if (strcasecmp(arguments, "tiling") == 0) {
756 floating_criteria = false;
757 } else if (strcasecmp(arguments, "ft") == 0) {
758 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
759 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
760 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
764 floating_criteria = !client_is_floating(last_focused);
766 /* …or a number was specified */
767 if (sscanf(arguments, "%u", ×) != 1) {
768 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
772 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
773 if (++count < times) {
779 set_focus(conn, current, true);
785 /* Select the next client matching the criteria parsed above */
786 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
787 if (client_is_floating(current) == floating_criteria) {
788 set_focus(conn, current, true);
794 * Goes through the list of arguments (for exec()) and checks if the given argument
795 * is present. If not, it copies the arguments (because we cannot realloc it) and
796 * appends the given argument.
799 static char **append_argument(char **original, char *argument) {
801 for (num_args = 0; original[num_args] != NULL; num_args++) {
802 LOG("original argument: \"%s\"\n", original[num_args]);
803 /* If the argument is already present we return the original pointer */
804 if (strcmp(original[num_args], argument) == 0)
807 /* Copy the original array */
808 char **result = smalloc((num_args+2) * sizeof(char*));
809 memcpy(result, original, num_args * sizeof(char*));
810 result[num_args] = argument;
811 result[num_args+1] = NULL;
817 * Parses a command, see file CMDMODE for more information
820 void parse_command(xcb_connection_t *conn, const char *command) {
821 LOG("--- parsing command \"%s\" ---\n", command);
822 /* Get the first client from focus stack because floating clients are not
823 * in any container, therefore CUR_CELL is not appropriate. */
824 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
825 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
828 /* Hmm, just to be sure */
829 if (command[0] == '\0')
832 /* Is it an <exec>? Then execute the given command. */
833 if (STARTS_WITH(command, "exec ")) {
834 LOG("starting \"%s\"\n", command + strlen("exec "));
835 start_application(command+strlen("exec "));
839 /* Is it an <exit>? */
840 if (STARTS_WITH(command, "exit")) {
841 LOG("User issued exit-command, exiting without error.\n");
845 /* Is it <restart>? Then restart in place. */
846 if (STARTS_WITH(command, "restart")) {
847 LOG("restarting \"%s\"...\n", start_argv[0]);
848 /* make sure -a is in the argument list or append it */
849 start_argv = append_argument(start_argv, "-a");
851 execvp(start_argv[0], start_argv);
855 if (STARTS_WITH(command, "kill")) {
856 if (last_focused == NULL) {
857 LOG("There is no window to kill\n");
861 LOG("Killing current window\n");
862 client_kill(conn, last_focused);
866 /* Is it a jump to a specified workspace, row, col? */
867 if (STARTS_WITH(command, "jump ")) {
868 const char *arguments = command + strlen("jump ");
869 if (arguments[0] == '"')
870 jump_to_window(conn, arguments);
871 else jump_to_container(conn, arguments);
875 /* Should we travel the focus stack? */
876 if (STARTS_WITH(command, "focus")) {
877 const char *arguments = command + strlen("focus ");
878 travel_focus_stack(conn, arguments);
882 /* Is it 'f' for fullscreen? */
883 if (command[0] == 'f') {
884 if (last_focused == NULL)
886 client_toggle_fullscreen(conn, last_focused);
890 /* Is it just 's' for stacking or 'd' for default? */
891 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
892 if (last_focused == NULL || client_is_floating(last_focused)) {
893 LOG("not switching, this is a floating client\n");
896 LOG("Switching mode for current container\n");
897 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
901 if (command[0] == 'H') {
902 LOG("Hiding all floating windows\n");
903 floating_toggle_hide(conn, c_ws);
907 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
909 /* Is it a <with>? */
910 if (command[0] == 'w') {
912 /* TODO: implement */
913 if (command[0] == 'c') {
914 with = WITH_CONTAINER;
916 } else if (command[0] == 'w') {
917 with = WITH_WORKSPACE;
920 LOG("not yet implemented.\n");
925 /* Is it 't' for toggle tiling/floating? */
926 if (command[0] == 't') {
927 if (with == WITH_WORKSPACE) {
928 c_ws->auto_float = !c_ws->auto_float;
929 LOG("autofloat is now %d\n", c_ws->auto_float);
932 if (last_focused == NULL) {
933 LOG("Cannot toggle tiling/floating: workspace empty\n");
937 toggle_floating_mode(conn, last_focused, false);
938 /* delete all empty columns/rows */
939 cleanup_table(conn, last_focused->workspace);
941 /* Fix colspan/rowspan if it’d overlap */
942 fix_colrowspan(conn, last_focused->workspace);
944 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
949 /* It’s a normal <cmd> */
951 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
952 direction_t direction;
953 int times = strtol(command, &rest, 10);
955 LOG("Invalid command (\"%s\")\n", command);
960 /* No rest? This was a workspace number, not a times specification */
961 show_workspace(conn, times);
965 if (*rest == 'm' || *rest == 's') {
966 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
970 int workspace = strtol(rest, &rest, 10);
973 LOG("Invalid command (\"%s\")\n", command);
978 if (last_focused != NULL && client_is_floating(last_focused))
979 move_floating_window_to_workspace(conn, last_focused, workspace);
980 else move_current_window_to_workspace(conn, workspace);
984 if (last_focused == NULL) {
985 LOG("Not performing (no window found)\n");
989 if (client_is_floating(last_focused) &&
990 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
991 LOG("Not performing (floating)\n");
995 /* Now perform action to <where> */
996 while (*rest != '\0') {
999 else if (*rest == 'j')
1001 else if (*rest == 'k')
1003 else if (*rest == 'l')
1004 direction = D_RIGHT;
1006 LOG("unknown direction: %c\n", *rest);
1011 if (action == ACTION_FOCUS) {
1012 if (client_is_floating(last_focused)) {
1013 floating_focus_direction(conn, last_focused, direction);
1016 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1020 if (action == ACTION_MOVE) {
1021 if (client_is_floating(last_focused)) {
1022 floating_move(conn, last_focused, direction);
1025 if (with == WITH_WINDOW)
1026 move_current_window(conn, direction);
1027 else move_current_container(conn, direction);
1031 if (action == ACTION_SNAP) {
1032 snap_current_container(conn, direction);
1037 LOG("--- done ---\n");