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);
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);
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 next 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 = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
178 if (to_focus == NULL)
179 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
183 /* If we’re at the left-most position, move the rest of the table right */
184 if (current_col == 0) {
185 expand_table_cols_at_head(c_ws);
188 new = CUR_TABLE[--current_col][current_row];
191 if (current_col == (c_ws->cols-1))
192 expand_table_cols(c_ws);
194 new = CUR_TABLE[++current_col][current_row];
197 if (move_current_window_in_container(conn, current_client, D_UP))
200 /* if we’re at the up-most position, move the rest of the table down */
201 if (current_row == 0) {
202 expand_table_rows_at_head(c_ws);
205 new = CUR_TABLE[current_col][--current_row];
208 if (move_current_window_in_container(conn, current_client, D_DOWN))
211 if (current_row == (c_ws->rows-1))
212 expand_table_rows(c_ws);
214 new = CUR_TABLE[current_col][++current_row];
218 /* Remove it from the old container and put it into the new one */
219 remove_client_from_container(conn, current_client, container);
220 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
221 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
223 /* Update data structures */
224 current_client->container = new;
225 container->currently_focused = to_focus;
226 new->currently_focused = current_client;
228 Workspace *workspace = container->workspace;
230 /* delete all empty columns/rows */
231 cleanup_table(conn, workspace);
233 /* Fix colspan/rowspan if it’d overlap */
234 fix_colrowspan(conn, workspace);
238 set_focus(conn, current_client);
241 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
242 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
243 (direction == D_LEFT ? "left" : "right"))));
244 /* Get current window */
245 Container *container = CUR_CELL,
248 Container **old = &CUR_CELL;
250 /* There has to be a container, see focus_window() */
251 assert(container != NULL);
255 /* If we’re at the left-most position, move the rest of the table right */
256 if (current_col == 0) {
257 expand_table_cols_at_head(c_ws);
259 old = &CUR_TABLE[current_col+1][current_row];
261 new = CUR_TABLE[--current_col][current_row];
264 if (current_col == (c_ws->cols-1))
265 expand_table_cols(c_ws);
267 new = CUR_TABLE[++current_col][current_row];
270 /* if we’re at the up-most position, move the rest of the table down */
271 if (current_row == 0) {
272 expand_table_rows_at_head(c_ws);
274 old = &CUR_TABLE[current_col][current_row+1];
276 new = CUR_TABLE[current_col][--current_row];
279 if (current_row == (c_ws->rows-1))
280 expand_table_rows(c_ws);
282 new = CUR_TABLE[current_col][++current_row];
286 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
288 /* Swap the containers */
293 new->col = container->col;
294 new->row = container->row;
296 CUR_CELL = container;
297 container->col = col;
298 container->row = row;
300 Workspace *workspace = container->workspace;
302 /* delete all empty columns/rows */
303 cleanup_table(conn, workspace);
305 /* Fix colspan/rowspan if it’d overlap */
306 fix_colrowspan(conn, workspace);
312 * "Snaps" the current container (not possible for windows, because it works at table base)
313 * to the given direction, that is, adjusts cellspan/rowspan
316 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
317 LOG("snapping container to direction %d\n", direction);
319 Container *container = CUR_CELL;
321 assert(container != NULL);
325 /* Snap to the left is actually a move to the left and then a snap right */
326 if (!cell_exists(container->col - 1, container->row) ||
327 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
328 LOG("cannot snap to left - the cell is already used\n");
332 move_current_window(conn, D_LEFT);
333 snap_current_container(conn, D_RIGHT);
336 /* Check if the cell is used */
337 int new_col = container->col + container->colspan;
338 if (!cell_exists(new_col, container->row) ||
339 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
340 LOG("cannot snap to right - the cell is already used\n");
344 /* Check if there are other cells with rowspan, which are in our way.
345 * If so, reduce their rowspan. */
346 for (int i = container->row-1; i >= 0; i--) {
347 LOG("we got cell %d, %d with rowspan %d\n",
348 new_col, i, CUR_TABLE[new_col][i]->rowspan);
349 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
350 CUR_TABLE[new_col][i]->rowspan--;
351 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
354 container->colspan++;
358 if (!cell_exists(container->col, container->row - 1) ||
359 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
360 LOG("cannot snap to top - the cell is already used\n");
364 move_current_window(conn, D_UP);
365 snap_current_container(conn, D_DOWN);
368 LOG("snapping down\n");
369 int new_row = container->row + container->rowspan;
370 if (!cell_exists(container->col, new_row) ||
371 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
372 LOG("cannot snap down - the cell is already used\n");
376 for (int i = container->col-1; i >= 0; i--) {
377 LOG("we got cell %d, %d with colspan %d\n",
378 i, new_row, CUR_TABLE[i][new_row]->colspan);
379 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
380 CUR_TABLE[i][new_row]->colspan--;
381 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
385 container->rowspan++;
394 * Moves the currently selected window to the given workspace
397 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
398 LOG("Moving current window to workspace %d\n", workspace);
400 Container *container = CUR_CELL;
402 assert(container != NULL);
404 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
405 Workspace *t_ws = &(workspaces[workspace-1]);
407 Client *current_client = container->currently_focused;
408 if (current_client == NULL) {
409 LOG("No currently focused client in current container.\n");
412 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
413 if (to_focus == NULL)
414 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
416 if (t_ws->screen == NULL) {
417 LOG("initializing new workspace, setting num to %d\n", workspace-1);
418 t_ws->screen = container->workspace->screen;
419 /* Copy the dimensions from the virtual screen */
420 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
423 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
425 assert(to_container != NULL);
427 remove_client_from_container(conn, current_client, container);
429 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
430 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
433 current_client->container = to_container;
434 container->currently_focused = to_focus;
435 to_container->currently_focused = current_client;
437 /* If we’re moving it to an invisible screen, we need to unmap it */
438 if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
439 LOG("This workspace is not visible, unmapping\n");
440 xcb_unmap_window(conn, current_client->frame);
443 /* delete all empty columns/rows */
444 cleanup_table(conn, container->workspace);
450 * Switches to the given workspace
453 void show_workspace(xcb_connection_t *conn, int workspace) {
455 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
456 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
457 Workspace *t_ws = &(workspaces[workspace-1]);
459 LOG("show_workspace(%d)\n", workspace);
461 /* Store current_row/current_col */
462 c_ws->current_row = current_row;
463 c_ws->current_col = current_col;
465 /* Check if the workspace has not been used yet */
466 if (t_ws->screen == NULL) {
467 LOG("initializing new workspace, setting num to %d\n", workspace);
468 t_ws->screen = c_ws->screen;
469 /* Copy the dimensions from the virtual screen */
470 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
473 if (c_ws->screen != t_ws->screen) {
474 /* We need to switch to the other screen first */
475 LOG("moving over to other screen.\n");
477 /* Store the old client */
478 Client *old_client = CUR_CELL->currently_focused;
480 c_ws = &(workspaces[t_ws->screen->current_workspace]);
481 current_col = c_ws->current_col;
482 current_row = c_ws->current_row;
483 if (CUR_CELL->currently_focused != NULL)
484 warp_pointer_into(conn, CUR_CELL->currently_focused);
486 Rect *dims = &(c_ws->screen->rect);
487 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
488 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
491 /* Re-decorate the old client, it’s not focused anymore */
492 if ((old_client != NULL) && !old_client->dock)
493 redecorate_window(conn, old_client);
494 else xcb_flush(conn);
497 /* Check if we need to change something or if we’re already there */
498 if (c_ws->screen->current_workspace == (workspace-1))
501 t_ws->screen->current_workspace = workspace-1;
503 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
504 //xcb_grab_server(conn);
506 ignore_enter_notify_forall(conn, c_ws, true);
508 /* Unmap all clients of the current workspace */
509 int unmapped_clients = 0;
511 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
512 xcb_unmap_window(conn, client->frame);
516 /* If we did not unmap any clients, the workspace is empty and we can destroy it */
517 if (unmapped_clients == 0)
520 /* Unmap the stack windows on the current workspace, if any */
521 struct Stack_Window *stack_win;
522 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
523 if (stack_win->container->workspace == c_ws)
524 xcb_unmap_window(conn, stack_win->window);
526 ignore_enter_notify_forall(conn, c_ws, false);
528 c_ws = &workspaces[workspace-1];
529 current_row = c_ws->current_row;
530 current_col = c_ws->current_col;
531 LOG("new current row = %d, current col = %d\n", current_row, current_col);
533 ignore_enter_notify_forall(conn, c_ws, true);
535 /* Map all clients on the new workspace */
537 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
538 xcb_map_window(conn, client->frame);
540 /* Map all stack windows, if any */
541 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
542 if (stack_win->container->workspace == c_ws)
543 xcb_map_window(conn, stack_win->window);
545 ignore_enter_notify_forall(conn, c_ws, false);
547 /* Restore focus on the new workspace */
548 if (CUR_CELL->currently_focused != NULL)
549 set_focus(conn, CUR_CELL->currently_focused);
550 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
552 //xcb_ungrab_server(conn);
558 * Parses a command, see file CMDMODE for more information
561 void parse_command(xcb_connection_t *conn, const char *command) {
562 LOG("--- parsing command \"%s\" ---\n", command);
563 /* Hmm, just to be sure */
564 if (command[0] == '\0')
567 /* Is it an <exec>? Then execute the given command. */
568 if (STARTS_WITH(command, "exec ")) {
569 LOG("starting \"%s\"\n", command + strlen("exec "));
570 start_application(command+strlen("exec "));
574 /* Is it an <exit>? */
575 if (STARTS_WITH(command, "exit")) {
576 LOG("User issued exit-command, exiting without error.\n");
580 /* Is it <restart>? Then restart in place. */
581 if (STARTS_WITH(command, "restart")) {
582 LOG("restarting \"%s\"...\n", application_path);
583 execl(application_path, application_path, NULL);
587 if (STARTS_WITH(command, "kill")) {
588 if (CUR_CELL->currently_focused == NULL) {
589 LOG("There is no window to kill\n");
593 LOG("Killing current window\n");
594 kill_window(conn, CUR_CELL->currently_focused);
598 /* Is it 'f' for fullscreen? */
599 if (command[0] == 'f') {
600 if (CUR_CELL->currently_focused == NULL)
602 toggle_fullscreen(conn, CUR_CELL->currently_focused);
606 /* Is it just 's' for stacking or 'd' for default? */
607 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
608 LOG("Switching mode for current container\n");
609 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
613 enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
615 /* Is it a <with>? */
616 if (command[0] == 'w') {
618 /* TODO: implement */
619 if (command[0] == 'c') {
620 with = WITH_CONTAINER;
623 LOG("not yet implemented.\n");
628 /* It's a normal <cmd> */
630 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
631 direction_t direction;
632 int times = strtol(command, &rest, 10);
634 LOG("Invalid command (\"%s\")\n", command);
639 /* No rest? This was a tag number, not a times specification */
640 show_workspace(conn, times);
644 if (*rest == 'm' || *rest == 's') {
645 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
649 int workspace = strtol(rest, &rest, 10);
652 LOG("Invalid command (\"%s\")\n", command);
657 move_current_window_to_workspace(conn, workspace);
661 /* Now perform action to <where> */
662 while (*rest != '\0') {
665 else if (*rest == 'j')
667 else if (*rest == 'k')
669 else if (*rest == 'l')
672 LOG("unknown direction: %c\n", *rest);
676 if (action == ACTION_FOCUS)
677 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
678 else if (action == ACTION_MOVE) {
679 if (with == WITH_WINDOW)
680 move_current_window(conn, direction);
681 else move_current_container(conn, direction);
683 else if (action == ACTION_SNAP)
684 snap_current_container(conn, direction);
689 LOG("--- done ---\n");