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