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