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