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"
33 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
34 /* If this container is empty, we’re done */
35 if (container->currently_focused == NULL)
38 /* Get the previous/next client or wrap around */
39 Client *candidate = NULL;
40 if (direction == D_UP) {
41 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
42 candidate = CIRCLEQ_LAST(&(container->clients));
44 else if (direction == D_DOWN) {
45 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
46 candidate = CIRCLEQ_FIRST(&(container->clients));
47 } else LOG("Direction not implemented!\n");
49 /* If we could not switch, the container contains exactly one client. We return false */
50 if (candidate == container->currently_focused)
54 set_focus(conn, candidate, true);
59 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
61 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
62 LOG("focusing direction %d\n", direction);
64 int new_row = current_row,
65 new_col = current_col;
66 Container *container = CUR_CELL;
67 Workspace *t_ws = c_ws;
69 /* Makes sure new_col and new_row are within bounds of the new workspace */
70 void check_colrow_boundaries() {
71 if (new_col >= t_ws->cols)
72 new_col = (t_ws->cols - 1);
73 if (new_row >= t_ws->rows)
74 new_row = (t_ws->rows - 1);
77 /* There always is a container. If not, current_col or current_row is wrong */
78 assert(container != NULL);
80 if (container->workspace->fullscreen_client != NULL) {
81 LOG("You're in fullscreen mode. Won't switch focus\n");
85 /* For focusing screens, situation is different: we get the rect
86 * of the current screen, then get the screen which is on its
87 * right/left/bottom/top and just switch to the workspace on
88 * the target screen. */
89 if (thing == THING_SCREEN) {
90 i3Screen *cs = c_ws->screen;
92 Rect bounds = cs->rect;
94 if (direction == D_RIGHT)
95 bounds.x += bounds.width;
96 else if (direction == D_LEFT)
97 bounds.x -= bounds.width;
98 else if (direction == D_UP)
99 bounds.y -= bounds.height;
100 else bounds.y += bounds.height;
102 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
103 if (target == NULL) {
104 LOG("Target screen NULL\n");
105 /* Wrap around if the target screen is out of bounds */
106 if (direction == D_RIGHT)
107 target = get_screen_most(D_LEFT);
108 else if (direction == D_LEFT)
109 target = get_screen_most(D_RIGHT);
110 else if (direction == D_UP)
111 target = get_screen_most(D_DOWN);
112 else target = get_screen_most(D_UP);
115 LOG("Switching to ws %d\n", target->current_workspace + 1);
116 show_workspace(conn, target->current_workspace + 1);
120 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
121 if (direction == D_UP || direction == D_DOWN) {
122 if (thing == THING_WINDOW)
123 /* Let’s see if we can perform up/down focus in the current container */
124 if (focus_window_in_container(conn, container, direction))
127 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
128 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
129 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
130 /* Set new_row as a sane default, but it may get overwritten in a second */
133 /* Search from the top to correctly handle rowspanned containers */
134 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
135 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
142 /* Let’s see if there is a screen down/up there to which we can switch */
143 LOG("container is at %d with height %d\n", container->y, container->height);
145 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
146 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
147 LOG("Wrapping screen around vertically\n");
148 /* No screen found? Then wrap */
149 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
151 t_ws = &(workspaces[screen->current_workspace]);
152 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
155 check_colrow_boundaries();
157 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
158 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
159 LOG("Cell empty, checking for colspanned client above...\n");
160 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
161 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
167 LOG("Fixed it to new col %d\n", new_col);
169 } else if (direction == D_LEFT || direction == D_RIGHT) {
170 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
171 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
172 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
173 /* Set new_col as a sane default, but it may get overwritten in a second */
176 /* Search from the left to correctly handle colspanned containers */
177 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
178 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
185 /* Let’s see if there is a screen left/right here to which we can switch */
186 LOG("container is at %d with width %d\n", container->x, container->width);
188 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
189 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
190 LOG("Wrapping screen around horizontally\n");
191 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
193 t_ws = &(workspaces[screen->current_workspace]);
194 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
197 check_colrow_boundaries();
199 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
200 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
201 LOG("Cell empty, checking for rowspanned client above...\n");
202 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
203 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
209 LOG("Fixed it to new row %d\n", new_row);
212 LOG("direction unhandled\n");
216 check_colrow_boundaries();
218 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
219 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
223 * Tries to move the window inside its current container.
225 * Returns true if the window could be moved, false otherwise.
228 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
229 direction_t direction) {
230 assert(client->container != NULL);
232 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
233 CIRCLEQ_NEXT(client, clients));
235 if (other == CIRCLEQ_END(&(client->container->clients)))
238 LOG("i can do that\n");
239 /* We can move the client inside its current container */
240 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
241 if (direction == D_UP)
242 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
243 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
249 * Moves the current window or whole container to the given direction, creating a column/row if
253 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
254 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
255 (direction == D_LEFT ? "left" : "right"))));
256 /* Get current window */
257 Container *container = CUR_CELL,
260 /* There has to be a container, see focus_window() */
261 assert(container != NULL);
263 /* If there is no window or the dock window is focused, we’re done */
264 if (container->currently_focused == NULL ||
265 container->currently_focused->dock)
268 /* As soon as the client is moved away, the last focused client in the old
269 * container needs to get focus, if any. Therefore, we save it here. */
270 Client *current_client = container->currently_focused;
271 Client *to_focus = get_last_focused_client(conn, container, current_client);
273 if (to_focus == NULL) {
274 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
275 if (to_focus == NULL)
276 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
281 /* If we’re at the left-most position, move the rest of the table right */
282 if (current_col == 0) {
283 expand_table_cols_at_head(c_ws);
286 new = CUR_TABLE[--current_col][current_row];
289 if (current_col == (c_ws->cols-1))
290 expand_table_cols(c_ws);
292 new = CUR_TABLE[++current_col][current_row];
295 if (move_current_window_in_container(conn, current_client, D_UP))
298 /* if we’re at the up-most position, move the rest of the table down */
299 if (current_row == 0) {
300 expand_table_rows_at_head(c_ws);
303 new = CUR_TABLE[current_col][--current_row];
306 if (move_current_window_in_container(conn, current_client, D_DOWN))
309 if (current_row == (c_ws->rows-1))
310 expand_table_rows(c_ws);
312 new = CUR_TABLE[current_col][++current_row];
314 /* To make static analyzers happy: */
319 /* Remove it from the old container and put it into the new one */
320 client_remove_from_container(conn, current_client, container, true);
322 if (new->currently_focused != NULL)
323 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
324 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
325 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
327 /* Update data structures */
328 current_client->container = new;
329 current_client->workspace = new->workspace;
330 container->currently_focused = to_focus;
331 new->currently_focused = current_client;
333 Workspace *workspace = container->workspace;
335 /* delete all empty columns/rows */
336 cleanup_table(conn, workspace);
338 /* Fix colspan/rowspan if it’d overlap */
339 fix_colrowspan(conn, workspace);
341 render_workspace(conn, workspace->screen, workspace);
344 set_focus(conn, current_client, true);
347 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
348 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
349 (direction == D_LEFT ? "left" : "right"))));
350 /* Get current window */
351 Container *container = CUR_CELL,
354 Container **old = &CUR_CELL;
356 /* There has to be a container, see focus_window() */
357 assert(container != NULL);
361 /* If we’re at the left-most position, move the rest of the table right */
362 if (current_col == 0) {
363 expand_table_cols_at_head(c_ws);
365 old = &CUR_TABLE[current_col+1][current_row];
367 new = CUR_TABLE[--current_col][current_row];
370 if (current_col == (c_ws->cols-1))
371 expand_table_cols(c_ws);
373 new = CUR_TABLE[++current_col][current_row];
376 /* if we’re at the up-most position, move the rest of the table down */
377 if (current_row == 0) {
378 expand_table_rows_at_head(c_ws);
380 old = &CUR_TABLE[current_col][current_row+1];
382 new = CUR_TABLE[current_col][--current_row];
385 if (current_row == (c_ws->rows-1))
386 expand_table_rows(c_ws);
388 new = CUR_TABLE[current_col][++current_row];
390 /* To make static analyzers happy: */
395 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
397 /* Swap the containers */
402 new->col = container->col;
403 new->row = container->row;
405 CUR_CELL = container;
406 container->col = col;
407 container->row = row;
409 Workspace *workspace = container->workspace;
411 /* delete all empty columns/rows */
412 cleanup_table(conn, workspace);
414 /* Fix colspan/rowspan if it’d overlap */
415 fix_colrowspan(conn, workspace);
421 * "Snaps" the current container (not possible for windows, because it works at table base)
422 * to the given direction, that is, adjusts cellspan/rowspan
425 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
426 LOG("snapping container to direction %d\n", direction);
428 Container *container = CUR_CELL;
430 assert(container != NULL);
434 /* Snap to the left is actually a move to the left and then a snap right */
435 if (!cell_exists(container->col - 1, container->row) ||
436 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
437 LOG("cannot snap to left - the cell is already used\n");
441 move_current_window(conn, D_LEFT);
442 snap_current_container(conn, D_RIGHT);
445 /* Check if the cell is used */
446 int new_col = container->col + container->colspan;
447 for (int i = 0; i < container->rowspan; i++)
448 if (!cell_exists(new_col, container->row + i) ||
449 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
450 LOG("cannot snap to right - the cell is already used\n");
454 /* Check if there are other cells with rowspan, which are in our way.
455 * If so, reduce their rowspan. */
456 for (int i = container->row-1; i >= 0; i--) {
457 LOG("we got cell %d, %d with rowspan %d\n",
458 new_col, i, CUR_TABLE[new_col][i]->rowspan);
459 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
460 CUR_TABLE[new_col][i]->rowspan--;
461 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
464 container->colspan++;
468 if (!cell_exists(container->col, container->row - 1) ||
469 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
470 LOG("cannot snap to top - the cell is already used\n");
474 move_current_window(conn, D_UP);
475 snap_current_container(conn, D_DOWN);
478 LOG("snapping down\n");
479 int new_row = container->row + container->rowspan;
480 for (int i = 0; i < container->colspan; i++)
481 if (!cell_exists(container->col + i, new_row) ||
482 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
483 LOG("cannot snap down - the cell is already used\n");
487 for (int i = container->col-1; i >= 0; i--) {
488 LOG("we got cell %d, %d with colspan %d\n",
489 i, new_row, CUR_TABLE[i][new_row]->colspan);
490 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
491 CUR_TABLE[i][new_row]->colspan--;
492 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
496 container->rowspan++;
499 /* To make static analyzers happy: */
507 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
508 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
509 Workspace *t_ws = &(workspaces[workspace-1]),
510 *old_ws = client->workspace;
512 LOG("moving floating\n");
514 if (t_ws->screen == NULL) {
515 LOG("initializing new workspace, setting num to %d\n", workspace-1);
516 t_ws->screen = c_ws->screen;
517 /* Copy the dimensions from the virtual screen */
518 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
520 /* Check if there is already a fullscreen client on the destination workspace and
521 * stop moving if so. */
522 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
523 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
528 floating_assign_to_workspace(client, t_ws);
530 /* If we’re moving it to an invisible screen, we need to unmap it */
531 if (!workspace_is_visible(t_ws)) {
532 LOG("This workspace is not visible, unmapping\n");
533 xcb_unmap_window(conn, client->frame);
535 /* If this is not the case, we move the window to a workspace
536 * which is on another screen, so we also need to adjust its
538 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
539 uint32_t relative_x = client->rect.x - old_ws->rect.x,
540 relative_y = client->rect.y - old_ws->rect.y;
541 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
542 client->rect.x = t_ws->rect.x + relative_x;
543 client->rect.y = t_ws->rect.y + relative_y;
544 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
545 reposition_client(conn, client);
553 if (workspace_is_visible(t_ws))
554 set_focus(conn, client, true);
558 * Moves the currently selected window to the given workspace
561 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
562 LOG("Moving current window to workspace %d\n", workspace);
564 Container *container = CUR_CELL;
566 assert(container != NULL);
568 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
569 Workspace *t_ws = &(workspaces[workspace-1]);
571 Client *current_client = container->currently_focused;
572 if (current_client == NULL) {
573 LOG("No currently focused client in current container.\n");
576 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
577 if (to_focus == NULL)
578 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
580 if (t_ws->screen == NULL) {
581 LOG("initializing new workspace, setting num to %d\n", workspace-1);
582 t_ws->screen = container->workspace->screen;
583 /* Copy the dimensions from the virtual screen */
584 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
586 /* Check if there is already a fullscreen client on the destination workspace and
587 * stop moving if so. */
588 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
589 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
594 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
596 assert(to_container != NULL);
598 client_remove_from_container(conn, current_client, container, true);
599 if (container->workspace->fullscreen_client == current_client)
600 container->workspace->fullscreen_client = NULL;
602 /* TODO: insert it to the correct position */
603 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
605 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
608 current_client->container = to_container;
609 current_client->workspace = to_container->workspace;
610 container->currently_focused = to_focus;
611 to_container->currently_focused = current_client;
613 /* If we’re moving it to an invisible screen, we need to unmap it */
614 if (!workspace_is_visible(to_container->workspace)) {
615 LOG("This workspace is not visible, unmapping\n");
616 xcb_unmap_window(conn, current_client->frame);
618 if (current_client->fullscreen) {
619 LOG("Calling client_enter_fullscreen again\n");
620 client_enter_fullscreen(conn, current_client);
624 /* delete all empty columns/rows */
625 cleanup_table(conn, container->workspace);
629 if (workspace_is_visible(to_container->workspace))
630 set_focus(conn, current_client, true);
634 * Switches to the given workspace
637 void show_workspace(xcb_connection_t *conn, int workspace) {
639 bool need_warp = false;
640 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
641 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
642 Workspace *t_ws = &(workspaces[workspace-1]);
644 LOG("show_workspace(%d)\n", workspace);
646 /* Store current_row/current_col */
647 c_ws->current_row = current_row;
648 c_ws->current_col = current_col;
650 /* Check if the workspace has not been used yet */
651 if (t_ws->screen == NULL) {
652 LOG("initializing new workspace, setting num to %d\n", workspace);
653 t_ws->screen = c_ws->screen;
654 /* Copy the dimensions from the virtual screen */
655 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
658 if (c_ws->screen != t_ws->screen) {
659 /* We need to switch to the other screen first */
660 LOG("moving over to other screen.\n");
662 /* Store the old client */
663 Client *old_client = CUR_CELL->currently_focused;
665 c_ws = &(workspaces[t_ws->screen->current_workspace]);
666 current_col = c_ws->current_col;
667 current_row = c_ws->current_row;
668 if (CUR_CELL->currently_focused != NULL)
671 Rect *dims = &(c_ws->screen->rect);
672 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
673 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
676 /* Re-decorate the old client, it’s not focused anymore */
677 if ((old_client != NULL) && !old_client->dock)
678 redecorate_window(conn, old_client);
679 else xcb_flush(conn);
682 /* Check if we need to change something or if we’re already there */
683 if (c_ws->screen->current_workspace == (workspace-1)) {
684 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
685 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
686 set_focus(conn, last_focused, true);
688 client_warp_pointer_into(conn, last_focused);
696 t_ws->screen->current_workspace = workspace-1;
697 Workspace *old_workspace = c_ws;
698 c_ws = &workspaces[workspace-1];
700 /* Unmap all clients of the old workspace */
701 unmap_workspace(conn, old_workspace);
703 current_row = c_ws->current_row;
704 current_col = c_ws->current_col;
705 LOG("new current row = %d, current col = %d\n", current_row, current_col);
707 ignore_enter_notify_forall(conn, c_ws, true);
709 /* Map all clients on the new workspace */
711 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
712 xcb_map_window(conn, client->frame);
714 /* Map all floating clients */
715 if (!c_ws->floating_hidden)
716 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
717 xcb_map_window(conn, client->frame);
719 /* Map all stack windows, if any */
720 struct Stack_Window *stack_win;
721 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
722 if (stack_win->container->workspace == c_ws)
723 xcb_map_window(conn, stack_win->window);
725 ignore_enter_notify_forall(conn, c_ws, false);
727 /* Restore focus on the new workspace */
728 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
729 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
730 set_focus(conn, last_focused, true);
732 client_warp_pointer_into(conn, last_focused);
735 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
741 * Jumps to the given window class / title.
742 * Title is matched using strstr, that is, matches if it appears anywhere
743 * in the string. Regular expressions seem to be a bit overkill here. However,
744 * if we need them for something else somewhen, we may introduce them here, too.
747 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
751 /* The first character is a quote, this was checked before */
752 classtitle = sstrdup(arguments+1);
753 /* The last character is a quote, we just set it to NULL */
754 classtitle[strlen(classtitle)-1] = '\0';
756 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
758 LOG("No matching client found.\n");
763 set_focus(conn, client, true);
767 * Jump directly to the specified workspace, row and col.
768 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
771 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
775 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
776 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
778 /* No match? Either no arguments were specified, or no numbers */
780 LOG("At least one valid argument required\n");
784 /* Move to the target workspace */
785 show_workspace(conn, ws);
790 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
792 /* Move to row/col */
793 if (row >= c_ws->rows)
794 row = c_ws->rows - 1;
795 if (col >= c_ws->cols)
796 col = c_ws->cols - 1;
798 LOG("Jumping to col %d, row %d\n", col, row);
799 if (c_ws->table[col][row]->currently_focused != NULL)
800 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
804 * Travels the focus stack by the given number of times (or once, if no argument
805 * was specified). That is, selects the window you were in before you focused
806 * the current window.
808 * The special values 'floating' (select the next floating window), 'tiling'
809 * (select the next tiling window), 'ft' (if the current window is floating,
810 * select the next tiling window and vice-versa) are also valid
813 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
814 /* Start count at -1 to always skip the first element */
815 int times, count = -1;
817 bool floating_criteria;
819 /* Either it’s one of the special values… */
820 if (strcasecmp(arguments, "floating") == 0) {
821 floating_criteria = true;
822 } else if (strcasecmp(arguments, "tiling") == 0) {
823 floating_criteria = false;
824 } else if (strcasecmp(arguments, "ft") == 0) {
825 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
826 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
827 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
831 floating_criteria = !client_is_floating(last_focused);
833 /* …or a number was specified */
834 if (sscanf(arguments, "%u", ×) != 1) {
835 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
839 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
840 if (++count < times) {
846 set_focus(conn, current, true);
852 /* Select the next client matching the criteria parsed above */
853 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
854 if (client_is_floating(current) == floating_criteria) {
855 set_focus(conn, current, true);
861 * Goes through the list of arguments (for exec()) and checks if the given argument
862 * is present. If not, it copies the arguments (because we cannot realloc it) and
863 * appends the given argument.
866 static char **append_argument(char **original, char *argument) {
868 for (num_args = 0; original[num_args] != NULL; num_args++) {
869 LOG("original argument: \"%s\"\n", original[num_args]);
870 /* If the argument is already present we return the original pointer */
871 if (strcmp(original[num_args], argument) == 0)
874 /* Copy the original array */
875 char **result = smalloc((num_args+2) * sizeof(char*));
876 memcpy(result, original, num_args * sizeof(char*));
877 result[num_args] = argument;
878 result[num_args+1] = NULL;
884 * Switch to next or previous existing workspace
887 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
891 if (direction == 'n') {
892 /* If we are on the last workspace, we cannot go any further */
896 for (i = c_ws->num + 1; i <= 9; i++) {
897 t_ws = &(workspaces[i]);
898 if (t_ws->screen != NULL)
901 } else if (direction == 'p') {
904 for (i = c_ws->num - 1; i >= 0 ; i--) {
905 t_ws = &(workspaces[i]);
906 if (t_ws->screen != NULL)
911 if (t_ws->screen != NULL)
912 show_workspace(conn, i+1);
916 * Parses a command, see file CMDMODE for more information
919 void parse_command(xcb_connection_t *conn, const char *command) {
920 LOG("--- parsing command \"%s\" ---\n", command);
921 /* Get the first client from focus stack because floating clients are not
922 * in any container, therefore CUR_CELL is not appropriate. */
923 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
924 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
927 /* Hmm, just to be sure */
928 if (command[0] == '\0')
931 /* Is it an <exec>? Then execute the given command. */
932 if (STARTS_WITH(command, "exec ")) {
933 LOG("starting \"%s\"\n", command + strlen("exec "));
934 start_application(command+strlen("exec "));
938 /* Is it an <exit>? */
939 if (STARTS_WITH(command, "exit")) {
940 LOG("User issued exit-command, exiting without error.\n");
944 /* Is it a <reload>? */
945 if (STARTS_WITH(command, "reload")) {
946 load_configuration(conn, NULL, true);
950 /* Is it <restart>? Then restart in place. */
951 if (STARTS_WITH(command, "restart")) {
952 LOG("restarting \"%s\"...\n", start_argv[0]);
953 /* make sure -a is in the argument list or append it */
954 start_argv = append_argument(start_argv, "-a");
956 execvp(start_argv[0], start_argv);
960 if (STARTS_WITH(command, "kill")) {
961 if (last_focused == NULL) {
962 LOG("There is no window to kill\n");
966 LOG("Killing current window\n");
967 client_kill(conn, last_focused);
971 /* Is it a jump to a specified workspace, row, col? */
972 if (STARTS_WITH(command, "jump ")) {
973 const char *arguments = command + strlen("jump ");
974 if (arguments[0] == '"')
975 jump_to_window(conn, arguments);
976 else jump_to_container(conn, arguments);
980 /* Should we travel the focus stack? */
981 if (STARTS_WITH(command, "focus")) {
982 const char *arguments = command + strlen("focus ");
983 travel_focus_stack(conn, arguments);
987 /* Is it 'f' for fullscreen? */
988 if (command[0] == 'f') {
989 if (last_focused == NULL)
991 client_toggle_fullscreen(conn, last_focused);
995 /* Is it just 's' for stacking or 'd' for default? */
996 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
997 if (last_focused != NULL && client_is_floating(last_focused)) {
998 LOG("not switching, this is a floating client\n");
1001 LOG("Switching mode for current container\n");
1002 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
1006 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1007 if (command[0] == 'b') {
1008 if (last_focused == NULL) {
1009 LOG("No window focused, cannot change border type\n");
1012 client_change_border(conn, last_focused, command[1]);
1016 if (command[0] == 'H') {
1017 LOG("Hiding all floating windows\n");
1018 floating_toggle_hide(conn, c_ws);
1022 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1024 /* Is it a <with>? */
1025 if (command[0] == 'w') {
1027 /* TODO: implement */
1028 if (command[0] == 'c') {
1029 with = WITH_CONTAINER;
1031 } else if (command[0] == 'w') {
1032 with = WITH_WORKSPACE;
1034 } else if (command[0] == 's') {
1038 LOG("not yet implemented.\n");
1043 /* Is it 't' for toggle tiling/floating? */
1044 if (command[0] == 't') {
1045 if (with == WITH_WORKSPACE) {
1046 c_ws->auto_float = !c_ws->auto_float;
1047 LOG("autofloat is now %d\n", c_ws->auto_float);
1050 if (last_focused == NULL) {
1051 LOG("Cannot toggle tiling/floating: workspace empty\n");
1055 toggle_floating_mode(conn, last_focused, false);
1056 /* delete all empty columns/rows */
1057 cleanup_table(conn, last_focused->workspace);
1059 /* Fix colspan/rowspan if it’d overlap */
1060 fix_colrowspan(conn, last_focused->workspace);
1062 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
1064 /* Re-focus the client because cleanup_table sets the focus to the last
1065 * focused client inside a container only. */
1066 set_focus(conn, last_focused, true);
1071 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1072 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1073 next_previous_workspace(conn, command[0]);
1077 /* It’s a normal <cmd> */
1079 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1080 direction_t direction;
1081 int times = strtol(command, &rest, 10);
1083 LOG("Invalid command (\"%s\")\n", command);
1087 if (*rest == '\0') {
1088 /* No rest? This was a workspace number, not a times specification */
1089 show_workspace(conn, times);
1093 if (*rest == 'm' || *rest == 's') {
1094 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1098 int workspace = strtol(rest, &rest, 10);
1101 LOG("Invalid command (\"%s\")\n", command);
1105 if (*rest == '\0') {
1106 if (last_focused != NULL && client_is_floating(last_focused))
1107 move_floating_window_to_workspace(conn, last_focused, workspace);
1108 else move_current_window_to_workspace(conn, workspace);
1112 if (last_focused == NULL) {
1113 LOG("Not performing (no window found)\n");
1117 if (client_is_floating(last_focused) &&
1118 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1119 LOG("Not performing (floating)\n");
1123 /* Now perform action to <where> */
1124 while (*rest != '\0') {
1127 else if (*rest == 'j')
1129 else if (*rest == 'k')
1131 else if (*rest == 'l')
1132 direction = D_RIGHT;
1134 LOG("unknown direction: %c\n", *rest);
1139 if (action == ACTION_FOCUS) {
1140 if (with == WITH_SCREEN) {
1141 focus_thing(conn, direction, THING_SCREEN);
1144 if (client_is_floating(last_focused)) {
1145 floating_focus_direction(conn, last_focused, direction);
1148 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1152 if (action == ACTION_MOVE) {
1153 if (with == WITH_SCREEN) {
1154 /* TODO: this should swap the screen’s contents
1155 * (e.g. all workspaces) with the next/previous/…
1157 LOG("Not yet implemented\n");
1160 if (client_is_floating(last_focused)) {
1161 floating_move(conn, last_focused, direction);
1164 if (with == WITH_WINDOW)
1165 move_current_window(conn, direction);
1166 else move_current_container(conn, direction);
1170 if (action == ACTION_SNAP) {
1171 if (with == WITH_SCREEN) {
1172 LOG("You cannot snap a screen (it makes no sense).\n");
1175 snap_current_container(conn, direction);
1180 LOG("--- done ---\n");