i3
tree.c
Go to the documentation of this file.
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) {
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;
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 (con->window != NULL) {
233  if (kill_window != DONT_KILL_WINDOW) {
234  x_window_kill(con->window->id, kill_window);
235  return false;
236  } else {
237  xcb_void_cookie_t cookie;
238  /* un-parent the window */
239  cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
240 
241  /* Ignore X11 errors for the ReparentWindow request.
242  * X11 Errors are returned when the window was already destroyed */
243  add_ignore_event(cookie.sequence, 0);
244 
245  /* We are no longer handling this window, thus set WM_STATE to
246  * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
247  long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
248  cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
249  con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
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  FREE(con->window->class_class);
256  FREE(con->window->class_instance);
257  i3string_free(con->window->name);
258  free(con->window);
259  }
260 
261  Con *ws = con_get_workspace(con);
262 
263  /* Figure out which container to focus next before detaching 'con'. */
264  if (con_is_floating(con)) {
265  if (con == focused) {
266  DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
267  next = con_next_focused(parent);
268 
269  dont_kill_parent = true;
270  DLOG("Alright, focusing %p\n", next);
271  } else {
272  next = NULL;
273  }
274  }
275 
276  /* Detach the container so that it will not be rendered anymore. */
277  con_detach(con);
278 
279  /* disable urgency timer, if needed */
280  if (con->urgency_timer != NULL) {
281  DLOG("Removing urgency timer of con %p\n", con);
283  ev_timer_stop(main_loop, con->urgency_timer);
284  FREE(con->urgency_timer);
285  }
286 
287  if (con->type != CT_FLOATING_CON) {
288  /* If the container is *not* floating, we might need to re-distribute
289  * percentage values for the resized containers. */
290  con_fix_percent(parent);
291  }
292 
293  /* Render the tree so that the surrounding containers take up the space
294  * which 'con' does no longer occupy. If we don’t render here, there will
295  * be a gap in our containers and that could trigger an EnterNotify for an
296  * underlying container, see ticket #660.
297  *
298  * Rendering has to be avoided when dont_kill_parent is set (when
299  * tree_close calls itself recursively) because the tree is in a
300  * non-renderable state during that time. */
301  if (!dont_kill_parent)
302  tree_render();
303 
304  /* kill the X11 part of this container */
305  x_con_kill(con);
306 
307  if (con_is_floating(con)) {
308  DLOG("Container was floating, killing floating container\n");
309  tree_close(parent, DONT_KILL_WINDOW, false, (con == focused));
310  DLOG("parent container killed\n");
311  }
312 
313  free(con->name);
314  FREE(con->deco_render_params);
316  free(con);
317 
318  /* in the case of floating windows, we already focused another container
319  * when closing the parent, so we can exit now. */
320  if (!next) {
321  DLOG("No next container, i will just exit now\n");
322  return true;
323  }
324 
325  if (was_mapped || con == focused) {
326  if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
327  DLOG("focusing %p / %s\n", next, next->name);
328  if (next->type == CT_DOCKAREA) {
329  /* Instead of focusing the dockarea, we need to restore focus to the workspace */
331  } else {
332  if (!force_set_focus && con != focused)
333  DLOG("not changing focus, the container was not focused before\n");
334  else con_focus(next);
335  }
336  }
337  else {
338  DLOG("not focusing because we're not killing anybody\n");
339  }
340  } else {
341  DLOG("not focusing, was not mapped\n");
342  }
343 
344  /* check if the parent container is empty now and close it */
345  if (!dont_kill_parent)
346  CALL(parent, on_remove_child);
347 
348  return true;
349 }
350 
351 /*
352  * Closes the current container using tree_close().
353  *
354  */
355 void tree_close_con(kill_window_t kill_window) {
356  assert(focused != NULL);
357  if (focused->type == CT_WORKSPACE) {
358  LOG("Cannot close workspace\n");
359  return;
360  }
361 
362  /* There *should* be no possibility to focus outputs / root container */
363  assert(focused->type != CT_OUTPUT);
364  assert(focused->type != CT_ROOT);
365 
366  /* Kill con */
367  tree_close(focused, kill_window, false, false);
368 }
369 
370 /*
371  * Splits (horizontally or vertically) the given container by creating a new
372  * container which contains the old one and the future ones.
373  *
374  */
375 void tree_split(Con *con, orientation_t orientation) {
376  if (con->type == CT_FLOATING_CON) {
377  DLOG("Floating containers can't be split.\n");
378  return;
379  }
380 
381  if (con->type == CT_WORKSPACE) {
382  if (con_num_children(con) < 2) {
383  DLOG("Just changing orientation of workspace\n");
384  con->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
385  return;
386  } else {
387  /* if there is more than one container on the workspace
388  * move them into a new container and handle this instead */
389  con = workspace_encapsulate(con);
390  }
391  }
392 
393  Con *parent = con->parent;
394 
395  /* Force re-rendering to make the indicator border visible. */
396  FREE(con->deco_render_params);
397  FREE(parent->deco_render_params);
398 
399  /* if we are in a container whose parent contains only one
400  * child (its split functionality is unused so far), we just change the
401  * orientation (more intuitive than splitting again) */
402  if (con_num_children(parent) == 1 &&
403  (parent->layout == L_SPLITH ||
404  parent->layout == L_SPLITV)) {
405  parent->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
406  DLOG("Just changing orientation of existing container\n");
407  return;
408  }
409 
410  DLOG("Splitting in orientation %d\n", orientation);
411 
412  /* 2: replace it with a new Con */
413  Con *new = con_new(NULL, NULL);
414  TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
415  TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
416  new->parent = parent;
417  new->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
418 
419  /* 3: swap 'percent' (resize factor) */
420  new->percent = con->percent;
421  con->percent = 0.0;
422 
423  /* 4: add it as a child to the new Con */
424  con_attach(con, new, false);
425 }
426 
427 /*
428  * Moves focus one level up. Returns true if focus changed.
429  *
430  */
431 bool level_up(void) {
432  /* Skip over floating containers and go directly to the grandparent
433  * (which should always be a workspace) */
434  if (focused->parent->type == CT_FLOATING_CON) {
435  con_focus(focused->parent->parent);
436  return true;
437  }
438 
439  /* We can focus up to the workspace, but not any higher in the tree */
440  if ((focused->parent->type != CT_CON &&
441  focused->parent->type != CT_WORKSPACE) ||
442  focused->type == CT_WORKSPACE) {
443  ELOG("'focus parent': Focus is already on the workspace, cannot go higher than that.\n");
444  return false;
445  }
446  con_focus(focused->parent);
447  return true;
448 }
449 
450 /*
451  * Moves focus one level down. Returns true if focus changed.
452  *
453  */
454 bool level_down(void) {
455  /* Go down the focus stack of the current node */
456  Con *next = TAILQ_FIRST(&(focused->focus_head));
457  if (next == TAILQ_END(&(focused->focus_head))) {
458  DLOG("cannot go down\n");
459  return false;
460  }
461  else if (next->type == CT_FLOATING_CON) {
462  /* Floating cons shouldn't be directly focused; try immediately
463  * going to the grandchild of the focused con. */
464  Con *child = TAILQ_FIRST(&(next->focus_head));
465  if (child == TAILQ_END(&(next->focus_head))) {
466  DLOG("cannot go down\n");
467  return false;
468  }
469  else
470  next = TAILQ_FIRST(&(next->focus_head));
471  }
472 
473  con_focus(next);
474  return true;
475 }
476 
477 static void mark_unmapped(Con *con) {
478  Con *current;
479 
480  con->mapped = false;
481  TAILQ_FOREACH(current, &(con->nodes_head), nodes)
482  mark_unmapped(current);
483  if (con->type == CT_WORKSPACE) {
484  /* We need to call mark_unmapped on floating nodes aswell since we can
485  * make containers floating. */
486  TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
487  mark_unmapped(current);
488  }
489 }
490 
491 /*
492  * Renders the tree, that is rendering all outputs using render_con() and
493  * pushing the changes to X11 using x_push_changes().
494  *
495  */
496 void tree_render(void) {
497  if (croot == NULL)
498  return;
499 
500  DLOG("-- BEGIN RENDERING --\n");
501  /* Reset map state for all nodes in tree */
502  /* TODO: a nicer method to walk all nodes would be good, maybe? */
503  mark_unmapped(croot);
504  croot->mapped = true;
505 
506  render_con(croot, false);
507 
508  x_push_changes(croot);
509  DLOG("-- END RENDERING --\n");
510 }
511 
512 /*
513  * Recursive function to walk the tree until a con can be found to focus.
514  *
515  */
516 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
517  /* When dealing with fullscreen containers, it's necessary to go up to the
518  * workspace level, because 'focus $dir' will start at the con's real
519  * position in the tree, and it may not be possible to get to the edge
520  * normally due to fullscreen focusing restrictions. */
521  if (con->fullscreen_mode == CF_OUTPUT && con->type != CT_WORKSPACE)
522  con = con_get_workspace(con);
523 
524  /* Stop recursing at workspaces after attempting to switch to next
525  * workspace if possible. */
526  if (con->type == CT_WORKSPACE) {
527  if (con_get_fullscreen_con(con, CF_GLOBAL)) {
528  DLOG("Cannot change workspace while in global fullscreen mode.\n");
529  return false;
530  }
531  Output *current_output = get_output_containing(con->rect.x, con->rect.y);
532  Output *next_output;
533 
534  if (!current_output)
535  return false;
536  DLOG("Current output is %s\n", current_output->name);
537 
538  /* Try to find next output */
539  direction_t direction;
540  if (way == 'n' && orientation == HORIZ)
541  direction = D_RIGHT;
542  else if (way == 'p' && orientation == HORIZ)
543  direction = D_LEFT;
544  else if (way == 'n' && orientation == VERT)
545  direction = D_DOWN;
546  else if (way == 'p' && orientation == VERT)
547  direction = D_UP;
548  else
549  return false;
550 
551  next_output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
552  if (!next_output)
553  return false;
554  DLOG("Next output is %s\n", next_output->name);
555 
556  /* Find visible workspace on next output */
557  Con *workspace = NULL;
558  GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
559 
560  /* Show next workspace and focus appropriate container if possible. */
561  if (!workspace)
562  return false;
563 
564  workspace_show(workspace);
565 
566  /* If a workspace has an active fullscreen container, one of its
567  * children should always be focused. The above workspace_show()
568  * should be adequate for that, so return. */
569  if (con_get_fullscreen_con(workspace, CF_OUTPUT))
570  return true;
571 
572  Con *focus = con_descend_direction(workspace, direction);
573  if (focus) {
574  con_focus(focus);
575  x_set_warp_to(&(focus->rect));
576  }
577  return true;
578  }
579 
580  Con *parent = con->parent;
581 
582  if (con->type == CT_FLOATING_CON) {
583  /* left/right focuses the previous/next floating container */
584  if (orientation == HORIZ) {
585  Con *next;
586  if (way == 'n')
587  next = TAILQ_NEXT(con, floating_windows);
588  else next = TAILQ_PREV(con, floating_head, floating_windows);
589 
590  /* If there is no next/previous container, wrap */
591  if (!next) {
592  if (way == 'n')
593  next = TAILQ_FIRST(&(parent->floating_head));
594  else next = TAILQ_LAST(&(parent->floating_head), floating_head);
595  }
596 
597  /* Still no next/previous container? bail out */
598  if (!next)
599  return false;
600 
602  return true;
603  } else {
604  /* up/down cycles through the Z-index */
605  /* TODO: implement cycling through the z-index */
606  return false;
607  }
608  }
609 
610  /* If the orientation does not match or there is no other con to focus, we
611  * need to go higher in the hierarchy */
612  if (con_orientation(parent) != orientation ||
613  con_num_children(parent) == 1)
614  return _tree_next(parent, way, orientation, wrap);
615 
616  Con *current = TAILQ_FIRST(&(parent->focus_head));
617  /* TODO: when can the following happen (except for floating windows, which
618  * are handled above)? */
619  if (TAILQ_EMPTY(&(parent->nodes_head))) {
620  DLOG("nothing to focus\n");
621  return false;
622  }
623 
624  Con *next;
625  if (way == 'n')
626  next = TAILQ_NEXT(current, nodes);
627  else next = TAILQ_PREV(current, nodes_head, nodes);
628 
629  if (!next) {
631  /* If there is no next/previous container, we check if we can focus one
632  * when going higher (without wrapping, though). If so, we are done, if
633  * not, we wrap */
634  if (_tree_next(parent, way, orientation, false))
635  return true;
636 
637  if (!wrap)
638  return false;
639  }
640 
641  if (way == 'n')
642  next = TAILQ_FIRST(&(parent->nodes_head));
643  else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
644  }
645 
646  /* Don't violate fullscreen focus restrictions. */
648  return false;
649 
650  /* 3: focus choice comes in here. at the moment we will go down
651  * until we find a window */
652  /* TODO: check for window, atm we only go down as far as possible */
654  return true;
655 }
656 
657 /*
658  * Changes focus in the given way (next/previous) and given orientation
659  * (horizontal/vertical).
660  *
661  */
662 void tree_next(char way, orientation_t orientation) {
663  _tree_next(focused, way, orientation, true);
664 }
665 
666 /*
667  * tree_flatten() removes pairs of redundant split containers, e.g.:
668  * [workspace, horizontal]
669  * [v-split] [child3]
670  * [h-split]
671  * [child1] [child2]
672  * In this example, the v-split and h-split container are redundant.
673  * Such a situation can be created by moving containers in a direction which is
674  * not the orientation of their parent container. i3 needs to create a new
675  * split container then and if you move containers this way multiple times,
676  * redundant chains of split-containers can be the result.
677  *
678  */
679 void tree_flatten(Con *con) {
680  Con *current, *child, *parent = con->parent;
681  DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
682 
683  /* We only consider normal containers without windows */
684  if (con->type != CT_CON ||
685  parent->layout == L_OUTPUT || /* con == "content" */
686  con->window != NULL)
687  goto recurse;
688 
689  /* Ensure it got only one child */
690  child = TAILQ_FIRST(&(con->nodes_head));
691  if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
692  goto recurse;
693 
694  DLOG("child = %p, con = %p, parent = %p\n", child, con, parent);
695 
696  /* The child must have a different orientation than the con but the same as
697  * the con’s parent to be redundant */
698  if (!con_is_split(con) ||
699  !con_is_split(child) ||
700  con_orientation(con) == con_orientation(child) ||
701  con_orientation(child) != con_orientation(parent))
702  goto recurse;
703 
704  DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
705  /* 1: save focus */
706  Con *focus_next = TAILQ_FIRST(&(child->focus_head));
707 
708  DLOG("detaching...\n");
709  /* 2: re-attach the children to the parent before con */
710  while (!TAILQ_EMPTY(&(child->nodes_head))) {
711  current = TAILQ_FIRST(&(child->nodes_head));
712  DLOG("detaching current=%p / %s\n", current, current->name);
713  con_detach(current);
714  DLOG("re-attaching\n");
715  /* We don’t use con_attach() here because for a CT_CON, the special
716  * case handling of con_attach() does not trigger. So all it would do
717  * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
718  * directly use the TAILQ macros. */
719  current->parent = parent;
720  TAILQ_INSERT_BEFORE(con, current, nodes);
721  DLOG("attaching to focus list\n");
722  TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
723  current->percent = con->percent;
724  }
725  DLOG("re-attached all\n");
726 
727  /* 3: restore focus, if con was focused */
728  if (focus_next != NULL &&
729  TAILQ_FIRST(&(parent->focus_head)) == con) {
730  DLOG("restoring focus to focus_next=%p\n", focus_next);
731  TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
732  TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
733  DLOG("restored focus.\n");
734  }
735 
736  /* 4: close the redundant cons */
737  DLOG("closing redundant cons\n");
738  tree_close(con, DONT_KILL_WINDOW, true, false);
739 
740  /* Well, we got to abort the recursion here because we destroyed the
741  * container. However, if tree_flatten() is called sufficiently often,
742  * there can’t be the situation of having two pairs of redundant containers
743  * at once. Therefore, we can safely abort the recursion on this level
744  * after flattening. */
745  return;
746 
747 recurse:
748  /* We cannot use normal foreach here because tree_flatten might close the
749  * current container. */
750  current = TAILQ_FIRST(&(con->nodes_head));
751  while (current != NULL) {
752  Con *next = TAILQ_NEXT(current, nodes);
753  tree_flatten(current);
754  current = next;
755  }
756 
757  current = TAILQ_FIRST(&(con->floating_head));
758  while (current != NULL) {
759  Con *next = TAILQ_NEXT(current, floating_windows);
760  tree_flatten(current);
761  current = next;
762  }
763 }