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