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