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);
79 croot->rect = (Rect) {
86 tree_append_json(focused, globbed, NULL);
88 DLOG("appended tree, using new root\n");
89 croot = TAILQ_FIRST(&(croot->nodes_head));
90 DLOG("new root = %p\n", croot);
91 Con *out = TAILQ_FIRST(&(croot->nodes_head));
92 DLOG("out = %p\n", out);
93 Con *ws = TAILQ_FIRST(&(out->nodes_head));
94 DLOG("ws = %p\n", ws);
96 /* For in-place restarting into v4.2, we need to make sure the new
97 * pseudo-output __i3 is present. */
98 if (strcmp(out->name, "__i3") != 0) {
99 DLOG("Adding pseudo-output __i3 during inplace restart\n");
100 Con *__i3 = _create___i3();
101 /* Ensure that it is the first output, other places in the code make
102 * that assumption. */
103 TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes);
104 TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes);
111 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
112 * root node are created in randr.c for each Output.
115 void tree_init(xcb_get_geometry_reply_t *geometry) {
116 croot = con_new(NULL, NULL);
118 croot->name = "root";
119 croot->type = CT_ROOT;
120 croot->layout = L_SPLITH;
121 croot->rect = (Rect) {
131 * Opens an empty container in the current container
134 Con *tree_open_con(Con *con, i3Window *window) {
136 /* every focusable Con has a parent (outputs have parent root) */
137 con = focused->parent;
138 /* If the parent is an output, we are on a workspace. In this case,
139 * the new container needs to be opened as a leaf of the workspace. */
140 if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
144 /* If the currently focused container is a floating container, we
145 * attach the new container to the currently focused spot in its
147 if (con->type == CT_FLOATING_CON) {
148 con = con_descend_tiling_focused(con->parent);
149 if (con->type != CT_WORKSPACE)
152 DLOG("con = %p\n", con);
157 /* 3. create the container and attach it to its parent */
158 Con *new = con_new(con, window);
159 new->layout = L_SPLITH;
161 /* 4: re-calculate child->percent for each child */
162 con_fix_percent(con);
167 static bool _is_con_mapped(Con *con) {
170 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
171 if (_is_con_mapped(child))
178 * Closes the given container including all children.
179 * Returns true if the container was killed or false if just WM_DELETE was sent
180 * and the window is expected to kill itself.
182 * The dont_kill_parent flag is specified when the function calls itself
183 * recursively while deleting a containers children.
185 * The force_set_focus flag is specified in the case of killing a floating
186 * window: tree_close() will be invoked for the CT_FLOATINGCON (the parent
187 * container) and focus should be set there.
190 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus) {
191 bool was_mapped = con->mapped;
192 Con *parent = con->parent;
195 /* Even if the container itself is not mapped, its children may be
196 * mapped (for example split containers don't have a mapped window on
197 * their own but usually contain mapped children). */
198 was_mapped = _is_con_mapped(con);
201 /* remove the urgency hint of the workspace (if set) */
204 con_update_parents_urgency(con);
205 workspace_update_urgent_flag(con_get_workspace(con));
208 /* Get the container which is next focused */
209 Con *next = con_next_focused(con);
210 DLOG("next = %p, focused = %p\n", next, focused);
212 DLOG("closing %p, kill_window = %d\n", con, kill_window);
213 Con *child, *nextchild;
214 bool abort_kill = false;
215 /* We cannot use TAILQ_FOREACH because the children get deleted
216 * in their parent’s nodes_head */
217 for (child = TAILQ_FIRST(&(con->nodes_head)); child;) {
218 nextchild = TAILQ_NEXT(child, nodes);
219 DLOG("killing child=%p\n", child);
220 if (!tree_close(child, kill_window, true, false))
226 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
230 if (con->window != NULL) {
231 if (kill_window != DONT_KILL_WINDOW) {
232 x_window_kill(con->window->id, kill_window);
235 xcb_void_cookie_t cookie;
236 /* Ignore any further events by clearing the event mask,
238 * then reparent it to the root window. */
239 xcb_change_window_attributes(conn, con->window->id,
240 XCB_CW_EVENT_MASK, (uint32_t[]) {XCB_NONE});
241 xcb_unmap_window(conn, con->window->id);
242 cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
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 /* We are no longer handling this window, thus set WM_STATE to
249 * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
250 long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
251 cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
252 con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
254 /* Ignore X11 errors for the ReparentWindow request.
255 * X11 Errors are returned when the window was already destroyed */
256 add_ignore_event(cookie.sequence, 0);
258 FREE(con->window->class_class);
259 FREE(con->window->class_instance);
260 i3string_free(con->window->name);
261 FREE(con->window->ran_assignments);
265 Con *ws = con_get_workspace(con);
267 /* Figure out which container to focus next before detaching 'con'. */
268 if (con_is_floating(con)) {
269 if (con == focused) {
270 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
271 next = con_next_focused(parent);
273 dont_kill_parent = true;
274 DLOG("Alright, focusing %p\n", next);
280 /* Detach the container so that it will not be rendered anymore. */
283 /* disable urgency timer, if needed */
284 if (con->urgency_timer != NULL) {
285 DLOG("Removing urgency timer of con %p\n", con);
286 workspace_update_urgent_flag(ws);
287 ev_timer_stop(main_loop, con->urgency_timer);
288 FREE(con->urgency_timer);
291 if (con->type != CT_FLOATING_CON) {
292 /* If the container is *not* floating, we might need to re-distribute
293 * percentage values for the resized containers. */
294 con_fix_percent(parent);
297 /* Render the tree so that the surrounding containers take up the space
298 * which 'con' does no longer occupy. If we don’t render here, there will
299 * be a gap in our containers and that could trigger an EnterNotify for an
300 * underlying container, see ticket #660.
302 * Rendering has to be avoided when dont_kill_parent is set (when
303 * tree_close calls itself recursively) because the tree is in a
304 * non-renderable state during that time. */
305 if (!dont_kill_parent)
308 /* kill the X11 part of this container */
311 if (con_is_floating(con)) {
312 DLOG("Container was floating, killing floating container\n");
313 tree_close(parent, DONT_KILL_WINDOW, false, (con == focused));
314 DLOG("parent container killed\n");
318 FREE(con->deco_render_params);
319 TAILQ_REMOVE(&all_cons, con, all_cons);
322 /* in the case of floating windows, we already focused another container
323 * when closing the parent, so we can exit now. */
325 DLOG("No next container, i will just exit now\n");
329 if (was_mapped || con == focused) {
330 if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
331 DLOG("focusing %p / %s\n", next, next->name);
332 if (next->type == CT_DOCKAREA) {
333 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
334 con_focus(con_descend_focused(output_get_content(next->parent)));
336 if (!force_set_focus && con != focused)
337 DLOG("not changing focus, the container was not focused before\n");
342 DLOG("not focusing because we're not killing anybody\n");
345 DLOG("not focusing, was not mapped\n");
348 /* check if the parent container is empty now and close it */
349 if (!dont_kill_parent)
350 CALL(parent, on_remove_child);
356 * Closes the current container using tree_close().
359 void tree_close_con(kill_window_t kill_window) {
360 assert(focused != NULL);
362 /* There *should* be no possibility to focus outputs / root container */
363 assert(focused->type != CT_OUTPUT);
364 assert(focused->type != CT_ROOT);
366 if (focused->type == CT_WORKSPACE) {
367 DLOG("Workspaces cannot be close, closing all children instead\n");
368 Con *child, *nextchild;
369 for (child = TAILQ_FIRST(&(focused->focus_head)); child;) {
370 nextchild = TAILQ_NEXT(child, focused);
371 DLOG("killing child=%p\n", child);
372 tree_close(child, kill_window, false, false);
380 tree_close(focused, kill_window, false, false);
384 * Splits (horizontally or vertically) the given container by creating a new
385 * container which contains the old one and the future ones.
388 void tree_split(Con *con, orientation_t orientation) {
389 if (con_is_floating(con)) {
390 DLOG("Floating containers can't be split.\n");
394 if (con->type == CT_WORKSPACE) {
395 if (con_num_children(con) < 2) {
396 DLOG("Just changing orientation of workspace\n");
397 con->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
400 /* if there is more than one container on the workspace
401 * move them into a new container and handle this instead */
402 con = workspace_encapsulate(con);
406 Con *parent = con->parent;
408 /* Force re-rendering to make the indicator border visible. */
409 FREE(con->deco_render_params);
410 FREE(parent->deco_render_params);
412 /* if we are in a container whose parent contains only one
413 * child (its split functionality is unused so far), we just change the
414 * orientation (more intuitive than splitting again) */
415 if (con_num_children(parent) == 1 &&
416 (parent->layout == L_SPLITH ||
417 parent->layout == L_SPLITV)) {
418 parent->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
419 DLOG("Just changing orientation of existing container\n");
423 DLOG("Splitting in orientation %d\n", orientation);
425 /* 2: replace it with a new Con */
426 Con *new = con_new(NULL, NULL);
427 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
428 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
429 new->parent = parent;
430 new->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
432 /* 3: swap 'percent' (resize factor) */
433 new->percent = con->percent;
436 /* 4: add it as a child to the new Con */
437 con_attach(con, new, false);
441 * Moves focus one level up. Returns true if focus changed.
444 bool level_up(void) {
445 /* Skip over floating containers and go directly to the grandparent
446 * (which should always be a workspace) */
447 if (focused->parent->type == CT_FLOATING_CON) {
448 con_focus(focused->parent->parent);
452 /* We can focus up to the workspace, but not any higher in the tree */
453 if ((focused->parent->type != CT_CON &&
454 focused->parent->type != CT_WORKSPACE) ||
455 focused->type == CT_WORKSPACE) {
456 ELOG("'focus parent': Focus is already on the workspace, cannot go higher than that.\n");
459 con_focus(focused->parent);
464 * Moves focus one level down. Returns true if focus changed.
467 bool level_down(void) {
468 /* Go down the focus stack of the current node */
469 Con *next = TAILQ_FIRST(&(focused->focus_head));
470 if (next == TAILQ_END(&(focused->focus_head))) {
471 DLOG("cannot go down\n");
473 } else if (next->type == CT_FLOATING_CON) {
474 /* Floating cons shouldn't be directly focused; try immediately
475 * going to the grandchild of the focused con. */
476 Con *child = TAILQ_FIRST(&(next->focus_head));
477 if (child == TAILQ_END(&(next->focus_head))) {
478 DLOG("cannot go down\n");
481 next = TAILQ_FIRST(&(next->focus_head));
488 static void mark_unmapped(Con *con) {
492 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
493 mark_unmapped(current);
494 if (con->type == CT_WORKSPACE) {
495 /* We need to call mark_unmapped on floating nodes aswell since we can
496 * make containers floating. */
497 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
498 mark_unmapped(current);
503 * Renders the tree, that is rendering all outputs using render_con() and
504 * pushing the changes to X11 using x_push_changes().
507 void tree_render(void) {
511 DLOG("-- BEGIN RENDERING --\n");
512 /* Reset map state for all nodes in tree */
513 /* TODO: a nicer method to walk all nodes would be good, maybe? */
514 mark_unmapped(croot);
515 croot->mapped = true;
517 render_con(croot, false);
519 x_push_changes(croot);
520 DLOG("-- END RENDERING --\n");
524 * Recursive function to walk the tree until a con can be found to focus.
527 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
528 /* When dealing with fullscreen containers, it's necessary to go up to the
529 * workspace level, because 'focus $dir' will start at the con's real
530 * position in the tree, and it may not be possible to get to the edge
531 * normally due to fullscreen focusing restrictions. */
532 if (con->fullscreen_mode == CF_OUTPUT && con->type != CT_WORKSPACE)
533 con = con_get_workspace(con);
535 /* Stop recursing at workspaces after attempting to switch to next
536 * workspace if possible. */
537 if (con->type == CT_WORKSPACE) {
538 if (con_get_fullscreen_con(con, CF_GLOBAL)) {
539 DLOG("Cannot change workspace while in global fullscreen mode.\n");
542 Output *current_output = get_output_containing(con->rect.x, con->rect.y);
547 DLOG("Current output is %s\n", current_output->name);
549 /* Try to find next output */
550 direction_t direction;
551 if (way == 'n' && orientation == HORIZ)
553 else if (way == 'p' && orientation == HORIZ)
555 else if (way == 'n' && orientation == VERT)
557 else if (way == 'p' && orientation == VERT)
562 next_output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
565 DLOG("Next output is %s\n", next_output->name);
567 /* Find visible workspace on next output */
568 Con *workspace = NULL;
569 GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
571 /* Show next workspace and focus appropriate container if possible. */
575 workspace_show(workspace);
577 /* If a workspace has an active fullscreen container, one of its
578 * children should always be focused. The above workspace_show()
579 * should be adequate for that, so return. */
580 if (con_get_fullscreen_con(workspace, CF_OUTPUT))
583 Con *focus = con_descend_direction(workspace, direction);
586 x_set_warp_to(&(focus->rect));
591 Con *parent = con->parent;
593 if (con->type == CT_FLOATING_CON) {
594 /* left/right focuses the previous/next floating container */
595 if (orientation == HORIZ) {
598 next = TAILQ_NEXT(con, floating_windows);
600 next = TAILQ_PREV(con, floating_head, floating_windows);
602 /* If there is no next/previous container, wrap */
605 next = TAILQ_FIRST(&(parent->floating_head));
607 next = TAILQ_LAST(&(parent->floating_head), floating_head);
610 /* Still no next/previous container? bail out */
614 con_focus(con_descend_focused(next));
617 /* up/down cycles through the Z-index */
618 /* TODO: implement cycling through the z-index */
623 /* If the orientation does not match or there is no other con to focus, we
624 * need to go higher in the hierarchy */
625 if (con_orientation(parent) != orientation ||
626 con_num_children(parent) == 1)
627 return _tree_next(parent, way, orientation, wrap);
629 Con *current = TAILQ_FIRST(&(parent->focus_head));
630 /* TODO: when can the following happen (except for floating windows, which
631 * are handled above)? */
632 if (TAILQ_EMPTY(&(parent->nodes_head))) {
633 DLOG("nothing to focus\n");
639 next = TAILQ_NEXT(current, nodes);
641 next = TAILQ_PREV(current, nodes_head, nodes);
644 if (!config.force_focus_wrapping) {
645 /* If there is no next/previous container, we check if we can focus one
646 * when going higher (without wrapping, though). If so, we are done, if
648 if (_tree_next(parent, way, orientation, false))
656 next = TAILQ_FIRST(&(parent->nodes_head));
658 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
661 /* Don't violate fullscreen focus restrictions. */
662 if (!con_fullscreen_permits_focusing(next))
665 /* 3: focus choice comes in here. at the moment we will go down
666 * until we find a window */
667 /* TODO: check for window, atm we only go down as far as possible */
668 con_focus(con_descend_focused(next));
673 * Changes focus in the given way (next/previous) and given orientation
674 * (horizontal/vertical).
677 void tree_next(char way, orientation_t orientation) {
678 _tree_next(focused, way, orientation, true);
682 * tree_flatten() removes pairs of redundant split containers, e.g.:
683 * [workspace, horizontal]
687 * In this example, the v-split and h-split container are redundant.
688 * Such a situation can be created by moving containers in a direction which is
689 * not the orientation of their parent container. i3 needs to create a new
690 * split container then and if you move containers this way multiple times,
691 * redundant chains of split-containers can be the result.
694 void tree_flatten(Con *con) {
695 Con *current, *child, *parent = con->parent;
696 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
698 /* We only consider normal containers without windows */
699 if (con->type != CT_CON ||
700 parent->layout == L_OUTPUT || /* con == "content" */
704 /* Ensure it got only one child */
705 child = TAILQ_FIRST(&(con->nodes_head));
706 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
709 DLOG("child = %p, con = %p, parent = %p\n", child, con, parent);
711 /* The child must have a different orientation than the con but the same as
712 * the con’s parent to be redundant */
713 if (!con_is_split(con) ||
714 !con_is_split(child) ||
715 (con->layout != L_SPLITH && con->layout != L_SPLITV) ||
716 (child->layout != L_SPLITH && child->layout != L_SPLITV) ||
717 con_orientation(con) == con_orientation(child) ||
718 con_orientation(child) != con_orientation(parent))
721 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
723 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
725 DLOG("detaching...\n");
726 /* 2: re-attach the children to the parent before con */
727 while (!TAILQ_EMPTY(&(child->nodes_head))) {
728 current = TAILQ_FIRST(&(child->nodes_head));
729 DLOG("detaching current=%p / %s\n", current, current->name);
731 DLOG("re-attaching\n");
732 /* We don’t use con_attach() here because for a CT_CON, the special
733 * case handling of con_attach() does not trigger. So all it would do
734 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
735 * directly use the TAILQ macros. */
736 current->parent = parent;
737 TAILQ_INSERT_BEFORE(con, current, nodes);
738 DLOG("attaching to focus list\n");
739 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
740 current->percent = con->percent;
742 DLOG("re-attached all\n");
744 /* 3: restore focus, if con was focused */
745 if (focus_next != NULL &&
746 TAILQ_FIRST(&(parent->focus_head)) == con) {
747 DLOG("restoring focus to focus_next=%p\n", focus_next);
748 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
749 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
750 DLOG("restored focus.\n");
753 /* 4: close the redundant cons */
754 DLOG("closing redundant cons\n");
755 tree_close(con, DONT_KILL_WINDOW, true, false);
757 /* Well, we got to abort the recursion here because we destroyed the
758 * container. However, if tree_flatten() is called sufficiently often,
759 * there can’t be the situation of having two pairs of redundant containers
760 * at once. Therefore, we can safely abort the recursion on this level
761 * after flattening. */
765 /* We cannot use normal foreach here because tree_flatten might close the
766 * current container. */
767 current = TAILQ_FIRST(&(con->nodes_head));
768 while (current != NULL) {
769 Con *next = TAILQ_NEXT(current, nodes);
770 tree_flatten(current);
774 current = TAILQ_FIRST(&(con->floating_head));
775 while (current != NULL) {
776 Con *next = TAILQ_NEXT(current, floating_windows);
777 tree_flatten(current);