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))
81 else if (direction == D_UP && cell_exists(current_col, current_row-1))
84 /* Let’s see if there is a screen down/up there to which we can switch */
85 LOG("container is at %d with height %d\n", container->y, container->height);
87 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
88 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
89 LOG("Wrapping screen around vertically\n");
90 /* No screen found? Then wrap */
91 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
93 t_ws = &(workspaces[screen->current_workspace]);
94 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
96 } else if (direction == D_LEFT || direction == D_RIGHT) {
97 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
99 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
102 /* Let’s see if there is a screen left/right here to which we can switch */
103 LOG("container is at %d with width %d\n", container->x, container->width);
105 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
106 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
107 LOG("Wrapping screen around horizontally\n");
108 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
110 t_ws = &(workspaces[screen->current_workspace]);
111 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
114 LOG("direction unhandled\n");
118 /* Bounds checking */
119 if (new_col >= t_ws->cols)
120 new_col = (t_ws->cols - 1);
121 if (new_row >= t_ws->rows)
122 new_row = (t_ws->rows - 1);
124 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
125 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
129 * Tries to move the window inside its current container.
131 * Returns true if the window could be moved, false otherwise.
134 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
135 direction_t direction) {
136 assert(client->container != NULL);
138 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
139 CIRCLEQ_NEXT(client, clients));
141 if (other == CIRCLEQ_END(&(client->container->clients)))
144 LOG("i can do that\n");
145 /* We can move the client inside its current container */
146 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
147 if (direction == D_UP)
148 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
149 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
155 * Moves the current window or whole container to the given direction, creating a column/row if
159 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
160 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
161 (direction == D_LEFT ? "left" : "right"))));
162 /* Get current window */
163 Container *container = CUR_CELL,
166 /* There has to be a container, see focus_window() */
167 assert(container != NULL);
169 /* If there is no window or the dock window is focused, we’re done */
170 if (container->currently_focused == NULL ||
171 container->currently_focused->dock)
174 /* As soon as the client is moved away, the last focused client in the old
175 * container needs to get focus, if any. Therefore, we save it here. */
176 Client *current_client = container->currently_focused;
177 Client *to_focus = get_last_focused_client(conn, container, current_client);
179 if (to_focus == NULL) {
180 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
181 if (to_focus == NULL)
182 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
187 /* If we’re at the left-most position, move the rest of the table right */
188 if (current_col == 0) {
189 expand_table_cols_at_head(c_ws);
192 new = CUR_TABLE[--current_col][current_row];
195 if (current_col == (c_ws->cols-1))
196 expand_table_cols(c_ws);
198 new = CUR_TABLE[++current_col][current_row];
201 if (move_current_window_in_container(conn, current_client, D_UP))
204 /* if we’re at the up-most position, move the rest of the table down */
205 if (current_row == 0) {
206 expand_table_rows_at_head(c_ws);
209 new = CUR_TABLE[current_col][--current_row];
212 if (move_current_window_in_container(conn, current_client, D_DOWN))
215 if (current_row == (c_ws->rows-1))
216 expand_table_rows(c_ws);
218 new = CUR_TABLE[current_col][++current_row];
222 /* Remove it from the old container and put it into the new one */
223 remove_client_from_container(conn, current_client, container);
224 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
225 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
227 /* Update data structures */
228 current_client->container = new;
229 container->currently_focused = to_focus;
230 new->currently_focused = current_client;
232 Workspace *workspace = container->workspace;
234 /* delete all empty columns/rows */
235 cleanup_table(conn, workspace);
237 /* Fix colspan/rowspan if it’d overlap */
238 fix_colrowspan(conn, workspace);
242 set_focus(conn, current_client, true);
245 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
246 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
247 (direction == D_LEFT ? "left" : "right"))));
248 /* Get current window */
249 Container *container = CUR_CELL,
252 Container **old = &CUR_CELL;
254 /* There has to be a container, see focus_window() */
255 assert(container != NULL);
259 /* If we’re at the left-most position, move the rest of the table right */
260 if (current_col == 0) {
261 expand_table_cols_at_head(c_ws);
263 old = &CUR_TABLE[current_col+1][current_row];
265 new = CUR_TABLE[--current_col][current_row];
268 if (current_col == (c_ws->cols-1))
269 expand_table_cols(c_ws);
271 new = CUR_TABLE[++current_col][current_row];
274 /* if we’re at the up-most position, move the rest of the table down */
275 if (current_row == 0) {
276 expand_table_rows_at_head(c_ws);
278 old = &CUR_TABLE[current_col][current_row+1];
280 new = CUR_TABLE[current_col][--current_row];
283 if (current_row == (c_ws->rows-1))
284 expand_table_rows(c_ws);
286 new = CUR_TABLE[current_col][++current_row];
290 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
292 /* Swap the containers */
297 new->col = container->col;
298 new->row = container->row;
300 CUR_CELL = container;
301 container->col = col;
302 container->row = row;
304 Workspace *workspace = container->workspace;
306 /* delete all empty columns/rows */
307 cleanup_table(conn, workspace);
309 /* Fix colspan/rowspan if it’d overlap */
310 fix_colrowspan(conn, workspace);
316 * "Snaps" the current container (not possible for windows, because it works at table base)
317 * to the given direction, that is, adjusts cellspan/rowspan
320 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
321 LOG("snapping container to direction %d\n", direction);
323 Container *container = CUR_CELL;
325 assert(container != NULL);
329 /* Snap to the left is actually a move to the left and then a snap right */
330 if (!cell_exists(container->col - 1, container->row) ||
331 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
332 LOG("cannot snap to left - the cell is already used\n");
336 move_current_window(conn, D_LEFT);
337 snap_current_container(conn, D_RIGHT);
340 /* Check if the cell is used */
341 int new_col = container->col + container->colspan;
342 if (!cell_exists(new_col, container->row) ||
343 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
344 LOG("cannot snap to right - the cell is already used\n");
348 /* Check if there are other cells with rowspan, which are in our way.
349 * If so, reduce their rowspan. */
350 for (int i = container->row-1; i >= 0; i--) {
351 LOG("we got cell %d, %d with rowspan %d\n",
352 new_col, i, CUR_TABLE[new_col][i]->rowspan);
353 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
354 CUR_TABLE[new_col][i]->rowspan--;
355 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
358 container->colspan++;
362 if (!cell_exists(container->col, container->row - 1) ||
363 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
364 LOG("cannot snap to top - the cell is already used\n");
368 move_current_window(conn, D_UP);
369 snap_current_container(conn, D_DOWN);
372 LOG("snapping down\n");
373 int new_row = container->row + container->rowspan;
374 if (!cell_exists(container->col, new_row) ||
375 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
376 LOG("cannot snap down - the cell is already used\n");
380 for (int i = container->col-1; i >= 0; i--) {
381 LOG("we got cell %d, %d with colspan %d\n",
382 i, new_row, CUR_TABLE[i][new_row]->colspan);
383 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
384 CUR_TABLE[i][new_row]->colspan--;
385 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
389 container->rowspan++;
398 * Moves the currently selected window to the given workspace
401 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
402 LOG("Moving current window to workspace %d\n", workspace);
404 Container *container = CUR_CELL;
406 assert(container != NULL);
408 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
409 Workspace *t_ws = &(workspaces[workspace-1]);
411 Client *current_client = container->currently_focused;
412 if (current_client == NULL) {
413 LOG("No currently focused client in current container.\n");
416 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
417 if (to_focus == NULL)
418 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
420 if (t_ws->screen == NULL) {
421 LOG("initializing new workspace, setting num to %d\n", workspace-1);
422 t_ws->screen = container->workspace->screen;
423 /* Copy the dimensions from the virtual screen */
424 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
426 /* Check if there is already a fullscreen client on the destination workspace and
427 * stop moving if so. */
428 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
429 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
434 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
436 assert(to_container != NULL);
438 remove_client_from_container(conn, current_client, container);
439 if (container->workspace->fullscreen_client == current_client)
440 container->workspace->fullscreen_client = NULL;
442 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
443 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
444 if (current_client->fullscreen)
445 t_ws->fullscreen_client = current_client;
448 current_client->container = to_container;
449 container->currently_focused = to_focus;
450 to_container->currently_focused = current_client;
452 /* If we’re moving it to an invisible screen, we need to unmap it */
453 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
454 LOG("This workspace is not visible, unmapping\n");
455 xcb_unmap_window(conn, current_client->frame);
458 /* delete all empty columns/rows */
459 cleanup_table(conn, container->workspace);
465 * Switches to the given workspace
468 void show_workspace(xcb_connection_t *conn, int workspace) {
470 bool need_warp = false;
471 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
472 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
473 Workspace *t_ws = &(workspaces[workspace-1]);
475 LOG("show_workspace(%d)\n", workspace);
477 /* Store current_row/current_col */
478 c_ws->current_row = current_row;
479 c_ws->current_col = current_col;
481 /* Check if the workspace has not been used yet */
482 if (t_ws->screen == NULL) {
483 LOG("initializing new workspace, setting num to %d\n", workspace);
484 t_ws->screen = c_ws->screen;
485 /* Copy the dimensions from the virtual screen */
486 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
489 if (c_ws->screen != t_ws->screen) {
490 /* We need to switch to the other screen first */
491 LOG("moving over to other screen.\n");
493 /* Store the old client */
494 Client *old_client = CUR_CELL->currently_focused;
496 c_ws = &(workspaces[t_ws->screen->current_workspace]);
497 current_col = c_ws->current_col;
498 current_row = c_ws->current_row;
499 if (CUR_CELL->currently_focused != NULL)
502 Rect *dims = &(c_ws->screen->rect);
503 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
504 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
507 /* Re-decorate the old client, it’s not focused anymore */
508 if ((old_client != NULL) && !old_client->dock)
509 redecorate_window(conn, old_client);
510 else xcb_flush(conn);
513 /* Check if we need to change something or if we’re already there */
514 if (c_ws->screen->current_workspace == (workspace-1)) {
515 if (CUR_CELL->currently_focused != NULL) {
516 set_focus(conn, CUR_CELL->currently_focused, true);
518 warp_pointer_into(conn, CUR_CELL->currently_focused);
525 t_ws->screen->current_workspace = workspace-1;
527 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
528 //xcb_grab_server(conn);
530 ignore_enter_notify_forall(conn, c_ws, true);
532 /* Unmap all clients of the current workspace */
533 int unmapped_clients = 0;
535 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
536 xcb_unmap_window(conn, client->frame);
540 /* If we did not unmap any clients, the workspace is empty and we can destroy it */
541 if (unmapped_clients == 0)
544 /* Unmap the stack windows on the current workspace, if any */
545 struct Stack_Window *stack_win;
546 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
547 if (stack_win->container->workspace == c_ws)
548 xcb_unmap_window(conn, stack_win->window);
550 ignore_enter_notify_forall(conn, c_ws, false);
552 c_ws = &workspaces[workspace-1];
553 current_row = c_ws->current_row;
554 current_col = c_ws->current_col;
555 LOG("new current row = %d, current col = %d\n", current_row, current_col);
557 ignore_enter_notify_forall(conn, c_ws, true);
559 /* Map all clients on the new workspace */
561 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
562 xcb_map_window(conn, client->frame);
564 /* Map all stack windows, if any */
565 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
566 if (stack_win->container->workspace == c_ws)
567 xcb_map_window(conn, stack_win->window);
569 ignore_enter_notify_forall(conn, c_ws, false);
571 /* Restore focus on the new workspace */
572 if (CUR_CELL->currently_focused != NULL) {
573 set_focus(conn, CUR_CELL->currently_focused, true);
575 warp_pointer_into(conn, CUR_CELL->currently_focused);
578 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
580 //xcb_ungrab_server(conn);
586 * Parses a command, see file CMDMODE for more information
589 void parse_command(xcb_connection_t *conn, const char *command) {
590 LOG("--- parsing command \"%s\" ---\n", command);
591 /* Hmm, just to be sure */
592 if (command[0] == '\0')
595 /* Is it an <exec>? Then execute the given command. */
596 if (STARTS_WITH(command, "exec ")) {
597 LOG("starting \"%s\"\n", command + strlen("exec "));
598 start_application(command+strlen("exec "));
602 /* Is it an <exit>? */
603 if (STARTS_WITH(command, "exit")) {
604 LOG("User issued exit-command, exiting without error.\n");
608 /* Is it <restart>? Then restart in place. */
609 if (STARTS_WITH(command, "restart")) {
610 LOG("restarting \"%s\"...\n", application_path);
611 execl(application_path, application_path, NULL);
615 if (STARTS_WITH(command, "kill")) {
616 if (CUR_CELL->currently_focused == NULL) {
617 LOG("There is no window to kill\n");
621 LOG("Killing current window\n");
622 kill_window(conn, CUR_CELL->currently_focused);
626 /* Is it 'f' for fullscreen? */
627 if (command[0] == 'f') {
628 if (CUR_CELL->currently_focused == NULL)
630 toggle_fullscreen(conn, CUR_CELL->currently_focused);
634 /* Is it just 's' for stacking or 'd' for default? */
635 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
636 LOG("Switching mode for current container\n");
637 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
641 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
643 /* Is it a <with>? */
644 if (command[0] == 'w') {
646 /* TODO: implement */
647 if (command[0] == 'c') {
648 with = WITH_CONTAINER;
651 LOG("not yet implemented.\n");
656 /* It's a normal <cmd> */
658 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
659 direction_t direction;
660 int times = strtol(command, &rest, 10);
662 LOG("Invalid command (\"%s\")\n", command);
667 /* No rest? This was a tag number, not a times specification */
668 show_workspace(conn, times);
672 if (*rest == 'm' || *rest == 's') {
673 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
677 int workspace = strtol(rest, &rest, 10);
680 LOG("Invalid command (\"%s\")\n", command);
685 move_current_window_to_workspace(conn, workspace);
689 /* Now perform action to <where> */
690 while (*rest != '\0') {
693 else if (*rest == 'j')
695 else if (*rest == 'k')
697 else if (*rest == 'l')
700 LOG("unknown direction: %c\n", *rest);
704 if (action == ACTION_FOCUS)
705 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
706 else if (action == ACTION_MOVE) {
707 if (with == WITH_WINDOW)
708 move_current_window(conn, direction);
709 else move_current_container(conn, direction);
711 else if (action == ACTION_SNAP)
712 snap_current_container(conn, direction);
717 LOG("--- done ---\n");