]> git.sur5r.net Git - i3/i3/blob - src/tree.c
Fix the percentages when moving containers.
[i3/i3] / src / tree.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 struct Con *croot;
8 struct Con *focused;
9
10 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
11
12 /*
13  * Loads tree from ~/.i3/_restart.json (used for in-place restarts).
14  *
15  */
16 bool tree_restore(const char *path) {
17     char *globbed = resolve_tilde(path);
18
19     if (!path_exists(globbed)) {
20         LOG("%s does not exist, not restoring tree\n", globbed);
21         free(globbed);
22         return false;
23     }
24
25     /* TODO: refactor the following */
26     croot = con_new(NULL);
27     focused = croot;
28
29     tree_append_json(globbed);
30
31     printf("appended tree, using new root\n");
32     croot = TAILQ_FIRST(&(croot->nodes_head));
33     printf("new root = %p\n", croot);
34     Con *out = TAILQ_FIRST(&(croot->nodes_head));
35     printf("out = %p\n", out);
36     Con *ws = TAILQ_FIRST(&(out->nodes_head));
37     printf("ws = %p\n", ws);
38
39     return true;
40 }
41
42 /*
43  * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
44  * root node are created in randr.c for each Output.
45  *
46  */
47 void tree_init() {
48     croot = con_new(NULL);
49     FREE(croot->name);
50     croot->name = "root";
51     croot->type = CT_ROOT;
52 }
53
54 /*
55  * Opens an empty container in the current container
56  *
57  */
58 Con *tree_open_con(Con *con) {
59     if (con == NULL) {
60         /* every focusable Con has a parent (outputs have parent root) */
61         con = focused->parent;
62         /* If the parent is an output, we are on a workspace. In this case,
63          * the new container needs to be opened as a leaf of the workspace. */
64         if (con->type == CT_OUTPUT)
65             con = focused;
66         /* If the currently focused container is a floating container, we
67          * attach the new container to the workspace */
68         if (con->type == CT_FLOATING_CON)
69             con = con->parent;
70     }
71
72     assert(con != NULL);
73
74     /* 3. create the container and attach it to its parent */
75     Con *new = con_new(con);
76
77     /* 4: re-calculate child->percent for each child */
78     con_fix_percent(con);
79
80     /* 5: focus the new container */
81     con_focus(new);
82
83     return new;
84 }
85
86 static bool _is_con_mapped(Con *con) {
87     Con *child;
88
89     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
90         if (_is_con_mapped(child))
91             return true;
92
93     return con->mapped;
94 }
95
96 /*
97  * Closes the given container including all children
98  *
99  */
100 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
101     bool was_mapped = con->mapped;
102     Con *parent = con->parent;
103
104     if (!was_mapped) {
105         /* Even if the container itself is not mapped, its children may be
106          * mapped (for example split containers don't have a mapped window on
107          * their own but usually contain mapped children). */
108         was_mapped = _is_con_mapped(con);
109     }
110
111     /* Get the container which is next focused */
112     Con *next = con_next_focused(con);
113     DLOG("next = %p, focused = %p\n", next, focused);
114
115     DLOG("closing %p, kill_window = %d\n", con, kill_window);
116     Con *child;
117     /* We cannot use TAILQ_FOREACH because the children get deleted
118      * in their parent’s nodes_head */
119     while (!TAILQ_EMPTY(&(con->nodes_head))) {
120         child = TAILQ_FIRST(&(con->nodes_head));
121         DLOG("killing child=%p\n", child);
122         tree_close(child, kill_window, true);
123     }
124
125     if (con->window != NULL) {
126         if (kill_window)
127             x_window_kill(con->window->id);
128         else {
129             /* un-parent the window */
130             xcb_reparent_window(conn, con->window->id, root, 0, 0);
131             /* TODO: client_unmap to set state to withdrawn */
132
133         }
134         FREE(con->window->class_class);
135         FREE(con->window->class_instance);
136         FREE(con->window->name_x);
137         FREE(con->window->name_json);
138         free(con->window);
139     }
140
141     /* kill the X11 part of this container */
142     x_con_kill(con);
143
144     con_detach(con);
145     if (con->type != CT_FLOATING_CON) {
146         /* If the container is *not* floating, we might need to re-distribute
147          * percentage values for the resized containers. */
148         con_fix_percent(parent);
149     }
150
151     if (con_is_floating(con)) {
152         Con *ws = con_get_workspace(con);
153         DLOG("Container was floating, killing floating container\n");
154         tree_close(parent, false, false);
155         DLOG("parent container killed\n");
156         if (con == focused) {
157             DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
158             next = ws;
159             /* now go down the focus stack as far as
160              * possible, excluding the current container */
161             while (!TAILQ_EMPTY(&(next->focus_head)))
162                 next = TAILQ_FIRST(&(next->focus_head));
163
164             dont_kill_parent = true;
165             DLOG("Alright, focusing %p\n", next);
166         } else {
167             next = NULL;
168         }
169     }
170
171     free(con->name);
172     TAILQ_REMOVE(&all_cons, con, all_cons);
173     free(con);
174
175     /* in the case of floating windows, we already focused another container
176      * when closing the parent, so we can exit now. */
177     if (!next) {
178         DLOG("No next container, i will just exit now\n");
179         return;
180     }
181
182     if (was_mapped || con == focused) {
183         DLOG("focusing %p / %s\n", next, next->name);
184         /* TODO: check if the container (or one of its children) was focused */
185         con_focus(next);
186     } else {
187         DLOG("not focusing, was not mapped\n");
188     }
189
190     /* check if the parent container is empty now and close it */
191     if (!dont_kill_parent &&
192         parent->type != CT_WORKSPACE &&
193         TAILQ_EMPTY(&(parent->nodes_head))) {
194         DLOG("Closing empty parent container\n");
195         /* TODO: check if this container would swallow any other client and
196          * don’t close it automatically. */
197         tree_close(parent, false, false);
198     }
199 }
200
201 /*
202  * Closes the current container using tree_close().
203  *
204  */
205 void tree_close_con() {
206     assert(focused != NULL);
207     if (focused->type == CT_WORKSPACE) {
208         LOG("Cannot close workspace\n");
209         return;
210     }
211
212     /* There *should* be no possibility to focus outputs / root container */
213     assert(focused->type != CT_OUTPUT);
214     assert(focused->type != CT_ROOT);
215
216     /* Kill con */
217     tree_close(focused, true, false);
218 }
219
220 /*
221  * Splits (horizontally or vertically) the given container by creating a new
222  * container which contains the old one and the future ones.
223  *
224  */
225 void tree_split(Con *con, orientation_t orientation) {
226     /* for a workspace, we just need to change orientation */
227     if (con->type == CT_WORKSPACE) {
228         DLOG("Workspace, simply changing orientation to %d\n", orientation);
229         con->orientation = orientation;
230         return;
231     }
232
233     Con *parent = con->parent;
234     /* if we are in a container whose parent contains only one
235      * child (its split functionality is unused so far), we just change the
236      * orientation (more intuitive than splitting again) */
237     if (con_num_children(parent) == 1) {
238         parent->orientation = orientation;
239         DLOG("Just changing orientation of existing container\n");
240         return;
241     }
242
243     DLOG("Splitting in orientation %d\n", orientation);
244
245     /* 2: replace it with a new Con */
246     Con *new = con_new(NULL);
247     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
248     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
249     new->parent = parent;
250     new->orientation = orientation;
251
252     /* 3: swap 'percent' (resize factor) */
253     new->percent = con->percent;
254     con->percent = 0.0;
255
256     /* 4: add it as a child to the new Con */
257     con_attach(con, new, false);
258 }
259
260 /*
261  * Moves focus one level up.
262  *
263  */
264 void level_up() {
265     /* We can focus up to the workspace, but not any higher in the tree */
266     if (focused->parent->type != CT_CON &&
267         focused->parent->type != CT_WORKSPACE) {
268         printf("cannot go up\n");
269         return;
270     }
271     con_focus(focused->parent);
272 }
273
274 /*
275  * Moves focus one level down.
276  *
277  */
278 void level_down() {
279     /* Go down the focus stack of the current node */
280     Con *next = TAILQ_FIRST(&(focused->focus_head));
281     if (next == TAILQ_END(&(focused->focus_head))) {
282         printf("cannot go down\n");
283         return;
284     }
285     con_focus(next);
286 }
287
288 static void mark_unmapped(Con *con) {
289     Con *current;
290
291     con->mapped = false;
292     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
293         mark_unmapped(current);
294     if (con->type == CT_WORKSPACE) {
295         /* We need to call mark_unmapped on floating nodes aswell since we can
296          * make containers floating. */
297         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
298             mark_unmapped(current);
299     }
300 }
301
302 /*
303  * Renders the tree, that is rendering all outputs using render_con() and
304  * pushing the changes to X11 using x_push_changes().
305  *
306  */
307 void tree_render() {
308     if (croot == NULL)
309         return;
310
311     DLOG("-- BEGIN RENDERING --\n");
312     /* Reset map state for all nodes in tree */
313     /* TODO: a nicer method to walk all nodes would be good, maybe? */
314     mark_unmapped(croot);
315     croot->mapped = true;
316
317     /* We start rendering at an output */
318     Con *output;
319     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
320         DLOG("output %p / %s\n", output, output->name);
321         render_con(output, false);
322     }
323     x_push_changes(croot);
324     DLOG("-- END RENDERING --\n");
325 }
326
327 /*
328  * Changes focus in the given way (next/previous) and given orientation
329  * (horizontal/vertical).
330  *
331  */
332 void tree_next(char way, orientation_t orientation) {
333     /* 1: get the first parent with the same orientation */
334     Con *parent = focused->parent;
335     while (focused->type != CT_WORKSPACE &&
336            con_orientation(parent) != orientation) {
337         LOG("need to go one level further up\n");
338         /* if the current parent is an output, we are at a workspace
339          * and the orientation still does not match */
340         if (parent->type == CT_WORKSPACE)
341             return;
342         parent = parent->parent;
343     }
344     Con *current = TAILQ_FIRST(&(parent->focus_head));
345     assert(current != TAILQ_END(&(parent->focus_head)));
346
347     /* 2: chose next (or previous) */
348     Con *next;
349     if (way == 'n') {
350         next = TAILQ_NEXT(current, nodes);
351         /* if we are at the end of the list, we need to wrap */
352         if (next == TAILQ_END(&(parent->nodes_head)))
353             next = TAILQ_FIRST(&(parent->nodes_head));
354     } else {
355         next = TAILQ_PREV(current, nodes_head, nodes);
356         /* if we are at the end of the list, we need to wrap */
357         if (next == TAILQ_END(&(parent->nodes_head)))
358             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
359     }
360
361     /* 3: focus choice comes in here. at the moment we will go down
362      * until we find a window */
363     /* TODO: check for window, atm we only go down as far as possible */
364     while (!TAILQ_EMPTY(&(next->focus_head)))
365         next = TAILQ_FIRST(&(next->focus_head));
366
367     DLOG("focusing %p\n", next);
368     con_focus(next);
369 }
370
371 /*
372  * Moves the current container in the given way (next/previous) and given
373  * orientation (horizontal/vertical).
374  *
375  */
376 void tree_move(char way, orientation_t orientation) {
377     /* 1: get the first parent with the same orientation */
378     Con *parent = focused->parent;
379     Con *old_parent = parent;
380     if (focused->type == CT_WORKSPACE)
381         return;
382     bool level_changed = false;
383     while (con_orientation(parent) != orientation) {
384         DLOG("need to go one level further up\n");
385         /* If the current parent is an output, we are at a workspace
386          * and the orientation still does not match. In this case, we split the
387          * workspace to have the same look & feel as in older i3 releases. */
388         if (parent->type == CT_WORKSPACE) {
389             DLOG("Arrived at workspace, splitting...\n");
390             /* 1: create a new split container */
391             Con *new = con_new(NULL);
392             new->parent = parent;
393
394             /* 2: copy layout and orientation from workspace */
395             new->layout = parent->layout;
396             new->orientation = parent->orientation;
397
398             Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
399             if (old_focused == TAILQ_END(&(parent->focus_head)))
400                 old_focused = NULL;
401
402             /* 3: move the existing cons of this workspace below the new con */
403             DLOG("Moving cons\n");
404             Con *child;
405             while (!TAILQ_EMPTY(&(parent->nodes_head))) {
406                 child = TAILQ_FIRST(&(parent->nodes_head));
407                 con_detach(child);
408                 con_attach(child, new, true);
409             }
410
411             /* 4: switch workspace orientation */
412             parent->orientation = orientation;
413
414             /* 5: attach the new split container to the workspace */
415             DLOG("Attaching new split to ws\n");
416             con_attach(new, parent, false);
417
418             /* 6: fix the percentages */
419             con_fix_percent(new);
420             con_fix_percent(parent);
421
422             if (old_focused)
423                 con_focus(old_focused);
424
425             level_changed = true;
426
427             break;
428         }
429         parent = parent->parent;
430         level_changed = true;
431     }
432     Con *current = TAILQ_FIRST(&(parent->focus_head));
433     assert(current != TAILQ_END(&(parent->focus_head)));
434
435     /* 2: chose next (or previous) */
436     Con *next = current;
437     if (way == 'n') {
438         LOG("i would insert it after %p / %s\n", next, next->name);
439
440         /* Have a look at the next container: If there is no next container or
441          * if it is a leaf node, we move the focused one left to it. However,
442          * for split containers, we descend into it. */
443         next = TAILQ_NEXT(next, nodes);
444         if (next == TAILQ_END(&(next->parent->nodes_head))) {
445             if (focused == current)
446                 return;
447             next = current;
448         } else {
449             if (level_changed && con_is_leaf(next)) {
450                 next = current;
451             } else {
452                 /* if this is a split container, we need to go down */
453                 while (!TAILQ_EMPTY(&(next->focus_head)))
454                     next = TAILQ_FIRST(&(next->focus_head));
455             }
456         }
457
458         con_detach(focused);
459         con_fix_percent(focused->parent);
460         focused->parent = next->parent;
461
462         TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
463         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
464         /* TODO: don’t influence focus handling? */
465     } else {
466         LOG("i would insert it before %p / %s\n", current, current->name);
467         bool gone_down = false;
468         next = TAILQ_PREV(next, nodes_head, nodes);
469         if (next == TAILQ_END(&(next->parent->nodes_head))) {
470             if (focused == current)
471                 return;
472             next = current;
473         } else {
474             if (level_changed && con_is_leaf(next)) {
475                 next = current;
476             } else {
477                 /* if this is a split container, we need to go down */
478                 while (!TAILQ_EMPTY(&(next->focus_head))) {
479                     gone_down = true;
480                     next = TAILQ_FIRST(&(next->focus_head));
481                 }
482             }
483         }
484
485         con_detach(focused);
486         con_fix_percent(focused);
487         focused->parent = next->parent;
488
489         /* After going down in the tree, we insert the container *after*
490          * the currently focused one even though the command used "before".
491          * This is to keep the user experience clear, since the before/after
492          * only signifies the direction of the movement on top-level */
493         if (gone_down)
494             TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
495         else TAILQ_INSERT_BEFORE(next, focused, nodes);
496         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
497         /* TODO: don’t influence focus handling? */
498     }
499
500     /* fix the percentages in the container we moved to */
501     int children = con_num_children(next->parent);
502     if (children == 1)
503         focused->percent = 1.0;
504     else
505         focused->percent = 1.0 / (children - 1);
506     con_fix_percent(next->parent);
507
508     /* We need to call con_focus() to fix the focus stack "above" the container
509      * we just inserted the focused container into (otherwise, the parent
510      * container(s) would still point to the old container(s)). */
511     con_focus(focused);
512
513     if (con_num_children(old_parent) == 0) {
514         DLOG("Old container empty after moving. Let's close it\n");
515         tree_close(old_parent, false, false);
516     }
517     else {
518         /* fix the percentages in the container we moved from */
519         con_fix_percent(old_parent);
520     }
521
522     tree_flatten(croot);
523 }
524
525 /*
526  * tree_flatten() removes pairs of redundant split containers, e.g.:
527  *       [workspace, horizontal]
528  *   [v-split]           [child3]
529  *   [h-split]
530  * [child1] [child2]
531  * In this example, the v-split and h-split container are redundant.
532  * Such a situation can be created by moving containers in a direction which is
533  * not the orientation of their parent container. i3 needs to create a new
534  * split container then and if you move containers this way multiple times,
535  * redundant chains of split-containers can be the result.
536  *
537  */
538 void tree_flatten(Con *con) {
539     Con *current, *child, *parent = con->parent;
540     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
541
542     /* We only consider normal containers without windows */
543     if (con->type != CT_CON || con->window != NULL)
544         goto recurse;
545
546     /* Ensure it got only one child */
547     child = TAILQ_FIRST(&(con->nodes_head));
548     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
549         goto recurse;
550
551     /* The child must have a different orientation than the con but the same as
552      * the con’s parent to be redundant */
553     if (con->orientation == NO_ORIENTATION ||
554         child->orientation == NO_ORIENTATION ||
555         con->orientation == child->orientation ||
556         child->orientation != parent->orientation)
557         goto recurse;
558
559     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
560     /* 1: save focus */
561     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
562
563     DLOG("detaching...\n");
564     /* 2: re-attach the children to the parent before con */
565     while (!TAILQ_EMPTY(&(child->nodes_head))) {
566         current = TAILQ_FIRST(&(child->nodes_head));
567         DLOG("detaching current=%p / %s\n", current, current->name);
568         con_detach(current);
569         DLOG("re-attaching\n");
570         /* We don’t use con_attach() here because for a CT_CON, the special
571          * case handling of con_attach() does not trigger. So all it would do
572          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
573          * directly use the TAILQ macros. */
574         current->parent = parent;
575         TAILQ_INSERT_BEFORE(con, current, nodes);
576         DLOG("attaching to focus list\n");
577         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
578     }
579     DLOG("re-attached all\n");
580
581     /* 3: restore focus, if con was focused */
582     if (focus_next != NULL &&
583         TAILQ_FIRST(&(parent->focus_head)) == con) {
584         DLOG("restoring focus to focus_next=%p\n", focus_next);
585         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
586         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
587         DLOG("restored focus.\n");
588     }
589
590     /* 4: close the redundant cons */
591     DLOG("closing redundant cons\n");
592     tree_close(con, false, true);
593
594     /* Well, we got to abort the recursion here because we destroyed the
595      * container. However, if tree_flatten() is called sufficiently often,
596      * there can’t be the situation of having two pairs of redundant containers
597      * at once. Therefore, we can safely abort the recursion on this level
598      * after flattening. */
599     return;
600
601 recurse:
602     /* We cannot use normal foreach here because tree_flatten might close the
603      * current container. */
604     current = TAILQ_FIRST(&(con->nodes_head));
605     while (current != NULL) {
606         Con *next = TAILQ_NEXT(current, nodes);
607         tree_flatten(current);
608         current = next;
609     }
610
611     current = TAILQ_FIRST(&(con->floating_head));
612     while (current != NULL) {
613         Con *next = TAILQ_NEXT(current, floating_windows);
614         tree_flatten(current);
615         current = next;
616     }
617 }