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));
427 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
429 assert(to_container != NULL);
431 remove_client_from_container(conn, current_client, container);
433 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
434 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
437 current_client->container = to_container;
438 container->currently_focused = to_focus;
439 to_container->currently_focused = current_client;
441 /* If we’re moving it to an invisible screen, we need to unmap it */
442 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
443 LOG("This workspace is not visible, unmapping\n");
444 xcb_unmap_window(conn, current_client->frame);
447 /* delete all empty columns/rows */
448 cleanup_table(conn, container->workspace);
454 * Switches to the given workspace
457 void show_workspace(xcb_connection_t *conn, int workspace) {
459 bool need_warp = false;
460 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
461 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
462 Workspace *t_ws = &(workspaces[workspace-1]);
464 LOG("show_workspace(%d)\n", workspace);
466 /* Store current_row/current_col */
467 c_ws->current_row = current_row;
468 c_ws->current_col = current_col;
470 /* Check if the workspace has not been used yet */
471 if (t_ws->screen == NULL) {
472 LOG("initializing new workspace, setting num to %d\n", workspace);
473 t_ws->screen = c_ws->screen;
474 /* Copy the dimensions from the virtual screen */
475 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
478 if (c_ws->screen != t_ws->screen) {
479 /* We need to switch to the other screen first */
480 LOG("moving over to other screen.\n");
482 /* Store the old client */
483 Client *old_client = CUR_CELL->currently_focused;
485 c_ws = &(workspaces[t_ws->screen->current_workspace]);
486 current_col = c_ws->current_col;
487 current_row = c_ws->current_row;
488 if (CUR_CELL->currently_focused != NULL)
491 Rect *dims = &(c_ws->screen->rect);
492 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
493 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
496 /* Re-decorate the old client, it’s not focused anymore */
497 if ((old_client != NULL) && !old_client->dock)
498 redecorate_window(conn, old_client);
499 else xcb_flush(conn);
502 /* Check if we need to change something or if we’re already there */
503 if (c_ws->screen->current_workspace == (workspace-1)) {
504 if (CUR_CELL->currently_focused != NULL) {
505 set_focus(conn, CUR_CELL->currently_focused, true);
507 warp_pointer_into(conn, CUR_CELL->currently_focused);
514 t_ws->screen->current_workspace = workspace-1;
516 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
517 //xcb_grab_server(conn);
519 ignore_enter_notify_forall(conn, c_ws, true);
521 /* Unmap all clients of the current workspace */
522 int unmapped_clients = 0;
524 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
525 xcb_unmap_window(conn, client->frame);
529 /* If we did not unmap any clients, the workspace is empty and we can destroy it */
530 if (unmapped_clients == 0)
533 /* Unmap the stack windows on the current workspace, if any */
534 struct Stack_Window *stack_win;
535 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
536 if (stack_win->container->workspace == c_ws)
537 xcb_unmap_window(conn, stack_win->window);
539 ignore_enter_notify_forall(conn, c_ws, false);
541 c_ws = &workspaces[workspace-1];
542 current_row = c_ws->current_row;
543 current_col = c_ws->current_col;
544 LOG("new current row = %d, current col = %d\n", current_row, current_col);
546 ignore_enter_notify_forall(conn, c_ws, true);
548 /* Map all clients on the new workspace */
550 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
551 xcb_map_window(conn, client->frame);
553 /* Map all stack windows, if any */
554 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
555 if (stack_win->container->workspace == c_ws)
556 xcb_map_window(conn, stack_win->window);
558 ignore_enter_notify_forall(conn, c_ws, false);
560 /* Restore focus on the new workspace */
561 if (CUR_CELL->currently_focused != NULL) {
562 set_focus(conn, CUR_CELL->currently_focused, true);
564 warp_pointer_into(conn, CUR_CELL->currently_focused);
567 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
569 //xcb_ungrab_server(conn);
575 * Parses a command, see file CMDMODE for more information
578 void parse_command(xcb_connection_t *conn, const char *command) {
579 LOG("--- parsing command \"%s\" ---\n", command);
580 /* Hmm, just to be sure */
581 if (command[0] == '\0')
584 /* Is it an <exec>? Then execute the given command. */
585 if (STARTS_WITH(command, "exec ")) {
586 LOG("starting \"%s\"\n", command + strlen("exec "));
587 start_application(command+strlen("exec "));
591 /* Is it an <exit>? */
592 if (STARTS_WITH(command, "exit")) {
593 LOG("User issued exit-command, exiting without error.\n");
597 /* Is it <restart>? Then restart in place. */
598 if (STARTS_WITH(command, "restart")) {
599 LOG("restarting \"%s\"...\n", application_path);
600 execl(application_path, application_path, NULL);
604 if (STARTS_WITH(command, "kill")) {
605 if (CUR_CELL->currently_focused == NULL) {
606 LOG("There is no window to kill\n");
610 LOG("Killing current window\n");
611 kill_window(conn, CUR_CELL->currently_focused);
615 /* Is it 'f' for fullscreen? */
616 if (command[0] == 'f') {
617 if (CUR_CELL->currently_focused == NULL)
619 toggle_fullscreen(conn, CUR_CELL->currently_focused);
623 /* Is it just 's' for stacking or 'd' for default? */
624 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
625 LOG("Switching mode for current container\n");
626 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
630 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
632 /* Is it a <with>? */
633 if (command[0] == 'w') {
635 /* TODO: implement */
636 if (command[0] == 'c') {
637 with = WITH_CONTAINER;
640 LOG("not yet implemented.\n");
645 /* It's a normal <cmd> */
647 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
648 direction_t direction;
649 int times = strtol(command, &rest, 10);
651 LOG("Invalid command (\"%s\")\n", command);
656 /* No rest? This was a tag number, not a times specification */
657 show_workspace(conn, times);
661 if (*rest == 'm' || *rest == 's') {
662 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
666 int workspace = strtol(rest, &rest, 10);
669 LOG("Invalid command (\"%s\")\n", command);
674 move_current_window_to_workspace(conn, workspace);
678 /* Now perform action to <where> */
679 while (*rest != '\0') {
682 else if (*rest == 'j')
684 else if (*rest == 'k')
686 else if (*rest == 'l')
689 LOG("unknown direction: %c\n", *rest);
693 if (action == ACTION_FOCUS)
694 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
695 else if (action == ACTION_MOVE) {
696 if (with == WITH_WINDOW)
697 move_current_window(conn, direction);
698 else move_current_container(conn, direction);
700 else if (action == ACTION_SNAP)
701 snap_current_container(conn, direction);
706 LOG("--- done ---\n");