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