]> git.sur5r.net Git - i3/i3/blob - src/tree.c
Implement dock mode, update testsuite
[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, bool focus_it) {
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     /* 5: focus the new container */
84     if (focus_it)
85         con_focus(new);
86
87     return new;
88 }
89
90 static bool _is_con_mapped(Con *con) {
91     Con *child;
92
93     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
94         if (_is_con_mapped(child))
95             return true;
96
97     return con->mapped;
98 }
99
100 /*
101  * Closes the given container including all children
102  *
103  */
104 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
105     bool was_mapped = con->mapped;
106     Con *parent = con->parent;
107
108     if (!was_mapped) {
109         /* Even if the container itself is not mapped, its children may be
110          * mapped (for example split containers don't have a mapped window on
111          * their own but usually contain mapped children). */
112         was_mapped = _is_con_mapped(con);
113     }
114
115     /* Get the container which is next focused */
116     Con *next = con_next_focused(con);
117     DLOG("next = %p, focused = %p\n", next, focused);
118
119     DLOG("closing %p, kill_window = %d\n", con, kill_window);
120     Con *child;
121     /* We cannot use TAILQ_FOREACH because the children get deleted
122      * in their parent’s nodes_head */
123     while (!TAILQ_EMPTY(&(con->nodes_head))) {
124         child = TAILQ_FIRST(&(con->nodes_head));
125         DLOG("killing child=%p\n", child);
126         tree_close(child, kill_window, true);
127     }
128
129     if (con->window != NULL) {
130         if (kill_window)
131             x_window_kill(con->window->id);
132         else {
133             /* un-parent the window */
134             xcb_reparent_window(conn, con->window->id, root, 0, 0);
135             /* TODO: client_unmap to set state to withdrawn */
136
137         }
138         FREE(con->window->class_class);
139         FREE(con->window->class_instance);
140         FREE(con->window->name_x);
141         FREE(con->window->name_json);
142         free(con->window);
143     }
144
145     /* kill the X11 part of this container */
146     x_con_kill(con);
147
148     con_detach(con);
149     if (con->type != CT_FLOATING_CON) {
150         /* If the container is *not* floating, we might need to re-distribute
151          * percentage values for the resized containers. */
152         con_fix_percent(parent);
153     }
154
155     if (con_is_floating(con)) {
156         Con *ws = con_get_workspace(con);
157         DLOG("Container was floating, killing floating container\n");
158         tree_close(parent, false, false);
159         DLOG("parent container killed\n");
160         if (con == focused) {
161             DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
162             /* go down the focus stack as far as possible */
163             next = con_descend_focused(ws);
164
165             dont_kill_parent = true;
166             DLOG("Alright, focusing %p\n", next);
167         } else {
168             next = NULL;
169         }
170     }
171
172     free(con->name);
173     TAILQ_REMOVE(&all_cons, con, all_cons);
174     free(con);
175
176     /* in the case of floating windows, we already focused another container
177      * when closing the parent, so we can exit now. */
178     if (!next) {
179         DLOG("No next container, i will just exit now\n");
180         return;
181     }
182
183     if (was_mapped || con == focused) {
184         if (kill_window || !dont_kill_parent || con == focused) {
185             DLOG("focusing %p / %s\n", next, next->name);
186             /* TODO: check if the container (or one of its children) was focused */
187             con_focus(next);
188         }
189         else {
190             DLOG("not focusing because we're not killing anybody");
191         }
192     } else {
193         DLOG("not focusing, was not mapped\n");
194     }
195
196     /* check if the parent container is empty now and close it */
197     if (!dont_kill_parent)
198         CALL(parent, on_remove_child);
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             con_num_children(parent) == 1)) {
338         LOG("need to go one level further up\n");
339         /* if the current parent is an output, we are at a workspace
340          * and the orientation still does not match */
341         if (parent->type == CT_WORKSPACE)
342             return;
343         parent = parent->parent;
344     }
345     Con *current = TAILQ_FIRST(&(parent->focus_head));
346     assert(current != TAILQ_END(&(parent->focus_head)));
347
348     if (TAILQ_EMPTY(&(parent->nodes_head))) {
349         DLOG("Nothing to focus here, move along...\n");
350         return;
351     }
352
353     /* 2: chose next (or previous) */
354     Con *next;
355     if (way == 'n') {
356         next = TAILQ_NEXT(current, nodes);
357         /* if we are at the end of the list, we need to wrap */
358         if (next == TAILQ_END(&(parent->nodes_head)))
359             next = TAILQ_FIRST(&(parent->nodes_head));
360     } else {
361         next = TAILQ_PREV(current, nodes_head, nodes);
362         /* if we are at the end of the list, we need to wrap */
363         if (next == TAILQ_END(&(parent->nodes_head)))
364             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
365     }
366
367     /* 3: focus choice comes in here. at the moment we will go down
368      * until we find a window */
369     /* TODO: check for window, atm we only go down as far as possible */
370     con_focus(con_descend_focused(next));
371 }
372
373 /*
374  * tree_flatten() removes pairs of redundant split containers, e.g.:
375  *       [workspace, horizontal]
376  *   [v-split]           [child3]
377  *   [h-split]
378  * [child1] [child2]
379  * In this example, the v-split and h-split container are redundant.
380  * Such a situation can be created by moving containers in a direction which is
381  * not the orientation of their parent container. i3 needs to create a new
382  * split container then and if you move containers this way multiple times,
383  * redundant chains of split-containers can be the result.
384  *
385  */
386 void tree_flatten(Con *con) {
387     Con *current, *child, *parent = con->parent;
388     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
389
390     /* We only consider normal containers without windows */
391     if (con->type != CT_CON || con->window != NULL)
392         goto recurse;
393
394     /* Ensure it got only one child */
395     child = TAILQ_FIRST(&(con->nodes_head));
396     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
397         goto recurse;
398
399     /* The child must have a different orientation than the con but the same as
400      * the con’s parent to be redundant */
401     if (con->orientation == NO_ORIENTATION ||
402         child->orientation == NO_ORIENTATION ||
403         con->orientation == child->orientation ||
404         child->orientation != parent->orientation)
405         goto recurse;
406
407     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
408     /* 1: save focus */
409     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
410
411     DLOG("detaching...\n");
412     /* 2: re-attach the children to the parent before con */
413     while (!TAILQ_EMPTY(&(child->nodes_head))) {
414         current = TAILQ_FIRST(&(child->nodes_head));
415         DLOG("detaching current=%p / %s\n", current, current->name);
416         con_detach(current);
417         DLOG("re-attaching\n");
418         /* We don’t use con_attach() here because for a CT_CON, the special
419          * case handling of con_attach() does not trigger. So all it would do
420          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
421          * directly use the TAILQ macros. */
422         current->parent = parent;
423         TAILQ_INSERT_BEFORE(con, current, nodes);
424         DLOG("attaching to focus list\n");
425         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
426         current->percent = con->percent;
427     }
428     DLOG("re-attached all\n");
429
430     /* 3: restore focus, if con was focused */
431     if (focus_next != NULL &&
432         TAILQ_FIRST(&(parent->focus_head)) == con) {
433         DLOG("restoring focus to focus_next=%p\n", focus_next);
434         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
435         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
436         DLOG("restored focus.\n");
437     }
438
439     /* 4: close the redundant cons */
440     DLOG("closing redundant cons\n");
441     tree_close(con, false, true);
442
443     /* Well, we got to abort the recursion here because we destroyed the
444      * container. However, if tree_flatten() is called sufficiently often,
445      * there can’t be the situation of having two pairs of redundant containers
446      * at once. Therefore, we can safely abort the recursion on this level
447      * after flattening. */
448     return;
449
450 recurse:
451     /* We cannot use normal foreach here because tree_flatten might close the
452      * current container. */
453     current = TAILQ_FIRST(&(con->nodes_head));
454     while (current != NULL) {
455         Con *next = TAILQ_NEXT(current, nodes);
456         tree_flatten(current);
457         current = next;
458     }
459
460     current = TAILQ_FIRST(&(con->floating_head));
461     while (current != NULL) {
462         Con *next = TAILQ_NEXT(current, floating_windows);
463         tree_flatten(current);
464         current = next;
465     }
466 }