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 (used for in-place restarts).
16 bool tree_restore(const char *path) {
17 char *globbed = resolve_tilde(path);
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);
31 printf("appended tree, using new root\n");
32 croot = TAILQ_FIRST(&(croot->nodes_head));
33 printf("new root = %p\n", croot);
34 Con *out = TAILQ_FIRST(&(croot->nodes_head));
35 printf("out = %p\n", out);
36 Con *ws = TAILQ_FIRST(&(out->nodes_head));
37 printf("ws = %p\n", ws);
43 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
44 * root node are created in randr.c for each Output.
48 croot = con_new(NULL);
51 croot->type = CT_ROOT;
55 * Opens an empty container in the current container
58 Con *tree_open_con(Con *con) {
60 /* every focusable Con has a parent (outputs have parent root) */
61 con = focused->parent;
62 /* If the parent is an output, we are on a workspace. In this case,
63 * the new container needs to be opened as a leaf of the workspace. */
64 if (con->type == CT_OUTPUT)
66 /* If the currently focused container is a floating container, we
67 * attach the new container to the workspace */
68 if (con->type == CT_FLOATING_CON)
74 /* 3. create the container and attach it to its parent */
75 Con *new = con_new(con);
77 /* 4: re-calculate child->percent for each child */
80 /* 5: focus the new container */
86 static bool _is_con_mapped(Con *con) {
89 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
90 if (_is_con_mapped(child))
97 * Closes the given container including all children
100 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
101 bool was_mapped = con->mapped;
102 Con *parent = con->parent;
105 /* Even if the container itself is not mapped, its children may be
106 * mapped (for example split containers don't have a mapped window on
107 * their own but usually contain mapped children). */
108 was_mapped = _is_con_mapped(con);
111 /* Get the container which is next focused */
112 Con *next = con_next_focused(con);
113 DLOG("next = %p, focused = %p\n", next, focused);
115 DLOG("closing %p, kill_window = %d\n", con, kill_window);
117 /* We cannot use TAILQ_FOREACH because the children get deleted
118 * in their parent’s nodes_head */
119 while (!TAILQ_EMPTY(&(con->nodes_head))) {
120 child = TAILQ_FIRST(&(con->nodes_head));
121 DLOG("killing child=%p\n", child);
122 tree_close(child, kill_window, true);
125 if (con->window != NULL) {
127 x_window_kill(con->window->id);
129 /* un-parent the window */
130 xcb_reparent_window(conn, con->window->id, root, 0, 0);
131 /* TODO: client_unmap to set state to withdrawn */
134 FREE(con->window->class_class);
135 FREE(con->window->class_instance);
136 FREE(con->window->name_x);
137 FREE(con->window->name_json);
141 /* kill the X11 part of this container */
145 if (con->type != CT_FLOATING_CON) {
146 /* If the container is *not* floating, we might need to re-distribute
147 * percentage values for the resized containers. */
148 con_fix_percent(parent);
151 if (con_is_floating(con)) {
152 Con *ws = con_get_workspace(con);
153 DLOG("Container was floating, killing floating container\n");
154 tree_close(parent, false, false);
155 DLOG("parent container killed\n");
156 if (con == focused) {
157 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
158 /* go down the focus stack as far as possible */
159 next = con_descend_focused(ws);
161 dont_kill_parent = true;
162 DLOG("Alright, focusing %p\n", next);
169 TAILQ_REMOVE(&all_cons, con, all_cons);
172 /* in the case of floating windows, we already focused another container
173 * when closing the parent, so we can exit now. */
175 DLOG("No next container, i will just exit now\n");
179 if (was_mapped || con == focused) {
180 if (kill_window || !dont_kill_parent || con == focused) {
181 DLOG("focusing %p / %s\n", next, next->name);
182 /* TODO: check if the container (or one of its children) was focused */
186 DLOG("not focusing because we're not killing anybody");
189 DLOG("not focusing, was not mapped\n");
192 /* check if the parent container is empty now and close it */
193 if (!dont_kill_parent)
194 CALL(parent, on_remove_child);
198 * Closes the current container using tree_close().
201 void tree_close_con() {
202 assert(focused != NULL);
203 if (focused->type == CT_WORKSPACE) {
204 LOG("Cannot close workspace\n");
208 /* There *should* be no possibility to focus outputs / root container */
209 assert(focused->type != CT_OUTPUT);
210 assert(focused->type != CT_ROOT);
213 tree_close(focused, true, false);
217 * Splits (horizontally or vertically) the given container by creating a new
218 * container which contains the old one and the future ones.
221 void tree_split(Con *con, orientation_t orientation) {
222 /* for a workspace, we just need to change orientation */
223 if (con->type == CT_WORKSPACE) {
224 DLOG("Workspace, simply changing orientation to %d\n", orientation);
225 con->orientation = orientation;
229 Con *parent = con->parent;
230 /* if we are in a container whose parent contains only one
231 * child (its split functionality is unused so far), we just change the
232 * orientation (more intuitive than splitting again) */
233 if (con_num_children(parent) == 1) {
234 parent->orientation = orientation;
235 DLOG("Just changing orientation of existing container\n");
239 DLOG("Splitting in orientation %d\n", orientation);
241 /* 2: replace it with a new Con */
242 Con *new = con_new(NULL);
243 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
244 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
245 new->parent = parent;
246 new->orientation = orientation;
248 /* 3: swap 'percent' (resize factor) */
249 new->percent = con->percent;
252 /* 4: add it as a child to the new Con */
253 con_attach(con, new, false);
257 * Moves focus one level up.
261 /* We can focus up to the workspace, but not any higher in the tree */
262 if (focused->parent->type != CT_CON &&
263 focused->parent->type != CT_WORKSPACE) {
264 printf("cannot go up\n");
267 con_focus(focused->parent);
271 * Moves focus one level down.
275 /* Go down the focus stack of the current node */
276 Con *next = TAILQ_FIRST(&(focused->focus_head));
277 if (next == TAILQ_END(&(focused->focus_head))) {
278 printf("cannot go down\n");
284 static void mark_unmapped(Con *con) {
288 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
289 mark_unmapped(current);
290 if (con->type == CT_WORKSPACE) {
291 /* We need to call mark_unmapped on floating nodes aswell since we can
292 * make containers floating. */
293 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
294 mark_unmapped(current);
299 * Renders the tree, that is rendering all outputs using render_con() and
300 * pushing the changes to X11 using x_push_changes().
307 DLOG("-- BEGIN RENDERING --\n");
308 /* Reset map state for all nodes in tree */
309 /* TODO: a nicer method to walk all nodes would be good, maybe? */
310 mark_unmapped(croot);
311 croot->mapped = true;
313 /* We start rendering at an output */
315 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
316 DLOG("output %p / %s\n", output, output->name);
317 render_con(output, false);
319 x_push_changes(croot);
320 DLOG("-- END RENDERING --\n");
324 * Changes focus in the given way (next/previous) and given orientation
325 * (horizontal/vertical).
328 void tree_next(char way, orientation_t orientation) {
329 /* 1: get the first parent with the same orientation */
330 Con *parent = focused->parent;
331 while (focused->type != CT_WORKSPACE &&
332 (con_orientation(parent) != orientation ||
333 con_num_children(parent) == 1)) {
334 LOG("need to go one level further up\n");
335 /* if the current parent is an output, we are at a workspace
336 * and the orientation still does not match */
337 if (parent->type == CT_WORKSPACE)
339 parent = parent->parent;
341 Con *current = TAILQ_FIRST(&(parent->focus_head));
342 assert(current != TAILQ_END(&(parent->focus_head)));
344 if (TAILQ_EMPTY(&(parent->nodes_head))) {
345 DLOG("Nothing to focus here, move along...\n");
349 /* 2: chose next (or previous) */
352 next = TAILQ_NEXT(current, nodes);
353 /* if we are at the end of the list, we need to wrap */
354 if (next == TAILQ_END(&(parent->nodes_head)))
355 next = TAILQ_FIRST(&(parent->nodes_head));
357 next = TAILQ_PREV(current, nodes_head, nodes);
358 /* if we are at the end of the list, we need to wrap */
359 if (next == TAILQ_END(&(parent->nodes_head)))
360 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
363 /* 3: focus choice comes in here. at the moment we will go down
364 * until we find a window */
365 /* TODO: check for window, atm we only go down as far as possible */
366 con_focus(con_descend_focused(next));
370 * tree_flatten() removes pairs of redundant split containers, e.g.:
371 * [workspace, horizontal]
375 * In this example, the v-split and h-split container are redundant.
376 * Such a situation can be created by moving containers in a direction which is
377 * not the orientation of their parent container. i3 needs to create a new
378 * split container then and if you move containers this way multiple times,
379 * redundant chains of split-containers can be the result.
382 void tree_flatten(Con *con) {
383 Con *current, *child, *parent = con->parent;
384 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
386 /* We only consider normal containers without windows */
387 if (con->type != CT_CON || con->window != NULL)
390 /* Ensure it got only one child */
391 child = TAILQ_FIRST(&(con->nodes_head));
392 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
395 /* The child must have a different orientation than the con but the same as
396 * the con’s parent to be redundant */
397 if (con->orientation == NO_ORIENTATION ||
398 child->orientation == NO_ORIENTATION ||
399 con->orientation == child->orientation ||
400 child->orientation != parent->orientation)
403 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
405 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
407 DLOG("detaching...\n");
408 /* 2: re-attach the children to the parent before con */
409 while (!TAILQ_EMPTY(&(child->nodes_head))) {
410 current = TAILQ_FIRST(&(child->nodes_head));
411 DLOG("detaching current=%p / %s\n", current, current->name);
413 DLOG("re-attaching\n");
414 /* We don’t use con_attach() here because for a CT_CON, the special
415 * case handling of con_attach() does not trigger. So all it would do
416 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
417 * directly use the TAILQ macros. */
418 current->parent = parent;
419 TAILQ_INSERT_BEFORE(con, current, nodes);
420 DLOG("attaching to focus list\n");
421 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
422 current->percent = con->percent;
424 DLOG("re-attached all\n");
426 /* 3: restore focus, if con was focused */
427 if (focus_next != NULL &&
428 TAILQ_FIRST(&(parent->focus_head)) == con) {
429 DLOG("restoring focus to focus_next=%p\n", focus_next);
430 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
431 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
432 DLOG("restored focus.\n");
435 /* 4: close the redundant cons */
436 DLOG("closing redundant cons\n");
437 tree_close(con, false, true);
439 /* Well, we got to abort the recursion here because we destroyed the
440 * container. However, if tree_flatten() is called sufficiently often,
441 * there can’t be the situation of having two pairs of redundant containers
442 * at once. Therefore, we can safely abort the recursion on this level
443 * after flattening. */
447 /* We cannot use normal foreach here because tree_flatten might close the
448 * current container. */
449 current = TAILQ_FIRST(&(con->nodes_head));
450 while (current != NULL) {
451 Con *next = TAILQ_NEXT(current, nodes);
452 tree_flatten(current);
456 current = TAILQ_FIRST(&(con->floating_head));
457 while (current != NULL) {
458 Con *next = TAILQ_NEXT(current, floating_windows);
459 tree_flatten(current);