2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * tree.c: Everything that primarily modifies the layout tree data structure.
15 struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
18 * Create the pseudo-output __i3. Output-independent workspaces such as
19 * __i3_scratch will live there.
22 static Con *_create___i3() {
23 Con *__i3 = con_new(croot, NULL);
25 __i3->name = sstrdup("__i3");
26 __i3->type = CT_OUTPUT;
27 __i3->layout = L_OUTPUT;
28 con_fix_percent(croot);
29 x_set_name(__i3, "[i3 con] pseudo-output __i3");
30 /* For retaining the correct position/size of a scratchpad window, the
31 * dimensions of the real outputs should be multiples of the __i3
33 __i3->rect.width = 1280;
34 __i3->rect.height = 1024;
36 /* Add a content container. */
37 DLOG("adding main content container\n");
38 Con *content = con_new(NULL, NULL);
39 content->type = CT_CON;
41 content->name = sstrdup("content");
43 x_set_name(content, "[i3 con] content __i3");
44 con_attach(content, __i3, false);
46 /* Attach the __i3_scratch workspace. */
47 Con *ws = con_new(NULL, NULL);
48 ws->type = CT_WORKSPACE;
50 ws->name = sstrdup("__i3_scratch");
51 con_attach(ws, content, false);
52 x_set_name(ws, "[i3 con] workspace __i3_scratch");
53 ws->fullscreen_mode = CF_OUTPUT;
59 * Loads tree from 'path' (used for in-place restarts).
62 bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry) {
63 char *globbed = resolve_tilde(path);
65 if (!path_exists(globbed)) {
66 LOG("%s does not exist, not restoring tree\n", globbed);
71 /* TODO: refactor the following */
72 croot = con_new(NULL, NULL);
81 tree_append_json(globbed);
83 printf("appended tree, using new root\n");
84 croot = TAILQ_FIRST(&(croot->nodes_head));
85 printf("new root = %p\n", croot);
86 Con *out = TAILQ_FIRST(&(croot->nodes_head));
87 printf("out = %p\n", out);
88 Con *ws = TAILQ_FIRST(&(out->nodes_head));
89 printf("ws = %p\n", ws);
91 /* For in-place restarting into v4.2, we need to make sure the new
92 * pseudo-output __i3 is present. */
93 if (strcmp(out->name, "__i3") != 0) {
94 DLOG("Adding pseudo-output __i3 during inplace restart\n");
95 Con *__i3 = _create___i3();
96 /* Ensure that it is the first output, other places in the code make
98 TAILQ_REMOVE(&(croot->nodes_head), __i3, nodes);
99 TAILQ_INSERT_HEAD(&(croot->nodes_head), __i3, nodes);
106 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
107 * root node are created in randr.c for each Output.
110 void tree_init(xcb_get_geometry_reply_t *geometry) {
111 croot = con_new(NULL, NULL);
113 croot->name = "root";
114 croot->type = CT_ROOT;
115 croot->rect = (Rect){
126 * Opens an empty container in the current container
129 Con *tree_open_con(Con *con, i3Window *window) {
131 /* every focusable Con has a parent (outputs have parent root) */
132 con = focused->parent;
133 /* If the parent is an output, we are on a workspace. In this case,
134 * the new container needs to be opened as a leaf of the workspace. */
135 if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
139 /* If the currently focused container is a floating container, we
140 * attach the new container to the currently focused spot in its
142 if (con->type == CT_FLOATING_CON) {
143 con = con_descend_tiling_focused(con->parent);
144 if (con->type != CT_WORKSPACE)
147 DLOG("con = %p\n", con);
152 /* 3. create the container and attach it to its parent */
153 Con *new = con_new(con, window);
155 /* 4: re-calculate child->percent for each child */
156 con_fix_percent(con);
161 static bool _is_con_mapped(Con *con) {
164 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
165 if (_is_con_mapped(child))
172 * Closes the given container including all children.
173 * Returns true if the container was killed or false if just WM_DELETE was sent
174 * and the window is expected to kill itself.
176 * The dont_kill_parent flag is specified when the function calls itself
177 * recursively while deleting a containers children.
179 * The force_set_focus flag is specified in the case of killing a floating
180 * window: tree_close() will be invoked for the CT_FLOATINGCON (the parent
181 * container) and focus should be set there.
184 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus) {
185 bool was_mapped = con->mapped;
186 Con *parent = con->parent;
189 /* Even if the container itself is not mapped, its children may be
190 * mapped (for example split containers don't have a mapped window on
191 * their own but usually contain mapped children). */
192 was_mapped = _is_con_mapped(con);
195 /* Get the container which is next focused */
196 Con *next = con_next_focused(con);
197 DLOG("next = %p, focused = %p\n", next, focused);
199 DLOG("closing %p, kill_window = %d\n", con, kill_window);
200 Con *child, *nextchild;
201 bool abort_kill = false;
202 /* We cannot use TAILQ_FOREACH because the children get deleted
203 * in their parent’s nodes_head */
204 for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) {
205 nextchild = TAILQ_NEXT(child, nodes);
206 DLOG("killing child=%p\n", child);
207 if (!tree_close(child, kill_window, true, false))
213 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
217 if (con->window != NULL) {
218 if (kill_window != DONT_KILL_WINDOW) {
219 x_window_kill(con->window->id, kill_window);
222 xcb_void_cookie_t cookie;
223 /* un-parent the window */
224 cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
226 /* Ignore X11 errors for the ReparentWindow request.
227 * X11 Errors are returned when the window was already destroyed */
228 add_ignore_event(cookie.sequence, 0);
230 /* We are no longer handling this window, thus set WM_STATE to
231 * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
232 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
233 cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
234 con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
236 /* Ignore X11 errors for the ReparentWindow request.
237 * X11 Errors are returned when the window was already destroyed */
238 add_ignore_event(cookie.sequence, 0);
240 FREE(con->window->class_class);
241 FREE(con->window->class_instance);
242 FREE(con->window->name_x);
243 FREE(con->window->name_json);
247 /* kill the X11 part of this container */
251 if (con->type != CT_FLOATING_CON) {
252 /* If the container is *not* floating, we might need to re-distribute
253 * percentage values for the resized containers. */
254 con_fix_percent(parent);
257 if (con_is_floating(con)) {
258 Con *ws = con_get_workspace(con);
259 DLOG("Container was floating, killing floating container\n");
260 tree_close(parent, DONT_KILL_WINDOW, false, (con == focused));
261 DLOG("parent container killed\n");
262 if (con == focused) {
263 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
264 /* go down the focus stack as far as possible */
265 next = con_descend_focused(ws);
267 dont_kill_parent = true;
268 DLOG("Alright, focusing %p\n", next);
275 FREE(con->deco_render_params);
276 TAILQ_REMOVE(&all_cons, con, all_cons);
279 /* in the case of floating windows, we already focused another container
280 * when closing the parent, so we can exit now. */
282 DLOG("No next container, i will just exit now\n");
286 if (was_mapped || con == focused) {
287 if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
288 DLOG("focusing %p / %s\n", next, next->name);
289 if (next->type == CT_DOCKAREA) {
290 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
291 con_focus(con_descend_focused(output_get_content(next->parent)));
293 if (!force_set_focus && con != focused)
294 DLOG("not changing focus, the container was not focused before\n");
295 else con_focus(next);
299 DLOG("not focusing because we're not killing anybody");
302 DLOG("not focusing, was not mapped\n");
305 /* check if the parent container is empty now and close it */
306 if (!dont_kill_parent)
307 CALL(parent, on_remove_child);
313 * Closes the current container using tree_close().
316 void tree_close_con(kill_window_t kill_window) {
317 assert(focused != NULL);
318 if (focused->type == CT_WORKSPACE) {
319 LOG("Cannot close workspace\n");
323 /* There *should* be no possibility to focus outputs / root container */
324 assert(focused->type != CT_OUTPUT);
325 assert(focused->type != CT_ROOT);
328 tree_close(focused, kill_window, false, false);
332 * Splits (horizontally or vertically) the given container by creating a new
333 * container which contains the old one and the future ones.
336 void tree_split(Con *con, orientation_t orientation) {
337 /* for a workspace, we just need to change orientation */
338 if (con->type == CT_WORKSPACE) {
339 DLOG("Workspace, simply changing orientation to %d\n", orientation);
340 con->orientation = orientation;
344 Con *parent = con->parent;
345 /* if we are in a container whose parent contains only one
346 * child (its split functionality is unused so far), we just change the
347 * orientation (more intuitive than splitting again) */
348 if (con_num_children(parent) == 1) {
349 parent->orientation = orientation;
350 FREE(con->deco_render_params);
351 FREE(parent->deco_render_params);
352 DLOG("Just changing orientation of existing container\n");
356 DLOG("Splitting in orientation %d\n", orientation);
358 /* 2: replace it with a new Con */
359 Con *new = con_new(NULL, NULL);
360 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
361 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
362 new->parent = parent;
363 new->orientation = orientation;
365 /* 3: swap 'percent' (resize factor) */
366 new->percent = con->percent;
369 /* 4: add it as a child to the new Con */
370 con_attach(con, new, false);
374 * Moves focus one level up.
378 /* We cannot go up when we are in fullscreen mode at the moment, that would
379 * be totally not intuitive */
380 if (focused->fullscreen_mode != CF_NONE) {
381 LOG("Currently in fullscreen, not going up\n");
384 /* We can focus up to the workspace, but not any higher in the tree */
385 if ((focused->parent->type != CT_CON &&
386 focused->parent->type != CT_WORKSPACE) ||
387 focused->type == CT_WORKSPACE) {
388 LOG("Cannot go up any further\n");
391 con_focus(focused->parent);
395 * Moves focus one level down.
399 /* Go down the focus stack of the current node */
400 Con *next = TAILQ_FIRST(&(focused->focus_head));
401 if (next == TAILQ_END(&(focused->focus_head))) {
402 printf("cannot go down\n");
408 static void mark_unmapped(Con *con) {
412 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
413 mark_unmapped(current);
414 if (con->type == CT_WORKSPACE) {
415 /* We need to call mark_unmapped on floating nodes aswell since we can
416 * make containers floating. */
417 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
418 mark_unmapped(current);
423 * Renders the tree, that is rendering all outputs using render_con() and
424 * pushing the changes to X11 using x_push_changes().
431 DLOG("-- BEGIN RENDERING --\n");
432 /* Reset map state for all nodes in tree */
433 /* TODO: a nicer method to walk all nodes would be good, maybe? */
434 mark_unmapped(croot);
435 croot->mapped = true;
437 render_con(croot, false);
439 x_push_changes(croot);
440 DLOG("-- END RENDERING --\n");
444 * Recursive function to walk the tree until a con can be found to focus.
447 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
448 /* Stop recursing at workspaces after attempting to switch to next
449 * workspace if possible. */
450 if (con->type == CT_WORKSPACE) {
451 Output *current_output = get_output_containing(con->rect.x, con->rect.y);
456 DLOG("Current output is %s\n", current_output->name);
458 /* Try to find next output */
459 direction_t direction;
460 if (way == 'n' && orientation == HORIZ)
462 else if (way == 'p' && orientation == HORIZ)
464 else if (way == 'n' && orientation == VERT)
466 else if (way == 'p' && orientation == VERT)
471 next_output = get_output_next(direction, current_output);
474 DLOG("Next output is %s\n", next_output->name);
476 /* Find visible workspace on next output */
477 Con *workspace = NULL;
478 GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
480 /* Show next workspace and focus appropriate container if possible. */
484 workspace_show(workspace);
485 Con *focus = con_descend_direction(workspace, direction);
488 x_set_warp_to(&(focus->rect));
493 Con *parent = con->parent;
495 if (con->type == CT_FLOATING_CON) {
496 /* left/right focuses the previous/next floating container */
497 if (orientation == HORIZ) {
500 next = TAILQ_NEXT(con, floating_windows);
501 else next = TAILQ_PREV(con, floating_head, floating_windows);
503 /* If there is no next/previous container, wrap */
506 next = TAILQ_FIRST(&(parent->floating_head));
507 else next = TAILQ_LAST(&(parent->floating_head), floating_head);
510 /* Still no next/previous container? bail out */
514 con_focus(con_descend_focused(next));
517 /* up/down cycles through the Z-index */
518 /* TODO: implement cycling through the z-index */
523 /* If the orientation does not match or there is no other con to focus, we
524 * need to go higher in the hierarchy */
525 if (con_orientation(parent) != orientation ||
526 con_num_children(parent) == 1)
527 return _tree_next(parent, way, orientation, wrap);
529 Con *current = TAILQ_FIRST(&(parent->focus_head));
530 /* TODO: when can the following happen (except for floating windows, which
531 * are handled above)? */
532 if (TAILQ_EMPTY(&(parent->nodes_head))) {
533 DLOG("nothing to focus\n");
539 next = TAILQ_NEXT(current, nodes);
540 else next = TAILQ_PREV(current, nodes_head, nodes);
543 if (!config.force_focus_wrapping) {
544 /* If there is no next/previous container, we check if we can focus one
545 * when going higher (without wrapping, though). If so, we are done, if
547 if (_tree_next(parent, way, orientation, false))
555 next = TAILQ_FIRST(&(parent->nodes_head));
556 else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
559 /* 3: focus choice comes in here. at the moment we will go down
560 * until we find a window */
561 /* TODO: check for window, atm we only go down as far as possible */
562 con_focus(con_descend_focused(next));
567 * Changes focus in the given way (next/previous) and given orientation
568 * (horizontal/vertical).
571 void tree_next(char way, orientation_t orientation) {
572 _tree_next(focused, way, orientation, true);
576 * tree_flatten() removes pairs of redundant split containers, e.g.:
577 * [workspace, horizontal]
581 * In this example, the v-split and h-split container are redundant.
582 * Such a situation can be created by moving containers in a direction which is
583 * not the orientation of their parent container. i3 needs to create a new
584 * split container then and if you move containers this way multiple times,
585 * redundant chains of split-containers can be the result.
588 void tree_flatten(Con *con) {
589 Con *current, *child, *parent = con->parent;
590 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
592 /* We only consider normal containers without windows */
593 if (con->type != CT_CON || con->window != NULL)
596 /* Ensure it got only one child */
597 child = TAILQ_FIRST(&(con->nodes_head));
598 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
601 /* The child must have a different orientation than the con but the same as
602 * the con’s parent to be redundant */
603 if (con->orientation == NO_ORIENTATION ||
604 child->orientation == NO_ORIENTATION ||
605 con->orientation == child->orientation ||
606 child->orientation != parent->orientation)
609 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
611 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
613 DLOG("detaching...\n");
614 /* 2: re-attach the children to the parent before con */
615 while (!TAILQ_EMPTY(&(child->nodes_head))) {
616 current = TAILQ_FIRST(&(child->nodes_head));
617 DLOG("detaching current=%p / %s\n", current, current->name);
619 DLOG("re-attaching\n");
620 /* We don’t use con_attach() here because for a CT_CON, the special
621 * case handling of con_attach() does not trigger. So all it would do
622 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
623 * directly use the TAILQ macros. */
624 current->parent = parent;
625 TAILQ_INSERT_BEFORE(con, current, nodes);
626 DLOG("attaching to focus list\n");
627 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
628 current->percent = con->percent;
630 DLOG("re-attached all\n");
632 /* 3: restore focus, if con was focused */
633 if (focus_next != NULL &&
634 TAILQ_FIRST(&(parent->focus_head)) == con) {
635 DLOG("restoring focus to focus_next=%p\n", focus_next);
636 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
637 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
638 DLOG("restored focus.\n");
641 /* 4: close the redundant cons */
642 DLOG("closing redundant cons\n");
643 tree_close(con, DONT_KILL_WINDOW, true, false);
645 /* Well, we got to abort the recursion here because we destroyed the
646 * container. However, if tree_flatten() is called sufficiently often,
647 * there can’t be the situation of having two pairs of redundant containers
648 * at once. Therefore, we can safely abort the recursion on this level
649 * after flattening. */
653 /* We cannot use normal foreach here because tree_flatten might close the
654 * current container. */
655 current = TAILQ_FIRST(&(con->nodes_head));
656 while (current != NULL) {
657 Con *next = TAILQ_NEXT(current, nodes);
658 tree_flatten(current);
662 current = TAILQ_FIRST(&(con->floating_head));
663 while (current != NULL) {
664 Con *next = TAILQ_NEXT(current, floating_windows);
665 tree_flatten(current);