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