2 #define I3__FILE__ "tree.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
9 * tree.c: Everything that primarily modifies the layout tree data structure.
17 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
20 * Create the pseudo-output __i3. Output-independent workspaces such as
21 * __i3_scratch will live there.
24 static Con *_create___i3(void) {
25 Con *__i3 = con_new(croot, NULL);
27 __i3->name = sstrdup("__i3");
28 __i3->type = CT_OUTPUT;
29 __i3->layout = L_OUTPUT;
30 con_fix_percent(croot);
31 x_set_name(__i3, "[i3 con] pseudo-output __i3");
32 /* For retaining the correct position/size of a scratchpad window, the
33 * dimensions of the real outputs should be multiples of the __i3
34 * pseudo-output. Ensuring that is the job of scratchpad_fix_resolution()
35 * which gets called after this function and after detecting all the
36 * outputs (or whenever an output changes). */
37 __i3->rect.width = 1280;
38 __i3->rect.height = 1024;
40 /* Add a content container. */
41 DLOG("adding main content container\n");
42 Con *content = con_new(NULL, NULL);
43 content->type = CT_CON;
45 content->name = sstrdup("content");
46 content->layout = L_SPLITH;
48 x_set_name(content, "[i3 con] content __i3");
49 con_attach(content, __i3, false);
51 /* Attach the __i3_scratch workspace. */
52 Con *ws = con_new(NULL, NULL);
53 ws->type = CT_WORKSPACE;
55 ws->name = sstrdup("__i3_scratch");
56 ws->layout = L_SPLITH;
57 con_attach(ws, content, false);
58 x_set_name(ws, "[i3 con] workspace __i3_scratch");
59 ws->fullscreen_mode = CF_OUTPUT;
65 * Loads tree from 'path' (used for in-place restarts).
68 bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry) {
69 char *globbed = resolve_tilde(path);
71 if (!path_exists(globbed)) {
72 LOG("%s does not exist, not restoring tree\n", globbed);
77 /* TODO: refactor the following */
78 croot = con_new(NULL, NULL);
87 tree_append_json(globbed);
89 printf("appended tree, using new root\n");
90 croot = TAILQ_FIRST(&(croot->nodes_head));
91 printf("new root = %p\n", croot);
92 Con *out = TAILQ_FIRST(&(croot->nodes_head));
93 printf("out = %p\n", out);
94 Con *ws = TAILQ_FIRST(&(out->nodes_head));
95 printf("ws = %p\n", ws);
97 /* For in-place restarting into v4.2, we need to make sure the new
98 * pseudo-output __i3 is present. */
99 if (strcmp(out->name, "__i3") != 0) {
100 DLOG("Adding pseudo-output __i3 during inplace restart\n");
101 Con *__i3 = _create___i3();
102 /* Ensure that it is the first output, other places in the code make
103 * that assumption. */
104 TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes);
105 TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes);
112 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
113 * root node are created in randr.c for each Output.
116 void tree_init(xcb_get_geometry_reply_t *geometry) {
117 croot = con_new(NULL, NULL);
119 croot->name = "root";
120 croot->type = CT_ROOT;
121 croot->layout = L_SPLITH;
122 croot->rect = (Rect){
133 * Opens an empty container in the current container
136 Con *tree_open_con(Con *con, i3Window *window) {
138 /* every focusable Con has a parent (outputs have parent root) */
139 con = focused->parent;
140 /* If the parent is an output, we are on a workspace. In this case,
141 * the new container needs to be opened as a leaf of the workspace. */
142 if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
146 /* If the currently focused container is a floating container, we
147 * attach the new container to the currently focused spot in its
149 if (con->type == CT_FLOATING_CON) {
150 con = con_descend_tiling_focused(con->parent);
151 if (con->type != CT_WORKSPACE)
154 DLOG("con = %p\n", con);
159 /* 3. create the container and attach it to its parent */
160 Con *new = con_new(con, window);
161 new->layout = L_SPLITH;
163 /* 4: re-calculate child->percent for each child */
164 con_fix_percent(con);
169 static bool _is_con_mapped(Con *con) {
172 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
173 if (_is_con_mapped(child))
180 * Closes the given container including all children.
181 * Returns true if the container was killed or false if just WM_DELETE was sent
182 * and the window is expected to kill itself.
184 * The dont_kill_parent flag is specified when the function calls itself
185 * recursively while deleting a containers children.
187 * The force_set_focus flag is specified in the case of killing a floating
188 * window: tree_close() will be invoked for the CT_FLOATINGCON (the parent
189 * container) and focus should be set there.
192 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus) {
193 bool was_mapped = con->mapped;
194 Con *parent = con->parent;
197 /* Even if the container itself is not mapped, its children may be
198 * mapped (for example split containers don't have a mapped window on
199 * their own but usually contain mapped children). */
200 was_mapped = _is_con_mapped(con);
203 /* Get the container which is next focused */
204 Con *next = con_next_focused(con);
205 DLOG("next = %p, focused = %p\n", next, focused);
207 DLOG("closing %p, kill_window = %d\n", con, kill_window);
208 Con *child, *nextchild;
209 bool abort_kill = false;
210 /* We cannot use TAILQ_FOREACH because the children get deleted
211 * in their parent’s nodes_head */
212 for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) {
213 nextchild = TAILQ_NEXT(child, nodes);
214 DLOG("killing child=%p\n", child);
215 if (!tree_close(child, kill_window, true, false))
221 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
225 if (con->window != NULL) {
226 if (kill_window != DONT_KILL_WINDOW) {
227 x_window_kill(con->window->id, kill_window);
230 xcb_void_cookie_t cookie;
231 /* un-parent the window */
232 cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
234 /* Ignore X11 errors for the ReparentWindow request.
235 * X11 Errors are returned when the window was already destroyed */
236 add_ignore_event(cookie.sequence, 0);
238 /* We are no longer handling this window, thus set WM_STATE to
239 * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
240 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
241 cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
242 con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
244 /* Ignore X11 errors for the ReparentWindow request.
245 * X11 Errors are returned when the window was already destroyed */
246 add_ignore_event(cookie.sequence, 0);
248 FREE(con->window->class_class);
249 FREE(con->window->class_instance);
250 i3string_free(con->window->name);
254 Con *ws = con_get_workspace(con);
256 /* Figure out which container to focus next before detaching 'con'. */
257 if (con_is_floating(con)) {
258 if (con == focused) {
259 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
260 next = con_next_focused(parent);
262 dont_kill_parent = true;
263 DLOG("Alright, focusing %p\n", next);
269 /* Detach the container so that it will not be rendered anymore. */
272 /* disable urgency timer, if needed */
273 if (con->urgency_timer != NULL) {
274 DLOG("Removing urgency timer of con %p\n", con);
275 workspace_update_urgent_flag(ws);
276 ev_timer_stop(main_loop, con->urgency_timer);
277 FREE(con->urgency_timer);
280 if (con->type != CT_FLOATING_CON) {
281 /* If the container is *not* floating, we might need to re-distribute
282 * percentage values for the resized containers. */
283 con_fix_percent(parent);
286 /* Render the tree so that the surrounding containers take up the space
287 * which 'con' does no longer occupy. If we don’t render here, there will
288 * be a gap in our containers and that could trigger an EnterNotify for an
289 * underlying container, see ticket #660.
291 * Rendering has to be avoided when dont_kill_parent is set (when
292 * tree_close calls itself recursively) because the tree is in a
293 * non-renderable state during that time. */
294 if (!dont_kill_parent)
297 /* kill the X11 part of this container */
300 if (con_is_floating(con)) {
301 DLOG("Container was floating, killing floating container\n");
302 tree_close(parent, DONT_KILL_WINDOW, false, (con == focused));
303 DLOG("parent container killed\n");
307 FREE(con->deco_render_params);
308 TAILQ_REMOVE(&all_cons, con, all_cons);
311 /* in the case of floating windows, we already focused another container
312 * when closing the parent, so we can exit now. */
314 DLOG("No next container, i will just exit now\n");
318 if (was_mapped || con == focused) {
319 if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
320 DLOG("focusing %p / %s\n", next, next->name);
321 if (next->type == CT_DOCKAREA) {
322 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
323 con_focus(con_descend_focused(output_get_content(next->parent)));
325 if (!force_set_focus && con != focused)
326 DLOG("not changing focus, the container was not focused before\n");
327 else con_focus(next);
331 DLOG("not focusing because we're not killing anybody\n");
334 DLOG("not focusing, was not mapped\n");
337 /* check if the parent container is empty now and close it */
338 if (!dont_kill_parent)
339 CALL(parent, on_remove_child);
345 * Closes the current container using tree_close().
348 void tree_close_con(kill_window_t kill_window) {
349 assert(focused != NULL);
350 if (focused->type == CT_WORKSPACE) {
351 LOG("Cannot close workspace\n");
355 /* There *should* be no possibility to focus outputs / root container */
356 assert(focused->type != CT_OUTPUT);
357 assert(focused->type != CT_ROOT);
360 tree_close(focused, kill_window, false, false);
364 * Splits (horizontally or vertically) the given container by creating a new
365 * container which contains the old one and the future ones.
368 void tree_split(Con *con, orientation_t orientation) {
369 /* for a workspace, we just need to change orientation */
370 if (con->type == CT_WORKSPACE) {
371 DLOG("Workspace, simply changing orientation to %d\n", orientation);
372 con->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
375 else if (con->type == CT_FLOATING_CON) {
376 DLOG("Floating containers can't be split.\n");
380 Con *parent = con->parent;
382 /* Force re-rendering to make the indicator border visible. */
383 FREE(con->deco_render_params);
384 FREE(parent->deco_render_params);
386 /* if we are in a container whose parent contains only one
387 * child (its split functionality is unused so far), we just change the
388 * orientation (more intuitive than splitting again) */
389 if (con_num_children(parent) == 1 &&
390 (parent->layout == L_SPLITH ||
391 parent->layout == L_SPLITV)) {
392 parent->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
393 DLOG("Just changing orientation of existing container\n");
397 DLOG("Splitting in orientation %d\n", orientation);
399 /* 2: replace it with a new Con */
400 Con *new = con_new(NULL, NULL);
401 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
402 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
403 new->parent = parent;
404 new->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
406 /* 3: swap 'percent' (resize factor) */
407 new->percent = con->percent;
410 /* 4: add it as a child to the new Con */
411 con_attach(con, new, false);
415 * Moves focus one level up. Returns true if focus changed.
418 bool level_up(void) {
419 /* Skip over floating containers and go directly to the grandparent
420 * (which should always be a workspace) */
421 if (focused->parent->type == CT_FLOATING_CON) {
422 con_focus(focused->parent->parent);
426 /* We can focus up to the workspace, but not any higher in the tree */
427 if ((focused->parent->type != CT_CON &&
428 focused->parent->type != CT_WORKSPACE) ||
429 focused->type == CT_WORKSPACE) {
430 ELOG("'focus parent': Focus is already on the workspace, cannot go higher than that.\n");
433 con_focus(focused->parent);
438 * Moves focus one level down. Returns true if focus changed.
441 bool level_down(void) {
442 /* Go down the focus stack of the current node */
443 Con *next = TAILQ_FIRST(&(focused->focus_head));
444 if (next == TAILQ_END(&(focused->focus_head))) {
445 DLOG("cannot go down\n");
448 else if (next->type == CT_FLOATING_CON) {
449 /* Floating cons shouldn't be directly focused; try immediately
450 * going to the grandchild of the focused con. */
451 Con *child = TAILQ_FIRST(&(next->focus_head));
452 if (child == TAILQ_END(&(next->focus_head))) {
453 DLOG("cannot go down\n");
457 next = TAILQ_FIRST(&(next->focus_head));
464 static void mark_unmapped(Con *con) {
468 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
469 mark_unmapped(current);
470 if (con->type == CT_WORKSPACE) {
471 /* We need to call mark_unmapped on floating nodes aswell since we can
472 * make containers floating. */
473 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
474 mark_unmapped(current);
479 * Renders the tree, that is rendering all outputs using render_con() and
480 * pushing the changes to X11 using x_push_changes().
483 void tree_render(void) {
487 DLOG("-- BEGIN RENDERING --\n");
488 /* Reset map state for all nodes in tree */
489 /* TODO: a nicer method to walk all nodes would be good, maybe? */
490 mark_unmapped(croot);
491 croot->mapped = true;
493 render_con(croot, false);
495 x_push_changes(croot);
496 DLOG("-- END RENDERING --\n");
500 * Recursive function to walk the tree until a con can be found to focus.
503 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
504 /* When dealing with fullscreen containers, it's necessary to go up to the
505 * workspace level, because 'focus $dir' will start at the con's real
506 * position in the tree, and it may not be possible to get to the edge
507 * normally due to fullscreen focusing restrictions. */
508 if (con->fullscreen_mode == CF_OUTPUT && con->type != CT_WORKSPACE)
509 con = con_get_workspace(con);
511 /* Stop recursing at workspaces after attempting to switch to next
512 * workspace if possible. */
513 if (con->type == CT_WORKSPACE) {
514 if (con_get_fullscreen_con(con, CF_GLOBAL)) {
515 DLOG("Cannot change workspace while in global fullscreen mode.\n");
518 Output *current_output = get_output_containing(con->rect.x, con->rect.y);
523 DLOG("Current output is %s\n", current_output->name);
525 /* Try to find next output */
526 direction_t direction;
527 if (way == 'n' && orientation == HORIZ)
529 else if (way == 'p' && orientation == HORIZ)
531 else if (way == 'n' && orientation == VERT)
533 else if (way == 'p' && orientation == VERT)
538 next_output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
541 DLOG("Next output is %s\n", next_output->name);
543 /* Find visible workspace on next output */
544 Con *workspace = NULL;
545 GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
547 /* Show next workspace and focus appropriate container if possible. */
551 workspace_show(workspace);
553 /* If a workspace has an active fullscreen container, one of its
554 * children should always be focused. The above workspace_show()
555 * should be adequate for that, so return. */
556 if (con_get_fullscreen_con(workspace, CF_OUTPUT))
559 Con *focus = con_descend_direction(workspace, direction);
562 x_set_warp_to(&(focus->rect));
567 Con *parent = con->parent;
569 if (con->type == CT_FLOATING_CON) {
570 /* left/right focuses the previous/next floating container */
571 if (orientation == HORIZ) {
574 next = TAILQ_NEXT(con, floating_windows);
575 else next = TAILQ_PREV(con, floating_head, floating_windows);
577 /* If there is no next/previous container, wrap */
580 next = TAILQ_FIRST(&(parent->floating_head));
581 else next = TAILQ_LAST(&(parent->floating_head), floating_head);
584 /* Still no next/previous container? bail out */
588 con_focus(con_descend_focused(next));
591 /* up/down cycles through the Z-index */
592 /* TODO: implement cycling through the z-index */
597 /* If the orientation does not match or there is no other con to focus, we
598 * need to go higher in the hierarchy */
599 if (con_orientation(parent) != orientation ||
600 con_num_children(parent) == 1)
601 return _tree_next(parent, way, orientation, wrap);
603 Con *current = TAILQ_FIRST(&(parent->focus_head));
604 /* TODO: when can the following happen (except for floating windows, which
605 * are handled above)? */
606 if (TAILQ_EMPTY(&(parent->nodes_head))) {
607 DLOG("nothing to focus\n");
613 next = TAILQ_NEXT(current, nodes);
614 else next = TAILQ_PREV(current, nodes_head, nodes);
617 if (!config.force_focus_wrapping) {
618 /* If there is no next/previous container, we check if we can focus one
619 * when going higher (without wrapping, though). If so, we are done, if
621 if (_tree_next(parent, way, orientation, false))
629 next = TAILQ_FIRST(&(parent->nodes_head));
630 else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
633 /* Don't violate fullscreen focus restrictions. */
634 if (!con_fullscreen_permits_focusing(next))
637 /* 3: focus choice comes in here. at the moment we will go down
638 * until we find a window */
639 /* TODO: check for window, atm we only go down as far as possible */
640 con_focus(con_descend_focused(next));
645 * Changes focus in the given way (next/previous) and given orientation
646 * (horizontal/vertical).
649 void tree_next(char way, orientation_t orientation) {
650 _tree_next(focused, way, orientation, true);
654 * tree_flatten() removes pairs of redundant split containers, e.g.:
655 * [workspace, horizontal]
659 * In this example, the v-split and h-split container are redundant.
660 * Such a situation can be created by moving containers in a direction which is
661 * not the orientation of their parent container. i3 needs to create a new
662 * split container then and if you move containers this way multiple times,
663 * redundant chains of split-containers can be the result.
666 void tree_flatten(Con *con) {
667 Con *current, *child, *parent = con->parent;
668 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
670 /* We only consider normal containers without windows */
671 if (con->type != CT_CON ||
672 parent->layout == L_OUTPUT || /* con == "content" */
676 /* Ensure it got only one child */
677 child = TAILQ_FIRST(&(con->nodes_head));
678 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
681 DLOG("child = %p, con = %p, parent = %p\n", child, con, parent);
683 /* The child must have a different orientation than the con but the same as
684 * the con’s parent to be redundant */
685 if (!con_is_split(con) ||
686 !con_is_split(child) ||
687 con_orientation(con) == con_orientation(child) ||
688 con_orientation(child) != con_orientation(parent))
691 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
693 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
695 DLOG("detaching...\n");
696 /* 2: re-attach the children to the parent before con */
697 while (!TAILQ_EMPTY(&(child->nodes_head))) {
698 current = TAILQ_FIRST(&(child->nodes_head));
699 DLOG("detaching current=%p / %s\n", current, current->name);
701 DLOG("re-attaching\n");
702 /* We don’t use con_attach() here because for a CT_CON, the special
703 * case handling of con_attach() does not trigger. So all it would do
704 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
705 * directly use the TAILQ macros. */
706 current->parent = parent;
707 TAILQ_INSERT_BEFORE(con, current, nodes);
708 DLOG("attaching to focus list\n");
709 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
710 current->percent = con->percent;
712 DLOG("re-attached all\n");
714 /* 3: restore focus, if con was focused */
715 if (focus_next != NULL &&
716 TAILQ_FIRST(&(parent->focus_head)) == con) {
717 DLOG("restoring focus to focus_next=%p\n", focus_next);
718 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
719 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
720 DLOG("restored focus.\n");
723 /* 4: close the redundant cons */
724 DLOG("closing redundant cons\n");
725 tree_close(con, DONT_KILL_WINDOW, true, false);
727 /* Well, we got to abort the recursion here because we destroyed the
728 * container. However, if tree_flatten() is called sufficiently often,
729 * there can’t be the situation of having two pairs of redundant containers
730 * at once. Therefore, we can safely abort the recursion on this level
731 * after flattening. */
735 /* We cannot use normal foreach here because tree_flatten might close the
736 * current container. */
737 current = TAILQ_FIRST(&(con->nodes_head));
738 while (current != NULL) {
739 Con *next = TAILQ_NEXT(current, nodes);
740 tree_flatten(current);
744 current = TAILQ_FIRST(&(con->floating_head));
745 while (current != NULL) {
746 Con *next = TAILQ_NEXT(current, floating_windows);
747 tree_flatten(current);