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->parent->type == CT_OUTPUT && con->type != CT_DOCKAREA) {
68 /* If the currently focused container is a floating container, we
69 * attach the new container to the workspace */
70 if (con->type == CT_FLOATING_CON)
72 DLOG("con = %p\n", con);
77 /* 3. create the container and attach it to its parent */
78 Con *new = con_new(con);
80 /* 4: re-calculate child->percent for each child */
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.
98 * Returns true if the container was killed or false if just WM_DELETE was sent
99 * and the window is expected to kill itself.
102 bool tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
103 bool was_mapped = con->mapped;
104 Con *parent = con->parent;
107 /* Even if the container itself is not mapped, its children may be
108 * mapped (for example split containers don't have a mapped window on
109 * their own but usually contain mapped children). */
110 was_mapped = _is_con_mapped(con);
113 /* Get the container which is next focused */
114 Con *next = con_next_focused(con);
115 DLOG("next = %p, focused = %p\n", next, focused);
117 DLOG("closing %p, kill_window = %d\n", con, kill_window);
118 Con *child, *nextchild;
119 bool abort_kill = false;
120 /* We cannot use TAILQ_FOREACH because the children get deleted
121 * in their parent’s nodes_head */
122 for (child = TAILQ_FIRST(&(con->nodes_head)); child; ) {
123 nextchild = TAILQ_NEXT(child, nodes);
124 DLOG("killing child=%p\n", child);
125 if (!tree_close(child, kill_window, true))
131 DLOG("One of the children could not be killed immediately (WM_DELETE sent), aborting.\n");
135 if (con->window != NULL) {
137 x_window_kill(con->window->id);
140 /* un-parent the window */
141 xcb_reparent_window(conn, con->window->id, root, 0, 0);
142 /* We are no longer handling this window, thus set WM_STATE to
143 * WM_STATE_WITHDRAWN (see ICCCM 4.1.3.1) */
144 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
145 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
146 A_WM_STATE, A_WM_STATE, 32, 2, data);
148 FREE(con->window->class_class);
149 FREE(con->window->class_instance);
150 FREE(con->window->name_x);
151 FREE(con->window->name_json);
155 /* kill the X11 part of this container */
159 if (con->type != CT_FLOATING_CON) {
160 /* If the container is *not* floating, we might need to re-distribute
161 * percentage values for the resized containers. */
162 con_fix_percent(parent);
165 if (con_is_floating(con)) {
166 Con *ws = con_get_workspace(con);
167 DLOG("Container was floating, killing floating container\n");
168 tree_close(parent, false, false);
169 DLOG("parent container killed\n");
170 if (con == focused) {
171 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
172 /* go down the focus stack as far as possible */
173 next = con_descend_focused(ws);
175 dont_kill_parent = true;
176 DLOG("Alright, focusing %p\n", next);
183 FREE(con->deco_render_params);
184 TAILQ_REMOVE(&all_cons, con, all_cons);
187 /* in the case of floating windows, we already focused another container
188 * when closing the parent, so we can exit now. */
190 DLOG("No next container, i will just exit now\n");
194 if (was_mapped || con == focused) {
195 if (kill_window || !dont_kill_parent || con == focused) {
196 DLOG("focusing %p / %s\n", next, next->name);
197 /* TODO: check if the container (or one of its children) was focused */
198 if (next->type == CT_DOCKAREA) {
199 /* Instead of focusing the dockarea, we need to restore focus to the workspace */
200 con_focus(con_descend_focused(output_get_content(next->parent)));
206 DLOG("not focusing because we're not killing anybody");
209 DLOG("not focusing, was not mapped\n");
212 /* check if the parent container is empty now and close it */
213 if (!dont_kill_parent)
214 CALL(parent, on_remove_child);
220 * Closes the current container using tree_close().
223 void tree_close_con() {
224 assert(focused != NULL);
225 if (focused->type == CT_WORKSPACE) {
226 LOG("Cannot close workspace\n");
230 /* There *should* be no possibility to focus outputs / root container */
231 assert(focused->type != CT_OUTPUT);
232 assert(focused->type != CT_ROOT);
235 tree_close(focused, true, false);
239 * Splits (horizontally or vertically) the given container by creating a new
240 * container which contains the old one and the future ones.
243 void tree_split(Con *con, orientation_t orientation) {
244 /* for a workspace, we just need to change orientation */
245 if (con->type == CT_WORKSPACE) {
246 DLOG("Workspace, simply changing orientation to %d\n", orientation);
247 con->orientation = orientation;
251 Con *parent = con->parent;
252 /* if we are in a container whose parent contains only one
253 * child (its split functionality is unused so far), we just change the
254 * orientation (more intuitive than splitting again) */
255 if (con_num_children(parent) == 1) {
256 parent->orientation = orientation;
257 DLOG("Just changing orientation of existing container\n");
261 DLOG("Splitting in orientation %d\n", orientation);
263 /* 2: replace it with a new Con */
264 Con *new = con_new(NULL);
265 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
266 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
267 new->parent = parent;
268 new->orientation = orientation;
270 /* 3: swap 'percent' (resize factor) */
271 new->percent = con->percent;
274 /* 4: add it as a child to the new Con */
275 con_attach(con, new, false);
279 * Moves focus one level up.
283 /* We cannot go up when we are in fullscreen mode at the moment, that would
284 * be totally not intuitive */
285 if (focused->fullscreen_mode != CF_NONE) {
286 LOG("Currently in fullscreen, not going up\n");
289 /* We can focus up to the workspace, but not any higher in the tree */
290 if ((focused->parent->type != CT_CON &&
291 focused->parent->type != CT_WORKSPACE) ||
292 focused->type == CT_WORKSPACE) {
293 LOG("Cannot go up any further\n");
296 con_focus(focused->parent);
300 * Moves focus one level down.
304 /* Go down the focus stack of the current node */
305 Con *next = TAILQ_FIRST(&(focused->focus_head));
306 if (next == TAILQ_END(&(focused->focus_head))) {
307 printf("cannot go down\n");
313 static void mark_unmapped(Con *con) {
317 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
318 mark_unmapped(current);
319 if (con->type == CT_WORKSPACE) {
320 /* We need to call mark_unmapped on floating nodes aswell since we can
321 * make containers floating. */
322 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
323 mark_unmapped(current);
328 * Renders the tree, that is rendering all outputs using render_con() and
329 * pushing the changes to X11 using x_push_changes().
336 DLOG("-- BEGIN RENDERING --\n");
337 /* Reset map state for all nodes in tree */
338 /* TODO: a nicer method to walk all nodes would be good, maybe? */
339 mark_unmapped(croot);
340 croot->mapped = true;
342 /* We start rendering at an output */
344 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
345 DLOG("output %p / %s\n", output, output->name);
346 render_con(output, false);
348 x_push_changes(croot);
349 DLOG("-- END RENDERING --\n");
353 * Changes focus in the given way (next/previous) and given orientation
354 * (horizontal/vertical).
357 void tree_next(char way, orientation_t orientation) {
358 /* 1: get the first parent with the same orientation */
359 Con *parent = focused->parent;
360 while (focused->type != CT_WORKSPACE &&
361 (con_orientation(parent) != orientation ||
362 con_num_children(parent) == 1)) {
363 LOG("need to go one level further up\n");
364 /* if the current parent is an output, we are at a workspace
365 * and the orientation still does not match */
366 if (parent->type == CT_WORKSPACE)
368 parent = parent->parent;
370 Con *current = TAILQ_FIRST(&(parent->focus_head));
371 assert(current != TAILQ_END(&(parent->focus_head)));
373 if (TAILQ_EMPTY(&(parent->nodes_head))) {
374 DLOG("Nothing to focus here, move along...\n");
378 /* 2: chose next (or previous) */
381 next = TAILQ_NEXT(current, nodes);
382 /* if we are at the end of the list, we need to wrap */
383 if (next == TAILQ_END(&(parent->nodes_head)))
384 next = TAILQ_FIRST(&(parent->nodes_head));
386 next = TAILQ_PREV(current, nodes_head, nodes);
387 /* if we are at the end of the list, we need to wrap */
388 if (next == TAILQ_END(&(parent->nodes_head)))
389 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
392 /* 3: focus choice comes in here. at the moment we will go down
393 * until we find a window */
394 /* TODO: check for window, atm we only go down as far as possible */
395 con_focus(con_descend_focused(next));
399 * tree_flatten() removes pairs of redundant split containers, e.g.:
400 * [workspace, horizontal]
404 * In this example, the v-split and h-split container are redundant.
405 * Such a situation can be created by moving containers in a direction which is
406 * not the orientation of their parent container. i3 needs to create a new
407 * split container then and if you move containers this way multiple times,
408 * redundant chains of split-containers can be the result.
411 void tree_flatten(Con *con) {
412 Con *current, *child, *parent = con->parent;
413 DLOG("Checking if I can flatten con = %p / %s\n", con, con->name);
415 /* We only consider normal containers without windows */
416 if (con->type != CT_CON || con->window != NULL)
419 /* Ensure it got only one child */
420 child = TAILQ_FIRST(&(con->nodes_head));
421 if (child == NULL || TAILQ_NEXT(child, nodes) != NULL)
424 /* The child must have a different orientation than the con but the same as
425 * the con’s parent to be redundant */
426 if (con->orientation == NO_ORIENTATION ||
427 child->orientation == NO_ORIENTATION ||
428 con->orientation == child->orientation ||
429 child->orientation != parent->orientation)
432 DLOG("Alright, I have to flatten this situation now. Stay calm.\n");
434 Con *focus_next = TAILQ_FIRST(&(child->focus_head));
436 DLOG("detaching...\n");
437 /* 2: re-attach the children to the parent before con */
438 while (!TAILQ_EMPTY(&(child->nodes_head))) {
439 current = TAILQ_FIRST(&(child->nodes_head));
440 DLOG("detaching current=%p / %s\n", current, current->name);
442 DLOG("re-attaching\n");
443 /* We don’t use con_attach() here because for a CT_CON, the special
444 * case handling of con_attach() does not trigger. So all it would do
445 * is calling TAILQ_INSERT_AFTER, but with the wrong container. So we
446 * directly use the TAILQ macros. */
447 current->parent = parent;
448 TAILQ_INSERT_BEFORE(con, current, nodes);
449 DLOG("attaching to focus list\n");
450 TAILQ_INSERT_TAIL(&(parent->focus_head), current, focused);
451 current->percent = con->percent;
453 DLOG("re-attached all\n");
455 /* 3: restore focus, if con was focused */
456 if (focus_next != NULL &&
457 TAILQ_FIRST(&(parent->focus_head)) == con) {
458 DLOG("restoring focus to focus_next=%p\n", focus_next);
459 TAILQ_REMOVE(&(parent->focus_head), focus_next, focused);
460 TAILQ_INSERT_HEAD(&(parent->focus_head), focus_next, focused);
461 DLOG("restored focus.\n");
464 /* 4: close the redundant cons */
465 DLOG("closing redundant cons\n");
466 tree_close(con, false, true);
468 /* Well, we got to abort the recursion here because we destroyed the
469 * container. However, if tree_flatten() is called sufficiently often,
470 * there can’t be the situation of having two pairs of redundant containers
471 * at once. Therefore, we can safely abort the recursion on this level
472 * after flattening. */
476 /* We cannot use normal foreach here because tree_flatten might close the
477 * current container. */
478 current = TAILQ_FIRST(&(con->nodes_head));
479 while (current != NULL) {
480 Con *next = TAILQ_NEXT(current, nodes);
481 tree_flatten(current);
485 current = TAILQ_FIRST(&(con->floating_head));
486 while (current != NULL) {
487 Con *next = TAILQ_NEXT(current, floating_windows);
488 tree_flatten(current);