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