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