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