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