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(void) {
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;
346 /* Force re-rendering to make the indicator border visible. */
347 FREE(con->deco_render_params);
348 FREE(parent->deco_render_params);
350 /* if we are in a container whose parent contains only one
351 * child (its split functionality is unused so far), we just change the
352 * orientation (more intuitive than splitting again) */
353 if (con_num_children(parent) == 1 &&
354 parent->layout == L_DEFAULT) {
355 parent->orientation = orientation;
356 DLOG("Just changing orientation of existing container\n");
360 DLOG("Splitting in orientation %d\n", orientation);
362 /* 2: replace it with a new Con */
363 Con *new = con_new(NULL, NULL);
364 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
365 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
366 new->parent = parent;
367 new->orientation = orientation;
369 /* 3: swap 'percent' (resize factor) */
370 new->percent = con->percent;
373 /* 4: add it as a child to the new Con */
374 con_attach(con, new, false);
378 * Moves focus one level up.
381 void level_up(void) {
382 /* We cannot go up when we are in fullscreen mode at the moment, that would
383 * be totally not intuitive */
384 if (focused->fullscreen_mode != CF_NONE) {
385 LOG("Currently in fullscreen, not going up\n");
388 /* We can focus up to the workspace, but not any higher in the tree */
389 if ((focused->parent->type != CT_CON &&
390 focused->parent->type != CT_WORKSPACE) ||
391 focused->type == CT_WORKSPACE) {
392 LOG("Cannot go up any further\n");
395 con_focus(focused->parent);
399 * Moves focus one level down.
402 void level_down(void) {
403 /* Go down the focus stack of the current node */
404 Con *next = TAILQ_FIRST(&(focused->focus_head));
405 if (next == TAILQ_END(&(focused->focus_head))) {
406 printf("cannot go down\n");
412 static void mark_unmapped(Con *con) {
416 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
417 mark_unmapped(current);
418 if (con->type == CT_WORKSPACE) {
419 /* We need to call mark_unmapped on floating nodes aswell since we can
420 * make containers floating. */
421 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
422 mark_unmapped(current);
427 * Renders the tree, that is rendering all outputs using render_con() and
428 * pushing the changes to X11 using x_push_changes().
431 void tree_render(void) {
435 DLOG("-- BEGIN RENDERING --\n");
436 /* Reset map state for all nodes in tree */
437 /* TODO: a nicer method to walk all nodes would be good, maybe? */
438 mark_unmapped(croot);
439 croot->mapped = true;
441 render_con(croot, false);
443 x_push_changes(croot);
444 DLOG("-- END RENDERING --\n");
448 * Recursive function to walk the tree until a con can be found to focus.
451 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
452 /* Stop recursing at workspaces after attempting to switch to next
453 * workspace if possible. */
454 if (con->type == CT_WORKSPACE) {
455 Output *current_output = get_output_containing(con->rect.x, con->rect.y);
460 DLOG("Current output is %s\n", current_output->name);
462 /* Try to find next output */
463 direction_t direction;
464 if (way == 'n' && orientation == HORIZ)
466 else if (way == 'p' && orientation == HORIZ)
468 else if (way == 'n' && orientation == VERT)
470 else if (way == 'p' && orientation == VERT)
475 next_output = get_output_next(direction, current_output);
478 DLOG("Next output is %s\n", next_output->name);
480 /* Find visible workspace on next output */
481 Con *workspace = NULL;
482 GREP_FIRST(workspace, output_get_content(next_output->con), workspace_is_visible(child));
484 /* Show next workspace and focus appropriate container if possible. */
488 workspace_show(workspace);
489 Con *focus = con_descend_direction(workspace, direction);
492 x_set_warp_to(&(focus->rect));
497 Con *parent = con->parent;
499 if (con->type == CT_FLOATING_CON) {
500 /* left/right focuses the previous/next floating container */
501 if (orientation == HORIZ) {
504 next = TAILQ_NEXT(con, floating_windows);
505 else next = TAILQ_PREV(con, floating_head, floating_windows);
507 /* If there is no next/previous container, wrap */
510 next = TAILQ_FIRST(&(parent->floating_head));
511 else next = TAILQ_LAST(&(parent->floating_head), floating_head);
514 /* Still no next/previous container? bail out */
518 con_focus(con_descend_focused(next));
521 /* up/down cycles through the Z-index */
522 /* TODO: implement cycling through the z-index */
527 /* If the orientation does not match or there is no other con to focus, we
528 * need to go higher in the hierarchy */
529 if (con_orientation(parent) != orientation ||
530 con_num_children(parent) == 1)
531 return _tree_next(parent, way, orientation, wrap);
533 Con *current = TAILQ_FIRST(&(parent->focus_head));
534 /* TODO: when can the following happen (except for floating windows, which
535 * are handled above)? */
536 if (TAILQ_EMPTY(&(parent->nodes_head))) {
537 DLOG("nothing to focus\n");
543 next = TAILQ_NEXT(current, nodes);
544 else next = TAILQ_PREV(current, nodes_head, nodes);
547 if (!config.force_focus_wrapping) {
548 /* If there is no next/previous container, we check if we can focus one
549 * when going higher (without wrapping, though). If so, we are done, if
551 if (_tree_next(parent, way, orientation, false))
559 next = TAILQ_FIRST(&(parent->nodes_head));
560 else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
563 /* 3: focus choice comes in here. at the moment we will go down
564 * until we find a window */
565 /* TODO: check for window, atm we only go down as far as possible */
566 con_focus(con_descend_focused(next));
571 * Changes focus in the given way (next/previous) and given orientation
572 * (horizontal/vertical).
575 void tree_next(char way, orientation_t orientation) {
576 _tree_next(focused, way, orientation, true);
580 * tree_flatten() removes pairs of redundant split containers, e.g.:
581 * [workspace, horizontal]
585 * In this example, the v-split and h-split container are redundant.
586 * Such a situation can be created by moving containers in a direction which is
587 * not the orientation of their parent container. i3 needs to create a new
588 * split container then and if you move containers this way multiple times,
589 * redundant chains of split-containers can be the result.
592 void tree_flatten(Con *con) {
593 Con *current, *child, *parent = con->parent;
594 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
596 /* We only consider normal containers without windows */
597 if (con->type != CT_CON || con->window != NULL)
600 /* Ensure it got only one child */
601 child = TAILQ_FIRST(&(con->nodes_head));
602 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
605 /* The child must have a different orientation than the con but the same as
606 * the con’s parent to be redundant */
607 if (con->orientation == NO_ORIENTATION ||
608 child->orientation == NO_ORIENTATION ||
609 con->orientation == child->orientation ||
610 child->orientation != parent->orientation)
613 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
615 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
617 DLOG("detaching...\n");
618 /* 2: re-attach the children to the parent before con */
619 while (!TAILQ_EMPTY(&(child->nodes_head))) {
620 current = TAILQ_FIRST(&(child->nodes_head));
621 DLOG("detaching current=%p / %s\n", current, current->name);
623 DLOG("re-attaching\n");
624 /* We don’t use con_attach() here because for a CT_CON, the special
625 * case handling of con_attach() does not trigger. So all it would do
626 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
627 * directly use the TAILQ macros. */
628 current->parent = parent;
629 TAILQ_INSERT_BEFORE(con, current, nodes);
630 DLOG("attaching to focus list\n");
631 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
632 current->percent = con->percent;
634 DLOG("re-attached all\n");
636 /* 3: restore focus, if con was focused */
637 if (focus_next != NULL &&
638 TAILQ_FIRST(&(parent->focus_head)) == con) {
639 DLOG("restoring focus to focus_next=%p\n", focus_next);
640 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
641 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
642 DLOG("restored focus.\n");
645 /* 4: close the redundant cons */
646 DLOG("closing redundant cons\n");
647 tree_close(con, DONT_KILL_WINDOW, true, false);
649 /* Well, we got to abort the recursion here because we destroyed the
650 * container. However, if tree_flatten() is called sufficiently often,
651 * there can’t be the situation of having two pairs of redundant containers
652 * at once. Therefore, we can safely abort the recursion on this level
653 * after flattening. */
657 /* We cannot use normal foreach here because tree_flatten might close the
658 * current container. */
659 current = TAILQ_FIRST(&(con->nodes_head));
660 while (current != NULL) {
661 Con *next = TAILQ_NEXT(current, nodes);
662 tree_flatten(current);
666 current = TAILQ_FIRST(&(con->floating_head));
667 while (current != NULL) {
668 Con *next = TAILQ_NEXT(current, floating_windows);
669 tree_flatten(current);