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