]> git.sur5r.net Git - i3/i3/blob - src/tree.c
0233a12588c9c508396e69d8efa0ea610ddd840b
[i3/i3] / src / tree.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 struct Con *croot;
8 struct Con *focused;
9
10 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
11
12 /*
13  * Loads tree from ~/.i3/_restart.json (used for in-place restarts).
14  *
15  */
16 bool tree_restore(const char *path) {
17     char *globbed = resolve_tilde(path);
18
19     if (!path_exists(globbed)) {
20         LOG("%s does not exist, not restoring tree\n", globbed);
21         free(globbed);
22         return false;
23     }
24
25     /* TODO: refactor the following */
26     croot = con_new(NULL);
27     focused = croot;
28
29     tree_append_json(globbed);
30
31     printf("appended tree, using new root\n");
32     croot = TAILQ_FIRST(&(croot->nodes_head));
33     printf("new root = %p\n", croot);
34     Con *out = TAILQ_FIRST(&(croot->nodes_head));
35     printf("out = %p\n", out);
36     Con *ws = TAILQ_FIRST(&(out->nodes_head));
37     printf("ws = %p\n", ws);
38
39     return true;
40 }
41
42 /*
43  * Initializes the tree by creating the root node, adding all RandR outputs
44  * to the tree (that means randr_init() has to be called before) and
45  * assigning a workspace to each RandR output.
46  *
47  */
48 void tree_init() {
49     Output *output;
50
51     croot = con_new(NULL);
52     FREE(croot->name);
53     croot->name = "root";
54     croot->type = CT_ROOT;
55
56     Con *ws;
57     int c = 1;
58     /* add the outputs */
59     TAILQ_FOREACH(output, &outputs, outputs) {
60         if (!output->active)
61             continue;
62
63         Con *oc = con_new(croot);
64         FREE(oc->name);
65         oc->name = strdup(output->name);
66         oc->type = CT_OUTPUT;
67         oc->rect = output->rect;
68         output->con = oc;
69
70         char *name;
71         asprintf(&name, "[i3 con] output %s", oc->name);
72         x_set_name(oc, name);
73         free(name);
74
75         /* add a workspace to this output */
76         ws = con_new(NULL);
77         ws->type = CT_WORKSPACE;
78         ws->num = c;
79         FREE(ws->name);
80         asprintf(&(ws->name), "%d", c);
81         c++;
82         con_attach(ws, oc, false);
83
84         asprintf(&name, "[i3 con] workspace %s", ws->name);
85         x_set_name(ws, name);
86         free(name);
87
88         ws->fullscreen_mode = CF_OUTPUT;
89         ws->orientation = HORIZ;
90     }
91
92     con_focus(ws);
93 }
94
95 /*
96  * Opens an empty container in the current container
97  *
98  */
99 Con *tree_open_con(Con *con) {
100     if (con == NULL) {
101         /* every focusable Con has a parent (outputs have parent root) */
102         con = focused->parent;
103         /* If the parent is an output, we are on a workspace. In this case,
104          * the new container needs to be opened as a leaf of the workspace. */
105         if (con->type == CT_OUTPUT)
106             con = focused;
107         /* If the currently focused container is a floating container, we
108          * attach the new container to the workspace */
109         if (con->type == CT_FLOATING_CON)
110             con = con->parent;
111     }
112
113     assert(con != NULL);
114
115     /* 3: re-calculate child->percent for each child */
116     con_fix_percent(con, WINDOW_ADD);
117
118     /* 4: add a new container leaf to this con */
119     Con *new = con_new(con);
120     con_focus(new);
121
122     return new;
123 }
124
125 /*
126  * vanishing is the container that is about to be closed (so any floating
127  * client which has old_parent == vanishing needs to be "re-parented").
128  *
129  */
130 static void fix_floating_parent(Con *con, Con *vanishing) {
131     Con *child;
132
133     if (con->old_parent == vanishing) {
134         LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
135                 vanishing, con, vanishing->parent);
136         con->old_parent = vanishing->parent;
137     }
138
139     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
140         fix_floating_parent(child, vanishing);
141
142     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
143         fix_floating_parent(child, vanishing);
144 }
145
146 static bool _is_con_mapped(Con *con) {
147     Con *child;
148
149     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
150         if (_is_con_mapped(child))
151             return true;
152
153     return con->mapped;
154 }
155
156 /*
157  * Closes the given container including all children
158  *
159  */
160 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
161     bool was_mapped = con->mapped;
162     Con *parent = con->parent;
163
164     if (!was_mapped) {
165         /* Even if the container itself is not mapped, its children may be
166          * mapped (for example split containers don't have a mapped window on
167          * their own but usually contain mapped children). */
168         was_mapped = _is_con_mapped(con);
169     }
170
171     /* check floating clients and adjust old_parent if necessary */
172     fix_floating_parent(croot, con);
173
174     /* Get the container which is next focused */
175     Con *next = con_next_focused(con);
176     DLOG("next = %p, focused = %p\n", next, focused);
177
178     DLOG("closing %p, kill_window = %d\n", con, kill_window);
179     Con *child;
180     /* We cannot use TAILQ_FOREACH because the children get deleted
181      * in their parent’s nodes_head */
182     while (!TAILQ_EMPTY(&(con->nodes_head))) {
183         child = TAILQ_FIRST(&(con->nodes_head));
184         DLOG("killing child=%p\n", child);
185         tree_close(child, kill_window, true);
186     }
187
188     if (con->window != NULL) {
189         if (kill_window)
190             x_window_kill(con->window->id);
191         else {
192             /* un-parent the window */
193             xcb_reparent_window(conn, con->window->id, root, 0, 0);
194             /* TODO: client_unmap to set state to withdrawn */
195
196         }
197         FREE(con->window->class_class);
198         FREE(con->window->class_instance);
199         FREE(con->window->name_x);
200         FREE(con->window->name_json);
201         free(con->window);
202     }
203
204     /* kill the X11 part of this container */
205     x_con_kill(con);
206
207     con_detach(con);
208     if (con->type != CT_FLOATING_CON) {
209         /* If the container is *not* floating, we might need to re-distribute
210          * percentage values for the resized containers. */
211         con_fix_percent(parent, WINDOW_REMOVE);
212     }
213
214     if (con_is_floating(con)) {
215         Con *ws = con_get_workspace(con);
216         DLOG("Container was floating, killing floating container\n");
217         tree_close(parent, false, false);
218         DLOG("parent container killed\n");
219         if (con == focused) {
220             DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
221             next = con_next_focused(ws);
222             dont_kill_parent = true;
223             DLOG("Alright, focusing %p\n", next);
224         } else {
225             next = NULL;
226         }
227     }
228
229     free(con->name);
230     TAILQ_REMOVE(&all_cons, con, all_cons);
231     free(con);
232
233     /* in the case of floating windows, we already focused another container
234      * when closing the parent, so we can exit now. */
235     if (!next) {
236         DLOG("No next container, i will just exit now\n");
237         return;
238     }
239
240     if (was_mapped || con == focused) {
241         DLOG("focusing %p / %s\n", next, next->name);
242         /* TODO: check if the container (or one of its children) was focused */
243         con_focus(next);
244     } else {
245         DLOG("not focusing, was not mapped\n");
246     }
247
248     /* check if the parent container is empty now and close it */
249     if (!dont_kill_parent &&
250         parent->type != CT_WORKSPACE &&
251         TAILQ_EMPTY(&(parent->nodes_head))) {
252         DLOG("Closing empty parent container\n");
253         /* TODO: check if this container would swallow any other client and
254          * don’t close it automatically. */
255         tree_close(parent, false, false);
256     }
257 }
258
259 /*
260  * Closes the current container using tree_close().
261  *
262  */
263 void tree_close_con() {
264     assert(focused != NULL);
265     if (focused->type == CT_WORKSPACE) {
266         LOG("Cannot close workspace\n");
267         return;
268     }
269
270     /* There *should* be no possibility to focus outputs / root container */
271     assert(focused->type != CT_OUTPUT);
272     assert(focused->type != CT_ROOT);
273
274     /* Kill con */
275     tree_close(focused, true, false);
276 }
277
278 /*
279  * Splits (horizontally or vertically) the given container by creating a new
280  * container which contains the old one and the future ones.
281  *
282  */
283 void tree_split(Con *con, orientation_t orientation) {
284     /* for a workspace, we just need to change orientation */
285     if (con->type == CT_WORKSPACE) {
286         DLOG("Workspace, simply changing orientation to %d\n", orientation);
287         con->orientation = orientation;
288         return;
289     }
290
291     Con *parent = con->parent;
292     /* if we are in a container whose parent contains only one
293      * child (its split functionality is unused so far), we just change the
294      * orientation (more intuitive than splitting again) */
295     if (con_num_children(parent) == 1) {
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);
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 can focus up to the workspace, but not any higher in the tree */
324     if (focused->parent->type != CT_CON &&
325         focused->parent->type != CT_WORKSPACE) {
326         printf("cannot go up\n");
327         return;
328     }
329     con_focus(focused->parent);
330 }
331
332 /*
333  * Moves focus one level down.
334  *
335  */
336 void level_down() {
337     /* Go down the focus stack of the current node */
338     Con *next = TAILQ_FIRST(&(focused->focus_head));
339     if (next == TAILQ_END(&(focused->focus_head))) {
340         printf("cannot go down\n");
341         return;
342     }
343     con_focus(next);
344 }
345
346 static void mark_unmapped(Con *con) {
347     Con *current;
348
349     con->mapped = false;
350     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
351         mark_unmapped(current);
352     if (con->type == CT_WORKSPACE) {
353         TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
354             current->mapped = false;
355             Con *child = TAILQ_FIRST(&(current->nodes_head));
356             child->mapped = false;
357         }
358     }
359 }
360
361 /*
362  * Renders the tree, that is rendering all outputs using render_con() and
363  * pushing the changes to X11 using x_push_changes().
364  *
365  */
366 void tree_render() {
367     if (croot == NULL)
368         return;
369
370     printf("-- BEGIN RENDERING --\n");
371     /* Reset map state for all nodes in tree */
372     /* TODO: a nicer method to walk all nodes would be good, maybe? */
373     mark_unmapped(croot);
374     croot->mapped = true;
375
376     /* We start rendering at an output */
377     Con *output;
378     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
379         printf("output %p / %s\n", output, output->name);
380         render_con(output, false);
381     }
382     x_push_changes(croot);
383     printf("-- END RENDERING --\n");
384 }
385
386 /*
387  * Changes focus in the given way (next/previous) and given orientation
388  * (horizontal/vertical).
389  *
390  */
391 void tree_next(char way, orientation_t orientation) {
392     /* 1: get the first parent with the same orientation */
393     Con *parent = focused->parent;
394     while (focused->type != CT_WORKSPACE &&
395            con_orientation(parent) != orientation) {
396         LOG("need to go one level further up\n");
397         /* if the current parent is an output, we are at a workspace
398          * and the orientation still does not match */
399         if (parent->type == CT_WORKSPACE)
400             return;
401         parent = parent->parent;
402     }
403     Con *current = TAILQ_FIRST(&(parent->focus_head));
404     assert(current != TAILQ_END(&(parent->focus_head)));
405
406     /* 2: chose next (or previous) */
407     Con *next;
408     if (way == 'n') {
409         next = TAILQ_NEXT(current, nodes);
410         /* if we are at the end of the list, we need to wrap */
411         if (next == TAILQ_END(&(parent->nodes_head)))
412             next = TAILQ_FIRST(&(parent->nodes_head));
413     } else {
414         next = TAILQ_PREV(current, nodes_head, nodes);
415         /* if we are at the end of the list, we need to wrap */
416         if (next == TAILQ_END(&(parent->nodes_head)))
417             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
418     }
419
420     /* 3: focus choice comes in here. at the moment we will go down
421      * until we find a window */
422     /* TODO: check for window, atm we only go down as far as possible */
423     while (!TAILQ_EMPTY(&(next->focus_head)))
424         next = TAILQ_FIRST(&(next->focus_head));
425
426     DLOG("focusing %p\n", next);
427     con_focus(next);
428 }
429
430 /*
431  * Moves the current container in the given way (next/previous) and given
432  * orientation (horizontal/vertical).
433  *
434  */
435 void tree_move(char way, orientation_t orientation) {
436     /* 1: get the first parent with the same orientation */
437     Con *parent = focused->parent;
438     Con *old_parent = parent;
439     if (focused->type == CT_WORKSPACE)
440         return;
441     bool level_changed = false;
442     while (con_orientation(parent) != orientation) {
443         DLOG("need to go one level further up\n");
444         /* If the current parent is an output, we are at a workspace
445          * and the orientation still does not match. In this case, we split the
446          * workspace to have the same look & feel as in older i3 releases. */
447         if (parent->type == CT_WORKSPACE) {
448             DLOG("Arrived at workspace, splitting...\n");
449             /* 1: create a new split container */
450             Con *new = con_new(NULL);
451             new->parent = parent;
452
453             /* 2: copy layout and orientation from workspace */
454             new->layout = parent->layout;
455             new->orientation = parent->orientation;
456
457             Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
458             if (old_focused == TAILQ_END(&(parent->focus_head)))
459                 old_focused = NULL;
460
461             /* 3: move the existing cons of this workspace below the new con */
462             DLOG("Moving cons\n");
463             Con *child;
464             while (!TAILQ_EMPTY(&(parent->nodes_head))) {
465                 child = TAILQ_FIRST(&(parent->nodes_head));
466                 con_detach(child);
467                 con_attach(child, new, true);
468             }
469
470             /* 4: switch workspace orientation */
471             parent->orientation = orientation;
472
473             /* 4: attach the new split container to the workspace */
474             DLOG("Attaching new split to ws\n");
475             con_attach(new, parent, false);
476
477             if (old_focused)
478                 con_focus(old_focused);
479
480             level_changed = true;
481
482             break;
483         }
484         parent = parent->parent;
485         level_changed = true;
486     }
487     Con *current = TAILQ_FIRST(&(parent->focus_head));
488     assert(current != TAILQ_END(&(parent->focus_head)));
489
490     /* 2: chose next (or previous) */
491     Con *next = current;
492     if (way == 'n') {
493         LOG("i would insert it after %p / %s\n", next, next->name);
494
495         /* Have a look at the next container: If there is no next container or
496          * if it is a leaf node, we move the focused one left to it. However,
497          * for split containers, we descend into it. */
498         next = TAILQ_NEXT(next, nodes);
499         if (next == TAILQ_END(&(next->parent->nodes_head))) {
500             if (focused == current)
501                 return;
502             next = current;
503         } else {
504             if (level_changed && con_is_leaf(next)) {
505                 next = current;
506             } else {
507                 /* if this is a split container, we need to go down */
508                 while (!TAILQ_EMPTY(&(next->focus_head)))
509                     next = TAILQ_FIRST(&(next->focus_head));
510             }
511         }
512
513         con_detach(focused);
514         focused->parent = next->parent;
515
516         TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
517         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
518         /* TODO: don’t influence focus handling? */
519     } else {
520         LOG("i would insert it before %p / %s\n", current, current->name);
521         bool gone_down = false;
522         next = TAILQ_PREV(next, nodes_head, nodes);
523         if (next == TAILQ_END(&(next->parent->nodes_head))) {
524             if (focused == current)
525                 return;
526             next = current;
527         } else {
528             if (level_changed && con_is_leaf(next)) {
529                 next = current;
530             } else {
531                 /* if this is a split container, we need to go down */
532                 while (!TAILQ_EMPTY(&(next->focus_head))) {
533                     gone_down = true;
534                     next = TAILQ_FIRST(&(next->focus_head));
535                 }
536             }
537         }
538
539         con_detach(focused);
540         focused->parent = next->parent;
541
542         /* After going down in the tree, we insert the container *after*
543          * the currently focused one even though the command used "before".
544          * This is to keep the user experience clear, since the before/after
545          * only signifies the direction of the movement on top-level */
546         if (gone_down)
547             TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
548         else TAILQ_INSERT_BEFORE(next, focused, nodes);
549         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
550         /* TODO: don’t influence focus handling? */
551     }
552
553     /* We need to call con_focus() to fix the focus stack "above" the container
554      * we just inserted the focused container into (otherwise, the parent
555      * container(s) would still point to the old container(s)). */
556     con_focus(focused);
557
558     if (con_num_children(old_parent) == 0) {
559         DLOG("Old container empty after moving. Let's close it\n");
560         tree_close(old_parent, false, false);
561     }
562 }