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