4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
30 #include "workspace.h"
34 #include "sighandler.h"
38 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
39 /* If this container is empty, we’re done */
40 if (container->currently_focused == NULL)
43 /* Get the previous/next client or wrap around */
44 Client *candidate = NULL;
45 if (direction == D_UP) {
46 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
47 candidate = CIRCLEQ_LAST(&(container->clients));
49 else if (direction == D_DOWN) {
50 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
51 candidate = CIRCLEQ_FIRST(&(container->clients));
52 } else ELOG("Direction not implemented!\n");
54 /* If we could not switch, the container contains exactly one client. We return false */
55 if (candidate == container->currently_focused)
59 set_focus(conn, candidate, true);
64 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
66 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
68 LOG("Jumping to \"%s\"\n", mark);
71 TAILQ_FOREACH(ws, workspaces, workspaces)
72 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
73 if (current->mark == NULL || strcmp(current->mark, mark) != 0)
76 set_focus(conn, current, true);
77 workspace_show(conn, current->workspace->num + 1);
81 ELOG("No window with this mark found\n");
84 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
85 DLOG("focusing direction %d\n", direction);
87 int new_row = current_row,
88 new_col = current_col;
89 Container *container = CUR_CELL;
90 Workspace *t_ws = c_ws;
92 /* Makes sure new_col and new_row are within bounds of the new workspace */
93 #define CHECK_COLROW_BOUNDARIES \
95 if (new_col >= t_ws->cols) \
96 new_col = (t_ws->cols - 1); \
97 if (new_row >= t_ws->rows) \
98 new_row = (t_ws->rows - 1); \
101 /* There always is a container. If not, current_col or current_row is wrong */
102 assert(container != NULL);
104 if (container->workspace->fullscreen_client != NULL) {
105 LOG("You're in fullscreen mode. Forcing focus to operate on whole screens\n");
106 thing = THING_SCREEN;
109 /* For focusing screens, situation is different: we get the rect
110 * of the current screen, then get the screen which is on its
111 * right/left/bottom/top and just switch to the workspace on
112 * the target screen. */
113 if (thing == THING_SCREEN) {
114 Output *cs = c_ws->output;
116 Rect bounds = cs->rect;
118 if (direction == D_RIGHT)
119 bounds.x += bounds.width;
120 else if (direction == D_LEFT)
121 bounds.x -= bounds.width;
122 else if (direction == D_UP)
123 bounds.y -= bounds.height;
124 else bounds.y += bounds.height;
126 Output *target = get_output_containing(bounds.x, bounds.y);
127 if (target == NULL) {
128 DLOG("Target output NULL\n");
129 /* Wrap around if the target screen is out of bounds */
130 if (direction == D_RIGHT)
131 target = get_output_most(D_LEFT, cs);
132 else if (direction == D_LEFT)
133 target = get_output_most(D_RIGHT, cs);
134 else if (direction == D_UP)
135 target = get_output_most(D_DOWN, cs);
136 else target = get_output_most(D_UP, cs);
139 DLOG("Switching to ws %d\n", target->current_workspace + 1);
140 workspace_show(conn, target->current_workspace->num + 1);
144 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
145 if (direction == D_UP || direction == D_DOWN) {
146 if (thing == THING_WINDOW)
147 /* Let’s see if we can perform up/down focus in the current container */
148 if (focus_window_in_container(conn, container, direction))
151 if (direction == D_DOWN && cell_exists(t_ws, current_col, current_row+1))
152 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
153 else if (direction == D_UP && cell_exists(c_ws, current_col, current_row-1)) {
154 /* Set new_row as a sane default, but it may get overwritten in a second */
157 /* Search from the top to correctly handle rowspanned containers */
158 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
159 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
166 /* Let’s see if there is a screen down/up there to which we can switch */
167 DLOG("container is at %d with height %d\n", container->y, container->height);
169 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
170 if ((output = get_output_containing(container->x, destination_y)) == NULL) {
171 DLOG("Wrapping screen around vertically\n");
172 /* No screen found? Then wrap */
173 output = get_output_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->output);
175 t_ws = output->current_workspace;
176 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
179 CHECK_COLROW_BOUNDARIES;
181 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
182 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
183 DLOG("Cell empty, checking for colspanned client above...\n");
184 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
185 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
189 DLOG("Fixed it to new col %d\n", new_col);
194 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
195 DLOG("Cell still empty, checking for full cols above spanned width...\n");
196 DLOG("new_col = %d\n", new_col);
197 DLOG("colspan = %d\n", container->colspan);
198 for (int cols = new_col;
199 cols < container->col + container->colspan;
200 cols += t_ws->table[cols][new_row]->colspan) {
201 DLOG("candidate: new_row = %d, cols = %d\n", new_row, cols);
202 if (t_ws->table[cols][new_row]->currently_focused == NULL)
206 DLOG("Fixed it to new col %d\n", new_col);
210 } else if (direction == D_LEFT || direction == D_RIGHT) {
211 if (direction == D_RIGHT && cell_exists(t_ws, current_col+1, current_row))
212 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
213 else if (direction == D_LEFT && cell_exists(t_ws, current_col-1, current_row)) {
214 /* Set new_col as a sane default, but it may get overwritten in a second */
217 /* Search from the left to correctly handle colspanned containers */
218 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
219 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
226 /* Let’s see if there is a screen left/right here to which we can switch */
227 DLOG("container is at %d with width %d\n", container->x, container->width);
229 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
230 if ((output = get_output_containing(destination_x, container->y)) == NULL) {
231 DLOG("Wrapping screen around horizontally\n");
232 output = get_output_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->output);
234 t_ws = output->current_workspace;
235 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
238 CHECK_COLROW_BOUNDARIES;
240 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
241 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
242 DLOG("Cell empty, checking for rowspanned client above...\n");
243 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
244 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
248 DLOG("Fixed it to new row %d\n", new_row);
253 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
254 DLOG("Cell still empty, checking for full cols near full spanned height...\n");
255 DLOG("new_row = %d\n", new_row);
256 DLOG("rowspan = %d\n", container->rowspan);
257 for (int rows = new_row;
258 rows < container->row + container->rowspan;
259 rows += t_ws->table[new_col][rows]->rowspan) {
260 DLOG("candidate: new_col = %d, rows = %d\n", new_col, rows);
261 if (t_ws->table[new_col][rows]->currently_focused == NULL)
265 DLOG("Fixed it to new col %d\n", new_row);
271 ELOG("direction unhandled\n");
275 CHECK_COLROW_BOUNDARIES;
277 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
278 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
282 * Tries to move the window inside its current container.
284 * Returns true if the window could be moved, false otherwise.
287 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
288 direction_t direction) {
289 assert(client->container != NULL);
291 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
292 CIRCLEQ_NEXT(client, clients));
294 if (other == CIRCLEQ_END(&(client->container->clients)))
297 DLOG("i can do that\n");
298 /* We can move the client inside its current container */
299 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
300 if (direction == D_UP)
301 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
302 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
308 * Moves the current window or whole container to the given direction, creating a column/row if
312 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
313 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
314 (direction == D_LEFT ? "left" : "right"))));
315 /* Get current window */
316 Container *container = CUR_CELL,
319 /* There has to be a container, see focus_window() */
320 assert(container != NULL);
322 /* If there is no window or the dock window is focused, we’re done */
323 if (container->currently_focused == NULL ||
324 container->currently_focused->dock)
327 /* As soon as the client is moved away, the last focused client in the old
328 * container needs to get focus, if any. Therefore, we save it here. */
329 Client *current_client = container->currently_focused;
330 Client *to_focus = get_last_focused_client(conn, container, current_client);
332 if (to_focus == NULL) {
333 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
334 if (to_focus == NULL)
335 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
340 /* If we’re at the left-most position, move the rest of the table right */
341 if (current_col == 0) {
342 expand_table_cols_at_head(c_ws);
345 new = CUR_TABLE[--current_col][current_row];
348 if (current_col == (c_ws->cols-1))
349 expand_table_cols(c_ws);
351 new = CUR_TABLE[++current_col][current_row];
354 if (move_current_window_in_container(conn, current_client, D_UP))
357 /* if we’re at the up-most position, move the rest of the table down */
358 if (current_row == 0) {
359 expand_table_rows_at_head(c_ws);
362 new = CUR_TABLE[current_col][--current_row];
365 if (move_current_window_in_container(conn, current_client, D_DOWN))
368 if (current_row == (c_ws->rows-1))
369 expand_table_rows(c_ws);
371 new = CUR_TABLE[current_col][++current_row];
373 /* To make static analyzers happy: */
378 /* Remove it from the old container and put it into the new one */
379 client_remove_from_container(conn, current_client, container, true);
381 if (new->currently_focused != NULL)
382 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
383 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
384 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
386 /* Update data structures */
387 current_client->container = new;
388 current_client->workspace = new->workspace;
389 container->currently_focused = to_focus;
390 new->currently_focused = current_client;
392 Workspace *workspace = container->workspace;
394 /* delete all empty columns/rows */
395 cleanup_table(conn, workspace);
397 /* Fix colspan/rowspan if it’d overlap */
398 fix_colrowspan(conn, workspace);
400 render_workspace(conn, workspace->output, workspace);
403 set_focus(conn, current_client, true);
406 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
407 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
408 (direction == D_LEFT ? "left" : "right"))));
409 /* Get current window */
410 Container *container = CUR_CELL,
413 Container **old = &CUR_CELL;
415 /* There has to be a container, see focus_window() */
416 assert(container != NULL);
420 /* If we’re at the left-most position, move the rest of the table right */
421 if (current_col == 0) {
422 expand_table_cols_at_head(c_ws);
424 old = &CUR_TABLE[current_col+1][current_row];
426 new = CUR_TABLE[--current_col][current_row];
429 if (current_col == (c_ws->cols-1))
430 expand_table_cols(c_ws);
432 new = CUR_TABLE[++current_col][current_row];
435 /* if we’re at the up-most position, move the rest of the table down */
436 if (current_row == 0) {
437 expand_table_rows_at_head(c_ws);
439 old = &CUR_TABLE[current_col][current_row+1];
441 new = CUR_TABLE[current_col][--current_row];
444 if (current_row == (c_ws->rows-1))
445 expand_table_rows(c_ws);
447 new = CUR_TABLE[current_col][++current_row];
449 /* To make static analyzers happy: */
454 DLOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
456 /* Swap the containers */
461 new->col = container->col;
462 new->row = container->row;
464 CUR_CELL = container;
465 container->col = col;
466 container->row = row;
468 Workspace *workspace = container->workspace;
470 /* delete all empty columns/rows */
471 cleanup_table(conn, workspace);
473 /* Fix colspan/rowspan if it’d overlap */
474 fix_colrowspan(conn, workspace);
480 * "Snaps" the current container (not possible for windows, because it works at table base)
481 * to the given direction, that is, adjusts cellspan/rowspan
484 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
485 LOG("snapping container to direction %d\n", direction);
487 Container *container = CUR_CELL;
489 assert(container != NULL);
493 /* Snap to the left is actually a move to the left and then a snap right */
494 if (!cell_exists(container->workspace, container->col - 1, container->row) ||
495 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
496 ELOG("cannot snap to left - the cell is already used\n");
500 move_current_window(conn, D_LEFT);
501 snap_current_container(conn, D_RIGHT);
504 /* Check if the cell is used */
505 int new_col = container->col + container->colspan;
506 for (int i = 0; i < container->rowspan; i++)
507 if (!cell_exists(container->workspace, new_col, container->row + i) ||
508 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
509 ELOG("cannot snap to right - the cell is already used\n");
513 /* Check if there are other cells with rowspan, which are in our way.
514 * If so, reduce their rowspan. */
515 for (int i = container->row-1; i >= 0; i--) {
516 DLOG("we got cell %d, %d with rowspan %d\n",
517 new_col, i, CUR_TABLE[new_col][i]->rowspan);
518 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
519 CUR_TABLE[new_col][i]->rowspan--;
520 DLOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
523 container->colspan++;
527 if (!cell_exists(container->workspace, container->col, container->row - 1) ||
528 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
529 ELOG("cannot snap to top - the cell is already used\n");
533 move_current_window(conn, D_UP);
534 snap_current_container(conn, D_DOWN);
537 DLOG("snapping down\n");
538 int new_row = container->row + container->rowspan;
539 for (int i = 0; i < container->colspan; i++)
540 if (!cell_exists(container->workspace, container->col + i, new_row) ||
541 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
542 ELOG("cannot snap down - the cell is already used\n");
546 for (int i = container->col-1; i >= 0; i--) {
547 DLOG("we got cell %d, %d with colspan %d\n",
548 i, new_row, CUR_TABLE[i][new_row]->colspan);
549 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
550 CUR_TABLE[i][new_row]->colspan--;
551 DLOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
555 container->rowspan++;
558 /* To make static analyzers happy: */
566 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
567 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
568 Workspace *t_ws = workspace_get(workspace-1),
569 *old_ws = client->workspace;
571 LOG("moving floating\n");
573 workspace_initialize(t_ws, c_ws->output, false);
575 /* Check if there is already a fullscreen client on the destination workspace and
576 * stop moving if so. */
577 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
578 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
582 floating_assign_to_workspace(client, t_ws);
584 /* If we’re moving it to an invisible screen, we need to unmap it */
585 if (!workspace_is_visible(t_ws)) {
586 DLOG("This workspace is not visible, unmapping\n");
587 client_unmap(conn, client);
589 /* If this is not the case, we move the window to a workspace
590 * which is on another screen, so we also need to adjust its
592 DLOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
593 uint32_t relative_x = client->rect.x - old_ws->rect.x,
594 relative_y = client->rect.y - old_ws->rect.y;
595 DLOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
596 if (client->fullscreen) {
597 client_enter_fullscreen(conn, client, false);
598 memcpy(&(client->rect), &(t_ws->rect), sizeof(Rect));
600 client->rect.x = t_ws->rect.x + relative_x;
601 client->rect.y = t_ws->rect.y + relative_y;
602 DLOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
603 reposition_client(conn, client);
608 /* Configure the window above all tiling windows (or below a fullscreen
610 if (t_ws->fullscreen_client != NULL) {
611 uint32_t values[] = { t_ws->fullscreen_client->frame, XCB_STACK_MODE_BELOW };
612 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
615 SLIST_FOREACH(last_tiling, &(t_ws->focus_stack), focus_clients)
616 if (!client_is_floating(last_tiling))
618 if (last_tiling != SLIST_END(&(t_ws->focus_stack))) {
619 uint32_t values[] = { last_tiling->frame, XCB_STACK_MODE_ABOVE };
620 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
628 if (workspace_is_visible(t_ws)) {
629 client_warp_pointer_into(conn, client);
630 set_focus(conn, client, true);
635 * Moves the currently selected window to the given workspace
638 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
639 LOG("Moving current window to workspace %d\n", workspace);
641 Container *container = CUR_CELL;
643 assert(container != NULL);
645 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
646 Workspace *t_ws = workspace_get(workspace-1);
648 Client *current_client = container->currently_focused;
649 if (current_client == NULL) {
650 ELOG("No currently focused client in current container.\n");
653 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
654 if (to_focus == NULL)
655 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
657 workspace_initialize(t_ws, container->workspace->output, false);
658 /* Check if there is already a fullscreen client on the destination workspace and
659 * stop moving if so. */
660 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
661 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
665 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
667 assert(to_container != NULL);
669 client_remove_from_container(conn, current_client, container, true);
670 if (container->workspace->fullscreen_client == current_client)
671 container->workspace->fullscreen_client = NULL;
673 /* TODO: insert it to the correct position */
674 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
676 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
679 current_client->container = to_container;
680 current_client->workspace = to_container->workspace;
681 container->currently_focused = to_focus;
682 to_container->currently_focused = current_client;
684 /* If we’re moving it to an invisible screen, we need to unmap it */
685 if (!workspace_is_visible(to_container->workspace)) {
686 DLOG("This workspace is not visible, unmapping\n");
687 client_unmap(conn, current_client);
689 if (current_client->fullscreen) {
690 DLOG("Calling client_enter_fullscreen again\n");
691 client_enter_fullscreen(conn, current_client, false);
695 /* delete all empty columns/rows */
696 cleanup_table(conn, container->workspace);
700 if (workspace_is_visible(to_container->workspace)) {
701 client_warp_pointer_into(conn, current_client);
702 set_focus(conn, current_client, true);
707 * Brings the given window class / title to the current workspace.
710 static void bring_window_here(xcb_connection_t *conn, const char *arguments) {
714 /* The first character is a quote, this was checked before */
715 classtitle = sstrdup(arguments+1);
716 /* The last character is a quote, we just set it to NULL */
717 classtitle[strlen(classtitle)-1] = '\0';
719 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
721 ELOG("No matching client found.\n");
725 /* This is the workspace num that we are on */
726 int current_workspace_num = c_ws->output->current_workspace->num + 1;
727 /* This is the workspace num that the client is on */
728 int clients_workspace_num = client->workspace->num + 1;
731 workspace_show(conn, clients_workspace_num);
732 set_focus(conn, client, true);
733 if (client_is_floating(client))
734 move_floating_window_to_workspace(conn, client, current_workspace_num);
735 else move_current_window_to_workspace(conn, current_workspace_num);
736 workspace_show(conn, current_workspace_num);
737 set_focus(conn, client, true);
742 * Jumps to the given window class / title.
743 * Title is matched using strstr, that is, matches if it appears anywhere
744 * in the string. Regular expressions seem to be a bit overkill here. However,
745 * if we need them for something else somewhen, we may introduce them here, too.
748 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
752 /* The first character is a quote, this was checked before */
753 classtitle = sstrdup(arguments+1);
754 /* The last character is a quote, we just set it to NULL */
755 classtitle[strlen(classtitle)-1] = '\0';
757 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
759 ELOG("No matching client found.\n");
764 workspace_show(conn, client->workspace->num + 1);
765 set_focus(conn, client, true);
769 * Jump directly to the specified workspace, row and col.
770 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
773 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
777 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
778 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
780 /* No match? Either no arguments were specified, or no numbers */
782 ELOG("At least one valid argument required\n");
786 /* Move to the target workspace */
787 workspace_show(conn, ws);
792 DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
794 /* Move to row/col */
795 if (row >= c_ws->rows)
796 row = c_ws->rows - 1;
797 if (col >= c_ws->cols)
798 col = c_ws->cols - 1;
800 DLOG("Jumping to col %d, row %d\n", col, row);
801 if (c_ws->table[col][row]->currently_focused != NULL)
802 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
806 * Travels the focus stack by the given number of times (or once, if no argument
807 * was specified). That is, selects the window you were in before you focused
808 * the current window.
810 * The special values 'floating' (select the next floating window), 'tiling'
811 * (select the next tiling window), 'ft' (if the current window is floating,
812 * select the next tiling window and vice-versa) are also valid
815 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
816 /* Start count at -1 to always skip the first element */
817 int times, count = -1;
819 bool floating_criteria;
821 /* Either it’s one of the special values… */
822 if (strcasecmp(arguments, "floating") == 0) {
823 floating_criteria = true;
824 } else if (strcasecmp(arguments, "tiling") == 0) {
825 floating_criteria = false;
826 } else if (strcasecmp(arguments, "ft") == 0) {
827 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
828 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
829 ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
833 floating_criteria = !client_is_floating(last_focused);
835 /* …or a number was specified */
836 if (sscanf(arguments, "%u", ×) != 1) {
837 ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
841 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
842 if (++count < times) {
848 set_focus(conn, current, true);
854 /* Select the next client matching the criteria parsed above */
855 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
856 if (client_is_floating(current) == floating_criteria) {
857 set_focus(conn, current, true);
863 * Switch to next or previous existing workspace
866 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
867 Workspace *ws = c_ws;
869 if (direction == 'n') {
871 ws = TAILQ_NEXT(ws, workspaces);
873 if (ws == TAILQ_END(workspaces))
874 ws = TAILQ_FIRST(workspaces);
879 if (ws->output == NULL)
882 workspace_show(conn, ws->num + 1);
885 } else if (direction == 'p') {
887 ws = TAILQ_PREV(ws, workspaces_head, workspaces);
889 if (ws == TAILQ_END(workspaces))
890 ws = TAILQ_LAST(workspaces, workspaces_head);
895 if (ws->output == NULL)
898 workspace_show(conn, ws->num + 1);
904 static void parse_move_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
905 if (!client_is_floating(last_focused)) {
906 LOG("You can only move floating clients with the \"move\" command\n");
910 DLOG("Moving a floating client\n");
911 if (STARTS_WITH(command, "left")) {
912 command += strlen("left");
913 last_focused->rect.x -= atoi(command);
914 } else if (STARTS_WITH(command, "right")) {
915 command += strlen("right");
916 last_focused->rect.x += atoi(command);
917 } else if (STARTS_WITH(command, "top")) {
918 command += strlen("top");
919 last_focused->rect.y -= atoi(command);
920 } else if (STARTS_WITH(command, "bottom")) {
921 command += strlen("bottom");
922 last_focused->rect.y += atoi(command);
924 ELOG("Syntax: move <left|right|top|bottom> <pixels>\n");
927 /* resize_client flushes */
928 resize_client(conn, last_focused);
931 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
933 resize_orientation_t orientation = O_VERTICAL;
934 Container *con = last_focused->container;
935 Workspace *ws = last_focused->workspace;
937 if (client_is_floating(last_focused)) {
938 DLOG("Resizing a floating client\n");
939 if (STARTS_WITH(command, "left")) {
940 command += strlen("left");
941 last_focused->rect.width += atoi(command);
942 last_focused->rect.x -= atoi(command);
943 } else if (STARTS_WITH(command, "right")) {
944 command += strlen("right");
945 last_focused->rect.width += atoi(command);
946 } else if (STARTS_WITH(command, "top")) {
947 command += strlen("top");
948 last_focused->rect.height += atoi(command);
949 last_focused->rect.y -= atoi(command);
950 } else if (STARTS_WITH(command, "bottom")) {
951 command += strlen("bottom");
952 last_focused->rect.height += atoi(command);
954 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
958 /* resize_client flushes */
959 resize_client(conn, last_focused);
964 if (STARTS_WITH(command, "left")) {
967 first = con->col - 1;
969 command += strlen("left");
970 } else if (STARTS_WITH(command, "right")) {
971 first = con->col + (con->colspan - 1);
972 DLOG("column %d\n", first);
974 if (!cell_exists(ws, first, con->row) ||
975 (first == (ws->cols-1)))
979 command += strlen("right");
980 } else if (STARTS_WITH(command, "top")) {
983 first = con->row - 1;
985 orientation = O_HORIZONTAL;
986 command += strlen("top");
987 } else if (STARTS_WITH(command, "bottom")) {
988 first = con->row + (con->rowspan - 1);
989 if (!cell_exists(ws, con->col, first) ||
990 (first == (ws->rows-1)))
994 orientation = O_HORIZONTAL;
995 command += strlen("bottom");
997 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
1001 int pixels = atoi(command);
1005 resize_container(conn, ws, first, second, orientation, pixels);
1009 * Parses a command, see file CMDMODE for more information
1012 void parse_command(xcb_connection_t *conn, const char *command) {
1013 LOG("--- parsing command \"%s\" ---\n", command);
1014 /* Get the first client from focus stack because floating clients are not
1015 * in any container, therefore CUR_CELL is not appropriate. */
1016 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
1017 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
1018 last_focused = NULL;
1020 /* Hmm, just to be sure */
1021 if (command[0] == '\0')
1024 /* Is it an <exec>? Then execute the given command. */
1025 if (STARTS_WITH(command, "exec ")) {
1026 LOG("starting \"%s\"\n", command + strlen("exec "));
1027 start_application(command+strlen("exec "));
1031 if (STARTS_WITH(command, "mark")) {
1032 if (last_focused == NULL) {
1033 ELOG("There is no window to mark\n");
1036 const char *rest = command + strlen("mark");
1037 while (*rest == ' ')
1039 if (*rest == '\0') {
1040 DLOG("interactive mark starting\n");
1041 start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
1043 LOG("mark with \"%s\"\n", rest);
1044 client_mark(conn, last_focused, rest);
1049 if (STARTS_WITH(command, "goto")) {
1050 const char *rest = command + strlen("goto");
1051 while (*rest == ' ')
1053 if (*rest == '\0') {
1054 DLOG("interactive go to mark starting\n");
1055 start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
1057 LOG("go to \"%s\"\n", rest);
1058 jump_to_mark(conn, rest);
1063 if (STARTS_WITH(command, "stack-limit ")) {
1064 if (last_focused == NULL || client_is_floating(last_focused)) {
1065 ELOG("No container focused\n");
1068 const char *rest = command + strlen("stack-limit ");
1069 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
1070 last_focused->container->stack_limit = STACK_LIMIT_ROWS;
1071 rest += strlen("rows ");
1072 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
1073 last_focused->container->stack_limit = STACK_LIMIT_COLS;
1074 rest += strlen("cols ");
1076 ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
1080 last_focused->container->stack_limit_value = atoi(rest);
1081 if (last_focused->container->stack_limit_value == 0)
1082 last_focused->container->stack_limit = STACK_LIMIT_NONE;
1087 if (STARTS_WITH(command, "resize ")) {
1088 if (last_focused == NULL)
1090 const char *rest = command + strlen("resize ");
1091 parse_resize_command(conn, last_focused, rest);
1095 if (STARTS_WITH(command, "move ")) {
1096 if (last_focused == NULL)
1098 const char *rest = command + strlen("move ");
1099 parse_move_command(conn, last_focused, rest);
1103 if (STARTS_WITH(command, "mode ")) {
1104 const char *rest = command + strlen("mode ");
1105 switch_mode(conn, rest);
1109 /* Is it an <exit>? */
1110 if (STARTS_WITH(command, "exit")) {
1111 LOG("User issued exit-command, exiting without error.\n");
1112 restore_geometry(global_conn);
1117 /* Is it a <reload>? */
1118 if (STARTS_WITH(command, "reload")) {
1119 load_configuration(conn, NULL, true);
1120 render_layout(conn);
1121 /* Send an IPC event just in case the ws names have changed */
1122 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1126 /* Is it <restart>? Then restart in place. */
1127 if (STARTS_WITH(command, "restart")) {
1131 if (STARTS_WITH(command, "kill")) {
1132 if (last_focused == NULL) {
1133 ELOG("There is no window to kill\n");
1137 LOG("Killing current window\n");
1138 client_kill(conn, last_focused);
1142 if (STARTS_WITH(command, "bring ")) {
1143 const char *arguments = command + strlen("bring ");
1144 if (arguments[0] == '"')
1145 bring_window_here(conn, arguments);
1149 /* Is it a jump to a specified workspace, row, col? */
1150 if (STARTS_WITH(command, "jump ")) {
1151 const char *arguments = command + strlen("jump ");
1152 if (arguments[0] == '"')
1153 jump_to_window(conn, arguments);
1154 else jump_to_container(conn, arguments);
1158 /* Should we travel the focus stack? */
1159 if (STARTS_WITH(command, "focus")) {
1160 const char *arguments = command + strlen("focus ");
1161 travel_focus_stack(conn, arguments);
1165 /* Is it 'f' for fullscreen, or 'fg' for fullscreen_global? */
1166 if (command[0] == 'f') {
1167 if (last_focused == NULL)
1169 if (command[1] == 'g')
1170 client_toggle_fullscreen_global(conn, last_focused);
1172 client_toggle_fullscreen(conn, last_focused);
1176 /* Is it just 's' for stacking or 'd' for default? */
1177 if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1178 if (last_focused != NULL && client_is_floating(last_focused)) {
1179 ELOG("not switching, this is a floating client\n");
1182 LOG("Switching mode for current container\n");
1183 int new_mode = MODE_DEFAULT;
1184 if (command[0] == 's' && CUR_CELL->mode != MODE_STACK)
1185 new_mode = MODE_STACK;
1186 if (command[0] == 'T' && CUR_CELL->mode != MODE_TABBED)
1187 new_mode = MODE_TABBED;
1188 switch_layout_mode(conn, CUR_CELL, new_mode);
1192 /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1193 /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1194 if (command[0] == 'b') {
1195 if (last_focused == NULL) {
1196 ELOG("No window focused, cannot change border type\n");
1200 char com = command[1];
1201 if (command[1] == 't') {
1202 if (last_focused->titlebar_position == TITLEBAR_TOP &&
1203 !last_focused->borderless)
1205 else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1206 !last_focused->borderless)
1211 client_change_border(conn, last_focused, com);
1215 if (command[0] == 'H') {
1216 LOG("Hiding all floating windows\n");
1217 floating_toggle_hide(conn, c_ws);
1221 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1223 /* Is it a <with>? */
1224 if (command[0] == 'w') {
1226 /* TODO: implement */
1227 if (command[0] == 'c') {
1228 with = WITH_CONTAINER;
1230 } else if (command[0] == 'w') {
1231 with = WITH_WORKSPACE;
1233 } else if (command[0] == 's') {
1237 ELOG("not yet implemented.\n");
1242 /* Is it 't' for toggle tiling/floating? */
1243 if (command[0] == 't') {
1244 if (with == WITH_WORKSPACE) {
1245 c_ws->auto_float = !c_ws->auto_float;
1246 LOG("autofloat is now %d\n", c_ws->auto_float);
1249 if (last_focused == NULL) {
1250 ELOG("Cannot toggle tiling/floating: workspace empty\n");
1254 Workspace *ws = last_focused->workspace;
1256 if (last_focused->fullscreen)
1257 client_leave_fullscreen(conn, last_focused);
1259 toggle_floating_mode(conn, last_focused, false);
1260 /* delete all empty columns/rows */
1261 cleanup_table(conn, ws);
1263 /* Fix colspan/rowspan if it’d overlap */
1264 fix_colrowspan(conn, ws);
1266 render_workspace(conn, ws->output, ws);
1268 /* Re-focus the client because cleanup_table sets the focus to the last
1269 * focused client inside a container only. */
1270 set_focus(conn, last_focused, true);
1275 /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1276 if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1277 next_previous_workspace(conn, command[0]);
1281 /* It’s a normal <cmd> */
1283 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1284 direction_t direction;
1285 int times = strtol(command, &rest, 10);
1287 ELOG("Invalid command (\"%s\")\n", command);
1291 if (*rest == '\0') {
1292 /* No rest? This was a workspace number, not a times specification */
1293 workspace_show(conn, times);
1297 if (*rest == 'm' || *rest == 's') {
1298 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1302 int workspace = strtol(rest, &rest, 10);
1305 ELOG("Invalid command (\"%s\")\n", command);
1309 if (*rest == '\0') {
1310 if (last_focused != NULL && client_is_floating(last_focused))
1311 move_floating_window_to_workspace(conn, last_focused, workspace);
1312 else move_current_window_to_workspace(conn, workspace);
1316 if (last_focused == NULL) {
1317 ELOG("Not performing (no window found)\n");
1321 if (client_is_floating(last_focused) &&
1322 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1323 ELOG("Not performing (floating)\n");
1327 /* Now perform action to <where> */
1328 while (*rest != '\0') {
1331 else if (*rest == 'j')
1333 else if (*rest == 'k')
1335 else if (*rest == 'l')
1336 direction = D_RIGHT;
1338 ELOG("unknown direction: %c\n", *rest);
1343 if (action == ACTION_FOCUS) {
1344 if (with == WITH_SCREEN) {
1345 focus_thing(conn, direction, THING_SCREEN);
1348 if (client_is_floating(last_focused)) {
1349 floating_focus_direction(conn, last_focused, direction);
1352 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1356 if (action == ACTION_MOVE) {
1357 if (with == WITH_SCREEN) {
1358 /* TODO: this should swap the screen’s contents
1359 * (e.g. all workspaces) with the next/previous/…
1361 ELOG("Not yet implemented\n");
1364 if (client_is_floating(last_focused)) {
1365 floating_move(conn, last_focused, direction);
1368 if (with == WITH_WINDOW)
1369 move_current_window(conn, direction);
1370 else move_current_container(conn, direction);
1374 if (action == ACTION_SNAP) {
1375 if (with == WITH_SCREEN) {
1376 ELOG("You cannot snap a screen (it makes no sense).\n");
1379 snap_current_container(conn, direction);