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