]> 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
346     /* Force re-rendering to make the indicator border visible. */
347     FREE(con->deco_render_params);
348     FREE(parent->deco_render_params);
349
350     /* if we are in a container whose parent contains only one
351      * child (its split functionality is unused so far), we just change the
352      * orientation (more intuitive than splitting again) */
353     if (con_num_children(parent) == 1) {
354         parent->orientation = orientation;
355         DLOG("Just changing orientation of existing container\n");
356         return;
357     }
358
359     DLOG("Splitting in orientation %d\n", orientation);
360
361     /* 2: replace it with a new Con */
362     Con *new = con_new(NULL, NULL);
363     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
364     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
365     new->parent = parent;
366     new->orientation = orientation;
367
368     /* 3: swap 'percent' (resize factor) */
369     new->percent = con->percent;
370     con->percent = 0.0;
371
372     /* 4: add it as a child to the new Con */
373     con_attach(con, new, false);
374 }
375
376 /*
377  * Moves focus one level up.
378  *
379  */
380 void level_up() {
381     /* We cannot go up when we are in fullscreen mode at the moment, that would
382      * be totally not intuitive */
383     if (focused->fullscreen_mode != CF_NONE) {
384         LOG("Currently in fullscreen, not going up\n");
385         return;
386     }
387     /* We can focus up to the workspace, but not any higher in the tree */
388     if ((focused->parent->type != CT_CON &&
389         focused->parent->type != CT_WORKSPACE) ||
390         focused->type == CT_WORKSPACE) {
391         LOG("Cannot go up any further\n");
392         return;
393     }
394     con_focus(focused->parent);
395 }
396
397 /*
398  * Moves focus one level down.
399  *
400  */
401 void level_down() {
402     /* Go down the focus stack of the current node */
403     Con *next = TAILQ_FIRST(&(focused->focus_head));
404     if (next == TAILQ_END(&(focused->focus_head))) {
405         printf("cannot go down\n");
406         return;
407     }
408     con_focus(next);
409 }
410
411 static void mark_unmapped(Con *con) {
412     Con *current;
413
414     con->mapped = false;
415     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
416         mark_unmapped(current);
417     if (con->type == CT_WORKSPACE) {
418         /* We need to call mark_unmapped on floating nodes aswell since we can
419          * make containers floating. */
420         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
421             mark_unmapped(current);
422     }
423 }
424
425 /*
426  * Renders the tree, that is rendering all outputs using render_con() and
427  * pushing the changes to X11 using x_push_changes().
428  *
429  */
430 void tree_render() {
431     if (croot == NULL)
432         return;
433
434     DLOG("-- BEGIN RENDERING --\n");
435     /* Reset map state for all nodes in tree */
436     /* TODO: a nicer method to walk all nodes would be good, maybe? */
437     mark_unmapped(croot);
438     croot->mapped = true;
439
440     render_con(croot, false);
441
442     x_push_changes(croot);
443     DLOG("-- END RENDERING --\n");
444 }
445
446 /*
447  * Recursive function to walk the tree until a con can be found to focus.
448  *
449  */
450 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
451     /* Stop recursing at workspaces after attempting to switch to next
452      * workspace if possible. */
453     if (con->type == CT_WORKSPACE) {
454         Output *current_output = get_output_containing(con->rect.x, con->rect.y);
455         Output *next_output;
456
457         if (!current_output)
458             return false;
459         DLOG("Current output is %s\n", current_output->name);
460
461         /* Try to find next output */
462         direction_t direction;
463         if (way == 'n' && orientation == HORIZ)
464             direction = D_RIGHT;
465         else if (way == 'p' && orientation == HORIZ)
466             direction = D_LEFT;
467         else if (way == 'n' && orientation == VERT)
468             direction = D_DOWN;
469         else if (way == 'p' && orientation == VERT)
470             direction = D_UP;
471         else
472             return false;
473
474         next_output = get_output_next(direction, current_output);
475         if (!next_output)
476             return false;
477         DLOG("Next output is %s\n", next_output->name);
478
479         /* Find visible workspace on next output */
480         Con *workspace = NULL;
481         GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
482
483         /* Show next workspace and focus appropriate container if possible. */
484         if (!workspace)
485             return false;
486
487         workspace_show(workspace);
488         Con *focus = con_descend_direction(workspace, direction);
489         if (focus) {
490             con_focus(focus);
491             x_set_warp_to(&(focus->rect));
492         }
493         return true;
494     }
495
496     Con *parent = con->parent;
497
498     if (con->type == CT_FLOATING_CON) {
499         /* left/right focuses the previous/next floating container */
500         if (orientation == HORIZ) {
501             Con *next;
502             if (way == 'n')
503                 next = TAILQ_NEXT(con, floating_windows);
504             else next = TAILQ_PREV(con, floating_head, floating_windows);
505
506             /* If there is no next/previous container, wrap */
507             if (!next) {
508                 if (way == 'n')
509                     next = TAILQ_FIRST(&(parent->floating_head));
510                 else next = TAILQ_LAST(&(parent->floating_head), floating_head);
511             }
512
513             /* Still no next/previous container? bail out */
514             if (!next)
515                 return false;
516
517             con_focus(con_descend_focused(next));
518             return true;
519         } else {
520             /* up/down cycles through the Z-index */
521             /* TODO: implement cycling through the z-index */
522             return false;
523         }
524     }
525
526     /* If the orientation does not match or there is no other con to focus, we
527      * need to go higher in the hierarchy */
528     if (con_orientation(parent) != orientation ||
529         con_num_children(parent) == 1)
530         return _tree_next(parent, way, orientation, wrap);
531
532     Con *current = TAILQ_FIRST(&(parent->focus_head));
533     /* TODO: when can the following happen (except for floating windows, which
534      * are handled above)? */
535     if (TAILQ_EMPTY(&(parent->nodes_head))) {
536         DLOG("nothing to focus\n");
537         return false;
538     }
539
540     Con *next;
541     if (way == 'n')
542         next = TAILQ_NEXT(current, nodes);
543     else next = TAILQ_PREV(current, nodes_head, nodes);
544
545     if (!next) {
546         if (!config.force_focus_wrapping) {
547             /* If there is no next/previous container, we check if we can focus one
548              * when going higher (without wrapping, though). If so, we are done, if
549              * not, we wrap */
550             if (_tree_next(parent, way, orientation, false))
551                 return true;
552
553             if (!wrap)
554                 return false;
555         }
556
557         if (way == 'n')
558             next = TAILQ_FIRST(&(parent->nodes_head));
559         else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
560     }
561
562     /* 3: focus choice comes in here. at the moment we will go down
563      * until we find a window */
564     /* TODO: check for window, atm we only go down as far as possible */
565     con_focus(con_descend_focused(next));
566     return true;
567 }
568
569 /*
570  * Changes focus in the given way (next/previous) and given orientation
571  * (horizontal/vertical).
572  *
573  */
574 void tree_next(char way, orientation_t orientation) {
575     _tree_next(focused, way, orientation, true);
576 }
577
578 /*
579  * tree_flatten() removes pairs of redundant split containers, e.g.:
580  *       [workspace, horizontal]
581  *   [v-split]           [child3]
582  *   [h-split]
583  * [child1] [child2]
584  * In this example, the v-split and h-split container are redundant.
585  * Such a situation can be created by moving containers in a direction which is
586  * not the orientation of their parent container. i3 needs to create a new
587  * split container then and if you move containers this way multiple times,
588  * redundant chains of split-containers can be the result.
589  *
590  */
591 void tree_flatten(Con *con) {
592     Con *current, *child, *parent = con->parent;
593     DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
594
595     /* We only consider normal containers without windows */
596     if (con->type != CT_CON || con->window != NULL)
597         goto recurse;
598
599     /* Ensure it got only one child */
600     child = TAILQ_FIRST(&(con->nodes_head));
601     if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
602         goto recurse;
603
604     /* The child must have a different orientation than the con but the same as
605      * the con’s parent to be redundant */
606     if (con->orientation == NO_ORIENTATION ||
607         child->orientation == NO_ORIENTATION ||
608         con->orientation == child->orientation ||
609         child->orientation != parent->orientation)
610         goto recurse;
611
612     DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
613     /* 1: save focus */
614     Con *focus_next = TAILQ_FIRST(&(child->focus_head));
615
616     DLOG("detaching...\n");
617     /* 2: re-attach the children to the parent before con */
618     while (!TAILQ_EMPTY(&(child->nodes_head))) {
619         current = TAILQ_FIRST(&(child->nodes_head));
620         DLOG("detaching current=%p / %s\n", current, current->name);
621         con_detach(current);
622         DLOG("re-attaching\n");
623         /* We don’t use con_attach() here because for a CT_CON, the special
624          * case handling of con_attach() does not trigger. So all it would do
625          * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
626          * directly use the TAILQ macros. */
627         current->parent = parent;
628         TAILQ_INSERT_BEFORE(con, current, nodes);
629         DLOG("attaching to focus list\n");
630         TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
631         current->percent = con->percent;
632     }
633     DLOG("re-attached all\n");
634
635     /* 3: restore focus, if con was focused */
636     if (focus_next != NULL &&
637         TAILQ_FIRST(&(parent->focus_head)) == con) {
638         DLOG("restoring focus to focus_next=%p\n", focus_next);
639         TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
640         TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
641         DLOG("restored focus.\n");
642     }
643
644     /* 4: close the redundant cons */
645     DLOG("closing redundant cons\n");
646     tree_close(con, DONT_KILL_WINDOW, true, false);
647
648     /* Well, we got to abort the recursion here because we destroyed the
649      * container. However, if tree_flatten() is called sufficiently often,
650      * there can’t be the situation of having two pairs of redundant containers
651      * at once. Therefore, we can safely abort the recursion on this level
652      * after flattening. */
653     return;
654
655 recurse:
656     /* We cannot use normal foreach here because tree_flatten might close the
657      * current container. */
658     current = TAILQ_FIRST(&(con->nodes_head));
659     while (current != NULL) {
660         Con *next = TAILQ_NEXT(current, nodes);
661         tree_flatten(current);
662         current = next;
663     }
664
665     current = TAILQ_FIRST(&(con->floating_head));
666     while (current != NULL) {
667         Con *next = TAILQ_NEXT(current, floating_windows);
668         tree_flatten(current);
669         current = next;
670     }
671 }