]> git.sur5r.net Git - i3/i3/blob - src/tree.c
Merge branch 'fix-close-focus'
[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  * The dont_kill_parent flag is specified when the function calls itself
118  * recursively while deleting a containers children.
119  *
120  * The force_set_focus flag is specified in the case of killing a floating
121  * window: tree_close() will be invoked for the CT_FLOATINGCON (the parent
122  * container) and focus should be set there.
123  *
124  */
125 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus) {
126     bool was_mapped = con->mapped;
127     Con *parent = con->parent;
128
129     if (!was_mapped) {
130         /* Even if the container itself is not mapped, its children may be
131          * mapped (for example split containers don't have a mapped window on
132          * their own but usually contain mapped children). */
133         was_mapped = _is_con_mapped(con);
134     }
135
136     /* Get the container which is next focused */
137     Con *next = con_next_focused(con);
138     DLOG("next = %p, focused = %p\n", next, focused);
139
140     DLOG("closing %p, kill_window = %d\n", con, kill_window);
141     Con *child, *nextchild;
142     bool abort_kill = false;
143     /* We cannot use TAILQ_FOREACH because the children get deleted
144      * in their parent’s nodes_head */
145     for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) {
146         nextchild = TAILQ_NEXT(child, nodes);
147         DLOG("killing child=%p\n", child);
148         if (!tree_close(child, kill_window, true, false))
149             abort_kill = true;
150         child = nextchild;
151     }
152
153     if (abort_kill) {
154         DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
155         return false;
156     }
157
158     if (con->window != NULL) {
159         if (kill_window != DONT_KILL_WINDOW) {
160             x_window_kill(con->window->id, kill_window);
161             return false;
162         } else {
163             xcb_void_cookie_t cookie;
164             /* un-parent the window */
165             cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
166
167             /* Ignore X11 errors for the ReparentWindow request.
168              * X11 Errors are returned when the window was already destroyed */
169             add_ignore_event(cookie.sequence, 0);
170
171             /* We are no longer handling this window, thus set WM_STATE to
172              * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
173             long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
174             cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
175                         con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
176
177             /* Ignore X11 errors for the ReparentWindow request.
178              * X11 Errors are returned when the window was already destroyed */
179             add_ignore_event(cookie.sequence, 0);
180         }
181         FREE(con->window->class_class);
182         FREE(con->window->class_instance);
183         FREE(con->window->name_x);
184         FREE(con->window->name_json);
185         free(con->window);
186     }
187
188     /* kill the X11 part of this container */
189     x_con_kill(con);
190
191     con_detach(con);
192     if (con->type != CT_FLOATING_CON) {
193         /* If the container is *not* floating, we might need to re-distribute
194          * percentage values for the resized containers. */
195         con_fix_percent(parent);
196     }
197
198     if (con_is_floating(con)) {
199         Con *ws = con_get_workspace(con);
200         DLOG("Container was floating, killing floating container\n");
201         tree_close(parent, DONT_KILL_WINDOW, false, (con == focused));
202         DLOG("parent container killed\n");
203         if (con == focused) {
204             DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
205             /* go down the focus stack as far as possible */
206             next = con_descend_focused(ws);
207
208             dont_kill_parent = true;
209             DLOG("Alright, focusing %p\n", next);
210         } else {
211             next = NULL;
212         }
213     }
214
215     free(con->name);
216     FREE(con->deco_render_params);
217     TAILQ_REMOVE(&all_cons, con, all_cons);
218     free(con);
219
220     /* in the case of floating windows, we already focused another container
221      * when closing the parent, so we can exit now. */
222     if (!next) {
223         DLOG("No next container, i will just exit now\n");
224         return true;
225     }
226
227     if (was_mapped || con == focused) {
228         if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
229             DLOG("focusing %p / %s\n", next, next->name);
230             if (next->type == CT_DOCKAREA) {
231                 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
232                 con_focus(con_descend_focused(output_get_content(next->parent)));
233             } else {
234                 if (!force_set_focus && con != focused)
235                     DLOG("not changing focus, the container was not focused before\n");
236                 else con_focus(next);
237             }
238         }
239         else {
240             DLOG("not focusing because we're not killing anybody");
241         }
242     } else {
243         DLOG("not focusing, was not mapped\n");
244     }
245
246     /* check if the parent container is empty now and close it */
247     if (!dont_kill_parent)
248         CALL(parent, on_remove_child);
249
250     return true;
251 }
252
253 /*
254  * Closes the current container using tree_close().
255  *
256  */
257 void tree_close_con(kill_window_t kill_window) {
258     assert(focused != NULL);
259     if (focused->type == CT_WORKSPACE) {
260         LOG("Cannot close workspace\n");
261         return;
262     }
263
264     /* There *should* be no possibility to focus outputs / root container */
265     assert(focused->type != CT_OUTPUT);
266     assert(focused->type != CT_ROOT);
267
268     /* Kill con */
269     tree_close(focused, kill_window, false, false);
270 }
271
272 /*
273  * Splits (horizontally or vertically) the given container by creating a new
274  * container which contains the old one and the future ones.
275  *
276  */
277 void tree_split(Con *con, orientation_t orientation) {
278     /* for a workspace, we just need to change orientation */
279     if (con->type == CT_WORKSPACE) {
280         DLOG("Workspace, simply changing orientation to %d\n", orientation);
281         con->orientation = orientation;
282         return;
283     }
284
285     Con *parent = con->parent;
286     /* if we are in a container whose parent contains only one
287      * child (its split functionality is unused so far), we just change the
288      * orientation (more intuitive than splitting again) */
289     if (con_num_children(parent) == 1) {
290         parent->orientation = orientation;
291         DLOG("Just changing orientation of existing container\n");
292         return;
293     }
294
295     DLOG("Splitting in orientation %d\n", orientation);
296
297     /* 2: replace it with a new Con */
298     Con *new = con_new(NULL, NULL);
299     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
300     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
301     new->parent = parent;
302     new->orientation = orientation;
303
304     /* 3: swap 'percent' (resize factor) */
305     new->percent = con->percent;
306     con->percent = 0.0;
307
308     /* 4: add it as a child to the new Con */
309     con_attach(con, new, false);
310 }
311
312 /*
313  * Moves focus one level up.
314  *
315  */
316 void level_up() {
317     /* We cannot go up when we are in fullscreen mode at the moment, that would
318      * be totally not intuitive */
319     if (focused->fullscreen_mode != CF_NONE) {
320         LOG("Currently in fullscreen, not going up\n");
321         return;
322     }
323     /* We can focus up to the workspace, but not any higher in the tree */
324     if ((focused->parent->type != CT_CON &&
325         focused->parent->type != CT_WORKSPACE) ||
326         focused->type == CT_WORKSPACE) {
327         LOG("Cannot go up any further\n");
328         return;
329     }
330     con_focus(focused->parent);
331 }
332
333 /*
334  * Moves focus one level down.
335  *
336  */
337 void level_down() {
338     /* Go down the focus stack of the current node */
339     Con *next = TAILQ_FIRST(&(focused->focus_head));
340     if (next == TAILQ_END(&(focused->focus_head))) {
341         printf("cannot go down\n");
342         return;
343     }
344     con_focus(next);
345 }
346
347 static void mark_unmapped(Con *con) {
348     Con *current;
349
350     con->mapped = false;
351     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
352         mark_unmapped(current);
353     if (con->type == CT_WORKSPACE) {
354         /* We need to call mark_unmapped on floating nodes aswell since we can
355          * make containers floating. */
356         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
357             mark_unmapped(current);
358     }
359 }
360
361 /*
362  * Renders the tree, that is rendering all outputs using render_con() and
363  * pushing the changes to X11 using x_push_changes().
364  *
365  */
366 void tree_render() {
367     if (croot == NULL)
368         return;
369
370     DLOG("-- BEGIN RENDERING --\n");
371     /* Reset map state for all nodes in tree */
372     /* TODO: a nicer method to walk all nodes would be good, maybe? */
373     mark_unmapped(croot);
374     croot->mapped = true;
375
376     render_con(croot, false);
377
378     x_push_changes(croot);
379     DLOG("-- END RENDERING --\n");
380 }
381
382 /*
383  * Recursive function to walk the tree until a con can be found to focus.
384  *
385  */
386 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
387     /* Stop recursing at workspaces after attempting to switch to next
388      * workspace if possible. */
389     if (con->type == CT_WORKSPACE) {
390         Output *current_output = get_output_containing(con->rect.x, con->rect.y);
391         Output *next_output;
392
393         if (!current_output)
394             return false;
395         DLOG("Current output is %s\n", current_output->name);
396
397         /* Try to find next output */
398         direction_t direction;
399         if (way == 'n' && orientation == HORIZ)
400             direction = D_RIGHT;
401         else if (way == 'p' && orientation == HORIZ)
402             direction = D_LEFT;
403         else if (way == 'n' && orientation == VERT)
404             direction = D_DOWN;
405         else if (way == 'p' && orientation == VERT)
406             direction = D_UP;
407         else
408             return false;
409
410         next_output = get_output_next(direction, current_output);
411         if (!next_output)
412             return false;
413         DLOG("Next output is %s\n", next_output->name);
414
415         /* Find visible workspace on next output */
416         Con *workspace = NULL;
417         GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
418
419         /* Show next workspace and focus appropriate container if possible. */
420         if (!workspace)
421             return false;
422
423         workspace_show(workspace->name);
424         Con *focus = con_descend_direction(workspace, direction);
425         if (focus) {
426             con_focus(focus);
427             x_set_warp_to(&(focus->rect));
428         }
429         return true;
430     }
431
432     if (con->type == CT_FLOATING_CON) {
433         /* TODO: implement focus for floating windows */
434         return false;
435     }
436
437     Con *parent = con->parent;
438
439     /* If the orientation does not match or there is no other con to focus, we
440      * need to go higher in the hierarchy */
441     if (con_orientation(parent) != orientation ||
442         con_num_children(parent) == 1)
443         return _tree_next(parent, way, orientation, wrap);
444
445     Con *current = TAILQ_FIRST(&(parent->focus_head));
446     /* TODO: when can the following happen (except for floating windows, which
447      * are handled above)? */
448     if (TAILQ_EMPTY(&(parent->nodes_head))) {
449         DLOG("nothing to focus\n");
450         return false;
451     }
452
453     Con *next;
454     if (way == 'n')
455         next = TAILQ_NEXT(current, nodes);
456     else next = TAILQ_PREV(current, nodes_head, nodes);
457
458     if (!next) {
459         if (!config.force_focus_wrapping) {
460             /* If there is no next/previous container, we check if we can focus one
461              * when going higher (without wrapping, though). If so, we are done, if
462              * not, we wrap */
463             if (_tree_next(parent, way, orientation, false))
464                 return true;
465
466             if (!wrap)
467                 return false;
468         }
469
470         if (way == 'n')
471             next = TAILQ_FIRST(&(parent->nodes_head));
472         else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
473     }
474
475     /* 3: focus choice comes in here. at the moment we will go down
476      * until we find a window */
477     /* TODO: check for window, atm we only go down as far as possible */
478     con_focus(con_descend_focused(next));
479     return true;
480 }
481
482 /*
483  * Changes focus in the given way (next/previous) and given orientation
484  * (horizontal/vertical).
485  *
486  */
487 void tree_next(char way, orientation_t orientation) {
488     _tree_next(focused, way, orientation, true);
489 }
490
491 /*
492  * tree_flatten() removes pairs of redundant split containers, e.g.:
493  *       [workspace, horizontal]
494  *   [v-split]           [child3]
495  *   [h-split]
496  * [child1] [child2]
497  * In this example, the v-split and h-split container are redundant.
498  * Such a situation can be created by moving containers in a direction which is
499  * not the orientation of their parent container. i3 needs to create a new
500  * split container then and if you move containers this way multiple times,
501  * redundant chains of split-containers can be the result.
502  *
503  */
504 void tree_flatten(Con *con) {
505     Con *current, *child, *parent = con->parent;
506     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
507
508     /* We only consider normal containers without windows */
509     if (con->type != CT_CON || con->window != NULL)
510         goto recurse;
511
512     /* Ensure it got only one child */
513     child = TAILQ_FIRST(&(con->nodes_head));
514     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
515         goto recurse;
516
517     /* The child must have a different orientation than the con but the same as
518      * the con’s parent to be redundant */
519     if (con->orientation == NO_ORIENTATION ||
520         child->orientation == NO_ORIENTATION ||
521         con->orientation == child->orientation ||
522         child->orientation != parent->orientation)
523         goto recurse;
524
525     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
526     /* 1: save focus */
527     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
528
529     DLOG("detaching...\n");
530     /* 2: re-attach the children to the parent before con */
531     while (!TAILQ_EMPTY(&(child->nodes_head))) {
532         current = TAILQ_FIRST(&(child->nodes_head));
533         DLOG("detaching current=%p / %s\n", current, current->name);
534         con_detach(current);
535         DLOG("re-attaching\n");
536         /* We don’t use con_attach() here because for a CT_CON, the special
537          * case handling of con_attach() does not trigger. So all it would do
538          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
539          * directly use the TAILQ macros. */
540         current->parent = parent;
541         TAILQ_INSERT_BEFORE(con, current, nodes);
542         DLOG("attaching to focus list\n");
543         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
544         current->percent = con->percent;
545     }
546     DLOG("re-attached all\n");
547
548     /* 3: restore focus, if con was focused */
549     if (focus_next != NULL &&
550         TAILQ_FIRST(&(parent->focus_head)) == con) {
551         DLOG("restoring focus to focus_next=%p\n", focus_next);
552         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
553         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
554         DLOG("restored focus.\n");
555     }
556
557     /* 4: close the redundant cons */
558     DLOG("closing redundant cons\n");
559     tree_close(con, DONT_KILL_WINDOW, true, false);
560
561     /* Well, we got to abort the recursion here because we destroyed the
562      * container. However, if tree_flatten() is called sufficiently often,
563      * there can’t be the situation of having two pairs of redundant containers
564      * at once. Therefore, we can safely abort the recursion on this level
565      * after flattening. */
566     return;
567
568 recurse:
569     /* We cannot use normal foreach here because tree_flatten might close the
570      * current container. */
571     current = TAILQ_FIRST(&(con->nodes_head));
572     while (current != NULL) {
573         Con *next = TAILQ_NEXT(current, nodes);
574         tree_flatten(current);
575         current = next;
576     }
577
578     current = TAILQ_FIRST(&(con->floating_head));
579     while (current != NULL) {
580         Con *next = TAILQ_NEXT(current, floating_windows);
581         tree_flatten(current);
582         current = next;
583     }
584 }