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