4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
27 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
28 /* If this container is empty, we’re done */
29 if (container->currently_focused == NULL)
32 /* Get the previous/next client or wrap around */
33 Client *candidate = NULL;
34 if (direction == D_UP) {
35 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
36 candidate = CIRCLEQ_LAST(&(container->clients));
38 else if (direction == D_DOWN) {
39 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
40 candidate = CIRCLEQ_FIRST(&(container->clients));
41 } else LOG("Direction not implemented!\n");
43 /* If we could not switch, the container contains exactly one client. We return false */
44 if (candidate == container->currently_focused)
48 set_focus(conn, candidate, true);
53 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
55 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
56 LOG("focusing direction %d\n", direction);
58 int new_row = current_row,
59 new_col = current_col;
61 Container *container = CUR_CELL;
62 Workspace *t_ws = c_ws;
64 /* There always is a container. If not, current_col or current_row is wrong */
65 assert(container != NULL);
67 if (container->workspace->fullscreen_client != NULL) {
68 LOG("You're in fullscreen mode. Won't switch focus\n");
72 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
73 if (direction == D_UP || direction == D_DOWN) {
74 if (thing == THING_WINDOW)
75 /* Let’s see if we can perform up/down focus in the current container */
76 if (focus_window_in_container(conn, container, direction))
79 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
80 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
81 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
82 /* Set new_row as a sane default, but it may get overwritten in a second */
85 /* Search from the top to correctly handle rowspanned containers */
86 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
87 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
94 /* Let’s see if there is a screen down/up there to which we can switch */
95 LOG("container is at %d with height %d\n", container->y, container->height);
97 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
98 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
99 LOG("Wrapping screen around vertically\n");
100 /* No screen found? Then wrap */
101 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
103 t_ws = &(workspaces[screen->current_workspace]);
104 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
106 } else if (direction == D_LEFT || direction == D_RIGHT) {
107 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
108 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
109 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
110 /* Set new_col as a sane default, but it may get overwritten in a second */
113 /* Search from the left to correctly handle colspanned containers */
114 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
115 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
122 /* Let’s see if there is a screen left/right here to which we can switch */
123 LOG("container is at %d with width %d\n", container->x, container->width);
125 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
126 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
127 LOG("Wrapping screen around horizontally\n");
128 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
130 t_ws = &(workspaces[screen->current_workspace]);
131 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
134 LOG("direction unhandled\n");
138 /* Bounds checking */
139 if (new_col >= t_ws->cols)
140 new_col = (t_ws->cols - 1);
141 if (new_row >= t_ws->rows)
142 new_row = (t_ws->rows - 1);
144 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
145 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
149 * Tries to move the window inside its current container.
151 * Returns true if the window could be moved, false otherwise.
154 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
155 direction_t direction) {
156 assert(client->container != NULL);
158 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
159 CIRCLEQ_NEXT(client, clients));
161 if (other == CIRCLEQ_END(&(client->container->clients)))
164 LOG("i can do that\n");
165 /* We can move the client inside its current container */
166 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
167 if (direction == D_UP)
168 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
169 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
175 * Moves the current window or whole container to the given direction, creating a column/row if
179 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
180 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
181 (direction == D_LEFT ? "left" : "right"))));
182 /* Get current window */
183 Container *container = CUR_CELL,
186 /* There has to be a container, see focus_window() */
187 assert(container != NULL);
189 /* If there is no window or the dock window is focused, we’re done */
190 if (container->currently_focused == NULL ||
191 container->currently_focused->dock)
194 /* As soon as the client is moved away, the last focused client in the old
195 * container needs to get focus, if any. Therefore, we save it here. */
196 Client *current_client = container->currently_focused;
197 Client *to_focus = get_last_focused_client(conn, container, current_client);
199 if (to_focus == NULL) {
200 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
201 if (to_focus == NULL)
202 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
207 /* If we’re at the left-most position, move the rest of the table right */
208 if (current_col == 0) {
209 expand_table_cols_at_head(c_ws);
212 new = CUR_TABLE[--current_col][current_row];
215 if (current_col == (c_ws->cols-1))
216 expand_table_cols(c_ws);
218 new = CUR_TABLE[++current_col][current_row];
221 if (move_current_window_in_container(conn, current_client, D_UP))
224 /* if we’re at the up-most position, move the rest of the table down */
225 if (current_row == 0) {
226 expand_table_rows_at_head(c_ws);
229 new = CUR_TABLE[current_col][--current_row];
232 if (move_current_window_in_container(conn, current_client, D_DOWN))
235 if (current_row == (c_ws->rows-1))
236 expand_table_rows(c_ws);
238 new = CUR_TABLE[current_col][++current_row];
242 /* Remove it from the old container and put it into the new one */
243 remove_client_from_container(conn, current_client, container);
245 if (new->currently_focused != NULL)
246 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
247 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
248 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
250 /* Update data structures */
251 current_client->container = new;
252 current_client->workspace = new->workspace;
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 for (int i = 0; i < container->rowspan; i++)
367 if (!cell_exists(new_col, container->row + i) ||
368 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
369 LOG("cannot snap to right - the cell is already used\n");
373 /* Check if there are other cells with rowspan, which are in our way.
374 * If so, reduce their rowspan. */
375 for (int i = container->row-1; i >= 0; i--) {
376 LOG("we got cell %d, %d with rowspan %d\n",
377 new_col, i, CUR_TABLE[new_col][i]->rowspan);
378 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
379 CUR_TABLE[new_col][i]->rowspan--;
380 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
383 container->colspan++;
387 if (!cell_exists(container->col, container->row - 1) ||
388 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
389 LOG("cannot snap to top - the cell is already used\n");
393 move_current_window(conn, D_UP);
394 snap_current_container(conn, D_DOWN);
397 LOG("snapping down\n");
398 int new_row = container->row + container->rowspan;
399 for (int i = 0; i < container->colspan; i++)
400 if (!cell_exists(container->col + i, new_row) ||
401 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
402 LOG("cannot snap down - the cell is already used\n");
406 for (int i = container->col-1; i >= 0; i--) {
407 LOG("we got cell %d, %d with colspan %d\n",
408 i, new_row, CUR_TABLE[i][new_row]->colspan);
409 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
410 CUR_TABLE[i][new_row]->colspan--;
411 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
415 container->rowspan++;
424 * Moves the currently selected window to the given workspace
427 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
428 LOG("Moving current window to workspace %d\n", workspace);
430 Container *container = CUR_CELL;
432 assert(container != NULL);
434 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
435 Workspace *t_ws = &(workspaces[workspace-1]);
437 Client *current_client = container->currently_focused;
438 if (current_client == NULL) {
439 LOG("No currently focused client in current container.\n");
442 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
443 if (to_focus == NULL)
444 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
446 if (t_ws->screen == NULL) {
447 LOG("initializing new workspace, setting num to %d\n", workspace-1);
448 t_ws->screen = container->workspace->screen;
449 /* Copy the dimensions from the virtual screen */
450 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
452 /* Check if there is already a fullscreen client on the destination workspace and
453 * stop moving if so. */
454 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
455 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
460 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
462 assert(to_container != NULL);
464 remove_client_from_container(conn, current_client, container);
465 if (container->workspace->fullscreen_client == current_client)
466 container->workspace->fullscreen_client = NULL;
468 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
469 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
470 if (current_client->fullscreen)
471 t_ws->fullscreen_client = current_client;
474 current_client->container = to_container;
475 current_client->workspace = to_container->workspace;
476 container->currently_focused = to_focus;
477 to_container->currently_focused = current_client;
479 /* If we’re moving it to an invisible screen, we need to unmap it */
480 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
481 LOG("This workspace is not visible, unmapping\n");
482 xcb_unmap_window(conn, current_client->frame);
485 /* delete all empty columns/rows */
486 cleanup_table(conn, container->workspace);
492 * Switches to the given workspace
495 void show_workspace(xcb_connection_t *conn, int workspace) {
497 bool need_warp = false;
498 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
499 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
500 Workspace *t_ws = &(workspaces[workspace-1]);
502 LOG("show_workspace(%d)\n", workspace);
504 /* Store current_row/current_col */
505 c_ws->current_row = current_row;
506 c_ws->current_col = current_col;
508 /* Check if the workspace has not been used yet */
509 if (t_ws->screen == NULL) {
510 LOG("initializing new workspace, setting num to %d\n", workspace);
511 t_ws->screen = c_ws->screen;
512 /* Copy the dimensions from the virtual screen */
513 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
516 if (c_ws->screen != t_ws->screen) {
517 /* We need to switch to the other screen first */
518 LOG("moving over to other screen.\n");
520 /* Store the old client */
521 Client *old_client = CUR_CELL->currently_focused;
523 c_ws = &(workspaces[t_ws->screen->current_workspace]);
524 current_col = c_ws->current_col;
525 current_row = c_ws->current_row;
526 if (CUR_CELL->currently_focused != NULL)
529 Rect *dims = &(c_ws->screen->rect);
530 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
531 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
534 /* Re-decorate the old client, it’s not focused anymore */
535 if ((old_client != NULL) && !old_client->dock)
536 redecorate_window(conn, old_client);
537 else xcb_flush(conn);
540 /* Check if we need to change something or if we’re already there */
541 if (c_ws->screen->current_workspace == (workspace-1)) {
542 if (CUR_CELL->currently_focused != NULL) {
543 set_focus(conn, CUR_CELL->currently_focused, true);
545 warp_pointer_into(conn, CUR_CELL->currently_focused);
552 t_ws->screen->current_workspace = workspace-1;
554 /* Unmap all clients of the current workspace */
555 unmap_workspace(conn, c_ws);
557 c_ws = &workspaces[workspace-1];
558 current_row = c_ws->current_row;
559 current_col = c_ws->current_col;
560 LOG("new current row = %d, current col = %d\n", current_row, current_col);
562 ignore_enter_notify_forall(conn, c_ws, true);
564 /* Map all clients on the new workspace */
566 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
567 xcb_map_window(conn, client->frame);
569 /* Map all stack windows, if any */
570 struct Stack_Window *stack_win;
571 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
572 if (stack_win->container->workspace == c_ws)
573 xcb_map_window(conn, stack_win->window);
575 ignore_enter_notify_forall(conn, c_ws, false);
577 /* Restore focus on the new workspace */
578 if (CUR_CELL->currently_focused != NULL) {
579 set_focus(conn, CUR_CELL->currently_focused, true);
581 warp_pointer_into(conn, CUR_CELL->currently_focused);
584 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
590 * Parses a command, see file CMDMODE for more information
593 void parse_command(xcb_connection_t *conn, const char *command) {
594 LOG("--- parsing command \"%s\" ---\n", command);
595 /* Hmm, just to be sure */
596 if (command[0] == '\0')
599 /* Is it an <exec>? Then execute the given command. */
600 if (STARTS_WITH(command, "exec ")) {
601 LOG("starting \"%s\"\n", command + strlen("exec "));
602 start_application(command+strlen("exec "));
606 /* Is it an <exit>? */
607 if (STARTS_WITH(command, "exit")) {
608 LOG("User issued exit-command, exiting without error.\n");
612 /* Is it <restart>? Then restart in place. */
613 if (STARTS_WITH(command, "restart")) {
614 LOG("restarting \"%s\"...\n", start_argv[0]);
615 execvp(start_argv[0], start_argv);
619 if (STARTS_WITH(command, "kill")) {
620 if (CUR_CELL->currently_focused == NULL) {
621 LOG("There is no window to kill\n");
625 LOG("Killing current window\n");
626 kill_window(conn, CUR_CELL->currently_focused);
630 /* Is it 'f' for fullscreen? */
631 if (command[0] == 'f') {
632 if (CUR_CELL->currently_focused == NULL)
634 toggle_fullscreen(conn, CUR_CELL->currently_focused);
638 /* Is it just 's' for stacking or 'd' for default? */
639 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
640 LOG("Switching mode for current container\n");
641 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
645 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
647 /* Is it a <with>? */
648 if (command[0] == 'w') {
650 /* TODO: implement */
651 if (command[0] == 'c') {
652 with = WITH_CONTAINER;
655 LOG("not yet implemented.\n");
660 /* It's a normal <cmd> */
662 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
663 direction_t direction;
664 int times = strtol(command, &rest, 10);
666 LOG("Invalid command (\"%s\")\n", command);
671 /* No rest? This was a tag number, not a times specification */
672 show_workspace(conn, times);
676 if (*rest == 'm' || *rest == 's') {
677 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
681 int workspace = strtol(rest, &rest, 10);
684 LOG("Invalid command (\"%s\")\n", command);
689 move_current_window_to_workspace(conn, workspace);
693 /* Now perform action to <where> */
694 while (*rest != '\0') {
697 else if (*rest == 'j')
699 else if (*rest == 'k')
701 else if (*rest == 'l')
704 LOG("unknown direction: %c\n", *rest);
708 if (action == ACTION_FOCUS)
709 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
710 else if (action == ACTION_MOVE) {
711 if (with == WITH_WINDOW)
712 move_current_window(conn, direction);
713 else move_current_container(conn, direction);
715 else if (action == ACTION_SNAP)
716 snap_current_container(conn, direction);
721 LOG("--- done ---\n");