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");
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  focused = croot;
85 
86  tree_append_json(focused, globbed, NULL);
87 
88  DLOG("appended tree, using new root\n");
89  croot = TAILQ_FIRST(&(croot->nodes_head));
90  DLOG("new root = %p\n", croot);
91  Con *out = TAILQ_FIRST(&(croot->nodes_head));
92  DLOG("out = %p\n", out);
93  Con *ws = TAILQ_FIRST(&(out->nodes_head));
94  DLOG("ws = %p\n", ws);
95 
96  /* For in-place restarting into v4.2, we need to make sure the new
97  * pseudo-output __i3 is present. */
98  if (strcmp(out->name, "__i3") != 0) {
99  DLOG("Adding pseudo-output __i3 during inplace restart\n");
100  Con *__i3 = _create___i3();
101  /* Ensure that it is the first output, other places in the code make
102  * that assumption. */
103  TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes);
104  TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes);
105  }
106 
107  return true;
108 }
109 
110 /*
111  * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
112  * root node are created in randr.c for each Output.
113  *
114  */
115 void tree_init(xcb_get_geometry_reply_t *geometry) {
116  croot = con_new(NULL, NULL);
117  FREE(croot->name);
118  croot->name = "root";
119  croot->type = CT_ROOT;
120  croot->layout = L_SPLITH;
121  croot->rect = (Rect) {
122  geometry->x,
123  geometry->y,
124  geometry->width,
125  geometry->height};
126 
127  _create___i3();
128 }
129 
130 /*
131  * Opens an empty container in the current container
132  *
133  */
134 Con *tree_open_con(Con *con, i3Window *window) {
135  if (con == NULL) {
136  /* every focusable Con has a parent (outputs have parent root) */
137  con = focused->parent;
138  /* If the parent is an output, we are on a workspace. In this case,
139  * the new container needs to be opened as a leaf of the workspace. */
140  if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
141  con = focused;
142  }
143 
144  /* If the currently focused container is a floating container, we
145  * attach the new container to the currently focused spot in its
146  * workspace. */
147  if (con->type == CT_FLOATING_CON) {
149  if (con->type != CT_WORKSPACE)
150  con = con->parent;
151  }
152  DLOG("con = %p\n", con);
153  }
154 
155  assert(con != NULL);
156 
157  /* 3. create the container and attach it to its parent */
158  Con *new = con_new(con, window);
159  new->layout = L_SPLITH;
160 
161  /* 4: re-calculate child->percent for each child */
162  con_fix_percent(con);
163 
164  return new;
165 }
166 
167 static bool _is_con_mapped(Con *con) {
168  Con *child;
169 
170  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
171  if (_is_con_mapped(child))
172  return true;
173 
174  return con->mapped;
175 }
176 
177 /*
178  * Closes the given container including all children.
179  * Returns true if the container was killed or false if just WM_DELETE was sent
180  * and the window is expected to kill itself.
181  *
182  * The dont_kill_parent flag is specified when the function calls itself
183  * recursively while deleting a containers children.
184  *
185  * The force_set_focus flag is specified in the case of killing a floating
186  * window: tree_close() will be invoked for the CT_FLOATINGCON (the parent
187  * container) and focus should be set there.
188  *
189  */
190 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus) {
191  bool was_mapped = con->mapped;
192  Con *parent = con->parent;
193 
194  if (!was_mapped) {
195  /* Even if the container itself is not mapped, its children may be
196  * mapped (for example split containers don't have a mapped window on
197  * their own but usually contain mapped children). */
198  was_mapped = _is_con_mapped(con);
199  }
200 
201  /* remove the urgency hint of the workspace (if set) */
202  if (con->urgent) {
203  con->urgent = false;
206  }
207 
208  /* Get the container which is next focused */
209  Con *next = con_next_focused(con);
210  DLOG("next = %p, focused = %p\n", next, focused);
211 
212  DLOG("closing %p, kill_window = %d\n", con, kill_window);
213  Con *child, *nextchild;
214  bool abort_kill = false;
215  /* We cannot use TAILQ_FOREACH because the children get deleted
216  * in their parent’s nodes_head */
217  for (child = TAILQ_FIRST(&(con->nodes_head)); child;) {
218  nextchild = TAILQ_NEXT(child, nodes);
219  DLOG("killing child=%p\n", child);
220  if (!tree_close(child, kill_window, true, false))
221  abort_kill = true;
222  child = nextchild;
223  }
224 
225  if (abort_kill) {
226  DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
227  return false;
228  }
229 
230  if (con->window != NULL) {
231  if (kill_window != DONT_KILL_WINDOW) {
232  x_window_kill(con->window->id, kill_window);
233  return false;
234  } else {
235  xcb_void_cookie_t cookie;
236  /* Ignore any further events by clearing the event mask,
237  * unmap the window,
238  * then reparent it to the root window. */
239  xcb_change_window_attributes(conn, con->window->id,
240  XCB_CW_EVENT_MASK, (uint32_t[]) {XCB_NONE});
241  xcb_unmap_window(conn, con->window->id);
242  cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
243 
244  /* Ignore X11 errors for the ReparentWindow request.
245  * X11 Errors are returned when the window was already destroyed */
246  add_ignore_event(cookie.sequence, 0);
247 
248  /* We are no longer handling this window, thus set WM_STATE to
249  * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
250  long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
251  cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
252  con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
253 
254  /* Ignore X11 errors for the ReparentWindow request.
255  * X11 Errors are returned when the window was already destroyed */
256  add_ignore_event(cookie.sequence, 0);
257  }
258  ipc_send_window_event("close", con);
259  FREE(con->window->class_class);
260  FREE(con->window->class_instance);
261  i3string_free(con->window->name);
262  FREE(con->window->ran_assignments);
263  FREE(con->window);
264  }
265 
266  Con *ws = con_get_workspace(con);
267 
268  /* Figure out which container to focus next before detaching 'con'. */
269  if (con_is_floating(con)) {
270  if (con == focused) {
271  DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
272  next = con_next_focused(parent);
273 
274  dont_kill_parent = true;
275  DLOG("Alright, focusing %p\n", next);
276  } else {
277  next = NULL;
278  }
279  }
280 
281  /* Detach the container so that it will not be rendered anymore. */
282  con_detach(con);
283 
284  /* disable urgency timer, if needed */
285  if (con->urgency_timer != NULL) {
286  DLOG("Removing urgency timer of con %p\n", con);
288  ev_timer_stop(main_loop, con->urgency_timer);
289  FREE(con->urgency_timer);
290  }
291 
292  if (con->type != CT_FLOATING_CON) {
293  /* If the container is *not* floating, we might need to re-distribute
294  * percentage values for the resized containers. */
295  con_fix_percent(parent);
296  }
297 
298  /* Render the tree so that the surrounding containers take up the space
299  * which 'con' does no longer occupy. If we don’t render here, there will
300  * be a gap in our containers and that could trigger an EnterNotify for an
301  * underlying container, see ticket #660.
302  *
303  * Rendering has to be avoided when dont_kill_parent is set (when
304  * tree_close calls itself recursively) because the tree is in a
305  * non-renderable state during that time. */
306  if (!dont_kill_parent)
307  tree_render();
308 
309  /* kill the X11 part of this container */
310  x_con_kill(con);
311 
312  if (con_is_floating(con)) {
313  DLOG("Container was floating, killing floating container\n");
314  tree_close(parent, DONT_KILL_WINDOW, false, (con == focused));
315  DLOG("parent container killed\n");
316  }
317 
318  free(con->name);
319  FREE(con->deco_render_params);
321  free(con);
322 
323  /* in the case of floating windows, we already focused another container
324  * when closing the parent, so we can exit now. */
325  if (!next) {
326  DLOG("No next container, i will just exit now\n");
327  return true;
328  }
329 
330  if (was_mapped || con == focused) {
331  if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
332  DLOG("focusing %p / %s\n", next, next->name);
333  if (next->type == CT_DOCKAREA) {
334  /* Instead of focusing the dockarea, we need to restore focus to the workspace */
336  } else {
337  if (!force_set_focus && con != focused)
338  DLOG("not changing focus, the container was not focused before\n");
339  else
340  con_focus(next);
341  }
342  } else {
343  DLOG("not focusing because we're not killing anybody\n");
344  }
345  } else {
346  DLOG("not focusing, was not mapped\n");
347  }
348 
349  /* check if the parent container is empty now and close it */
350  if (!dont_kill_parent)
351  CALL(parent, on_remove_child);
352 
353  return true;
354 }
355 
356 /*
357  * Closes the current container using tree_close().
358  *
359  */
360 void tree_close_con(kill_window_t kill_window) {
361  assert(focused != NULL);
362 
363  /* There *should* be no possibility to focus outputs / root container */
364  assert(focused->type != CT_OUTPUT);
365  assert(focused->type != CT_ROOT);
366 
367  if (focused->type == CT_WORKSPACE) {
368  DLOG("Workspaces cannot be close, closing all children instead\n");
369  Con *child, *nextchild;
370  for (child = TAILQ_FIRST(&(focused->focus_head)); child;) {
371  nextchild = TAILQ_NEXT(child, focused);
372  DLOG("killing child=%p\n", child);
373  tree_close(child, kill_window, false, false);
374  child = nextchild;
375  }
376 
377  return;
378  }
379 
380  /* Kill con */
381  tree_close(focused, kill_window, false, false);
382 }
383 
384 /*
385  * Splits (horizontally or vertically) the given container by creating a new
386  * container which contains the old one and the future ones.
387  *
388  */
389 void tree_split(Con *con, orientation_t orientation) {
390  if (con_is_floating(con)) {
391  DLOG("Floating containers can't be split.\n");
392  return;
393  }
394 
395  if (con->type == CT_WORKSPACE) {
396  if (con_num_children(con) < 2) {
397  DLOG("Just changing orientation of workspace\n");
398  con->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
399  return;
400  } else {
401  /* if there is more than one container on the workspace
402  * move them into a new container and handle this instead */
403  con = workspace_encapsulate(con);
404  }
405  }
406 
407  Con *parent = con->parent;
408 
409  /* Force re-rendering to make the indicator border visible. */
411 
412  /* if we are in a container whose parent contains only one
413  * child (its split functionality is unused so far), we just change the
414  * orientation (more intuitive than splitting again) */
415  if (con_num_children(parent) == 1 &&
416  (parent->layout == L_SPLITH ||
417  parent->layout == L_SPLITV)) {
418  parent->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
419  DLOG("Just changing orientation of existing container\n");
420  return;
421  }
422 
423  DLOG("Splitting in orientation %d\n", orientation);
424 
425  /* 2: replace it with a new Con */
426  Con *new = con_new(NULL, NULL);
427  TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
428  TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
429  new->parent = parent;
430  new->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
431 
432  /* 3: swap 'percent' (resize factor) */
433  new->percent = con->percent;
434  con->percent = 0.0;
435 
436  /* 4: add it as a child to the new Con */
437  con_attach(con, new, false);
438 }
439 
440 /*
441  * Moves focus one level up. Returns true if focus changed.
442  *
443  */
444 bool level_up(void) {
445  /* Skip over floating containers and go directly to the grandparent
446  * (which should always be a workspace) */
447  if (focused->parent->type == CT_FLOATING_CON) {
448  con_focus(focused->parent->parent);
449  return true;
450  }
451 
452  /* We can focus up to the workspace, but not any higher in the tree */
453  if ((focused->parent->type != CT_CON &&
454  focused->parent->type != CT_WORKSPACE) ||
455  focused->type == CT_WORKSPACE) {
456  ELOG("'focus parent': Focus is already on the workspace, cannot go higher than that.\n");
457  return false;
458  }
459  con_focus(focused->parent);
460  return true;
461 }
462 
463 /*
464  * Moves focus one level down. Returns true if focus changed.
465  *
466  */
467 bool level_down(void) {
468  /* Go down the focus stack of the current node */
469  Con *next = TAILQ_FIRST(&(focused->focus_head));
470  if (next == TAILQ_END(&(focused->focus_head))) {
471  DLOG("cannot go down\n");
472  return false;
473  } else if (next->type == CT_FLOATING_CON) {
474  /* Floating cons shouldn't be directly focused; try immediately
475  * going to the grandchild of the focused con. */
476  Con *child = TAILQ_FIRST(&(next->focus_head));
477  if (child == TAILQ_END(&(next->focus_head))) {
478  DLOG("cannot go down\n");
479  return false;
480  } else
481  next = TAILQ_FIRST(&(next->focus_head));
482  }
483 
484  con_focus(next);
485  return true;
486 }
487 
488 static void mark_unmapped(Con *con) {
489  Con *current;
490 
491  con->mapped = false;
492  TAILQ_FOREACH(current, &(con->nodes_head), nodes)
493  mark_unmapped(current);
494  if (con->type == CT_WORKSPACE) {
495  /* We need to call mark_unmapped on floating nodes aswell since we can
496  * make containers floating. */
497  TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
498  mark_unmapped(current);
499  }
500 }
501 
502 /*
503  * Renders the tree, that is rendering all outputs using render_con() and
504  * pushing the changes to X11 using x_push_changes().
505  *
506  */
507 void tree_render(void) {
508  if (croot == NULL)
509  return;
510 
511  DLOG("-- BEGIN RENDERING --\n");
512  /* Reset map state for all nodes in tree */
513  /* TODO: a nicer method to walk all nodes would be good, maybe? */
514  mark_unmapped(croot);
515  croot->mapped = true;
516 
517  render_con(croot, false);
518 
519  x_push_changes(croot);
520  DLOG("-- END RENDERING --\n");
521 }
522 
523 /*
524  * Recursive function to walk the tree until a con can be found to focus.
525  *
526  */
527 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
528  /* When dealing with fullscreen containers, it's necessary to go up to the
529  * workspace level, because 'focus $dir' will start at the con's real
530  * position in the tree, and it may not be possible to get to the edge
531  * normally due to fullscreen focusing restrictions. */
532  if (con->fullscreen_mode == CF_OUTPUT && con->type != CT_WORKSPACE)
533  con = con_get_workspace(con);
534 
535  /* Stop recursing at workspaces after attempting to switch to next
536  * workspace if possible. */
537  if (con->type == CT_WORKSPACE) {
538  if (con_get_fullscreen_con(con, CF_GLOBAL)) {
539  DLOG("Cannot change workspace while in global fullscreen mode.\n");
540  return false;
541  }
542  Output *current_output = get_output_containing(con->rect.x, con->rect.y);
543  Output *next_output;
544 
545  if (!current_output)
546  return false;
547  DLOG("Current output is %s\n", current_output->name);
548 
549  /* Try to find next output */
550  direction_t direction;
551  if (way == 'n' && orientation == HORIZ)
552  direction = D_RIGHT;
553  else if (way == 'p' && orientation == HORIZ)
554  direction = D_LEFT;
555  else if (way == 'n' && orientation == VERT)
556  direction = D_DOWN;
557  else if (way == 'p' && orientation == VERT)
558  direction = D_UP;
559  else
560  return false;
561 
562  next_output = get_output_next(direction, current_output, CLOSEST_OUTPUT);
563  if (!next_output)
564  return false;
565  DLOG("Next output is %s\n", next_output->name);
566 
567  /* Find visible workspace on next output */
568  Con *workspace = NULL;
569  GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
570 
571  /* Show next workspace and focus appropriate container if possible. */
572  if (!workspace)
573  return false;
574 
575  workspace_show(workspace);
576 
577  /* If a workspace has an active fullscreen container, one of its
578  * children should always be focused. The above workspace_show()
579  * should be adequate for that, so return. */
580  if (con_get_fullscreen_con(workspace, CF_OUTPUT))
581  return true;
582 
583  Con *focus = con_descend_direction(workspace, direction);
584 
585  /* special case: if there was no tiling con to focus and the workspace
586  * has a floating con in the focus stack, focus the top of the focus
587  * stack (which may be floating) */
588  if (focus == workspace)
589  focus = con_descend_focused(workspace);
590 
591  if (focus) {
592  con_focus(focus);
593  x_set_warp_to(&(focus->rect));
594  }
595  return true;
596  }
597 
598  Con *parent = con->parent;
599 
600  if (con->type == CT_FLOATING_CON) {
601  if (orientation != HORIZ)
602  return false;
603 
604  /* left/right focuses the previous/next floating container */
605  Con *next;
606  if (way == 'n')
607  next = TAILQ_NEXT(con, floating_windows);
608  else
609  next = TAILQ_PREV(con, floating_head, floating_windows);
610 
611  /* If there is no next/previous container, wrap */
612  if (!next) {
613  if (way == 'n')
614  next = TAILQ_FIRST(&(parent->floating_head));
615  else
616  next = TAILQ_LAST(&(parent->floating_head), floating_head);
617  }
618 
619  /* Still no next/previous container? bail out */
620  if (!next)
621  return false;
622 
623  /* Raise the floating window on top of other windows preserving
624  * relative stack order */
625  while (TAILQ_LAST(&(parent->floating_head), floating_head) != next) {
626  Con *last = TAILQ_LAST(&(parent->floating_head), floating_head);
627  TAILQ_REMOVE(&(parent->floating_head), last, floating_windows);
628  TAILQ_INSERT_HEAD(&(parent->floating_head), last, floating_windows);
629  }
630 
632  return true;
633  }
634 
635  /* If the orientation does not match or there is no other con to focus, we
636  * need to go higher in the hierarchy */
637  if (con_orientation(parent) != orientation ||
638  con_num_children(parent) == 1)
639  return _tree_next(parent, way, orientation, wrap);
640 
641  Con *current = TAILQ_FIRST(&(parent->focus_head));
642  /* TODO: when can the following happen (except for floating windows, which
643  * are handled above)? */
644  if (TAILQ_EMPTY(&(parent->nodes_head))) {
645  DLOG("nothing to focus\n");
646  return false;
647  }
648 
649  Con *next;
650  if (way == 'n')
651  next = TAILQ_NEXT(current, nodes);
652  else
653  next = TAILQ_PREV(current, nodes_head, nodes);
654 
655  if (!next) {
657  /* If there is no next/previous container, we check if we can focus one
658  * when going higher (without wrapping, though). If so, we are done, if
659  * not, we wrap */
660  if (_tree_next(parent, way, orientation, false))
661  return true;
662 
663  if (!wrap)
664  return false;
665  }
666 
667  if (way == 'n')
668  next = TAILQ_FIRST(&(parent->nodes_head));
669  else
670  next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
671  }
672 
673  /* Don't violate fullscreen focus restrictions. */
675  return false;
676 
677  /* 3: focus choice comes in here. at the moment we will go down
678  * until we find a window */
679  /* TODO: check for window, atm we only go down as far as possible */
681  return true;
682 }
683 
684 /*
685  * Changes focus in the given way (next/previous) and given orientation
686  * (horizontal/vertical).
687  *
688  */
689 void tree_next(char way, orientation_t orientation) {
690  _tree_next(focused, way, orientation, true);
691 }
692 
693 /*
694  * tree_flatten() removes pairs of redundant split containers, e.g.:
695  * [workspace, horizontal]
696  * [v-split] [child3]
697  * [h-split]
698  * [child1] [child2]
699  * In this example, the v-split and h-split container are redundant.
700  * Such a situation can be created by moving containers in a direction which is
701  * not the orientation of their parent container. i3 needs to create a new
702  * split container then and if you move containers this way multiple times,
703  * redundant chains of split-containers can be the result.
704  *
705  */
706 void tree_flatten(Con *con) {
707  Con *current, *child, *parent = con->parent;
708  DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
709 
710  /* We only consider normal containers without windows */
711  if (con->type != CT_CON ||
712  parent->layout == L_OUTPUT || /* con == "content" */
713  con->window != NULL)
714  goto recurse;
715 
716  /* Ensure it got only one child */
717  child = TAILQ_FIRST(&(con->nodes_head));
718  if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
719  goto recurse;
720 
721  DLOG("child = %p, con = %p, parent = %p\n", child, con, parent);
722 
723  /* The child must have a different orientation than the con but the same as
724  * the con’s parent to be redundant */
725  if (!con_is_split(con) ||
726  !con_is_split(child) ||
727  (con->layout != L_SPLITH && con->layout != L_SPLITV) ||
728  (child->layout != L_SPLITH && child->layout != L_SPLITV) ||
729  con_orientation(con) == con_orientation(child) ||
730  con_orientation(child) != con_orientation(parent))
731  goto recurse;
732 
733  DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
734  /* 1: save focus */
735  Con *focus_next = TAILQ_FIRST(&(child->focus_head));
736 
737  DLOG("detaching...\n");
738  /* 2: re-attach the children to the parent before con */
739  while (!TAILQ_EMPTY(&(child->nodes_head))) {
740  current = TAILQ_FIRST(&(child->nodes_head));
741  DLOG("detaching current=%p / %s\n", current, current->name);
742  con_detach(current);
743  DLOG("re-attaching\n");
744  /* We don’t use con_attach() here because for a CT_CON, the special
745  * case handling of con_attach() does not trigger. So all it would do
746  * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
747  * directly use the TAILQ macros. */
748  current->parent = parent;
749  TAILQ_INSERT_BEFORE(con, current, nodes);
750  DLOG("attaching to focus list\n");
751  TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
752  current->percent = con->percent;
753  }
754  DLOG("re-attached all\n");
755 
756  /* 3: restore focus, if con was focused */
757  if (focus_next != NULL &&
758  TAILQ_FIRST(&(parent->focus_head)) == con) {
759  DLOG("restoring focus to focus_next=%p\n", focus_next);
760  TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
761  TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
762  DLOG("restored focus.\n");
763  }
764 
765  /* 4: close the redundant cons */
766  DLOG("closing redundant cons\n");
767  tree_close(con, DONT_KILL_WINDOW, true, false);
768 
769  /* Well, we got to abort the recursion here because we destroyed the
770  * container. However, if tree_flatten() is called sufficiently often,
771  * there can’t be the situation of having two pairs of redundant containers
772  * at once. Therefore, we can safely abort the recursion on this level
773  * after flattening. */
774  return;
775 
776 recurse:
777  /* We cannot use normal foreach here because tree_flatten might close the
778  * current container. */
779  current = TAILQ_FIRST(&(con->nodes_head));
780  while (current != NULL) {
781  Con *next = TAILQ_NEXT(current, nodes);
782  tree_flatten(current);
783  current = next;
784  }
785 
786  current = TAILQ_FIRST(&(con->floating_head));
787  while (current != NULL) {
788  Con *next = TAILQ_NEXT(current, floating_windows);
789  tree_flatten(current);
790  current = next;
791  }
792 }
struct Con * parent
Definition: data.h:532
void tree_next(char way, orientation_t orientation)
Changes focus in the given way (next/previous) and given orientation (horizontal/vertical).
Definition: tree.c:689
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
static bool _is_con_mapped(Con *con)
Definition: tree.c:167
bool mapped
Definition: data.h:500
bool urgent
Definition: data.h:504
char * name
Name of the output.
Definition: data.h:328
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:400
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition: tree.c:507
direction_t
Definition: data.h:53
uint32_t y
Definition: data.h:132
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:83
Config config
Definition: config.c:17
xcb_connection_t * conn
Definition: main.c:43
Con * workspace_encapsulate(Con *ws)
Creates a new container and re-parents all of children from the given workspace into it...
Definition: workspace.c:858
double percent
Definition: data.h:550
Output * get_output_next(direction_t direction, Output *current, output_close_far_t close_far)
Gets the output which is the next one in the given direction.
Definition: randr.c:161
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:298
Definition: data.h:56
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition: queue.h:394
#define LOG(fmt,...)
Definition: libi3.h:76
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition: workspace.c:228
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition: con.c:177
#define TAILQ_LAST(head, headname)
Definition: queue.h:339
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:458
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1029
struct Rect rect
Definition: data.h:534
struct all_cons_head all_cons
Definition: tree.c:17
struct Rect Rect
Definition: data.h:43
#define TAILQ_REPLACE(head, elm, elm2, field)
Definition: queue.h:413
Con * tree_open_con(Con *con, i3Window *window)
Opens an empty container in the current container.
Definition: tree.c:134
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1044
void con_force_split_parents_redraw(Con *con)
force parent split containers to be redrawn
Definition: con.c:23
struct ev_loop * main_loop
Definition: main.c:65
#define TAILQ_FIRST(head)
Definition: queue.h:336
Definition: data.h:53
struct Window * window
Definition: data.h:567
char * class_instance
Definition: data.h:358
bool level_up(void)
Moves focus one level up.
Definition: tree.c:444
void x_window_kill(xcb_window_t window, kill_window_t kill_window)
Kills the given X11 window using WM_DELETE_WINDOW (if supported).
Definition: x.c:272
An Output is a physical output on your graphics driver.
Definition: data.h:313
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
Con * con_new(Con *parent, i3Window *window)
Definition: con.c:67
void tree_append_json(Con *con, const char *filename, char **errormsg)
Definition: load_layout.c:511
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:344
Con * con_descend_direction(Con *con, direction_t direction)
Definition: con.c:1070
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition: x.c:1143
enum Con::@18 type
void con_update_parents_urgency(Con *con)
Make all parent containers urgent if con is urgent or clear the urgent flag of all parent containers ...
Definition: con.c:1613
#define DLOG(fmt,...)
Definition: libi3.h:86
Con * con_next_focused(Con *con)
Returns the container which will be focused next when the given container is not available anymore...
Definition: con.c:924
void tree_split(Con *con, orientation_t orientation)
Splits (horizontally or vertically) the given container by creating a new container which contains th...
Definition: tree.c:389
#define XCB_ICCCM_WM_STATE_WITHDRAWN
Definition: xcb_compat.h:20
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
Definition: con.c:346
bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus)
Closes the given container including all children.
Definition: tree.c:190
bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry)
Loads tree from ~/.i3/_restart.json (used for in-place restarts).
Definition: tree.c:68
uint32_t height
Definition: data.h:134
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:573
Definition: data.h:54
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
Con * con
Pointer to the Con which represents this output.
Definition: data.h:331
void x_push_changes(Con *con)
Pushes all changes (state of each node, see x_push_node() and the window stack) to X11...
Definition: x.c:884
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
Definition: data.h:530
char * name
Definition: data.h:540
fullscreen_mode_t fullscreen_mode
Definition: data.h:583
Definition: data.h:99
bool level_down(void)
Moves focus one level down.
Definition: tree.c:467
bool con_fullscreen_permits_focusing(Con *con)
Returns true if changing the focus to con would be allowed considering the fullscreen focus constrain...
Definition: con.c:1545
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name...
Definition: x.c:1101
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:193
void add_ignore_event(const int sequence, const int response_type)
Adds the given sequence to the list of events which are ignored.
#define TAILQ_EMPTY(head)
Definition: queue.h:344
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:507
void workspace_update_urgent_flag(Con *ws)
Goes through all clients on the given workspace and updates the workspace’s urgent flag accordingly...
Definition: workspace.c:772
void tree_flatten(Con *con)
tree_flatten() removes pairs of redundant split containers, e.g.
Definition: tree.c:706
void i3string_free(i3String *str)
Free an i3String.
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
Definition: util.c:168
bool con_is_split(Con *con)
Definition: con.c:246
Definition: data.h:55
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
bool force_focus_wrapping
Think of the following layout: Horizontal workspace with a tabbed con on the left of the screen and a...
Definition: config.h:142
#define ELOG(fmt,...)
Definition: libi3.h:81
Definition: data.h:58
#define GREP_FIRST(dest, head, condition)
Definition: util.h:39
layout_t layout
Definition: data.h:598
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:889
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:499
uint32_t x
Definition: data.h:131
Definition: data.h:97
orientation_t
Definition: data.h:57
#define TAILQ_END(head)
Definition: queue.h:337
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
Definition: randr.c:80
#define CALL(obj, member,...)
Definition: util.h:56
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
struct Con * croot
Definition: tree.c:14
xcb_window_t root
Definition: main.c:56
void tree_init(xcb_get_geometry_reply_t *geometry)
Initializes the tree by creating the root node, adding all RandR outputs to the tree (that means rand...
Definition: tree.c:115
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
static void mark_unmapped(Con *con)
Definition: tree.c:488
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
Definition: util.c:198
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:18
i3String * name
The name of the window.
Definition: data.h:361
xcb_window_t id
Definition: data.h:345
char * class_class
Definition: data.h:357
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:523
struct Con * focused
Definition: tree.c:15
Definition: data.h:59
void tree_close_con(kill_window_t kill_window)
Closes the current container using tree_close().
Definition: tree.c:360
#define FREE(pointer)
Definition: util.h:48
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container, in "container".
Definition: ipc.c:1175
Assignment ** ran_assignments
Definition: data.h:355
static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap)
Definition: tree.c:527
void x_con_kill(Con *con)
Kills the window decoration associated with the given container.
Definition: x.c:228
void render_con(Con *con, bool render_fullscreen)
"Renders" the given container (and its children), meaning that all rects are updated correctly...
Definition: render.c:126
kill_window_t
parameter to specify whether tree_close() and x_window_kill() should kill only this specific window o...
Definition: data.h:66
uint32_t width
Definition: data.h:133
Definition: data.h:98
struct ev_timer * urgency_timer
Definition: data.h:570
static Con * _create___i3(void)
Definition: tree.c:24