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