]> git.sur5r.net Git - i3/i3/blob - src/tree.c
5a3a761368c540056797fc09120358097d71d6ee
[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     }
78
79     con_focus(ws);
80 }
81
82 /*
83  * Opens an empty container in the current container
84  *
85  */
86 Con *tree_open_con(Con *con) {
87     if (con == NULL) {
88         /* every focusable Con has a parent (outputs have parent root) */
89         con = focused->parent;
90         /* If the parent is an output, we are on a workspace. In this case,
91          * the new container needs to be opened as a leaf of the workspace. */
92         if (con->type == CT_OUTPUT)
93             con = focused;
94     }
95
96     assert(con != NULL);
97
98     /* 3: re-calculate child->percent for each child */
99     con_fix_percent(con, WINDOW_ADD);
100
101     /* 4: add a new container leaf to this con */
102     Con *new = con_new(con);
103     con_focus(new);
104
105     return new;
106 }
107
108 /*
109  * Closes the given container including all children
110  *
111  */
112 void tree_close(Con *con, bool kill_window) {
113     /* TODO: check floating clients and adjust old_parent if necessary */
114
115     /* Get the container which is next focused */
116     Con *next;
117     if (con->type == CT_FLOATING_CON) {
118         next = TAILQ_NEXT(con, floating_windows);
119         if (next == TAILQ_END(&(con->parent->floating_head)))
120             next = con->parent;
121     } else {
122         next = TAILQ_NEXT(con, focused);
123         if (next == TAILQ_END(&(con->parent->nodes_head)))
124             next = con->parent;
125     }
126
127     LOG("closing %p\n", con);
128     Con *child;
129     /* We cannot use TAILQ_FOREACH because the children get deleted
130      * in their parent’s nodes_head */
131     while (!TAILQ_EMPTY(&(con->nodes_head))) {
132         child = TAILQ_FIRST(&(con->nodes_head));
133         tree_close(child, kill_window);
134     }
135
136     if (con->window != NULL) {
137         if (kill_window)
138             x_window_kill(con->window->id);
139         else {
140             /* un-parent the window */
141             xcb_reparent_window(conn, con->window->id, root, 0, 0);
142             /* TODO: client_unmap to set state to withdrawn */
143
144         }
145         free(con->window);
146     }
147
148     /* kill the X11 part of this container */
149     x_con_kill(con);
150
151     con_detach(con);
152     con_fix_percent(con->parent, WINDOW_REMOVE);
153
154     free(con->name);
155     TAILQ_REMOVE(&all_cons, con, all_cons);
156     free(con);
157
158     /* TODO: check if the container (or one of its children) was focused */
159     con_focus(next);
160 }
161
162 void tree_close_con() {
163     assert(focused != NULL);
164     if (focused->type == CT_WORKSPACE) {
165         LOG("Cannot close workspace\n");
166         return;
167     }
168
169     /* Kill con */
170     tree_close(focused, true);
171 }
172
173 /*
174  * Splits (horizontally or vertically) the given container by creating a new
175  * container which contains the old one and the future ones.
176  *
177  */
178 void tree_split(Con *con, orientation_t orientation) {
179     /* for a workspace, we just need to change orientation */
180     if (con->type == CT_WORKSPACE) {
181         con->orientation = orientation;
182         return;
183     }
184     /* 2: replace it with a new Con */
185     Con *new = con_new(NULL);
186     Con *parent = con->parent;
187     TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
188     TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
189     new->parent = parent;
190     new->orientation = orientation;
191
192     /* 3: add it as a child to the new Con */
193     con_attach(con, new);
194 }
195
196 void level_up() {
197     /* We can focus up to the workspace, but not any higher in the tree */
198     if (focused->parent->type != CT_CON &&
199         focused->parent->type != CT_WORKSPACE) {
200         printf("cannot go up\n");
201         return;
202     }
203     con_focus(focused->parent);
204 }
205
206 void level_down() {
207     /* Go down the focus stack of the current node */
208     Con *next = TAILQ_FIRST(&(focused->focus_head));
209     if (next == TAILQ_END(&(focused->focus_head))) {
210         printf("cannot go down\n");
211         return;
212     }
213     con_focus(next);
214 }
215
216 static void mark_unmapped(Con *con) {
217     Con *current;
218
219     con->mapped = false;
220     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
221         mark_unmapped(current);
222 }
223
224 void tree_render() {
225     if (croot == NULL)
226         return;
227
228     printf("-- BEGIN RENDERING --\n");
229     /* Reset map state for all nodes in tree */
230     /* TODO: a nicer method to walk all nodes would be good, maybe? */
231     mark_unmapped(croot);
232     croot->mapped = true;
233
234     /* We start rendering at an output */
235     Con *output;
236     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
237         printf("output %p / %s\n", output, output->name);
238         render_con(output);
239     }
240     x_push_changes(croot);
241     printf("-- END RENDERING --\n");
242 }
243
244 void tree_next(char way, orientation_t orientation) {
245     /* 1: get the first parent with the same orientation */
246     Con *parent = focused->parent;
247     while (parent->orientation != orientation) {
248         LOG("need to go one level further up\n");
249         /* if the current parent is an output, we are at a workspace
250          * and the orientation still does not match */
251         if (parent->type == CT_WORKSPACE)
252             return;
253         parent = parent->parent;
254     }
255     Con *current = TAILQ_FIRST(&(parent->focus_head));
256     assert(current != TAILQ_END(&(parent->focus_head)));
257
258     /* 2: chose next (or previous) */
259     Con *next;
260     if (way == 'n') {
261         next = TAILQ_NEXT(current, nodes);
262         /* if we are at the end of the list, we need to wrap */
263         if (next == TAILQ_END(&(parent->nodes_head)))
264             next = TAILQ_FIRST(&(parent->nodes_head));
265     } else {
266         next = TAILQ_PREV(current, nodes_head, nodes);
267         /* if we are at the end of the list, we need to wrap */
268         if (next == TAILQ_END(&(parent->nodes_head)))
269             next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
270     }
271
272     /* 3: focus choice comes in here. at the moment we will go down
273      * until we find a window */
274     /* TODO: check for window, atm we only go down as far as possible */
275     while (!TAILQ_EMPTY(&(next->focus_head)))
276         next = TAILQ_FIRST(&(next->focus_head));
277
278     con_focus(next);
279 }
280
281 void tree_move(char way, orientation_t orientation) {
282     /* 1: get the first parent with the same orientation */
283     Con *parent = focused->parent;
284     if (focused->type == CT_WORKSPACE)
285         return;
286     bool level_changed = false;
287     while (parent->orientation != orientation) {
288         LOG("need to go one level further up\n");
289         /* if the current parent is an output, we are at a workspace
290          * and the orientation still does not match */
291         if (parent->type == CT_WORKSPACE)
292             return;
293         parent = parent->parent;
294         level_changed = true;
295     }
296     Con *current = TAILQ_FIRST(&(parent->focus_head));
297     assert(current != TAILQ_END(&(parent->focus_head)));
298
299     /* 2: chose next (or previous) */
300     Con *next = current;
301     if (way == 'n') {
302         LOG("i would insert it after %p / %s\n", next, next->name);
303         if (!level_changed) {
304             next = TAILQ_NEXT(next, nodes);
305             if (next == TAILQ_END(&(next->parent->nodes_head))) {
306                 LOG("cannot move further to the right\n");
307                 return;
308             }
309         }
310
311         con_detach(focused);
312         focused->parent = next->parent;
313
314         TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
315         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
316         /* TODO: don’t influence focus handling? */
317     } else {
318         LOG("i would insert it before %p / %s\n", current, current->name);
319         if (!level_changed) {
320             next = TAILQ_PREV(next, nodes_head, nodes);
321             if (next == TAILQ_END(&(next->parent->nodes_head))) {
322                 LOG("cannot move further\n");
323                 return;
324             }
325         }
326
327         con_detach(focused);
328         focused->parent = next->parent;
329
330         TAILQ_INSERT_BEFORE(next, focused, nodes);
331         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
332         /* TODO: don’t influence focus handling? */
333     }
334 }