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