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