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