2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * tree.c: Everything that primarily modifies the layout tree data structure.
15 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
18 * Create the pseudo-output __i3. Output-independent workspaces such as
19 * __i3_scratch will live there.
22 static Con *_create___i3(void) {
23 Con *__i3 = con_new(croot, NULL);
25 __i3->name = sstrdup("__i3");
26 __i3->type = CT_OUTPUT;
27 __i3->layout = L_OUTPUT;
28 con_fix_percent(croot);
29 x_set_name(__i3, "[i3 con] pseudo-output __i3");
30 /* For retaining the correct position/size of a scratchpad window, the
31 * dimensions of the real outputs should be multiples of the __i3
32 * pseudo-output. Ensuring that is the job of scratchpad_fix_resolution()
33 * which gets called after this function and after detecting all the
34 * outputs (or whenever an output changes). */
35 __i3->rect.width = 1280;
36 __i3->rect.height = 1024;
38 /* Add a content container. */
39 DLOG("adding main content container\n");
40 Con *content = con_new(NULL, NULL);
41 content->type = CT_CON;
43 content->name = sstrdup("content");
44 content->layout = L_SPLITH;
46 x_set_name(content, "[i3 con] content __i3");
47 con_attach(content, __i3, false);
49 /* Attach the __i3_scratch workspace. */
50 Con *ws = con_new(NULL, NULL);
51 ws->type = CT_WORKSPACE;
53 ws->name = sstrdup("__i3_scratch");
54 ws->layout = L_SPLITH;
55 con_attach(ws, content, false);
56 x_set_name(ws, "[i3 con] workspace __i3_scratch");
57 ws->fullscreen_mode = CF_OUTPUT;
63 * Loads tree from 'path' (used for in-place restarts).
66 bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry) {
68 char *globbed = resolve_tilde(path);
71 if (!path_exists(globbed)) {
72 LOG("%s does not exist, not restoring tree\n", globbed);
77 if ((len = slurp(globbed, &buf)) < 0) {
78 /* slurp already logged an error. */
82 /* TODO: refactor the following */
83 croot = con_new(NULL, NULL);
91 tree_append_json(focused, buf, len, NULL);
93 DLOG("appended tree, using new root\n");
94 croot = TAILQ_FIRST(&(croot->nodes_head));
96 /* tree_append_json failed. Continuing here would segfault. */
99 DLOG("new root = %p\n", croot);
100 Con *out = TAILQ_FIRST(&(croot->nodes_head));
101 DLOG("out = %p\n", out);
102 Con *ws = TAILQ_FIRST(&(out->nodes_head));
103 DLOG("ws = %p\n", ws);
105 /* For in-place restarting into v4.2, we need to make sure the new
106 * pseudo-output __i3 is present. */
107 if (strcmp(out->name, "__i3") != 0) {
108 DLOG("Adding pseudo-output __i3 during inplace restart\n");
109 Con *__i3 = _create___i3();
110 /* Ensure that it is the first output, other places in the code make
111 * that assumption. */
112 TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes);
113 TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes);
116 restore_open_placeholder_windows(croot);
126 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
127 * root node are created in randr.c for each Output.
130 void tree_init(xcb_get_geometry_reply_t *geometry) {
131 croot = con_new(NULL, NULL);
133 croot->name = "root";
134 croot->type = CT_ROOT;
135 croot->layout = L_SPLITH;
136 croot->rect = (Rect){
146 * Opens an empty container in the current container
149 Con *tree_open_con(Con *con, i3Window *window) {
151 /* every focusable Con has a parent (outputs have parent root) */
152 con = focused->parent;
153 /* If the parent is an output, we are on a workspace. In this case,
154 * the new container needs to be opened as a leaf of the workspace. */
155 if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
159 /* If the currently focused container is a floating container, we
160 * attach the new container to the currently focused spot in its
162 if (con->type == CT_FLOATING_CON) {
163 con = con_descend_tiling_focused(con->parent);
164 if (con->type != CT_WORKSPACE)
167 DLOG("con = %p\n", con);
172 /* 3. create the container and attach it to its parent */
173 Con *new = con_new(con, window);
174 new->layout = L_SPLITH;
176 /* 4: re-calculate child->percent for each child */
177 con_fix_percent(con);
183 * Closes the given container including all children.
184 * Returns true if the container was killed or false if just WM_DELETE was sent
185 * and the window is expected to kill itself.
187 * The dont_kill_parent flag is specified when the function calls itself
188 * recursively while deleting a containers children.
191 bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent) {
192 Con *parent = con->parent;
194 /* remove the urgency hint of the workspace (if set) */
196 con_set_urgency(con, false);
197 con_update_parents_urgency(con);
198 workspace_update_urgent_flag(con_get_workspace(con));
201 DLOG("closing %p, kill_window = %d\n", con, kill_window);
202 Con *child, *nextchild;
203 bool abort_kill = false;
204 /* We cannot use TAILQ_FOREACH because the children get deleted
205 * in their parent’s nodes_head */
206 for (child = TAILQ_FIRST(&(con->nodes_head)); child;) {
207 nextchild = TAILQ_NEXT(child, nodes);
208 DLOG("killing child=%p\n", child);
209 if (!tree_close_internal(child, kill_window, true)) {
216 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
220 if (con->window != NULL) {
221 if (kill_window != DONT_KILL_WINDOW) {
222 x_window_kill(con->window->id, kill_window);
225 xcb_void_cookie_t cookie;
226 /* Ignore any further events by clearing the event mask,
228 * then reparent it to the root window. */
229 xcb_change_window_attributes(conn, con->window->id,
230 XCB_CW_EVENT_MASK, (uint32_t[]){XCB_NONE});
231 xcb_unmap_window(conn, con->window->id);
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 /* Remove the window from the save set. All windows in the save set
245 * will be mapped when i3 closes its connection (e.g. when
246 * restarting). This is not what we want, since some apps keep
247 * unmapped windows around and don’t expect them to suddenly be
248 * mapped. See https://bugs.i3wm.org/1617 */
249 xcb_change_save_set(conn, XCB_SET_MODE_DELETE, con->window->id);
251 /* Stop receiving ShapeNotify events. */
252 if (shape_supported) {
253 xcb_shape_select_input(conn, con->window->id, false);
256 /* Ignore X11 errors for the ReparentWindow request.
257 * X11 Errors are returned when the window was already destroyed */
258 add_ignore_event(cookie.sequence, 0);
260 ipc_send_window_event("close", con);
261 window_free(con->window);
265 Con *ws = con_get_workspace(con);
267 /* Figure out which container to focus next before detaching 'con'. */
268 Con *next = (con == focused) ? con_next_focused(con) : NULL;
269 DLOG("next = %p, focused = %p\n", next, focused);
271 /* Detach the container so that it will not be rendered anymore. */
274 /* disable urgency timer, if needed */
275 if (con->urgency_timer != NULL) {
276 DLOG("Removing urgency timer of con %p\n", con);
277 workspace_update_urgent_flag(ws);
278 ev_timer_stop(main_loop, con->urgency_timer);
279 FREE(con->urgency_timer);
282 if (con->type != CT_FLOATING_CON) {
283 /* If the container is *not* floating, we might need to re-distribute
284 * percentage values for the resized containers. */
285 con_fix_percent(parent);
288 /* Render the tree so that the surrounding containers take up the space
289 * which 'con' does no longer occupy. If we don’t render here, there will
290 * be a gap in our containers and that could trigger an EnterNotify for an
291 * underlying container, see ticket #660.
293 * Rendering has to be avoided when dont_kill_parent is set (when
294 * tree_close_internal calls itself recursively) because the tree is in a
295 * non-renderable state during that time. */
296 if (!dont_kill_parent)
299 /* kill the X11 part of this container */
303 DLOG("Closing workspace container %s, updating EWMH atoms\n", ws->name);
304 ewmh_update_desktop_properties();
312 DLOG("not changing focus, the container was not focused before\n");
315 /* check if the parent container is empty now and close it */
316 if (!dont_kill_parent)
317 CALL(parent, on_remove_child);
323 * Splits (horizontally or vertically) the given container by creating a new
324 * container which contains the old one and the future ones.
327 void tree_split(Con *con, orientation_t orientation) {
328 if (con_is_floating(con)) {
329 DLOG("Floating containers can't be split.\n");
333 if (con->type == CT_WORKSPACE) {
334 if (con_num_children(con) < 2) {
335 if (con_num_children(con) == 0) {
336 DLOG("Changing workspace_layout to L_DEFAULT\n");
337 con->workspace_layout = L_DEFAULT;
339 DLOG("Changing orientation of workspace\n");
340 con->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
343 /* if there is more than one container on the workspace
344 * move them into a new container and handle this instead */
345 con = workspace_encapsulate(con);
349 Con *parent = con->parent;
351 /* Force re-rendering to make the indicator border visible. */
352 con_force_split_parents_redraw(con);
354 /* if we are in a container whose parent contains only one
355 * child (its split functionality is unused so far), we just change the
356 * orientation (more intuitive than splitting again) */
357 if (con_num_children(parent) == 1 &&
358 (parent->layout == L_SPLITH ||
359 parent->layout == L_SPLITV)) {
360 parent->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
361 DLOG("Just changing orientation of existing container\n");
365 DLOG("Splitting in orientation %d\n", orientation);
367 /* 2: replace it with a new Con */
368 Con *new = con_new(NULL, NULL);
369 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
370 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
371 new->parent = parent;
372 new->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
374 /* 3: swap 'percent' (resize factor) */
375 new->percent = con->percent;
378 /* 4: add it as a child to the new Con */
379 con_attach(con, new, false);
383 * Moves focus one level up. Returns true if focus changed.
386 bool level_up(void) {
387 /* Skip over floating containers and go directly to the grandparent
388 * (which should always be a workspace) */
389 if (focused->parent->type == CT_FLOATING_CON) {
390 con_activate(focused->parent->parent);
394 /* We can focus up to the workspace, but not any higher in the tree */
395 if ((focused->parent->type != CT_CON &&
396 focused->parent->type != CT_WORKSPACE) ||
397 focused->type == CT_WORKSPACE) {
398 ELOG("'focus parent': Focus is already on the workspace, cannot go higher than that.\n");
401 con_activate(focused->parent);
406 * Moves focus one level down. Returns true if focus changed.
409 bool level_down(void) {
410 /* Go down the focus stack of the current node */
411 Con *next = TAILQ_FIRST(&(focused->focus_head));
412 if (next == TAILQ_END(&(focused->focus_head))) {
413 DLOG("cannot go down\n");
415 } else if (next->type == CT_FLOATING_CON) {
416 /* Floating cons shouldn't be directly focused; try immediately
417 * going to the grandchild of the focused con. */
418 Con *child = TAILQ_FIRST(&(next->focus_head));
419 if (child == TAILQ_END(&(next->focus_head))) {
420 DLOG("cannot go down\n");
423 next = TAILQ_FIRST(&(next->focus_head));
430 static void mark_unmapped(Con *con) {
434 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
435 mark_unmapped(current);
436 if (con->type == CT_WORKSPACE) {
437 /* We need to call mark_unmapped on floating nodes as well since we can
438 * make containers floating. */
439 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
440 mark_unmapped(current);
445 * Renders the tree, that is rendering all outputs using render_con() and
446 * pushing the changes to X11 using x_push_changes().
449 void tree_render(void) {
453 DLOG("-- BEGIN RENDERING --\n");
454 /* Reset map state for all nodes in tree */
455 /* TODO: a nicer method to walk all nodes would be good, maybe? */
456 mark_unmapped(croot);
457 croot->mapped = true;
461 x_push_changes(croot);
462 DLOG("-- END RENDERING --\n");
466 * Recursive function to walk the tree until a con can be found to focus.
469 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
470 /* When dealing with fullscreen containers, it's necessary to go up to the
471 * workspace level, because 'focus $dir' will start at the con's real
472 * position in the tree, and it may not be possible to get to the edge
473 * normally due to fullscreen focusing restrictions. */
474 if (con->fullscreen_mode == CF_OUTPUT && con->type != CT_WORKSPACE)
475 con = con_get_workspace(con);
477 /* Stop recursing at workspaces after attempting to switch to next
478 * workspace if possible. */
479 if (con->type == CT_WORKSPACE) {
480 if (con_get_fullscreen_con(con, CF_GLOBAL)) {
481 DLOG("Cannot change workspace while in global fullscreen mode.\n");
484 Output *current_output = get_output_containing(con->rect.x, con->rect.y);
489 DLOG("Current output is %s\n", output_primary_name(current_output));
491 /* Try to find next output */
492 direction_t direction;
493 if (way == 'n' && orientation == HORIZ)
495 else if (way == 'p' && orientation == HORIZ)
497 else if (way == 'n' && orientation == VERT)
499 else if (way == 'p' && orientation == VERT)
504 next_output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
507 DLOG("Next output is %s\n", output_primary_name(next_output));
509 /* Find visible workspace on next output */
510 Con *workspace = NULL;
511 GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
513 /* Show next workspace and focus appropriate container if possible. */
517 /* Use descend_focused first to give higher priority to floating or
518 * tiling fullscreen containers. */
519 Con *focus = con_descend_focused(workspace);
520 if (focus->fullscreen_mode == CF_NONE) {
521 Con *focus_tiling = con_descend_tiling_focused(workspace);
522 /* If descend_tiling returned a workspace then focus is either a
523 * floating container or the same workspace. */
524 if (focus_tiling != workspace) {
525 focus = focus_tiling;
529 workspace_show(workspace);
531 x_set_warp_to(&(focus->rect));
535 Con *parent = con->parent;
537 if (con->type == CT_FLOATING_CON) {
538 if (orientation != HORIZ)
541 /* left/right focuses the previous/next floating container */
544 next = TAILQ_NEXT(con, floating_windows);
546 next = TAILQ_PREV(con, floating_head, floating_windows);
548 /* If there is no next/previous container, wrap */
551 next = TAILQ_FIRST(&(parent->floating_head));
553 next = TAILQ_LAST(&(parent->floating_head), floating_head);
556 /* Still no next/previous container? bail out */
560 /* Raise the floating window on top of other windows preserving
561 * relative stack order */
562 while (TAILQ_LAST(&(parent->floating_head), floating_head) != next) {
563 Con *last = TAILQ_LAST(&(parent->floating_head), floating_head);
564 TAILQ_REMOVE(&(parent->floating_head), last, floating_windows);
565 TAILQ_INSERT_HEAD(&(parent->floating_head), last, floating_windows);
568 con_activate(con_descend_focused(next));
572 /* If the orientation does not match or there is no other con to focus, we
573 * need to go higher in the hierarchy */
574 if (con_orientation(parent) != orientation ||
575 con_num_children(parent) == 1)
576 return _tree_next(parent, way, orientation, wrap);
578 Con *current = TAILQ_FIRST(&(parent->focus_head));
579 /* TODO: when can the following happen (except for floating windows, which
580 * are handled above)? */
581 if (TAILQ_EMPTY(&(parent->nodes_head))) {
582 DLOG("nothing to focus\n");
588 next = TAILQ_NEXT(current, nodes);
590 next = TAILQ_PREV(current, nodes_head, nodes);
593 if (config.focus_wrapping != FOCUS_WRAPPING_FORCE) {
594 /* If there is no next/previous container, we check if we can focus one
595 * when going higher (without wrapping, though). If so, we are done, if
597 if (_tree_next(parent, way, orientation, false))
605 next = TAILQ_FIRST(&(parent->nodes_head));
607 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
610 /* Don't violate fullscreen focus restrictions. */
611 if (!con_fullscreen_permits_focusing(next))
614 /* 3: focus choice comes in here. at the moment we will go down
615 * until we find a window */
616 /* TODO: check for window, atm we only go down as far as possible */
617 con_activate(con_descend_focused(next));
622 * Changes focus in the given way (next/previous) and given orientation
623 * (horizontal/vertical).
626 void tree_next(char way, orientation_t orientation) {
627 _tree_next(focused, way, orientation,
628 config.focus_wrapping != FOCUS_WRAPPING_OFF);
632 * tree_flatten() removes pairs of redundant split containers, e.g.:
633 * [workspace, horizontal]
637 * In this example, the v-split and h-split container are redundant.
638 * Such a situation can be created by moving containers in a direction which is
639 * not the orientation of their parent container. i3 needs to create a new
640 * split container then and if you move containers this way multiple times,
641 * redundant chains of split-containers can be the result.
644 void tree_flatten(Con *con) {
645 Con *current, *child, *parent = con->parent;
646 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
648 /* We only consider normal containers without windows */
649 if (con->type != CT_CON ||
650 parent->layout == L_OUTPUT || /* con == "content" */
654 /* Ensure it got only one child */
655 child = TAILQ_FIRST(&(con->nodes_head));
656 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
659 DLOG("child = %p, con = %p, parent = %p\n", child, con, parent);
661 /* The child must have a different orientation than the con but the same as
662 * the con’s parent to be redundant */
663 if (!con_is_split(con) ||
664 !con_is_split(child) ||
665 (con->layout != L_SPLITH && con->layout != L_SPLITV) ||
666 (child->layout != L_SPLITH && child->layout != L_SPLITV) ||
667 con_orientation(con) == con_orientation(child) ||
668 con_orientation(child) != con_orientation(parent))
671 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
673 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
675 DLOG("detaching...\n");
676 /* 2: re-attach the children to the parent before con */
677 while (!TAILQ_EMPTY(&(child->nodes_head))) {
678 current = TAILQ_FIRST(&(child->nodes_head));
679 DLOG("detaching current=%p / %s\n", current, current->name);
681 DLOG("re-attaching\n");
682 /* We don’t use con_attach() here because for a CT_CON, the special
683 * case handling of con_attach() does not trigger. So all it would do
684 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
685 * directly use the TAILQ macros. */
686 current->parent = parent;
687 TAILQ_INSERT_BEFORE(con, current, nodes);
688 DLOG("attaching to focus list\n");
689 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
690 current->percent = con->percent;
692 DLOG("re-attached all\n");
694 /* 3: restore focus, if con was focused */
695 if (focus_next != NULL &&
696 TAILQ_FIRST(&(parent->focus_head)) == con) {
697 DLOG("restoring focus to focus_next=%p\n", focus_next);
698 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
699 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
700 DLOG("restored focus.\n");
703 /* 4: close the redundant cons */
704 DLOG("closing redundant cons\n");
705 tree_close_internal(con, DONT_KILL_WINDOW, true);
707 /* Well, we got to abort the recursion here because we destroyed the
708 * container. However, if tree_flatten() is called sufficiently often,
709 * there can’t be the situation of having two pairs of redundant containers
710 * at once. Therefore, we can safely abort the recursion on this level
711 * after flattening. */
715 /* We cannot use normal foreach here because tree_flatten might close the
716 * current container. */
717 current = TAILQ_FIRST(&(con->nodes_head));
718 while (current != NULL) {
719 Con *next = TAILQ_NEXT(current, nodes);
720 tree_flatten(current);
724 current = TAILQ_FIRST(&(con->floating_head));
725 while (current != NULL) {
726 Con *next = TAILQ_NEXT(current, floating_windows);
727 tree_flatten(current);