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