]> git.sur5r.net Git - i3/i3/blob - src/tree.c
491548da56d55160b3931f37096decf57a518ce3
[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() {
17     char *globbed = glob_path("~/.i3/_restart.json");
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     char *old_restart = glob_path("~/.i3/_restart.json.old");
31     unlink(old_restart);
32     rename(globbed, old_restart);
33     free(globbed);
34     free(old_restart);
35
36     printf("appended tree, using new root\n");
37     croot = TAILQ_FIRST(&(croot->nodes_head));
38     printf("new root = %p\n", croot);
39     Con *out = TAILQ_FIRST(&(croot->nodes_head));
40     printf("out = %p\n", out);
41     Con *ws = TAILQ_FIRST(&(out->nodes_head));
42     printf("ws = %p\n", ws);
43     con_focus(ws);
44
45     return true;
46 }
47
48 /*
49  * Initializes the tree by creating the root node, adding all RandR outputs
50  * to the tree (that means randr_init() has to be called before) and
51  * assigning a workspace to each RandR output.
52  *
53  */
54 void tree_init() {
55     Output *output;
56
57     croot = con_new(NULL);
58     croot->name = "root";
59     croot->type = CT_ROOT;
60
61     Con *ws;
62     /* add the outputs */
63     TAILQ_FOREACH(output, &outputs, outputs) {
64         if (!output->active)
65             continue;
66
67         Con *oc = con_new(croot);
68         oc->name = strdup(output->name);
69         oc->type = CT_OUTPUT;
70         oc->rect = output->rect;
71
72         /* add a workspace to this output */
73         ws = con_new(oc);
74         ws->type = CT_WORKSPACE;
75         ws->name = strdup("1");
76         ws->fullscreen_mode = CF_OUTPUT;
77         ws->orientation = HORIZ;
78     }
79
80     con_focus(ws);
81 }
82
83 /*
84  * Opens an empty container in the current container
85  *
86  */
87 Con *tree_open_con(Con *con) {
88     if (con == NULL) {
89         /* every focusable Con has a parent (outputs have parent root) */
90         con = focused->parent;
91         /* If the parent is an output, we are on a workspace. In this case,
92          * the new container needs to be opened as a leaf of the workspace. */
93         if (con->type == CT_OUTPUT)
94             con = focused;
95     }
96
97     assert(con != NULL);
98
99     /* 3: re-calculate child->percent for each child */
100     con_fix_percent(con, WINDOW_ADD);
101
102     /* 4: add a new container leaf to this con */
103     Con *new = con_new(con);
104     con_focus(new);
105
106     return new;
107 }
108
109 /*
110  * vanishing is the container that is about to be closed (so any floating
111  * client which has old_parent == vanishing needs to be "re-parented").
112  *
113  */
114 static void fix_floating_parent(Con *con, Con *vanishing) {
115     Con *child;
116
117     if (con->old_parent == vanishing) {
118         LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
119                 vanishing, con, vanishing->parent);
120         con->old_parent = vanishing->parent;
121     }
122
123     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
124         fix_floating_parent(child, vanishing);
125
126     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
127         fix_floating_parent(child, vanishing);
128 }
129
130 /*
131  * Closes the given container including all children
132  *
133  */
134 void tree_close(Con *con, bool kill_window) {
135     Con *parent = con->parent;
136
137     /* check floating clients and adjust old_parent if necessary */
138     fix_floating_parent(croot, con);
139
140     /* Get the container which is next focused */
141     Con *next;
142     if (con->type == CT_FLOATING_CON) {
143         next = TAILQ_NEXT(con, floating_windows);
144         if (next == TAILQ_END(&(parent->floating_head)))
145             next = con_get_workspace(con);
146     } else {
147         next = TAILQ_NEXT(con, focused);
148         if (next == TAILQ_END(&(parent->nodes_head))) {
149             next = parent;
150             while (!TAILQ_EMPTY(&(next->focus_head)) &&
151                    TAILQ_FIRST(&(next->focus_head)) != con)
152                 next = TAILQ_FIRST(&(next->focus_head));
153         }
154     }
155
156     DLOG("closing %p, kill_window = %d\n", con, kill_window);
157     Con *child;
158     /* We cannot use TAILQ_FOREACH because the children get deleted
159      * in their parent’s nodes_head */
160     while (!TAILQ_EMPTY(&(con->nodes_head))) {
161         child = TAILQ_FIRST(&(con->nodes_head));
162         tree_close(child, kill_window);
163     }
164
165     if (con->window != NULL) {
166         if (kill_window)
167             x_window_kill(con->window->id);
168         else {
169             /* un-parent the window */
170             xcb_reparent_window(conn, con->window->id, root, 0, 0);
171             /* TODO: client_unmap to set state to withdrawn */
172
173         }
174         free(con->window);
175     }
176
177     /* kill the X11 part of this container */
178     x_con_kill(con);
179
180     con_detach(con);
181     con_fix_percent(parent, WINDOW_REMOVE);
182
183     if (con_is_floating(con)) {
184         DLOG("Container was floating, killing floating container\n");
185
186         TAILQ_REMOVE(&(parent->parent->floating_head), parent, floating_windows);
187         TAILQ_REMOVE(&(parent->parent->focus_head), parent, focused);
188         tree_close(parent, false);
189         next = NULL;
190     }
191
192     free(con->name);
193     TAILQ_REMOVE(&all_cons, con, all_cons);
194     free(con);
195
196     /* in the case of floating windows, we already focused another container
197      * when closing the parent, so we can exit now. */
198     if (!next)
199         return;
200
201     DLOG("focusing %p / %s\n", next, next->name);
202     /* TODO: check if the container (or one of its children) was focused */
203     con_focus(next);
204
205     /* check if the parent container is empty now and close it */
206     if (parent->type != CT_WORKSPACE &&
207         TAILQ_EMPTY(&(parent->nodes_head))) {
208         DLOG("Closing empty parent container\n");
209         /* TODO: check if this container would swallow any other client and
210          * don’t close it automatically. */
211         tree_close(parent, false);
212     }
213 }
214
215 /*
216  * Closes the current container using tree_close().
217  *
218  */
219 void tree_close_con() {
220     assert(focused != NULL);
221     if (focused->type == CT_WORKSPACE) {
222         LOG("Cannot close workspace\n");
223         return;
224     }
225
226     /* Kill con */
227     tree_close(focused, true);
228 }
229
230 /*
231  * Splits (horizontally or vertically) the given container by creating a new
232  * container which contains the old one and the future ones.
233  *
234  */
235 void tree_split(Con *con, orientation_t orientation) {
236     /* for a workspace, we just need to change orientation */
237     if (con->type == CT_WORKSPACE) {
238         DLOG("Workspace, simply changing orientation to %d\n", orientation);
239         con->orientation = orientation;
240         return;
241     }
242
243     Con *parent = con->parent;
244     /* if we are in a container whose parent contains only one
245      * child and has the same orientation like we are trying to
246      * set, this operation is a no-op to not confuse the user */
247     if (parent->orientation == orientation &&
248         TAILQ_NEXT(con, nodes) == TAILQ_END(&(parent->nodes_head))) {
249         DLOG("Not splitting the same way again\n");
250         return;
251     }
252
253     DLOG("Splitting in orientation %d\n", orientation);
254
255     /* 2: replace it with a new Con */
256     Con *new = con_new(NULL);
257     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
258     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
259     new->parent = parent;
260     new->orientation = orientation;
261
262     /* 3: add it as a child to the new Con */
263     con_attach(con, new);
264 }
265
266 /*
267  * Moves focus one level up.
268  *
269  */
270 void level_up() {
271     /* We can focus up to the workspace, but not any higher in the tree */
272     if (focused->parent->type != CT_CON &&
273         focused->parent->type != CT_WORKSPACE) {
274         printf("cannot go up\n");
275         return;
276     }
277     con_focus(focused->parent);
278 }
279
280 /*
281  * Moves focus one level down.
282  *
283  */
284 void level_down() {
285     /* Go down the focus stack of the current node */
286     Con *next = TAILQ_FIRST(&(focused->focus_head));
287     if (next == TAILQ_END(&(focused->focus_head))) {
288         printf("cannot go down\n");
289         return;
290     }
291     con_focus(next);
292 }
293
294 static void mark_unmapped(Con *con) {
295     Con *current;
296
297     con->mapped = false;
298     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
299         mark_unmapped(current);
300 }
301
302 /*
303  * Renders the tree, that is rendering all outputs using render_con() and
304  * pushing the changes to X11 using x_push_changes().
305  *
306  */
307 void tree_render() {
308     if (croot == NULL)
309         return;
310
311     printf("-- BEGIN RENDERING --\n");
312     /* Reset map state for all nodes in tree */
313     /* TODO: a nicer method to walk all nodes would be good, maybe? */
314     mark_unmapped(croot);
315     croot->mapped = true;
316
317     /* We start rendering at an output */
318     Con *output;
319     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
320         printf("output %p / %s\n", output, output->name);
321         render_con(output);
322     }
323     x_push_changes(croot);
324     printf("-- END RENDERING --\n");
325 }
326
327 /*
328  * Changes focus in the given way (next/previous) and given orientation
329  * (horizontal/vertical).
330  *
331  */
332 void tree_next(char way, orientation_t orientation) {
333     /* 1: get the first parent with the same orientation */
334     Con *parent = focused->parent;
335     while (focused->type != CT_WORKSPACE &&
336            con_orientation(parent) != orientation) {
337         LOG("need to go one level further up\n");
338         /* if the current parent is an output, we are at a workspace
339          * and the orientation still does not match */
340         if (parent->type == CT_WORKSPACE)
341             return;
342         parent = parent->parent;
343     }
344     Con *current = TAILQ_FIRST(&(parent->focus_head));
345     assert(current != TAILQ_END(&(parent->focus_head)));
346
347     /* 2: chose next (or previous) */
348     Con *next;
349     if (way == 'n') {
350         next = TAILQ_NEXT(current, nodes);
351         /* if we are at the end of the list, we need to wrap */
352         if (next == TAILQ_END(&(parent->nodes_head)))
353             next = TAILQ_FIRST(&(parent->nodes_head));
354     } else {
355         next = TAILQ_PREV(current, nodes_head, nodes);
356         /* if we are at the end of the list, we need to wrap */
357         if (next == TAILQ_END(&(parent->nodes_head)))
358             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
359     }
360
361     /* 3: focus choice comes in here. at the moment we will go down
362      * until we find a window */
363     /* TODO: check for window, atm we only go down as far as possible */
364     while (!TAILQ_EMPTY(&(next->focus_head)))
365         next = TAILQ_FIRST(&(next->focus_head));
366
367     con_focus(next);
368 }
369
370 /*
371  * Moves the current container in the given way (next/previous) and given
372  * orientation (horizontal/vertical).
373  *
374  */
375 void tree_move(char way, orientation_t orientation) {
376     /* 1: get the first parent with the same orientation */
377     Con *parent = focused->parent;
378     if (focused->type == CT_WORKSPACE)
379         return;
380     bool level_changed = false;
381     while (con_orientation(parent) != orientation) {
382         LOG("need to go one level further up\n");
383         /* if the current parent is an output, we are at a workspace
384          * and the orientation still does not match */
385         if (parent->type == CT_WORKSPACE)
386             return;
387         parent = parent->parent;
388         level_changed = true;
389     }
390     Con *current = TAILQ_FIRST(&(parent->focus_head));
391     assert(current != TAILQ_END(&(parent->focus_head)));
392
393     /* 2: chose next (or previous) */
394     Con *next = current;
395     if (way == 'n') {
396         LOG("i would insert it after %p / %s\n", next, next->name);
397         if (!level_changed) {
398             next = TAILQ_NEXT(next, nodes);
399             if (next == TAILQ_END(&(next->parent->nodes_head))) {
400                 LOG("cannot move further to the right\n");
401                 return;
402             }
403
404             /* if this is a split container, we need to go down */
405             while (!TAILQ_EMPTY(&(next->focus_head)))
406                 next = TAILQ_FIRST(&(next->focus_head));
407         }
408
409         con_detach(focused);
410         focused->parent = next->parent;
411
412         TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
413         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
414         /* TODO: don’t influence focus handling? */
415     } else {
416         LOG("i would insert it before %p / %s\n", current, current->name);
417         bool gone_down = false;
418         if (!level_changed) {
419             next = TAILQ_PREV(next, nodes_head, nodes);
420             if (next == TAILQ_END(&(next->parent->nodes_head))) {
421                 LOG("cannot move further\n");
422                 return;
423             }
424
425             /* if this is a split container, we need to go down */
426             while (!TAILQ_EMPTY(&(next->focus_head))) {
427                 gone_down = true;
428                 next = TAILQ_FIRST(&(next->focus_head));
429             }
430         }
431
432         con_detach(focused);
433         focused->parent = next->parent;
434
435         /* After going down in the tree, we insert the container *after*
436          * the currently focused one even though the command used "before".
437          * This is to keep the user experience clear, since the before/after
438          * only signifies the direction of the movement on top-level */
439         if (gone_down)
440             TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
441         else TAILQ_INSERT_BEFORE(next, focused, nodes);
442         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
443         /* TODO: don’t influence focus handling? */
444     }
445 }