4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
28 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
29 /* If this container is empty, we’re done */
30 if (container->currently_focused == NULL)
33 /* Get the previous/next client or wrap around */
34 Client *candidate = NULL;
35 if (direction == D_UP) {
36 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
37 candidate = CIRCLEQ_LAST(&(container->clients));
39 else if (direction == D_DOWN) {
40 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
41 candidate = CIRCLEQ_FIRST(&(container->clients));
42 } else LOG("Direction not implemented!\n");
44 /* If we could not switch, the container contains exactly one client. We return false */
45 if (candidate == container->currently_focused)
49 set_focus(conn, candidate, true);
54 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
56 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
57 LOG("focusing direction %d\n", direction);
59 int new_row = current_row,
60 new_col = current_col;
62 Container *container = CUR_CELL;
63 Workspace *t_ws = c_ws;
65 /* There always is a container. If not, current_col or current_row is wrong */
66 assert(container != NULL);
68 if (container->workspace->fullscreen_client != NULL) {
69 LOG("You're in fullscreen mode. Won't switch focus\n");
73 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
74 if (direction == D_UP || direction == D_DOWN) {
75 if (thing == THING_WINDOW)
76 /* Let’s see if we can perform up/down focus in the current container */
77 if (focus_window_in_container(conn, container, direction))
80 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
81 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
82 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
83 /* Set new_row as a sane default, but it may get overwritten in a second */
86 /* Search from the top to correctly handle rowspanned containers */
87 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
88 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
95 /* Let’s see if there is a screen down/up there to which we can switch */
96 LOG("container is at %d with height %d\n", container->y, container->height);
98 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
99 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
100 LOG("Wrapping screen around vertically\n");
101 /* No screen found? Then wrap */
102 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
104 t_ws = &(workspaces[screen->current_workspace]);
105 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
107 } else if (direction == D_LEFT || direction == D_RIGHT) {
108 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
109 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
110 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
111 /* Set new_col as a sane default, but it may get overwritten in a second */
114 /* Search from the left to correctly handle colspanned containers */
115 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
116 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
123 /* Let’s see if there is a screen left/right here to which we can switch */
124 LOG("container is at %d with width %d\n", container->x, container->width);
126 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
127 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
128 LOG("Wrapping screen around horizontally\n");
129 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
131 t_ws = &(workspaces[screen->current_workspace]);
132 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
135 LOG("direction unhandled\n");
139 /* Bounds checking */
140 if (new_col >= t_ws->cols)
141 new_col = (t_ws->cols - 1);
142 if (new_row >= t_ws->rows)
143 new_row = (t_ws->rows - 1);
145 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
146 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
150 * Tries to move the window inside its current container.
152 * Returns true if the window could be moved, false otherwise.
155 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
156 direction_t direction) {
157 assert(client->container != NULL);
159 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
160 CIRCLEQ_NEXT(client, clients));
162 if (other == CIRCLEQ_END(&(client->container->clients)))
165 LOG("i can do that\n");
166 /* We can move the client inside its current container */
167 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
168 if (direction == D_UP)
169 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
170 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
176 * Moves the current window or whole container to the given direction, creating a column/row if
180 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
181 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
182 (direction == D_LEFT ? "left" : "right"))));
183 /* Get current window */
184 Container *container = CUR_CELL,
187 /* There has to be a container, see focus_window() */
188 assert(container != NULL);
190 /* If there is no window or the dock window is focused, we’re done */
191 if (container->currently_focused == NULL ||
192 container->currently_focused->dock)
195 /* As soon as the client is moved away, the last focused client in the old
196 * container needs to get focus, if any. Therefore, we save it here. */
197 Client *current_client = container->currently_focused;
198 Client *to_focus = get_last_focused_client(conn, container, current_client);
200 if (to_focus == NULL) {
201 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
202 if (to_focus == NULL)
203 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
208 /* If we’re at the left-most position, move the rest of the table right */
209 if (current_col == 0) {
210 expand_table_cols_at_head(c_ws);
213 new = CUR_TABLE[--current_col][current_row];
216 if (current_col == (c_ws->cols-1))
217 expand_table_cols(c_ws);
219 new = CUR_TABLE[++current_col][current_row];
222 if (move_current_window_in_container(conn, current_client, D_UP))
225 /* if we’re at the up-most position, move the rest of the table down */
226 if (current_row == 0) {
227 expand_table_rows_at_head(c_ws);
230 new = CUR_TABLE[current_col][--current_row];
233 if (move_current_window_in_container(conn, current_client, D_DOWN))
236 if (current_row == (c_ws->rows-1))
237 expand_table_rows(c_ws);
239 new = CUR_TABLE[current_col][++current_row];
243 /* Remove it from the old container and put it into the new one */
244 client_remove_from_container(conn, current_client, container);
246 if (new->currently_focused != NULL)
247 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
248 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
249 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
251 /* Update data structures */
252 current_client->container = new;
253 container->currently_focused = to_focus;
254 new->currently_focused = current_client;
256 Workspace *workspace = container->workspace;
258 /* delete all empty columns/rows */
259 cleanup_table(conn, workspace);
261 /* Fix colspan/rowspan if it’d overlap */
262 fix_colrowspan(conn, workspace);
266 set_focus(conn, current_client, true);
269 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
270 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
271 (direction == D_LEFT ? "left" : "right"))));
272 /* Get current window */
273 Container *container = CUR_CELL,
276 Container **old = &CUR_CELL;
278 /* There has to be a container, see focus_window() */
279 assert(container != NULL);
283 /* If we’re at the left-most position, move the rest of the table right */
284 if (current_col == 0) {
285 expand_table_cols_at_head(c_ws);
287 old = &CUR_TABLE[current_col+1][current_row];
289 new = CUR_TABLE[--current_col][current_row];
292 if (current_col == (c_ws->cols-1))
293 expand_table_cols(c_ws);
295 new = CUR_TABLE[++current_col][current_row];
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);
302 old = &CUR_TABLE[current_col][current_row+1];
304 new = CUR_TABLE[current_col][--current_row];
307 if (current_row == (c_ws->rows-1))
308 expand_table_rows(c_ws);
310 new = CUR_TABLE[current_col][++current_row];
314 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
316 /* Swap the containers */
321 new->col = container->col;
322 new->row = container->row;
324 CUR_CELL = container;
325 container->col = col;
326 container->row = row;
328 Workspace *workspace = container->workspace;
330 /* delete all empty columns/rows */
331 cleanup_table(conn, workspace);
333 /* Fix colspan/rowspan if it’d overlap */
334 fix_colrowspan(conn, workspace);
340 * "Snaps" the current container (not possible for windows, because it works at table base)
341 * to the given direction, that is, adjusts cellspan/rowspan
344 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
345 LOG("snapping container to direction %d\n", direction);
347 Container *container = CUR_CELL;
349 assert(container != NULL);
353 /* Snap to the left is actually a move to the left and then a snap right */
354 if (!cell_exists(container->col - 1, container->row) ||
355 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
356 LOG("cannot snap to left - the cell is already used\n");
360 move_current_window(conn, D_LEFT);
361 snap_current_container(conn, D_RIGHT);
364 /* Check if the cell is used */
365 int new_col = container->col + container->colspan;
366 if (!cell_exists(new_col, container->row) ||
367 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
368 LOG("cannot snap to right - the cell is already used\n");
372 /* Check if there are other cells with rowspan, which are in our way.
373 * If so, reduce their rowspan. */
374 for (int i = container->row-1; i >= 0; i--) {
375 LOG("we got cell %d, %d with rowspan %d\n",
376 new_col, i, CUR_TABLE[new_col][i]->rowspan);
377 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
378 CUR_TABLE[new_col][i]->rowspan--;
379 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
382 container->colspan++;
386 if (!cell_exists(container->col, container->row - 1) ||
387 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
388 LOG("cannot snap to top - the cell is already used\n");
392 move_current_window(conn, D_UP);
393 snap_current_container(conn, D_DOWN);
396 LOG("snapping down\n");
397 int new_row = container->row + container->rowspan;
398 if (!cell_exists(container->col, new_row) ||
399 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
400 LOG("cannot snap down - the cell is already used\n");
404 for (int i = container->col-1; i >= 0; i--) {
405 LOG("we got cell %d, %d with colspan %d\n",
406 i, new_row, CUR_TABLE[i][new_row]->colspan);
407 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
408 CUR_TABLE[i][new_row]->colspan--;
409 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
413 container->rowspan++;
422 * Moves the currently selected window to the given workspace
425 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
426 LOG("Moving current window to workspace %d\n", workspace);
428 Container *container = CUR_CELL;
430 assert(container != NULL);
432 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
433 Workspace *t_ws = &(workspaces[workspace-1]);
435 Client *current_client = container->currently_focused;
436 if (current_client == NULL) {
437 LOG("No currently focused client in current container.\n");
440 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
441 if (to_focus == NULL)
442 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
444 if (t_ws->screen == NULL) {
445 LOG("initializing new workspace, setting num to %d\n", workspace-1);
446 t_ws->screen = container->workspace->screen;
447 /* Copy the dimensions from the virtual screen */
448 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
450 /* Check if there is already a fullscreen client on the destination workspace and
451 * stop moving if so. */
452 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
453 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
458 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
460 assert(to_container != NULL);
462 client_remove_from_container(conn, current_client, container);
463 if (container->workspace->fullscreen_client == current_client)
464 container->workspace->fullscreen_client = NULL;
466 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
467 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
468 if (current_client->fullscreen)
469 t_ws->fullscreen_client = current_client;
472 current_client->container = to_container;
473 container->currently_focused = to_focus;
474 to_container->currently_focused = current_client;
476 /* If we’re moving it to an invisible screen, we need to unmap it */
477 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
478 LOG("This workspace is not visible, unmapping\n");
479 xcb_unmap_window(conn, current_client->frame);
482 /* delete all empty columns/rows */
483 cleanup_table(conn, container->workspace);
489 * Switches to the given workspace
492 void show_workspace(xcb_connection_t *conn, int workspace) {
494 bool need_warp = false;
495 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
496 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
497 Workspace *t_ws = &(workspaces[workspace-1]);
499 LOG("show_workspace(%d)\n", workspace);
501 /* Store current_row/current_col */
502 c_ws->current_row = current_row;
503 c_ws->current_col = current_col;
505 /* Check if the workspace has not been used yet */
506 if (t_ws->screen == NULL) {
507 LOG("initializing new workspace, setting num to %d\n", workspace);
508 t_ws->screen = c_ws->screen;
509 /* Copy the dimensions from the virtual screen */
510 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
513 if (c_ws->screen != t_ws->screen) {
514 /* We need to switch to the other screen first */
515 LOG("moving over to other screen.\n");
517 /* Store the old client */
518 Client *old_client = CUR_CELL->currently_focused;
520 c_ws = &(workspaces[t_ws->screen->current_workspace]);
521 current_col = c_ws->current_col;
522 current_row = c_ws->current_row;
523 if (CUR_CELL->currently_focused != NULL)
526 Rect *dims = &(c_ws->screen->rect);
527 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
528 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
531 /* Re-decorate the old client, it’s not focused anymore */
532 if ((old_client != NULL) && !old_client->dock)
533 redecorate_window(conn, old_client);
534 else xcb_flush(conn);
537 /* Check if we need to change something or if we’re already there */
538 if (c_ws->screen->current_workspace == (workspace-1)) {
539 if (CUR_CELL->currently_focused != NULL) {
540 set_focus(conn, CUR_CELL->currently_focused, true);
542 client_warp_pointer_into(conn, CUR_CELL->currently_focused);
549 t_ws->screen->current_workspace = workspace-1;
551 /* Unmap all clients of the current workspace */
552 unmap_workspace(conn, c_ws);
554 c_ws = &workspaces[workspace-1];
555 current_row = c_ws->current_row;
556 current_col = c_ws->current_col;
557 LOG("new current row = %d, current col = %d\n", current_row, current_col);
559 ignore_enter_notify_forall(conn, c_ws, true);
561 /* Map all clients on the new workspace */
563 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
564 xcb_map_window(conn, client->frame);
566 /* Map all stack windows, if any */
567 struct Stack_Window *stack_win;
568 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
569 if (stack_win->container->workspace == c_ws)
570 xcb_map_window(conn, stack_win->window);
572 ignore_enter_notify_forall(conn, c_ws, false);
574 /* Restore focus on the new workspace */
575 if (CUR_CELL->currently_focused != NULL) {
576 set_focus(conn, CUR_CELL->currently_focused, true);
578 client_warp_pointer_into(conn, CUR_CELL->currently_focused);
581 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
587 * Jumps to the given window class / title.
588 * Title is matched using strstr, that is, matches if it appears anywhere
589 * in the string. Regular expressions seem to be a bit overkill here. However,
590 * if we need them for something else somewhen, we may introduce them here, too.
593 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
597 /* The first character is a quote, this was checked before */
598 classtitle = sstrdup(arguments+1);
599 /* The last character is a quote, we just set it to NULL */
600 classtitle[strlen(classtitle)-1] = '\0';
602 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
604 LOG("No matching client found.\n");
609 set_focus(conn, client, true);
613 * Jump directly to the specified workspace, row and col.
614 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
617 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
621 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
622 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
624 /* No match? Either no arguments were specified, or no numbers */
626 LOG("At least one valid argument required\n");
630 /* Move to the target workspace */
631 show_workspace(conn, ws);
636 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
638 /* Move to row/col */
639 if (row >= c_ws->rows)
640 row = c_ws->rows - 1;
641 if (col >= c_ws->cols)
642 col = c_ws->cols - 1;
644 LOG("Jumping to col %d, row %d\n", col, row);
645 if (c_ws->table[col][row]->currently_focused != NULL)
646 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
650 * Travels the focus stack by the given number of times (or once, if no argument
651 * was specified). That is, selects the window you were in before you focused
652 * the current window.
655 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
656 /* Start count at -1 to always skip the first element */
657 int times, count = -1;
660 if (sscanf(arguments, "%u", ×) != 1) {
661 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
665 Workspace *ws = CUR_CELL->workspace;
667 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
668 if (++count < times) {
674 set_focus(conn, current, true);
680 * Parses a command, see file CMDMODE for more information
683 void parse_command(xcb_connection_t *conn, const char *command) {
684 LOG("--- parsing command \"%s\" ---\n", command);
685 /* Hmm, just to be sure */
686 if (command[0] == '\0')
689 /* Is it an <exec>? Then execute the given command. */
690 if (STARTS_WITH(command, "exec ")) {
691 LOG("starting \"%s\"\n", command + strlen("exec "));
692 start_application(command+strlen("exec "));
696 /* Is it an <exit>? */
697 if (STARTS_WITH(command, "exit")) {
698 LOG("User issued exit-command, exiting without error.\n");
702 /* Is it <restart>? Then restart in place. */
703 if (STARTS_WITH(command, "restart")) {
704 LOG("restarting \"%s\"...\n", start_argv[0]);
705 execvp(start_argv[0], start_argv);
709 if (STARTS_WITH(command, "kill")) {
710 if (CUR_CELL->currently_focused == NULL) {
711 LOG("There is no window to kill\n");
715 LOG("Killing current window\n");
716 client_kill(conn, CUR_CELL->currently_focused);
720 /* Is it a jump to a specified workspae, row, col? */
721 if (STARTS_WITH(command, "jump ")) {
722 const char *arguments = command + strlen("jump ");
723 if (arguments[0] == '"')
724 jump_to_window(conn, arguments);
725 else jump_to_container(conn, arguments);
729 /* Should we travel the focus stack? */
730 if (STARTS_WITH(command, "focus")) {
731 const char *arguments = command + strlen("focus");
732 travel_focus_stack(conn, arguments);
736 /* Is it 'f' for fullscreen? */
737 if (command[0] == 'f') {
738 if (CUR_CELL->currently_focused == NULL)
740 toggle_fullscreen(conn, CUR_CELL->currently_focused);
744 /* Is it just 's' for stacking or 'd' for default? */
745 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
746 LOG("Switching mode for current container\n");
747 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
751 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
753 /* Is it a <with>? */
754 if (command[0] == 'w') {
756 /* TODO: implement */
757 if (command[0] == 'c') {
758 with = WITH_CONTAINER;
761 LOG("not yet implemented.\n");
766 /* It's a normal <cmd> */
768 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
769 direction_t direction;
770 int times = strtol(command, &rest, 10);
772 LOG("Invalid command (\"%s\")\n", command);
777 /* No rest? This was a tag number, not a times specification */
778 show_workspace(conn, times);
782 if (*rest == 'm' || *rest == 's') {
783 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
787 int workspace = strtol(rest, &rest, 10);
790 LOG("Invalid command (\"%s\")\n", command);
795 move_current_window_to_workspace(conn, workspace);
799 /* Now perform action to <where> */
800 while (*rest != '\0') {
803 else if (*rest == 'j')
805 else if (*rest == 'k')
807 else if (*rest == 'l')
810 LOG("unknown direction: %c\n", *rest);
814 if (action == ACTION_FOCUS)
815 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
816 else if (action == ACTION_MOVE) {
817 if (with == WITH_WINDOW)
818 move_current_window(conn, direction);
819 else move_current_container(conn, direction);
821 else if (action == ACTION_SNAP)
822 snap_current_container(conn, direction);
827 LOG("--- done ---\n");