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