]> git.sur5r.net Git - i3/i3/blob - src/tree.c
Don't mess with the percentages in tree_flatten.
[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             /* go down the focus stack as far as possible */
159             next = con_descend_focused(ws);
160
161             dont_kill_parent = true;
162             DLOG("Alright, focusing %p\n", next);
163         } else {
164             next = NULL;
165         }
166     }
167
168     free(con->name);
169     TAILQ_REMOVE(&all_cons, con, all_cons);
170     free(con);
171
172     /* in the case of floating windows, we already focused another container
173      * when closing the parent, so we can exit now. */
174     if (!next) {
175         DLOG("No next container, i will just exit now\n");
176         return;
177     }
178
179     if (was_mapped || con == focused) {
180         DLOG("focusing %p / %s\n", next, next->name);
181         /* TODO: check if the container (or one of its children) was focused */
182         con_focus(next);
183     } else {
184         DLOG("not focusing, was not mapped\n");
185     }
186
187     /* check if the parent container is empty now and close it */
188     if (!dont_kill_parent &&
189         parent->type != CT_WORKSPACE &&
190         TAILQ_EMPTY(&(parent->nodes_head))) {
191         DLOG("Closing empty parent container\n");
192         /* TODO: check if this container would swallow any other client and
193          * don’t close it automatically. */
194         tree_close(parent, false, false);
195     }
196 }
197
198 /*
199  * Closes the current container using tree_close().
200  *
201  */
202 void tree_close_con() {
203     assert(focused != NULL);
204     if (focused->type == CT_WORKSPACE) {
205         LOG("Cannot close workspace\n");
206         return;
207     }
208
209     /* There *should* be no possibility to focus outputs / root container */
210     assert(focused->type != CT_OUTPUT);
211     assert(focused->type != CT_ROOT);
212
213     /* Kill con */
214     tree_close(focused, true, false);
215 }
216
217 /*
218  * Splits (horizontally or vertically) the given container by creating a new
219  * container which contains the old one and the future ones.
220  *
221  */
222 void tree_split(Con *con, orientation_t orientation) {
223     /* for a workspace, we just need to change orientation */
224     if (con->type == CT_WORKSPACE) {
225         DLOG("Workspace, simply changing orientation to %d\n", orientation);
226         con->orientation = orientation;
227         return;
228     }
229
230     Con *parent = con->parent;
231     /* if we are in a container whose parent contains only one
232      * child (its split functionality is unused so far), we just change the
233      * orientation (more intuitive than splitting again) */
234     if (con_num_children(parent) == 1) {
235         parent->orientation = orientation;
236         DLOG("Just changing orientation of existing container\n");
237         return;
238     }
239
240     DLOG("Splitting in orientation %d\n", orientation);
241
242     /* 2: replace it with a new Con */
243     Con *new = con_new(NULL);
244     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
245     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
246     new->parent = parent;
247     new->orientation = orientation;
248
249     /* 3: swap 'percent' (resize factor) */
250     new->percent = con->percent;
251     con->percent = 0.0;
252
253     /* 4: add it as a child to the new Con */
254     con_attach(con, new, false);
255 }
256
257 /*
258  * Moves focus one level up.
259  *
260  */
261 void level_up() {
262     /* We can focus up to the workspace, but not any higher in the tree */
263     if (focused->parent->type != CT_CON &&
264         focused->parent->type != CT_WORKSPACE) {
265         printf("cannot go up\n");
266         return;
267     }
268     con_focus(focused->parent);
269 }
270
271 /*
272  * Moves focus one level down.
273  *
274  */
275 void level_down() {
276     /* Go down the focus stack of the current node */
277     Con *next = TAILQ_FIRST(&(focused->focus_head));
278     if (next == TAILQ_END(&(focused->focus_head))) {
279         printf("cannot go down\n");
280         return;
281     }
282     con_focus(next);
283 }
284
285 static void mark_unmapped(Con *con) {
286     Con *current;
287
288     con->mapped = false;
289     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
290         mark_unmapped(current);
291     if (con->type == CT_WORKSPACE) {
292         /* We need to call mark_unmapped on floating nodes aswell since we can
293          * make containers floating. */
294         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
295             mark_unmapped(current);
296     }
297 }
298
299 /*
300  * Renders the tree, that is rendering all outputs using render_con() and
301  * pushing the changes to X11 using x_push_changes().
302  *
303  */
304 void tree_render() {
305     if (croot == NULL)
306         return;
307
308     DLOG("-- BEGIN RENDERING --\n");
309     /* Reset map state for all nodes in tree */
310     /* TODO: a nicer method to walk all nodes would be good, maybe? */
311     mark_unmapped(croot);
312     croot->mapped = true;
313
314     /* We start rendering at an output */
315     Con *output;
316     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
317         DLOG("output %p / %s\n", output, output->name);
318         render_con(output, false);
319     }
320     x_push_changes(croot);
321     DLOG("-- END RENDERING --\n");
322 }
323
324 /*
325  * Changes focus in the given way (next/previous) and given orientation
326  * (horizontal/vertical).
327  *
328  */
329 void tree_next(char way, orientation_t orientation) {
330     /* 1: get the first parent with the same orientation */
331     Con *parent = focused->parent;
332     while (focused->type != CT_WORKSPACE &&
333            con_orientation(parent) != orientation) {
334         LOG("need to go one level further up\n");
335         /* if the current parent is an output, we are at a workspace
336          * and the orientation still does not match */
337         if (parent->type == CT_WORKSPACE)
338             return;
339         parent = parent->parent;
340     }
341     Con *current = TAILQ_FIRST(&(parent->focus_head));
342     assert(current != TAILQ_END(&(parent->focus_head)));
343
344     if (TAILQ_EMPTY(&(parent->nodes_head))) {
345         DLOG("Nothing to focus here, move along...\n");
346         return;
347     }
348
349     /* 2: chose next (or previous) */
350     Con *next;
351     if (way == 'n') {
352         next = TAILQ_NEXT(current, nodes);
353         /* if we are at the end of the list, we need to wrap */
354         if (next == TAILQ_END(&(parent->nodes_head)))
355             next = TAILQ_FIRST(&(parent->nodes_head));
356     } else {
357         next = TAILQ_PREV(current, nodes_head, nodes);
358         /* if we are at the end of the list, we need to wrap */
359         if (next == TAILQ_END(&(parent->nodes_head)))
360             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
361     }
362
363     /* 3: focus choice comes in here. at the moment we will go down
364      * until we find a window */
365     /* TODO: check for window, atm we only go down as far as possible */
366     con_focus(con_descend_focused(next));
367 }
368
369 /*
370  * Moves the current container in the given way (next/previous) and given
371  * orientation (horizontal/vertical).
372  *
373  */
374 void tree_move(char way, orientation_t orientation) {
375     /* 1: get the first parent with the same orientation */
376     Con *parent = focused->parent;
377     Con *old_parent = parent;
378     if (focused->type == CT_WORKSPACE)
379         return;
380     bool level_changed = false;
381     while (con_orientation(parent) != orientation) {
382         DLOG("need to go one level further up\n");
383         /* If the current parent is an output, we are at a workspace
384          * and the orientation still does not match. In this case, we split the
385          * workspace to have the same look & feel as in older i3 releases. */
386         if (parent->type == CT_WORKSPACE) {
387             DLOG("Arrived at workspace\n");
388             /* In case of moving a window out of a floating con, there might be
389              * not a single tiling container. Makes no sense to split then, so
390              * just use the workspace as target */
391             if (TAILQ_EMPTY(&(parent->nodes_head)))
392                 break;
393
394             /* 1: create a new split container */
395             Con *new = con_new(NULL);
396             new->parent = parent;
397
398             /* 2: copy layout and orientation from workspace */
399             new->layout = parent->layout;
400             new->orientation = parent->orientation;
401
402             Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
403             if (old_focused == TAILQ_END(&(parent->focus_head)))
404                 old_focused = NULL;
405
406             /* 3: move the existing cons of this workspace below the new con */
407             DLOG("Moving cons\n");
408             Con *child;
409             while (!TAILQ_EMPTY(&(parent->nodes_head))) {
410                 child = TAILQ_FIRST(&(parent->nodes_head));
411                 con_detach(child);
412                 con_attach(child, new, true);
413             }
414
415             /* 4: switch workspace orientation */
416             parent->orientation = orientation;
417
418             /* 5: attach the new split container to the workspace */
419             DLOG("Attaching new split to ws\n");
420             con_attach(new, parent, false);
421
422             /* 6: fix the percentages */
423             con_fix_percent(parent);
424
425             if (old_focused)
426                 con_focus(old_focused);
427
428             level_changed = true;
429
430             break;
431         }
432         parent = parent->parent;
433         level_changed = true;
434     }
435     Con *current = TAILQ_FIRST(&(parent->focus_head));
436     assert(current != TAILQ_END(&(parent->focus_head)));
437
438     /* If we have no tiling cons (when moving a window out of a floating con to
439      * an otherwise empty workspace for example), we just attach the window to
440      * the workspace. */
441     bool fix_percent = false;
442     if (TAILQ_EMPTY(&(parent->nodes_head))) {
443         con_detach(focused);
444         con_fix_percent(focused->parent);
445         focused->parent = parent;
446
447         TAILQ_INSERT_HEAD(&(parent->nodes_head), focused, nodes);
448         TAILQ_INSERT_HEAD(&(parent->focus_head), focused, focused);
449     } else {
450         /* 2: chose next (or previous) */
451         Con *next = current;
452         if (way == 'n') {
453             LOG("i would insert it after %p / %s\n", next, next->name);
454
455             /* Have a look at the next container: If there is no next container or
456              * if it is a leaf node, we move the focused one left to it. However,
457              * for split containers, we descend into it. */
458             next = TAILQ_NEXT(next, nodes);
459             if (next == TAILQ_END(&(next->parent->nodes_head))) {
460                 if (focused == current)
461                     return;
462                 next = current;
463             } else {
464                 if (level_changed && con_is_leaf(next)) {
465                     next = current;
466                 } else {
467                     /* if this is a split container, we need to go down */
468                     next = con_descend_focused(next);
469                 }
470             }
471
472             con_detach(focused);
473             if (focused->parent != next->parent) {
474                 con_fix_percent(focused->parent);
475                 focused->parent = next->parent;
476                 fix_percent = true;
477             }
478
479             TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
480             TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
481             /* TODO: don’t influence focus handling? */
482         } else {
483             LOG("i would insert it before %p / %s\n", current, current->name);
484             bool gone_down = false;
485             next = TAILQ_PREV(next, nodes_head, nodes);
486             if (next == TAILQ_END(&(next->parent->nodes_head))) {
487                 if (focused == current)
488                     return;
489                 next = current;
490             } else {
491                 if (level_changed && con_is_leaf(next)) {
492                     next = current;
493                 } else {
494                     /* if this is a split container, we need to go down */
495                     while (!TAILQ_EMPTY(&(next->focus_head))) {
496                         gone_down = true;
497                         next = TAILQ_FIRST(&(next->focus_head));
498                     }
499                 }
500             }
501
502             con_detach(focused);
503             if (focused->parent != next->parent) {
504                 con_fix_percent(focused->parent);
505                 focused->parent = next->parent;
506                 fix_percent = true;
507             }
508
509             /* After going down in the tree, we insert the container *after*
510              * the currently focused one even though the command used "before".
511              * This is to keep the user experience clear, since the before/after
512              * only signifies the direction of the movement on top-level */
513             if (gone_down)
514                 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
515             else TAILQ_INSERT_BEFORE(next, focused, nodes);
516             TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
517             /* TODO: don’t influence focus handling? */
518         }
519     }
520
521     /* fix the percentages in the container we moved to */
522     if (fix_percent) {
523         int children = con_num_children(focused->parent);
524         if (children == 1)
525             focused->percent = 1.0;
526         else
527             focused->percent = 1.0 / (children - 1);
528         con_fix_percent(focused->parent);
529     }
530
531     /* We need to call con_focus() to fix the focus stack "above" the container
532      * we just inserted the focused container into (otherwise, the parent
533      * container(s) would still point to the old container(s)). */
534     con_focus(focused);
535
536     if (con_num_children(old_parent) == 0) {
537         DLOG("Old container empty after moving. Let's close it\n");
538         tree_close(old_parent, false, false);
539     } else if (level_changed) {
540         /* fix the percentages in the container we moved from */
541         con_fix_percent(old_parent);
542     }
543
544     tree_flatten(croot);
545 }
546
547 /*
548  * tree_flatten() removes pairs of redundant split containers, e.g.:
549  *       [workspace, horizontal]
550  *   [v-split]           [child3]
551  *   [h-split]
552  * [child1] [child2]
553  * In this example, the v-split and h-split container are redundant.
554  * Such a situation can be created by moving containers in a direction which is
555  * not the orientation of their parent container. i3 needs to create a new
556  * split container then and if you move containers this way multiple times,
557  * redundant chains of split-containers can be the result.
558  *
559  */
560 void tree_flatten(Con *con) {
561     Con *current, *child, *parent = con->parent;
562     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
563
564     /* We only consider normal containers without windows */
565     if (con->type != CT_CON || con->window != NULL)
566         goto recurse;
567
568     /* Ensure it got only one child */
569     child = TAILQ_FIRST(&(con->nodes_head));
570     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
571         goto recurse;
572
573     /* The child must have a different orientation than the con but the same as
574      * the con’s parent to be redundant */
575     if (con->orientation == NO_ORIENTATION ||
576         child->orientation == NO_ORIENTATION ||
577         con->orientation == child->orientation ||
578         child->orientation != parent->orientation)
579         goto recurse;
580
581     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
582     /* 1: save focus */
583     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
584
585     DLOG("detaching...\n");
586     /* 2: re-attach the children to the parent before con */
587     while (!TAILQ_EMPTY(&(child->nodes_head))) {
588         current = TAILQ_FIRST(&(child->nodes_head));
589         DLOG("detaching current=%p / %s\n", current, current->name);
590         con_detach(current);
591         DLOG("re-attaching\n");
592         /* We don’t use con_attach() here because for a CT_CON, the special
593          * case handling of con_attach() does not trigger. So all it would do
594          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
595          * directly use the TAILQ macros. */
596         current->parent = parent;
597         TAILQ_INSERT_BEFORE(con, current, nodes);
598         DLOG("attaching to focus list\n");
599         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
600         current->percent = con->percent;
601     }
602     DLOG("re-attached all\n");
603
604     /* 3: restore focus, if con was focused */
605     if (focus_next != NULL &&
606         TAILQ_FIRST(&(parent->focus_head)) == con) {
607         DLOG("restoring focus to focus_next=%p\n", focus_next);
608         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
609         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
610         DLOG("restored focus.\n");
611     }
612
613     /* 4: close the redundant cons */
614     DLOG("closing redundant cons\n");
615     tree_close(con, false, true);
616
617     /* Well, we got to abort the recursion here because we destroyed the
618      * container. However, if tree_flatten() is called sufficiently often,
619      * there can’t be the situation of having two pairs of redundant containers
620      * at once. Therefore, we can safely abort the recursion on this level
621      * after flattening. */
622     return;
623
624 recurse:
625     /* We cannot use normal foreach here because tree_flatten might close the
626      * current container. */
627     current = TAILQ_FIRST(&(con->nodes_head));
628     while (current != NULL) {
629         Con *next = TAILQ_NEXT(current, nodes);
630         tree_flatten(current);
631         current = next;
632     }
633
634     current = TAILQ_FIRST(&(con->floating_head));
635     while (current != NULL) {
636         Con *next = TAILQ_NEXT(current, floating_windows);
637         tree_flatten(current);
638         current = next;
639     }
640 }