]> git.sur5r.net Git - i3/i3/blob - src/tree.c
Bugfix: Make level up a noop during fullscreen mode (+testcase) (Thanks dothebart)
[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  *
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             return;
129         } else {
130             /* un-parent the window */
131             xcb_reparent_window(conn, con->window->id, root, 0, 0);
132             /* TODO: client_unmap to set state to withdrawn */
133
134         }
135         FREE(con->window->class_class);
136         FREE(con->window->class_instance);
137         FREE(con->window->name_x);
138         FREE(con->window->name_json);
139         free(con->window);
140     }
141
142     /* kill the X11 part of this container */
143     x_con_kill(con);
144
145     con_detach(con);
146     if (con->type != CT_FLOATING_CON) {
147         /* If the container is *not* floating, we might need to re-distribute
148          * percentage values for the resized containers. */
149         con_fix_percent(parent);
150     }
151
152     if (con_is_floating(con)) {
153         Con *ws = con_get_workspace(con);
154         DLOG("Container was floating, killing floating container\n");
155         tree_close(parent, false, false);
156         DLOG("parent container killed\n");
157         if (con == focused) {
158             DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
159             /* go down the focus stack as far as possible */
160             next = con_descend_focused(ws);
161
162             dont_kill_parent = true;
163             DLOG("Alright, focusing %p\n", next);
164         } else {
165             next = NULL;
166         }
167     }
168
169     free(con->name);
170     TAILQ_REMOVE(&all_cons, con, all_cons);
171     free(con);
172
173     /* in the case of floating windows, we already focused another container
174      * when closing the parent, so we can exit now. */
175     if (!next) {
176         DLOG("No next container, i will just exit now\n");
177         return;
178     }
179
180     if (was_mapped || con == focused) {
181         if (kill_window || !dont_kill_parent || con == focused) {
182             DLOG("focusing %p / %s\n", next, next->name);
183             /* TODO: check if the container (or one of its children) was focused */
184             if (next->type == CT_DOCKAREA) {
185                 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
186                 con_focus(con_descend_focused(output_get_content(next->parent)));
187             } else {
188                 con_focus(next);
189             }
190         }
191         else {
192             DLOG("not focusing because we're not killing anybody");
193         }
194     } else {
195         DLOG("not focusing, was not mapped\n");
196     }
197
198     /* check if the parent container is empty now and close it */
199     if (!dont_kill_parent)
200         CALL(parent, on_remove_child);
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 cannot go up when we are in fullscreen mode at the moment, that would
268      * be totally not intuitive */
269     if (focused->fullscreen_mode != CF_NONE) {
270         LOG("Currently in fullscreen, not going up\n");
271         return;
272     }
273     /* We can focus up to the workspace, but not any higher in the tree */
274     if ((focused->parent->type != CT_CON &&
275         focused->parent->type != CT_WORKSPACE) ||
276         focused->type == CT_WORKSPACE) {
277         LOG("Cannot go up any further\n");
278         return;
279     }
280     con_focus(focused->parent);
281 }
282
283 /*
284  * Moves focus one level down.
285  *
286  */
287 void level_down() {
288     /* Go down the focus stack of the current node */
289     Con *next = TAILQ_FIRST(&(focused->focus_head));
290     if (next == TAILQ_END(&(focused->focus_head))) {
291         printf("cannot go down\n");
292         return;
293     }
294     con_focus(next);
295 }
296
297 static void mark_unmapped(Con *con) {
298     Con *current;
299
300     con->mapped = false;
301     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
302         mark_unmapped(current);
303     if (con->type == CT_WORKSPACE) {
304         /* We need to call mark_unmapped on floating nodes aswell since we can
305          * make containers floating. */
306         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
307             mark_unmapped(current);
308     }
309 }
310
311 /*
312  * Renders the tree, that is rendering all outputs using render_con() and
313  * pushing the changes to X11 using x_push_changes().
314  *
315  */
316 void tree_render() {
317     if (croot == NULL)
318         return;
319
320     DLOG("-- BEGIN RENDERING --\n");
321     /* Reset map state for all nodes in tree */
322     /* TODO: a nicer method to walk all nodes would be good, maybe? */
323     mark_unmapped(croot);
324     croot->mapped = true;
325
326     /* We start rendering at an output */
327     Con *output;
328     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
329         DLOG("output %p / %s\n", output, output->name);
330         render_con(output, false);
331     }
332     x_push_changes(croot);
333     DLOG("-- END RENDERING --\n");
334 }
335
336 /*
337  * Changes focus in the given way (next/previous) and given orientation
338  * (horizontal/vertical).
339  *
340  */
341 void tree_next(char way, orientation_t orientation) {
342     /* 1: get the first parent with the same orientation */
343     Con *parent = focused->parent;
344     while (focused->type != CT_WORKSPACE &&
345            (con_orientation(parent) != orientation ||
346             con_num_children(parent) == 1)) {
347         LOG("need to go one level further up\n");
348         /* if the current parent is an output, we are at a workspace
349          * and the orientation still does not match */
350         if (parent->type == CT_WORKSPACE)
351             return;
352         parent = parent->parent;
353     }
354     Con *current = TAILQ_FIRST(&(parent->focus_head));
355     assert(current != TAILQ_END(&(parent->focus_head)));
356
357     if (TAILQ_EMPTY(&(parent->nodes_head))) {
358         DLOG("Nothing to focus here, move along...\n");
359         return;
360     }
361
362     /* 2: chose next (or previous) */
363     Con *next;
364     if (way == 'n') {
365         next = TAILQ_NEXT(current, nodes);
366         /* if we are at the end of the list, we need to wrap */
367         if (next == TAILQ_END(&(parent->nodes_head)))
368             next = TAILQ_FIRST(&(parent->nodes_head));
369     } else {
370         next = TAILQ_PREV(current, nodes_head, nodes);
371         /* if we are at the end of the list, we need to wrap */
372         if (next == TAILQ_END(&(parent->nodes_head)))
373             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
374     }
375
376     /* 3: focus choice comes in here. at the moment we will go down
377      * until we find a window */
378     /* TODO: check for window, atm we only go down as far as possible */
379     con_focus(con_descend_focused(next));
380 }
381
382 /*
383  * tree_flatten() removes pairs of redundant split containers, e.g.:
384  *       [workspace, horizontal]
385  *   [v-split]           [child3]
386  *   [h-split]
387  * [child1] [child2]
388  * In this example, the v-split and h-split container are redundant.
389  * Such a situation can be created by moving containers in a direction which is
390  * not the orientation of their parent container. i3 needs to create a new
391  * split container then and if you move containers this way multiple times,
392  * redundant chains of split-containers can be the result.
393  *
394  */
395 void tree_flatten(Con *con) {
396     Con *current, *child, *parent = con->parent;
397     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
398
399     /* We only consider normal containers without windows */
400     if (con->type != CT_CON || con->window != NULL)
401         goto recurse;
402
403     /* Ensure it got only one child */
404     child = TAILQ_FIRST(&(con->nodes_head));
405     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
406         goto recurse;
407
408     /* The child must have a different orientation than the con but the same as
409      * the con’s parent to be redundant */
410     if (con->orientation == NO_ORIENTATION ||
411         child->orientation == NO_ORIENTATION ||
412         con->orientation == child->orientation ||
413         child->orientation != parent->orientation)
414         goto recurse;
415
416     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
417     /* 1: save focus */
418     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
419
420     DLOG("detaching...\n");
421     /* 2: re-attach the children to the parent before con */
422     while (!TAILQ_EMPTY(&(child->nodes_head))) {
423         current = TAILQ_FIRST(&(child->nodes_head));
424         DLOG("detaching current=%p / %s\n", current, current->name);
425         con_detach(current);
426         DLOG("re-attaching\n");
427         /* We don’t use con_attach() here because for a CT_CON, the special
428          * case handling of con_attach() does not trigger. So all it would do
429          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
430          * directly use the TAILQ macros. */
431         current->parent = parent;
432         TAILQ_INSERT_BEFORE(con, current, nodes);
433         DLOG("attaching to focus list\n");
434         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
435         current->percent = con->percent;
436     }
437     DLOG("re-attached all\n");
438
439     /* 3: restore focus, if con was focused */
440     if (focus_next != NULL &&
441         TAILQ_FIRST(&(parent->focus_head)) == con) {
442         DLOG("restoring focus to focus_next=%p\n", focus_next);
443         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
444         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
445         DLOG("restored focus.\n");
446     }
447
448     /* 4: close the redundant cons */
449     DLOG("closing redundant cons\n");
450     tree_close(con, false, true);
451
452     /* Well, we got to abort the recursion here because we destroyed the
453      * container. However, if tree_flatten() is called sufficiently often,
454      * there can’t be the situation of having two pairs of redundant containers
455      * at once. Therefore, we can safely abort the recursion on this level
456      * after flattening. */
457     return;
458
459 recurse:
460     /* We cannot use normal foreach here because tree_flatten might close the
461      * current container. */
462     current = TAILQ_FIRST(&(con->nodes_head));
463     while (current != NULL) {
464         Con *next = TAILQ_NEXT(current, nodes);
465         tree_flatten(current);
466         current = next;
467     }
468
469     current = TAILQ_FIRST(&(con->floating_head));
470     while (current != NULL) {
471         Con *next = TAILQ_NEXT(current, floating_windows);
472         tree_flatten(current);
473         current = next;
474     }
475 }