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: re-calculate child->percent for each child */
75 con_fix_percent(con, WINDOW_ADD);
77 /* 4: add a new container leaf to this con */
78 Con *new = con_new(con);
85 * vanishing is the container that is about to be closed (so any floating
86 * client which has old_parent == vanishing needs to be "re-parented").
89 static void fix_floating_parent(Con *con, Con *vanishing) {
92 if (con->old_parent == vanishing) {
93 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
94 vanishing, con, vanishing->parent);
95 con->old_parent = vanishing->parent;
98 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
99 fix_floating_parent(child, vanishing);
101 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
102 fix_floating_parent(child, vanishing);
105 static bool _is_con_mapped(Con *con) {
108 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
109 if (_is_con_mapped(child))
116 * Closes the given container including all children
119 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
120 bool was_mapped = con->mapped;
121 Con *parent = con->parent;
124 /* Even if the container itself is not mapped, its children may be
125 * mapped (for example split containers don't have a mapped window on
126 * their own but usually contain mapped children). */
127 was_mapped = _is_con_mapped(con);
130 /* check floating clients and adjust old_parent if necessary */
131 fix_floating_parent(croot, con);
133 /* Get the container which is next focused */
134 Con *next = con_next_focused(con);
135 DLOG("next = %p, focused = %p\n", next, focused);
137 DLOG("closing %p, kill_window = %d\n", con, kill_window);
139 /* We cannot use TAILQ_FOREACH because the children get deleted
140 * in their parent’s nodes_head */
141 while (!TAILQ_EMPTY(&(con->nodes_head))) {
142 child = TAILQ_FIRST(&(con->nodes_head));
143 DLOG("killing child=%p\n", child);
144 tree_close(child, kill_window, true);
147 if (con->window != NULL) {
149 x_window_kill(con->window->id);
151 /* un-parent the window */
152 xcb_reparent_window(conn, con->window->id, root, 0, 0);
153 /* TODO: client_unmap to set state to withdrawn */
156 FREE(con->window->class_class);
157 FREE(con->window->class_instance);
158 FREE(con->window->name_x);
159 FREE(con->window->name_json);
163 /* kill the X11 part of this container */
167 if (con->type != CT_FLOATING_CON) {
168 /* If the container is *not* floating, we might need to re-distribute
169 * percentage values for the resized containers. */
170 con_fix_percent(parent, WINDOW_REMOVE);
173 if (con_is_floating(con)) {
174 Con *ws = con_get_workspace(con);
175 DLOG("Container was floating, killing floating container\n");
176 tree_close(parent, false, false);
177 DLOG("parent container killed\n");
178 if (con == focused) {
179 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
180 next = con_next_focused(ws);
181 dont_kill_parent = true;
182 DLOG("Alright, focusing %p\n", next);
189 TAILQ_REMOVE(&all_cons, con, all_cons);
192 /* in the case of floating windows, we already focused another container
193 * when closing the parent, so we can exit now. */
195 DLOG("No next container, i will just exit now\n");
199 if (was_mapped || con == focused) {
200 DLOG("focusing %p / %s\n", next, next->name);
201 /* TODO: check if the container (or one of its children) was focused */
204 DLOG("not focusing, was not mapped\n");
207 /* check if the parent container is empty now and close it */
208 if (!dont_kill_parent &&
209 parent->type != CT_WORKSPACE &&
210 TAILQ_EMPTY(&(parent->nodes_head))) {
211 DLOG("Closing empty parent container\n");
212 /* TODO: check if this container would swallow any other client and
213 * don’t close it automatically. */
214 tree_close(parent, false, false);
219 * Closes the current container using tree_close().
222 void tree_close_con() {
223 assert(focused != NULL);
224 if (focused->type == CT_WORKSPACE) {
225 LOG("Cannot close workspace\n");
229 /* There *should* be no possibility to focus outputs / root container */
230 assert(focused->type != CT_OUTPUT);
231 assert(focused->type != CT_ROOT);
234 tree_close(focused, true, false);
238 * Splits (horizontally or vertically) the given container by creating a new
239 * container which contains the old one and the future ones.
242 void tree_split(Con *con, orientation_t orientation) {
243 /* for a workspace, we just need to change orientation */
244 if (con->type == CT_WORKSPACE) {
245 DLOG("Workspace, simply changing orientation to %d\n", orientation);
246 con->orientation = orientation;
250 Con *parent = con->parent;
251 /* if we are in a container whose parent contains only one
252 * child (its split functionality is unused so far), we just change the
253 * orientation (more intuitive than splitting again) */
254 if (con_num_children(parent) == 1) {
255 parent->orientation = orientation;
256 DLOG("Just changing orientation of existing container\n");
260 DLOG("Splitting in orientation %d\n", orientation);
262 /* 2: replace it with a new Con */
263 Con *new = con_new(NULL);
264 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
265 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
266 new->parent = parent;
267 new->orientation = orientation;
269 /* 3: swap 'percent' (resize factor) */
270 new->percent = con->percent;
273 /* 4: add it as a child to the new Con */
274 con_attach(con, new, false);
278 * Moves focus one level up.
282 /* We can focus up to the workspace, but not any higher in the tree */
283 if (focused->parent->type != CT_CON &&
284 focused->parent->type != CT_WORKSPACE) {
285 printf("cannot go up\n");
288 con_focus(focused->parent);
292 * Moves focus one level down.
296 /* Go down the focus stack of the current node */
297 Con *next = TAILQ_FIRST(&(focused->focus_head));
298 if (next == TAILQ_END(&(focused->focus_head))) {
299 printf("cannot go down\n");
305 static void mark_unmapped(Con *con) {
309 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
310 mark_unmapped(current);
311 if (con->type == CT_WORKSPACE) {
312 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
313 current->mapped = false;
314 Con *child = TAILQ_FIRST(&(current->nodes_head));
315 child->mapped = false;
321 * Renders the tree, that is rendering all outputs using render_con() and
322 * pushing the changes to X11 using x_push_changes().
329 DLOG("-- BEGIN RENDERING --\n");
330 /* Reset map state for all nodes in tree */
331 /* TODO: a nicer method to walk all nodes would be good, maybe? */
332 mark_unmapped(croot);
333 croot->mapped = true;
335 /* We start rendering at an output */
337 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
338 DLOG("output %p / %s\n", output, output->name);
339 render_con(output, false);
341 x_push_changes(croot);
342 DLOG("-- END RENDERING --\n");
346 * Changes focus in the given way (next/previous) and given orientation
347 * (horizontal/vertical).
350 void tree_next(char way, orientation_t orientation) {
351 /* 1: get the first parent with the same orientation */
352 Con *parent = focused->parent;
353 while (focused->type != CT_WORKSPACE &&
354 con_orientation(parent) != orientation) {
355 LOG("need to go one level further up\n");
356 /* if the current parent is an output, we are at a workspace
357 * and the orientation still does not match */
358 if (parent->type == CT_WORKSPACE)
360 parent = parent->parent;
362 Con *current = TAILQ_FIRST(&(parent->focus_head));
363 assert(current != TAILQ_END(&(parent->focus_head)));
365 /* 2: chose next (or previous) */
368 next = TAILQ_NEXT(current, nodes);
369 /* if we are at the end of the list, we need to wrap */
370 if (next == TAILQ_END(&(parent->nodes_head)))
371 next = TAILQ_FIRST(&(parent->nodes_head));
373 next = TAILQ_PREV(current, nodes_head, nodes);
374 /* if we are at the end of the list, we need to wrap */
375 if (next == TAILQ_END(&(parent->nodes_head)))
376 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
379 /* 3: focus choice comes in here. at the moment we will go down
380 * until we find a window */
381 /* TODO: check for window, atm we only go down as far as possible */
382 while (!TAILQ_EMPTY(&(next->focus_head)))
383 next = TAILQ_FIRST(&(next->focus_head));
385 DLOG("focusing %p\n", next);
390 * Moves the current container in the given way (next/previous) and given
391 * orientation (horizontal/vertical).
394 void tree_move(char way, orientation_t orientation) {
395 /* 1: get the first parent with the same orientation */
396 Con *parent = focused->parent;
397 Con *old_parent = parent;
398 if (focused->type == CT_WORKSPACE)
400 bool level_changed = false;
401 while (con_orientation(parent) != orientation) {
402 DLOG("need to go one level further up\n");
403 /* If the current parent is an output, we are at a workspace
404 * and the orientation still does not match. In this case, we split the
405 * workspace to have the same look & feel as in older i3 releases. */
406 if (parent->type == CT_WORKSPACE) {
407 DLOG("Arrived at workspace, splitting...\n");
408 /* 1: create a new split container */
409 Con *new = con_new(NULL);
410 new->parent = parent;
412 /* 2: copy layout and orientation from workspace */
413 new->layout = parent->layout;
414 new->orientation = parent->orientation;
416 Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
417 if (old_focused == TAILQ_END(&(parent->focus_head)))
420 /* 3: move the existing cons of this workspace below the new con */
421 DLOG("Moving cons\n");
423 while (!TAILQ_EMPTY(&(parent->nodes_head))) {
424 child = TAILQ_FIRST(&(parent->nodes_head));
426 con_attach(child, new, true);
429 /* 4: switch workspace orientation */
430 parent->orientation = orientation;
432 /* 4: attach the new split container to the workspace */
433 DLOG("Attaching new split to ws\n");
434 con_attach(new, parent, false);
437 con_focus(old_focused);
439 level_changed = true;
443 parent = parent->parent;
444 level_changed = true;
446 Con *current = TAILQ_FIRST(&(parent->focus_head));
447 assert(current != TAILQ_END(&(parent->focus_head)));
449 /* 2: chose next (or previous) */
452 LOG("i would insert it after %p / %s\n", next, next->name);
454 /* Have a look at the next container: If there is no next container or
455 * if it is a leaf node, we move the focused one left to it. However,
456 * for split containers, we descend into it. */
457 next = TAILQ_NEXT(next, nodes);
458 if (next == TAILQ_END(&(next->parent->nodes_head))) {
459 if (focused == current)
463 if (level_changed && con_is_leaf(next)) {
466 /* if this is a split container, we need to go down */
467 while (!TAILQ_EMPTY(&(next->focus_head)))
468 next = TAILQ_FIRST(&(next->focus_head));
473 focused->parent = next->parent;
475 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
476 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
477 /* TODO: don’t influence focus handling? */
479 LOG("i would insert it before %p / %s\n", current, current->name);
480 bool gone_down = false;
481 next = TAILQ_PREV(next, nodes_head, nodes);
482 if (next == TAILQ_END(&(next->parent->nodes_head))) {
483 if (focused == current)
487 if (level_changed && con_is_leaf(next)) {
490 /* if this is a split container, we need to go down */
491 while (!TAILQ_EMPTY(&(next->focus_head))) {
493 next = TAILQ_FIRST(&(next->focus_head));
499 focused->parent = next->parent;
501 /* After going down in the tree, we insert the container *after*
502 * the currently focused one even though the command used "before".
503 * This is to keep the user experience clear, since the before/after
504 * only signifies the direction of the movement on top-level */
506 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
507 else TAILQ_INSERT_BEFORE(next, focused, nodes);
508 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
509 /* TODO: don’t influence focus handling? */
512 /* We need to call con_focus() to fix the focus stack "above" the container
513 * we just inserted the focused container into (otherwise, the parent
514 * container(s) would still point to the old container(s)). */
517 if (con_num_children(old_parent) == 0) {
518 DLOG("Old container empty after moving. Let's close it\n");
519 tree_close(old_parent, false, false);
526 * tree_flatten() removes pairs of redundant split containers, e.g.:
527 * [workspace, horizontal]
531 * In this example, the v-split and h-split container are redundant.
532 * Such a situation can be created by moving containers in a direction which is
533 * not the orientation of their parent container. i3 needs to create a new
534 * split container then and if you move containers this way multiple times,
535 * redundant chains of split-containers can be the result.
538 void tree_flatten(Con *con) {
539 Con *current, *child, *parent = con->parent;
540 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
542 /* We only consider normal containers without windows */
543 if (con->type != CT_CON || con->window != NULL)
546 /* Ensure it got only one child */
547 child = TAILQ_FIRST(&(con->nodes_head));
548 if (TAILQ_NEXT(child, nodes) != NULL)
551 /* The child must have a different orientation than the con but the same as
552 * the con’s parent to be redundant */
553 if (con->orientation == NO_ORIENTATION ||
554 child->orientation == NO_ORIENTATION ||
555 con->orientation == child->orientation ||
556 child->orientation != parent->orientation)
559 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
561 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
563 DLOG("detaching...\n");
564 /* 2: re-attach the children to the parent before con */
565 while (!TAILQ_EMPTY(&(child->nodes_head))) {
566 current = TAILQ_FIRST(&(child->nodes_head));
567 DLOG("detaching current=%p / %s\n", current, current->name);
569 DLOG("re-attaching\n");
570 /* We don’t use con_attach() here because for a CT_CON, the special
571 * case handling of con_attach() does not trigger. So all it would do
572 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
573 * directly use the TAILQ macros. */
574 current->parent = parent;
575 TAILQ_INSERT_BEFORE(con, current, nodes);
576 DLOG("attaching to focus list\n");
577 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
579 DLOG("re-attached all\n");
581 /* 3: restore focus, if con was focused */
582 if (focus_next != NULL &&
583 TAILQ_FIRST(&(parent->focus_head)) == con) {
584 DLOG("restoring focus to focus_next=%p\n", focus_next);
585 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
586 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
587 DLOG("restored focus.\n");
590 /* 4: close the redundant cons */
591 DLOG("closing redundant cons\n");
592 tree_close(con, false, true);
594 /* Well, we got to abort the recursion here because we destroyed the
595 * container. However, if tree_flatten() is called sufficiently often,
596 * there can’t be the situation of having two pairs of redundant containers
597 * at once. Therefore, we can safely abort the recursion on this level
598 * after flattening. */
602 /* We cannot use normal foreach here because tree_flatten might close the
603 * current container. */
604 current = TAILQ_FIRST(&(con->nodes_head));
605 while (current != NULL) {
606 Con *next = TAILQ_NEXT(current, nodes);
607 tree_flatten(current);
611 current = TAILQ_FIRST(&(con->floating_head));
612 while (current != NULL) {
613 Con *next = TAILQ_NEXT(current, floating_windows);
614 tree_flatten(current);