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