]> git.sur5r.net Git - i3/i3/blob - src/tree.c
6b66dd2e5d2fe0ff555959d15e5d0cd938ba67e1
[i3/i3] / src / tree.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * tree.c: Everything that primarily modifies the layout tree data structure.
8  *
9  */
10 #include "all.h"
11
12 struct Con *croot;
13 struct Con *focused;
14
15 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
16
17 /*
18  * Create the pseudo-output __i3. Output-independent workspaces such as
19  * __i3_scratch will live there.
20  *
21  */
22 static Con *_create___i3(void) {
23     Con *__i3 = con_new(croot, NULL);
24     FREE(__i3->name);
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;
37
38     /* Add a content container. */
39     DLOG("adding main content container\n");
40     Con *content = con_new(NULL, NULL);
41     content->type = CT_CON;
42     FREE(content->name);
43     content->name = sstrdup("content");
44     content->layout = L_SPLITH;
45
46     x_set_name(content, "[i3 con] content __i3");
47     con_attach(content, __i3, false);
48
49     /* Attach the __i3_scratch workspace. */
50     Con *ws = con_new(NULL, NULL);
51     ws->type = CT_WORKSPACE;
52     ws->num = -1;
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;
58
59     return __i3;
60 }
61
62 /*
63  * Loads tree from 'path' (used for in-place restarts).
64  *
65  */
66 bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry) {
67     bool result = false;
68     char *globbed = resolve_tilde(path);
69     char *buf = NULL;
70
71     if (!path_exists(globbed)) {
72         LOG("%s does not exist, not restoring tree\n", globbed);
73         goto out;
74     }
75
76     ssize_t len;
77     if ((len = slurp(globbed, &buf)) < 0) {
78         /* slurp already logged an error. */
79         goto out;
80     }
81
82     /* TODO: refactor the following */
83     croot = con_new(NULL, NULL);
84     croot->rect = (Rect){
85         geometry->x,
86         geometry->y,
87         geometry->width,
88         geometry->height};
89     focused = croot;
90
91     tree_append_json(focused, buf, len, NULL);
92
93     DLOG("appended tree, using new root\n");
94     croot = TAILQ_FIRST(&(croot->nodes_head));
95     DLOG("new root = %p\n", croot);
96     Con *out = TAILQ_FIRST(&(croot->nodes_head));
97     DLOG("out = %p\n", out);
98     Con *ws = TAILQ_FIRST(&(out->nodes_head));
99     DLOG("ws = %p\n", ws);
100
101     /* For in-place restarting into v4.2, we need to make sure the new
102      * pseudo-output __i3 is present. */
103     if (strcmp(out->name, "__i3") != 0) {
104         DLOG("Adding pseudo-output __i3 during inplace restart\n");
105         Con *__i3 = _create___i3();
106         /* Ensure that it is the first output, other places in the code make
107          * that assumption. */
108         TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes);
109         TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes);
110     }
111
112     restore_open_placeholder_windows(croot);
113     result = true;
114
115 out:
116     free(globbed);
117     free(buf);
118     return result;
119 }
120
121 /*
122  * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
123  * root node are created in randr.c for each Output.
124  *
125  */
126 void tree_init(xcb_get_geometry_reply_t *geometry) {
127     croot = con_new(NULL, NULL);
128     FREE(croot->name);
129     croot->name = "root";
130     croot->type = CT_ROOT;
131     croot->layout = L_SPLITH;
132     croot->rect = (Rect){
133         geometry->x,
134         geometry->y,
135         geometry->width,
136         geometry->height};
137
138     _create___i3();
139 }
140
141 /*
142  * Opens an empty container in the current container
143  *
144  */
145 Con *tree_open_con(Con *con, i3Window *window) {
146     if (con == NULL) {
147         /* every focusable Con has a parent (outputs have parent root) */
148         con = focused->parent;
149         /* If the parent is an output, we are on a workspace. In this case,
150          * the new container needs to be opened as a leaf of the workspace. */
151         if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
152             con = focused;
153         }
154
155         /* If the currently focused container is a floating container, we
156          * attach the new container to the currently focused spot in its
157          * workspace. */
158         if (con->type == CT_FLOATING_CON) {
159             con = con_descend_tiling_focused(con->parent);
160             if (con->type != CT_WORKSPACE)
161                 con = con->parent;
162         }
163         DLOG("con = %p\n", con);
164     }
165
166     assert(con != NULL);
167
168     /* 3. create the container and attach it to its parent */
169     Con *new = con_new(con, window);
170     new->layout = L_SPLITH;
171
172     /* 4: re-calculate child->percent for each child */
173     con_fix_percent(con);
174
175     return new;
176 }
177
178 /*
179  * Closes the given container including all children.
180  * Returns true if the container was killed or false if just WM_DELETE was sent
181  * and the window is expected to kill itself.
182  *
183  * The dont_kill_parent flag is specified when the function calls itself
184  * recursively while deleting a containers children.
185  *
186  */
187 bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent) {
188     Con *parent = con->parent;
189
190     /* remove the urgency hint of the workspace (if set) */
191     if (con->urgent) {
192         con_set_urgency(con, false);
193         con_update_parents_urgency(con);
194         workspace_update_urgent_flag(con_get_workspace(con));
195     }
196
197     DLOG("closing %p, kill_window = %d\n", con, kill_window);
198     Con *child, *nextchild;
199     bool abort_kill = false;
200     /* We cannot use TAILQ_FOREACH because the children get deleted
201      * in their parent’s nodes_head */
202     for (child = TAILQ_FIRST(&(con->nodes_head)); child;) {
203         nextchild = TAILQ_NEXT(child, nodes);
204         DLOG("killing child=%p\n", child);
205         if (!tree_close_internal(child, kill_window, true)) {
206             abort_kill = true;
207         }
208         child = nextchild;
209     }
210
211     if (abort_kill) {
212         DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
213         return false;
214     }
215
216     if (con->window != NULL) {
217         if (kill_window != DONT_KILL_WINDOW) {
218             x_window_kill(con->window->id, kill_window);
219             return false;
220         } else {
221             xcb_void_cookie_t cookie;
222             /* Ignore any further events by clearing the event mask,
223              * unmap the window,
224              * then reparent it to the root window. */
225             xcb_change_window_attributes(conn, con->window->id,
226                                          XCB_CW_EVENT_MASK, (uint32_t[]){XCB_NONE});
227             xcb_unmap_window(conn, con->window->id);
228             cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
229
230             /* Ignore X11 errors for the ReparentWindow request.
231              * X11 Errors are returned when the window was already destroyed */
232             add_ignore_event(cookie.sequence, 0);
233
234             /* We are no longer handling this window, thus set WM_STATE to
235              * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
236             long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
237             cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
238                                          con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
239
240             /* Remove the window from the save set. All windows in the save set
241              * will be mapped when i3 closes its connection (e.g. when
242              * restarting). This is not what we want, since some apps keep
243              * unmapped windows around and don’t expect them to suddenly be
244              * mapped. See https://bugs.i3wm.org/1617 */
245             xcb_change_save_set(conn, XCB_SET_MODE_DELETE, con->window->id);
246
247             /* Ignore X11 errors for the ReparentWindow request.
248              * X11 Errors are returned when the window was already destroyed */
249             add_ignore_event(cookie.sequence, 0);
250         }
251         ipc_send_window_event("close", con);
252         window_free(con->window);
253         con->window = NULL;
254     }
255
256     Con *ws = con_get_workspace(con);
257
258     /* Figure out which container to focus next before detaching 'con'. */
259     Con *next = (con == focused) ? con_next_focused(con) : NULL;
260     DLOG("next = %p, focused = %p\n", next, focused);
261
262     /* Detach the container so that it will not be rendered anymore. */
263     con_detach(con);
264
265     /* disable urgency timer, if needed */
266     if (con->urgency_timer != NULL) {
267         DLOG("Removing urgency timer of con %p\n", con);
268         workspace_update_urgent_flag(ws);
269         ev_timer_stop(main_loop, con->urgency_timer);
270         FREE(con->urgency_timer);
271     }
272
273     if (con->type != CT_FLOATING_CON) {
274         /* If the container is *not* floating, we might need to re-distribute
275          * percentage values for the resized containers. */
276         con_fix_percent(parent);
277     }
278
279     /* Render the tree so that the surrounding containers take up the space
280      * which 'con' does no longer occupy. If we don’t render here, there will
281      * be a gap in our containers and that could trigger an EnterNotify for an
282      * underlying container, see ticket #660.
283      *
284      * Rendering has to be avoided when dont_kill_parent is set (when
285      * tree_close_internal calls itself recursively) because the tree is in a
286      * non-renderable state during that time. */
287     if (!dont_kill_parent)
288         tree_render();
289
290     /* kill the X11 part of this container */
291     x_con_kill(con);
292
293     if (ws == con) {
294         DLOG("Closing a workspace container, updating EWMH atoms\n");
295         ewmh_update_number_of_desktops();
296         ewmh_update_desktop_names();
297         ewmh_update_wm_desktop();
298     }
299
300     con_free(con);
301
302     if (next) {
303         con_activate(next);
304     } else {
305         DLOG("not changing focus, the container was not focused before\n");
306     }
307
308     /* check if the parent container is empty now and close it */
309     if (!dont_kill_parent)
310         CALL(parent, on_remove_child);
311
312     return true;
313 }
314
315 /*
316  * Splits (horizontally or vertically) the given container by creating a new
317  * container which contains the old one and the future ones.
318  *
319  */
320 void tree_split(Con *con, orientation_t orientation) {
321     if (con_is_floating(con)) {
322         DLOG("Floating containers can't be split.\n");
323         return;
324     }
325
326     if (con->type == CT_WORKSPACE) {
327         if (con_num_children(con) < 2) {
328             if (con_num_children(con) == 0) {
329                 DLOG("Changing workspace_layout to L_DEFAULT\n");
330                 con->workspace_layout = L_DEFAULT;
331             }
332             DLOG("Changing orientation of workspace\n");
333             con->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
334             return;
335         } else {
336             /* if there is more than one container on the workspace
337              * move them into a new container and handle this instead */
338             con = workspace_encapsulate(con);
339         }
340     }
341
342     Con *parent = con->parent;
343
344     /* Force re-rendering to make the indicator border visible. */
345     con_force_split_parents_redraw(con);
346
347     /* if we are in a container whose parent contains only one
348      * child (its split functionality is unused so far), we just change the
349      * orientation (more intuitive than splitting again) */
350     if (con_num_children(parent) == 1 &&
351         (parent->layout == L_SPLITH ||
352          parent->layout == L_SPLITV)) {
353         parent->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
354         DLOG("Just changing orientation of existing container\n");
355         return;
356     }
357
358     DLOG("Splitting in orientation %d\n", orientation);
359
360     /* 2: replace it with a new Con */
361     Con *new = con_new(NULL, NULL);
362     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
363     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
364     new->parent = parent;
365     new->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
366
367     /* 3: swap 'percent' (resize factor) */
368     new->percent = con->percent;
369     con->percent = 0.0;
370
371     /* 4: add it as a child to the new Con */
372     con_attach(con, new, false);
373 }
374
375 /*
376  * Moves focus one level up. Returns true if focus changed.
377  *
378  */
379 bool level_up(void) {
380     /* Skip over floating containers and go directly to the grandparent
381      * (which should always be a workspace) */
382     if (focused->parent->type == CT_FLOATING_CON) {
383         con_activate(focused->parent->parent);
384         return true;
385     }
386
387     /* We can focus up to the workspace, but not any higher in the tree */
388     if ((focused->parent->type != CT_CON &&
389          focused->parent->type != CT_WORKSPACE) ||
390         focused->type == CT_WORKSPACE) {
391         ELOG("'focus parent': Focus is already on the workspace, cannot go higher than that.\n");
392         return false;
393     }
394     con_activate(focused->parent);
395     return true;
396 }
397
398 /*
399  * Moves focus one level down. Returns true if focus changed.
400  *
401  */
402 bool level_down(void) {
403     /* Go down the focus stack of the current node */
404     Con *next = TAILQ_FIRST(&(focused->focus_head));
405     if (next == TAILQ_END(&(focused->focus_head))) {
406         DLOG("cannot go down\n");
407         return false;
408     } else if (next->type == CT_FLOATING_CON) {
409         /* Floating cons shouldn't be directly focused; try immediately
410          * going to the grandchild of the focused con. */
411         Con *child = TAILQ_FIRST(&(next->focus_head));
412         if (child == TAILQ_END(&(next->focus_head))) {
413             DLOG("cannot go down\n");
414             return false;
415         } else
416             next = TAILQ_FIRST(&(next->focus_head));
417     }
418
419     con_activate(next);
420     return true;
421 }
422
423 static void mark_unmapped(Con *con) {
424     Con *current;
425
426     con->mapped = false;
427     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
428     mark_unmapped(current);
429     if (con->type == CT_WORKSPACE) {
430         /* We need to call mark_unmapped on floating nodes as well since we can
431          * make containers floating. */
432         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
433         mark_unmapped(current);
434     }
435 }
436
437 /*
438  * Renders the tree, that is rendering all outputs using render_con() and
439  * pushing the changes to X11 using x_push_changes().
440  *
441  */
442 void tree_render(void) {
443     if (croot == NULL)
444         return;
445
446     DLOG("-- BEGIN RENDERING --\n");
447     /* Reset map state for all nodes in tree */
448     /* TODO: a nicer method to walk all nodes would be good, maybe? */
449     mark_unmapped(croot);
450     croot->mapped = true;
451
452     render_con(croot, false);
453
454     x_push_changes(croot);
455     DLOG("-- END RENDERING --\n");
456 }
457
458 /*
459  * Recursive function to walk the tree until a con can be found to focus.
460  *
461  */
462 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
463     /* When dealing with fullscreen containers, it's necessary to go up to the
464      * workspace level, because 'focus $dir' will start at the con's real
465      * position in the tree, and it may not be possible to get to the edge
466      * normally due to fullscreen focusing restrictions. */
467     if (con->fullscreen_mode == CF_OUTPUT && con->type != CT_WORKSPACE)
468         con = con_get_workspace(con);
469
470     /* Stop recursing at workspaces after attempting to switch to next
471      * workspace if possible. */
472     if (con->type == CT_WORKSPACE) {
473         if (con_get_fullscreen_con(con, CF_GLOBAL)) {
474             DLOG("Cannot change workspace while in global fullscreen mode.\n");
475             return false;
476         }
477         Output *current_output = get_output_containing(con->rect.x, con->rect.y);
478         Output *next_output;
479
480         if (!current_output)
481             return false;
482         DLOG("Current output is %s\n", output_primary_name(current_output));
483
484         /* Try to find next output */
485         direction_t direction;
486         if (way == 'n' && orientation == HORIZ)
487             direction = D_RIGHT;
488         else if (way == 'p' && orientation == HORIZ)
489             direction = D_LEFT;
490         else if (way == 'n' && orientation == VERT)
491             direction = D_DOWN;
492         else if (way == 'p' && orientation == VERT)
493             direction = D_UP;
494         else
495             return false;
496
497         next_output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
498         if (!next_output)
499             return false;
500         DLOG("Next output is %s\n", output_primary_name(next_output));
501
502         /* Find visible workspace on next output */
503         Con *workspace = NULL;
504         GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
505
506         /* Show next workspace and focus appropriate container if possible. */
507         if (!workspace)
508             return false;
509
510         /* Use descend_focused first to give higher priority to floating or
511          * tiling fullscreen containers. */
512         Con *focus = con_descend_focused(workspace);
513         if (focus->fullscreen_mode == CF_NONE) {
514             Con *focus_tiling = con_descend_tiling_focused(workspace);
515             /* If descend_tiling returned a workspace then focus is either a
516              * floating container or the same workspace. */
517             if (focus_tiling != workspace) {
518                 focus = focus_tiling;
519             }
520         }
521
522         workspace_show(workspace);
523         con_activate(focus);
524         x_set_warp_to(&(focus->rect));
525         return true;
526     }
527
528     Con *parent = con->parent;
529
530     if (con->type == CT_FLOATING_CON) {
531         if (orientation != HORIZ)
532             return false;
533
534         /* left/right focuses the previous/next floating container */
535         Con *next;
536         if (way == 'n')
537             next = TAILQ_NEXT(con, floating_windows);
538         else
539             next = TAILQ_PREV(con, floating_head, floating_windows);
540
541         /* If there is no next/previous container, wrap */
542         if (!next) {
543             if (way == 'n')
544                 next = TAILQ_FIRST(&(parent->floating_head));
545             else
546                 next = TAILQ_LAST(&(parent->floating_head), floating_head);
547         }
548
549         /* Still no next/previous container? bail out */
550         if (!next)
551             return false;
552
553         /* Raise the floating window on top of other windows preserving
554          * relative stack order */
555         while (TAILQ_LAST(&(parent->floating_head), floating_head) != next) {
556             Con *last = TAILQ_LAST(&(parent->floating_head), floating_head);
557             TAILQ_REMOVE(&(parent->floating_head), last, floating_windows);
558             TAILQ_INSERT_HEAD(&(parent->floating_head), last, floating_windows);
559         }
560
561         con_activate(con_descend_focused(next));
562         return true;
563     }
564
565     /* If the orientation does not match or there is no other con to focus, we
566      * need to go higher in the hierarchy */
567     if (con_orientation(parent) != orientation ||
568         con_num_children(parent) == 1)
569         return _tree_next(parent, way, orientation, wrap);
570
571     Con *current = TAILQ_FIRST(&(parent->focus_head));
572     /* TODO: when can the following happen (except for floating windows, which
573      * are handled above)? */
574     if (TAILQ_EMPTY(&(parent->nodes_head))) {
575         DLOG("nothing to focus\n");
576         return false;
577     }
578
579     Con *next;
580     if (way == 'n')
581         next = TAILQ_NEXT(current, nodes);
582     else
583         next = TAILQ_PREV(current, nodes_head, nodes);
584
585     if (!next) {
586         if (config.focus_wrapping != FOCUS_WRAPPING_FORCE) {
587             /* If there is no next/previous container, we check if we can focus one
588              * when going higher (without wrapping, though). If so, we are done, if
589              * not, we wrap */
590             if (_tree_next(parent, way, orientation, false))
591                 return true;
592
593             if (!wrap)
594                 return false;
595         }
596
597         if (way == 'n')
598             next = TAILQ_FIRST(&(parent->nodes_head));
599         else
600             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
601     }
602
603     /* Don't violate fullscreen focus restrictions. */
604     if (!con_fullscreen_permits_focusing(next))
605         return false;
606
607     /* 3: focus choice comes in here. at the moment we will go down
608      * until we find a window */
609     /* TODO: check for window, atm we only go down as far as possible */
610     con_activate(con_descend_focused(next));
611     return true;
612 }
613
614 /*
615  * Changes focus in the given way (next/previous) and given orientation
616  * (horizontal/vertical).
617  *
618  */
619 void tree_next(char way, orientation_t orientation) {
620     _tree_next(focused, way, orientation,
621                config.focus_wrapping != FOCUS_WRAPPING_OFF);
622 }
623
624 /*
625  * tree_flatten() removes pairs of redundant split containers, e.g.:
626  *       [workspace, horizontal]
627  *   [v-split]           [child3]
628  *   [h-split]
629  * [child1] [child2]
630  * In this example, the v-split and h-split container are redundant.
631  * Such a situation can be created by moving containers in a direction which is
632  * not the orientation of their parent container. i3 needs to create a new
633  * split container then and if you move containers this way multiple times,
634  * redundant chains of split-containers can be the result.
635  *
636  */
637 void tree_flatten(Con *con) {
638     Con *current, *child, *parent = con->parent;
639     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
640
641     /* We only consider normal containers without windows */
642     if (con->type != CT_CON ||
643         parent->layout == L_OUTPUT || /* con == "content" */
644         con->window != NULL)
645         goto recurse;
646
647     /* Ensure it got only one child */
648     child = TAILQ_FIRST(&(con->nodes_head));
649     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
650         goto recurse;
651
652     DLOG("child = %p, con = %p, parent = %p\n", child, con, parent);
653
654     /* The child must have a different orientation than the con but the same as
655      * the con’s parent to be redundant */
656     if (!con_is_split(con) ||
657         !con_is_split(child) ||
658         (con->layout != L_SPLITH && con->layout != L_SPLITV) ||
659         (child->layout != L_SPLITH && child->layout != L_SPLITV) ||
660         con_orientation(con) == con_orientation(child) ||
661         con_orientation(child) != con_orientation(parent))
662         goto recurse;
663
664     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
665     /* 1: save focus */
666     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
667
668     DLOG("detaching...\n");
669     /* 2: re-attach the children to the parent before con */
670     while (!TAILQ_EMPTY(&(child->nodes_head))) {
671         current = TAILQ_FIRST(&(child->nodes_head));
672         DLOG("detaching current=%p / %s\n", current, current->name);
673         con_detach(current);
674         DLOG("re-attaching\n");
675         /* We don’t use con_attach() here because for a CT_CON, the special
676          * case handling of con_attach() does not trigger. So all it would do
677          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
678          * directly use the TAILQ macros. */
679         current->parent = parent;
680         TAILQ_INSERT_BEFORE(con, current, nodes);
681         DLOG("attaching to focus list\n");
682         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
683         current->percent = con->percent;
684     }
685     DLOG("re-attached all\n");
686
687     /* 3: restore focus, if con was focused */
688     if (focus_next != NULL &&
689         TAILQ_FIRST(&(parent->focus_head)) == con) {
690         DLOG("restoring focus to focus_next=%p\n", focus_next);
691         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
692         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
693         DLOG("restored focus.\n");
694     }
695
696     /* 4: close the redundant cons */
697     DLOG("closing redundant cons\n");
698     tree_close_internal(con, DONT_KILL_WINDOW, true);
699
700     /* Well, we got to abort the recursion here because we destroyed the
701      * container. However, if tree_flatten() is called sufficiently often,
702      * there can’t be the situation of having two pairs of redundant containers
703      * at once. Therefore, we can safely abort the recursion on this level
704      * after flattening. */
705     return;
706
707 recurse:
708     /* We cannot use normal foreach here because tree_flatten might close the
709      * current container. */
710     current = TAILQ_FIRST(&(con->nodes_head));
711     while (current != NULL) {
712         Con *next = TAILQ_NEXT(current, nodes);
713         tree_flatten(current);
714         current = next;
715     }
716
717     current = TAILQ_FIRST(&(con->floating_head));
718     while (current != NULL) {
719         Con *next = TAILQ_NEXT(current, floating_windows);
720         tree_flatten(current);
721         current = next;
722     }
723 }