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, xcb_get_geometry_reply_t *geometry) {
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, NULL);
35 tree_append_json(globbed);
37 printf("appended tree, using new root\n");
38 croot = TAILQ_FIRST(&(croot->nodes_head));
39 printf("new root = %p\n", croot);
40 Con *out = TAILQ_FIRST(&(croot->nodes_head));
41 printf("out = %p\n", out);
42 Con *ws = TAILQ_FIRST(&(out->nodes_head));
43 printf("ws = %p\n", ws);
49 * Initializes the tree by creating the root node. The CT_OUTPUT Cons below the
50 * root node are created in randr.c for each Output.
53 void tree_init(xcb_get_geometry_reply_t *geometry) {
54 croot = con_new(NULL, NULL);
57 croot->type = CT_ROOT;
67 * Opens an empty container in the current container
70 Con *tree_open_con(Con *con, i3Window *window) {
72 /* every focusable Con has a parent (outputs have parent root) */
73 con = focused->parent;
74 /* If the parent is an output, we are on a workspace. In this case,
75 * the new container needs to be opened as a leaf of the workspace. */
76 if (con->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
80 /* If the currently focused container is a floating container, we
81 * attach the new container to the currently focused spot in its
83 if (con->type == CT_FLOATING_CON) {
84 con = con_descend_tiling_focused(con->parent);
85 if (con->type != CT_WORKSPACE)
88 DLOG("con = %p\n", con);
93 /* 3. create the container and attach it to its parent */
94 Con *new = con_new(con, window);
96 /* 4: re-calculate child->percent for each child */
102 static bool _is_con_mapped(Con *con) {
105 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
106 if (_is_con_mapped(child))
113 * Closes the given container including all children.
114 * Returns true if the container was killed or false if just WM_DELETE was sent
115 * and the window is expected to kill itself.
118 bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent) {
119 bool was_mapped = con->mapped;
120 Con *parent = con->parent;
123 /* Even if the container itself is not mapped, its children may be
124 * mapped (for example split containers don't have a mapped window on
125 * their own but usually contain mapped children). */
126 was_mapped = _is_con_mapped(con);
129 /* Get the container which is next focused */
130 Con *next = con_next_focused(con);
131 DLOG("next = %p, focused = %p\n", next, focused);
133 DLOG("closing %p, kill_window = %d\n", con, kill_window);
134 Con *child, *nextchild;
135 bool abort_kill = false;
136 /* We cannot use TAILQ_FOREACH because the children get deleted
137 * in their parent’s nodes_head */
138 for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) {
139 nextchild = TAILQ_NEXT(child, nodes);
140 DLOG("killing child=%p\n", child);
141 if (!tree_close(child, kill_window, true))
147 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
151 if (con->window != NULL) {
152 if (kill_window != DONT_KILL_WINDOW) {
153 x_window_kill(con->window->id, kill_window);
156 xcb_void_cookie_t cookie;
157 /* un-parent the window */
158 cookie = xcb_reparent_window(conn, con->window->id, root, 0, 0);
160 /* Ignore X11 errors for the ReparentWindow request.
161 * X11 Errors are returned when the window was already destroyed */
162 add_ignore_event(cookie.sequence, 0);
164 /* We are no longer handling this window, thus set WM_STATE to
165 * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
166 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
167 cookie = xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
168 con->window->id, A_WM_STATE, A_WM_STATE, 32, 2, data);
170 /* Ignore X11 errors for the ReparentWindow request.
171 * X11 Errors are returned when the window was already destroyed */
172 add_ignore_event(cookie.sequence, 0);
174 FREE(con->window->class_class);
175 FREE(con->window->class_instance);
176 FREE(con->window->name_x);
177 FREE(con->window->name_json);
181 /* kill the X11 part of this container */
185 if (con->type != CT_FLOATING_CON) {
186 /* If the container is *not* floating, we might need to re-distribute
187 * percentage values for the resized containers. */
188 con_fix_percent(parent);
191 if (con_is_floating(con)) {
192 Con *ws = con_get_workspace(con);
193 DLOG("Container was floating, killing floating container\n");
194 tree_close(parent, DONT_KILL_WINDOW, false);
195 DLOG("parent container killed\n");
196 if (con == focused) {
197 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
198 /* go down the focus stack as far as possible */
199 next = con_descend_focused(ws);
201 dont_kill_parent = true;
202 DLOG("Alright, focusing %p\n", next);
209 FREE(con->deco_render_params);
210 TAILQ_REMOVE(&all_cons, con, all_cons);
213 /* in the case of floating windows, we already focused another container
214 * when closing the parent, so we can exit now. */
216 DLOG("No next container, i will just exit now\n");
220 if (was_mapped || con == focused) {
221 if ((kill_window != DONT_KILL_WINDOW) || !dont_kill_parent || con == focused) {
222 DLOG("focusing %p / %s\n", next, next->name);
223 /* TODO: check if the container (or one of its children) was focused */
224 if (next->type == CT_DOCKAREA) {
225 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
226 con_focus(con_descend_focused(output_get_content(next->parent)));
232 DLOG("not focusing because we're not killing anybody");
235 DLOG("not focusing, was not mapped\n");
238 /* check if the parent container is empty now and close it */
239 if (!dont_kill_parent)
240 CALL(parent, on_remove_child);
246 * Closes the current container using tree_close().
249 void tree_close_con(kill_window_t kill_window) {
250 assert(focused != NULL);
251 if (focused->type == CT_WORKSPACE) {
252 LOG("Cannot close workspace\n");
256 /* There *should* be no possibility to focus outputs / root container */
257 assert(focused->type != CT_OUTPUT);
258 assert(focused->type != CT_ROOT);
261 tree_close(focused, kill_window, false);
265 * Splits (horizontally or vertically) the given container by creating a new
266 * container which contains the old one and the future ones.
269 void tree_split(Con *con, orientation_t orientation) {
270 /* for a workspace, we just need to change orientation */
271 if (con->type == CT_WORKSPACE) {
272 DLOG("Workspace, simply changing orientation to %d\n", orientation);
273 con->orientation = orientation;
277 Con *parent = con->parent;
278 /* if we are in a container whose parent contains only one
279 * child (its split functionality is unused so far), we just change the
280 * orientation (more intuitive than splitting again) */
281 if (con_num_children(parent) == 1) {
282 parent->orientation = orientation;
283 DLOG("Just changing orientation of existing container\n");
287 DLOG("Splitting in orientation %d\n", orientation);
289 /* 2: replace it with a new Con */
290 Con *new = con_new(NULL, NULL);
291 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
292 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
293 new->parent = parent;
294 new->orientation = orientation;
296 /* 3: swap 'percent' (resize factor) */
297 new->percent = con->percent;
300 /* 4: add it as a child to the new Con */
301 con_attach(con, new, false);
305 * Moves focus one level up.
309 /* We cannot go up when we are in fullscreen mode at the moment, that would
310 * be totally not intuitive */
311 if (focused->fullscreen_mode != CF_NONE) {
312 LOG("Currently in fullscreen, not going up\n");
315 /* We can focus up to the workspace, but not any higher in the tree */
316 if ((focused->parent->type != CT_CON &&
317 focused->parent->type != CT_WORKSPACE) ||
318 focused->type == CT_WORKSPACE) {
319 LOG("Cannot go up any further\n");
322 con_focus(focused->parent);
326 * Moves focus one level down.
330 /* Go down the focus stack of the current node */
331 Con *next = TAILQ_FIRST(&(focused->focus_head));
332 if (next == TAILQ_END(&(focused->focus_head))) {
333 printf("cannot go down\n");
339 static void mark_unmapped(Con *con) {
343 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
344 mark_unmapped(current);
345 if (con->type == CT_WORKSPACE) {
346 /* We need to call mark_unmapped on floating nodes aswell since we can
347 * make containers floating. */
348 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
349 mark_unmapped(current);
354 * Renders the tree, that is rendering all outputs using render_con() and
355 * pushing the changes to X11 using x_push_changes().
362 DLOG("-- BEGIN RENDERING --\n");
363 /* Reset map state for all nodes in tree */
364 /* TODO: a nicer method to walk all nodes would be good, maybe? */
365 mark_unmapped(croot);
366 croot->mapped = true;
368 render_con(croot, false);
370 x_push_changes(croot);
371 DLOG("-- END RENDERING --\n");
375 * Recursive function to walk the tree until a con can be found to focus.
378 static bool _tree_next(Con *con, char way, orientation_t orientation, bool wrap) {
379 /* Stop recursing at workspaces */
380 if (con->type == CT_WORKSPACE)
383 if (con->type == CT_FLOATING_CON) {
384 /* TODO: implement focus for floating windows */
388 Con *parent = con->parent;
390 /* If the orientation does not match or there is no other con to focus, we
391 * need to go higher in the hierarchy */
392 if (con_orientation(parent) != orientation ||
393 con_num_children(parent) == 1)
394 return _tree_next(parent, way, orientation, wrap);
396 Con *current = TAILQ_FIRST(&(parent->focus_head));
397 /* TODO: when can the following happen (except for floating windows, which
398 * are handled above)? */
399 if (TAILQ_EMPTY(&(parent->nodes_head))) {
400 DLOG("nothing to focus\n");
406 next = TAILQ_NEXT(current, nodes);
407 else next = TAILQ_PREV(current, nodes_head, nodes);
410 if (!config.force_focus_wrapping) {
411 /* If there is no next/previous container, we check if we can focus one
412 * when going higher (without wrapping, though). If so, we are done, if
414 if (_tree_next(parent, way, orientation, false))
422 next = TAILQ_FIRST(&(parent->nodes_head));
423 else next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
426 /* 3: focus choice comes in here. at the moment we will go down
427 * until we find a window */
428 /* TODO: check for window, atm we only go down as far as possible */
429 con_focus(con_descend_focused(next));
434 * Changes focus in the given way (next/previous) and given orientation
435 * (horizontal/vertical).
438 void tree_next(char way, orientation_t orientation) {
439 _tree_next(focused, way, orientation, true);
443 * tree_flatten() removes pairs of redundant split containers, e.g.:
444 * [workspace, horizontal]
448 * In this example, the v-split and h-split container are redundant.
449 * Such a situation can be created by moving containers in a direction which is
450 * not the orientation of their parent container. i3 needs to create a new
451 * split container then and if you move containers this way multiple times,
452 * redundant chains of split-containers can be the result.
455 void tree_flatten(Con *con) {
456 Con *current, *child, *parent = con->parent;
457 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
459 /* We only consider normal containers without windows */
460 if (con->type != CT_CON || con->window != NULL)
463 /* Ensure it got only one child */
464 child = TAILQ_FIRST(&(con->nodes_head));
465 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
468 /* The child must have a different orientation than the con but the same as
469 * the con’s parent to be redundant */
470 if (con->orientation == NO_ORIENTATION ||
471 child->orientation == NO_ORIENTATION ||
472 con->orientation == child->orientation ||
473 child->orientation != parent->orientation)
476 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
478 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
480 DLOG("detaching...\n");
481 /* 2: re-attach the children to the parent before con */
482 while (!TAILQ_EMPTY(&(child->nodes_head))) {
483 current = TAILQ_FIRST(&(child->nodes_head));
484 DLOG("detaching current=%p / %s\n", current, current->name);
486 DLOG("re-attaching\n");
487 /* We don’t use con_attach() here because for a CT_CON, the special
488 * case handling of con_attach() does not trigger. So all it would do
489 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
490 * directly use the TAILQ macros. */
491 current->parent = parent;
492 TAILQ_INSERT_BEFORE(con, current, nodes);
493 DLOG("attaching to focus list\n");
494 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
495 current->percent = con->percent;
497 DLOG("re-attached all\n");
499 /* 3: restore focus, if con was focused */
500 if (focus_next != NULL &&
501 TAILQ_FIRST(&(parent->focus_head)) == con) {
502 DLOG("restoring focus to focus_next=%p\n", focus_next);
503 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
504 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
505 DLOG("restored focus.\n");
508 /* 4: close the redundant cons */
509 DLOG("closing redundant cons\n");
510 tree_close(con, DONT_KILL_WINDOW, true);
512 /* Well, we got to abort the recursion here because we destroyed the
513 * container. However, if tree_flatten() is called sufficiently often,
514 * there can’t be the situation of having two pairs of redundant containers
515 * at once. Therefore, we can safely abort the recursion on this level
516 * after flattening. */
520 /* We cannot use normal foreach here because tree_flatten might close the
521 * current container. */
522 current = TAILQ_FIRST(&(con->nodes_head));
523 while (current != NULL) {
524 Con *next = TAILQ_NEXT(current, nodes);
525 tree_flatten(current);
529 current = TAILQ_FIRST(&(con->floating_head));
530 while (current != NULL) {
531 Con *next = TAILQ_NEXT(current, floating_windows);
532 tree_flatten(current);