2 * vim:ts=4:sw=4:expandtab
10 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
13 * Loads tree from ~/.i3/_restart.json
17 char *globbed = glob_path("~/.i3/_restart.json");
19 if (!path_exists(globbed)) {
20 LOG("%s does not exist, not restoring tree\n", globbed);
25 /* TODO: refactor the following */
26 croot = con_new(NULL);
29 tree_append_json(globbed);
30 char *old_restart = glob_path("~/.i3/_restart.json.old");
32 rename(globbed, old_restart);
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);
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.
57 croot = con_new(NULL);
59 croot->type = CT_ROOT;
63 TAILQ_FOREACH(output, &outputs, outputs) {
67 Con *oc = con_new(croot);
68 oc->name = strdup(output->name);
70 oc->rect = output->rect;
72 /* add a workspace to this output */
74 ws->name = strdup("1");
75 ws->fullscreen_mode = CF_OUTPUT;
82 * Opens an empty container in the current container
85 Con *tree_open_con(Con *con) {
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)
97 /* 3: re-calculate child->percent for each child */
98 con_fix_percent(con, WINDOW_ADD);
100 /* 4: add a new container leaf to this con */
101 Con *new = con_new(con);
108 * Closes the given container including all children
111 void tree_close(Con *con) {
112 /* TODO: check floating clients and adjust old_parent if necessary */
114 /* Get the container which is next focused */
116 if (con->type == CT_FLOATING_CON) {
117 next = TAILQ_NEXT(con, floating_windows);
118 if (next == TAILQ_END(&(con->parent->floating_head)))
121 next = TAILQ_NEXT(con, focused);
122 if (next == TAILQ_END(&(con->parent->nodes_head)))
126 LOG("closing %p\n", con);
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));
135 /* kill the X11 part of this container */
139 con_fix_percent(con->parent, WINDOW_REMOVE);
141 if (con->window != NULL) {
142 x_window_kill(con->window->id);
146 TAILQ_REMOVE(&all_cons, con, all_cons);
149 /* TODO: check if the container (or one of its children) was focused */
153 void tree_close_con() {
154 assert(focused != NULL);
155 if (focused->parent->type == CT_OUTPUT) {
156 LOG("Cannot close workspace\n");
165 * Splits (horizontally or vertically) the given container by creating a new
166 * container which contains the old one and the future ones.
169 void tree_split(Con *con, orientation_t orientation) {
170 /* for a workspace, we just need to change orientation */
171 if (con->parent->type == CT_OUTPUT) {
172 con->orientation = orientation;
175 /* 2: replace it with a new Con */
176 Con *new = con_new(NULL);
177 Con *parent = con->parent;
178 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
179 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
180 new->parent = parent;
181 new->orientation = orientation;
183 /* 3: add it as a child to the new Con */
184 con_attach(con, new);
188 /* We can focus up to the workspace, but not any higher in the tree */
189 if (focused->parent->type != CT_CON) {
190 printf("cannot go up\n");
193 con_focus(focused->parent);
197 /* Go down the focus stack of the current node */
198 Con *next = TAILQ_FIRST(&(focused->focus_head));
199 if (next == TAILQ_END(&(focused->focus_head))) {
200 printf("cannot go down\n");
206 static void mark_unmapped(Con *con) {
210 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
211 mark_unmapped(current);
218 printf("-- BEGIN RENDERING --\n");
219 /* Reset map state for all nodes in tree */
220 /* TODO: a nicer method to walk all nodes would be good, maybe? */
221 mark_unmapped(croot);
222 croot->mapped = true;
224 /* We start rendering at an output */
226 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
227 printf("output %p / %s\n", output, output->name);
230 x_push_changes(croot);
231 printf("-- END RENDERING --\n");
234 void tree_next(char way, orientation_t orientation) {
235 /* 1: get the first parent with the same orientation */
236 Con *parent = focused->parent;
237 while (parent->orientation != orientation) {
238 LOG("need to go one level further up\n");
239 /* if the current parent is an output, we are at a workspace
240 * and the orientation still does not match */
241 if (parent->parent->type == CT_OUTPUT)
243 parent = parent->parent;
245 Con *current = TAILQ_FIRST(&(parent->focus_head));
246 assert(current != TAILQ_END(&(parent->focus_head)));
248 /* 2: chose next (or previous) */
251 next = TAILQ_NEXT(current, 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_FIRST(&(parent->nodes_head));
256 next = TAILQ_PREV(current, nodes_head, nodes);
257 /* if we are at the end of the list, we need to wrap */
258 if (next == TAILQ_END(&(parent->nodes_head)))
259 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
262 /* 3: focus choice comes in here. at the moment we will go down
263 * until we find a window */
264 /* TODO: check for window, atm we only go down as far as possible */
265 while (!TAILQ_EMPTY(&(next->focus_head)))
266 next = TAILQ_FIRST(&(next->focus_head));
271 void tree_move(char way, orientation_t orientation) {
272 /* 1: get the first parent with the same orientation */
273 Con *parent = focused->parent;
274 bool level_changed = false;
275 while (parent->orientation != orientation) {
276 LOG("need to go one level further up\n");
277 /* if the current parent is an output, we are at a workspace
278 * and the orientation still does not match */
279 if (parent->parent->type == CT_OUTPUT)
281 parent = parent->parent;
282 level_changed = true;
284 Con *current = TAILQ_FIRST(&(parent->focus_head));
285 assert(current != TAILQ_END(&(parent->focus_head)));
287 /* 2: chose next (or previous) */
290 LOG("i would insert it after %p / %s\n", next, next->name);
291 if (!level_changed) {
292 next = TAILQ_NEXT(next, nodes);
293 if (next == TAILQ_END(&(next->parent->nodes_head))) {
294 LOG("cannot move further to the right\n");
300 focused->parent = next->parent;
302 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
303 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
304 /* TODO: don’t influence focus handling? */
306 LOG("i would insert it before %p / %s\n", current, current->name);
307 if (!level_changed) {
308 next = TAILQ_PREV(next, nodes_head, nodes);
309 if (next == TAILQ_END(&(next->parent->nodes_head))) {
310 LOG("cannot move further\n");
316 focused->parent = next->parent;
318 TAILQ_INSERT_BEFORE(next, focused, nodes);
319 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
320 /* TODO: don’t influence focus handling? */