]> git.sur5r.net Git - i3/i3/blob - src/tree.c
Bugfix: Don’t run into an endless loop when killing con with children (Thanks mseed)
[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->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
65             con = focused;
66         }
67
68         /* If the currently focused container is a floating container, we
69          * attach the new container to the workspace */
70         if (con->type == CT_FLOATING_CON)
71             con = con->parent;
72         DLOG("con = %p\n", con);
73     }
74
75     assert(con != NULL);
76
77     /* 3. create the container and attach it to its parent */
78     Con *new = con_new(con);
79
80     /* 4: re-calculate child->percent for each child */
81     con_fix_percent(con);
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  * Returns true if the container was killed or false if just WM_DELETE was sent
99  * and the window is expected to kill itself.
100  *
101  */
102 bool tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
103     bool was_mapped = con->mapped;
104     Con *parent = con->parent;
105
106     if (!was_mapped) {
107         /* Even if the container itself is not mapped, its children may be
108          * mapped (for example split containers don't have a mapped window on
109          * their own but usually contain mapped children). */
110         was_mapped = _is_con_mapped(con);
111     }
112
113     /* Get the container which is next focused */
114     Con *next = con_next_focused(con);
115     DLOG("next = %p, focused = %p\n", next, focused);
116
117     DLOG("closing %p, kill_window = %d\n", con, kill_window);
118     Con *child, *nextchild;
119     bool abort_kill = false;
120     /* We cannot use TAILQ_FOREACH because the children get deleted
121      * in their parent’s nodes_head */
122     for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) {
123         nextchild = TAILQ_NEXT(child, nodes);
124         DLOG("killing child=%p\n", child);
125         if (!tree_close(child, kill_window, true))
126             abort_kill = true;
127         child = nextchild;
128     }
129
130     if (abort_kill) {
131         DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
132         return false;
133     }
134
135     if (con->window != NULL) {
136         if (kill_window) {
137             x_window_kill(con->window->id);
138             return false;
139         } else {
140             /* un-parent the window */
141             xcb_reparent_window(conn, con->window->id, root, 0, 0);
142             /* TODO: client_unmap to set state to withdrawn */
143
144         }
145         FREE(con->window->class_class);
146         FREE(con->window->class_instance);
147         FREE(con->window->name_x);
148         FREE(con->window->name_json);
149         free(con->window);
150     }
151
152     /* kill the X11 part of this container */
153     x_con_kill(con);
154
155     con_detach(con);
156     if (con->type != CT_FLOATING_CON) {
157         /* If the container is *not* floating, we might need to re-distribute
158          * percentage values for the resized containers. */
159         con_fix_percent(parent);
160     }
161
162     if (con_is_floating(con)) {
163         Con *ws = con_get_workspace(con);
164         DLOG("Container was floating, killing floating container\n");
165         tree_close(parent, false, false);
166         DLOG("parent container killed\n");
167         if (con == focused) {
168             DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
169             /* go down the focus stack as far as possible */
170             next = con_descend_focused(ws);
171
172             dont_kill_parent = true;
173             DLOG("Alright, focusing %p\n", next);
174         } else {
175             next = NULL;
176         }
177     }
178
179     free(con->name);
180     FREE(con->deco_render_params);
181     TAILQ_REMOVE(&all_cons, con, all_cons);
182     free(con);
183
184     /* in the case of floating windows, we already focused another container
185      * when closing the parent, so we can exit now. */
186     if (!next) {
187         DLOG("No next container, i will just exit now\n");
188         return true;
189     }
190
191     if (was_mapped || con == focused) {
192         if (kill_window || !dont_kill_parent || con == focused) {
193             DLOG("focusing %p / %s\n", next, next->name);
194             /* TODO: check if the container (or one of its children) was focused */
195             if (next->type == CT_DOCKAREA) {
196                 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
197                 con_focus(con_descend_focused(output_get_content(next->parent)));
198             } else {
199                 con_focus(next);
200             }
201         }
202         else {
203             DLOG("not focusing because we're not killing anybody");
204         }
205     } else {
206         DLOG("not focusing, was not mapped\n");
207     }
208
209     /* check if the parent container is empty now and close it */
210     if (!dont_kill_parent)
211         CALL(parent, on_remove_child);
212
213     return true;
214 }
215
216 /*
217  * Closes the current container using tree_close().
218  *
219  */
220 void tree_close_con() {
221     assert(focused != NULL);
222     if (focused->type == CT_WORKSPACE) {
223         LOG("Cannot close workspace\n");
224         return;
225     }
226
227     /* There *should* be no possibility to focus outputs / root container */
228     assert(focused->type != CT_OUTPUT);
229     assert(focused->type != CT_ROOT);
230
231     /* Kill con */
232     tree_close(focused, true, false);
233 }
234
235 /*
236  * Splits (horizontally or vertically) the given container by creating a new
237  * container which contains the old one and the future ones.
238  *
239  */
240 void tree_split(Con *con, orientation_t orientation) {
241     /* for a workspace, we just need to change orientation */
242     if (con->type == CT_WORKSPACE) {
243         DLOG("Workspace, simply changing orientation to %d\n", orientation);
244         con->orientation = orientation;
245         return;
246     }
247
248     Con *parent = con->parent;
249     /* if we are in a container whose parent contains only one
250      * child (its split functionality is unused so far), we just change the
251      * orientation (more intuitive than splitting again) */
252     if (con_num_children(parent) == 1) {
253         parent->orientation = orientation;
254         DLOG("Just changing orientation of existing container\n");
255         return;
256     }
257
258     DLOG("Splitting in orientation %d\n", orientation);
259
260     /* 2: replace it with a new Con */
261     Con *new = con_new(NULL);
262     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
263     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
264     new->parent = parent;
265     new->orientation = orientation;
266
267     /* 3: swap 'percent' (resize factor) */
268     new->percent = con->percent;
269     con->percent = 0.0;
270
271     /* 4: add it as a child to the new Con */
272     con_attach(con, new, false);
273 }
274
275 /*
276  * Moves focus one level up.
277  *
278  */
279 void level_up() {
280     /* We cannot go up when we are in fullscreen mode at the moment, that would
281      * be totally not intuitive */
282     if (focused->fullscreen_mode != CF_NONE) {
283         LOG("Currently in fullscreen, not going up\n");
284         return;
285     }
286     /* We can focus up to the workspace, but not any higher in the tree */
287     if ((focused->parent->type != CT_CON &&
288         focused->parent->type != CT_WORKSPACE) ||
289         focused->type == CT_WORKSPACE) {
290         LOG("Cannot go up any further\n");
291         return;
292     }
293     con_focus(focused->parent);
294 }
295
296 /*
297  * Moves focus one level down.
298  *
299  */
300 void level_down() {
301     /* Go down the focus stack of the current node */
302     Con *next = TAILQ_FIRST(&(focused->focus_head));
303     if (next == TAILQ_END(&(focused->focus_head))) {
304         printf("cannot go down\n");
305         return;
306     }
307     con_focus(next);
308 }
309
310 static void mark_unmapped(Con *con) {
311     Con *current;
312
313     con->mapped = false;
314     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
315         mark_unmapped(current);
316     if (con->type == CT_WORKSPACE) {
317         /* We need to call mark_unmapped on floating nodes aswell since we can
318          * make containers floating. */
319         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
320             mark_unmapped(current);
321     }
322 }
323
324 /*
325  * Renders the tree, that is rendering all outputs using render_con() and
326  * pushing the changes to X11 using x_push_changes().
327  *
328  */
329 void tree_render() {
330     if (croot == NULL)
331         return;
332
333     DLOG("-- BEGIN RENDERING --\n");
334     /* Reset map state for all nodes in tree */
335     /* TODO: a nicer method to walk all nodes would be good, maybe? */
336     mark_unmapped(croot);
337     croot->mapped = true;
338
339     /* We start rendering at an output */
340     Con *output;
341     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
342         DLOG("output %p / %s\n", output, output->name);
343         render_con(output, false);
344     }
345     x_push_changes(croot);
346     DLOG("-- END RENDERING --\n");
347 }
348
349 /*
350  * Changes focus in the given way (next/previous) and given orientation
351  * (horizontal/vertical).
352  *
353  */
354 void tree_next(char way, orientation_t orientation) {
355     /* 1: get the first parent with the same orientation */
356     Con *parent = focused->parent;
357     while (focused->type != CT_WORKSPACE &&
358            (con_orientation(parent) != orientation ||
359             con_num_children(parent) == 1)) {
360         LOG("need to go one level further up\n");
361         /* if the current parent is an output, we are at a workspace
362          * and the orientation still does not match */
363         if (parent->type == CT_WORKSPACE)
364             return;
365         parent = parent->parent;
366     }
367     Con *current = TAILQ_FIRST(&(parent->focus_head));
368     assert(current != TAILQ_END(&(parent->focus_head)));
369
370     if (TAILQ_EMPTY(&(parent->nodes_head))) {
371         DLOG("Nothing to focus here, move along...\n");
372         return;
373     }
374
375     /* 2: chose next (or previous) */
376     Con *next;
377     if (way == 'n') {
378         next = TAILQ_NEXT(current, nodes);
379         /* if we are at the end of the list, we need to wrap */
380         if (next == TAILQ_END(&(parent->nodes_head)))
381             next = TAILQ_FIRST(&(parent->nodes_head));
382     } else {
383         next = TAILQ_PREV(current, nodes_head, nodes);
384         /* if we are at the end of the list, we need to wrap */
385         if (next == TAILQ_END(&(parent->nodes_head)))
386             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
387     }
388
389     /* 3: focus choice comes in here. at the moment we will go down
390      * until we find a window */
391     /* TODO: check for window, atm we only go down as far as possible */
392     con_focus(con_descend_focused(next));
393 }
394
395 /*
396  * tree_flatten() removes pairs of redundant split containers, e.g.:
397  *       [workspace, horizontal]
398  *   [v-split]           [child3]
399  *   [h-split]
400  * [child1] [child2]
401  * In this example, the v-split and h-split container are redundant.
402  * Such a situation can be created by moving containers in a direction which is
403  * not the orientation of their parent container. i3 needs to create a new
404  * split container then and if you move containers this way multiple times,
405  * redundant chains of split-containers can be the result.
406  *
407  */
408 void tree_flatten(Con *con) {
409     Con *current, *child, *parent = con->parent;
410     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
411
412     /* We only consider normal containers without windows */
413     if (con->type != CT_CON || con->window != NULL)
414         goto recurse;
415
416     /* Ensure it got only one child */
417     child = TAILQ_FIRST(&(con->nodes_head));
418     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
419         goto recurse;
420
421     /* The child must have a different orientation than the con but the same as
422      * the con’s parent to be redundant */
423     if (con->orientation == NO_ORIENTATION ||
424         child->orientation == NO_ORIENTATION ||
425         con->orientation == child->orientation ||
426         child->orientation != parent->orientation)
427         goto recurse;
428
429     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
430     /* 1: save focus */
431     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
432
433     DLOG("detaching...\n");
434     /* 2: re-attach the children to the parent before con */
435     while (!TAILQ_EMPTY(&(child->nodes_head))) {
436         current = TAILQ_FIRST(&(child->nodes_head));
437         DLOG("detaching current=%p / %s\n", current, current->name);
438         con_detach(current);
439         DLOG("re-attaching\n");
440         /* We don’t use con_attach() here because for a CT_CON, the special
441          * case handling of con_attach() does not trigger. So all it would do
442          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
443          * directly use the TAILQ macros. */
444         current->parent = parent;
445         TAILQ_INSERT_BEFORE(con, current, nodes);
446         DLOG("attaching to focus list\n");
447         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
448         current->percent = con->percent;
449     }
450     DLOG("re-attached all\n");
451
452     /* 3: restore focus, if con was focused */
453     if (focus_next != NULL &&
454         TAILQ_FIRST(&(parent->focus_head)) == con) {
455         DLOG("restoring focus to focus_next=%p\n", focus_next);
456         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
457         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
458         DLOG("restored focus.\n");
459     }
460
461     /* 4: close the redundant cons */
462     DLOG("closing redundant cons\n");
463     tree_close(con, false, true);
464
465     /* Well, we got to abort the recursion here because we destroyed the
466      * container. However, if tree_flatten() is called sufficiently often,
467      * there can’t be the situation of having two pairs of redundant containers
468      * at once. Therefore, we can safely abort the recursion on this level
469      * after flattening. */
470     return;
471
472 recurse:
473     /* We cannot use normal foreach here because tree_flatten might close the
474      * current container. */
475     current = TAILQ_FIRST(&(con->nodes_head));
476     while (current != NULL) {
477         Con *next = TAILQ_NEXT(current, nodes);
478         tree_flatten(current);
479         current = next;
480     }
481
482     current = TAILQ_FIRST(&(con->floating_head));
483     while (current != NULL) {
484         Con *next = TAILQ_NEXT(current, floating_windows);
485         tree_flatten(current);
486         current = next;
487     }
488 }