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