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, adding all RandR outputs
44 * to the tree (that means randr_init() has to be called before) and
45 * assigning a workspace to each RandR output.
51 croot = con_new(NULL);
54 croot->type = CT_ROOT;
59 TAILQ_FOREACH(output, &outputs, outputs) {
63 Con *oc = con_new(croot);
65 oc->name = strdup(output->name);
67 oc->rect = output->rect;
71 asprintf(&name, "[i3 con] output %s", oc->name);
75 /* add a workspace to this output */
77 ws->type = CT_WORKSPACE;
80 asprintf(&(ws->name), "%d", c);
82 con_attach(ws, oc, false);
84 asprintf(&name, "[i3 con] workspace %s", ws->name);
88 ws->fullscreen_mode = CF_OUTPUT;
89 ws->orientation = HORIZ;
96 * Opens an empty container in the current container
99 Con *tree_open_con(Con *con) {
101 /* every focusable Con has a parent (outputs have parent root) */
102 con = focused->parent;
103 /* If the parent is an output, we are on a workspace. In this case,
104 * the new container needs to be opened as a leaf of the workspace. */
105 if (con->type == CT_OUTPUT)
107 /* If the currently focused container is a floating container, we
108 * attach the new container to the workspace */
109 if (con->type == CT_FLOATING_CON)
115 /* 3: re-calculate child->percent for each child */
116 con_fix_percent(con, WINDOW_ADD);
118 /* 4: add a new container leaf to this con */
119 Con *new = con_new(con);
126 * vanishing is the container that is about to be closed (so any floating
127 * client which has old_parent == vanishing needs to be "re-parented").
130 static void fix_floating_parent(Con *con, Con *vanishing) {
133 if (con->old_parent == vanishing) {
134 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
135 vanishing, con, vanishing->parent);
136 con->old_parent = vanishing->parent;
139 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
140 fix_floating_parent(child, vanishing);
142 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
143 fix_floating_parent(child, vanishing);
146 static bool _is_con_mapped(Con *con) {
149 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
150 if (_is_con_mapped(child))
157 * Closes the given container including all children
160 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
161 bool was_mapped = con->mapped;
162 Con *parent = con->parent;
165 /* Even if the container itself is not mapped, its children may be
166 * mapped (for example split containers don't have a mapped window on
167 * their own but usually contain mapped children). */
168 was_mapped = _is_con_mapped(con);
171 /* check floating clients and adjust old_parent if necessary */
172 fix_floating_parent(croot, con);
174 /* Get the container which is next focused */
175 Con *next = con_next_focused(con);
176 DLOG("next = %p, focused = %p\n", next, focused);
178 DLOG("closing %p, kill_window = %d\n", con, kill_window);
180 /* We cannot use TAILQ_FOREACH because the children get deleted
181 * in their parent’s nodes_head */
182 while (!TAILQ_EMPTY(&(con->nodes_head))) {
183 child = TAILQ_FIRST(&(con->nodes_head));
184 DLOG("killing child=%p\n", child);
185 tree_close(child, kill_window, true);
188 if (con->window != NULL) {
190 x_window_kill(con->window->id);
192 /* un-parent the window */
193 xcb_reparent_window(conn, con->window->id, root, 0, 0);
194 /* TODO: client_unmap to set state to withdrawn */
197 FREE(con->window->class_class);
198 FREE(con->window->class_instance);
199 FREE(con->window->name_x);
200 FREE(con->window->name_json);
204 /* kill the X11 part of this container */
208 if (con->type != CT_FLOATING_CON) {
209 /* If the container is *not* floating, we might need to re-distribute
210 * percentage values for the resized containers. */
211 con_fix_percent(parent, WINDOW_REMOVE);
214 if (con_is_floating(con)) {
215 Con *ws = con_get_workspace(con);
216 DLOG("Container was floating, killing floating container\n");
217 tree_close(parent, false, false);
218 DLOG("parent container killed\n");
219 if (con == focused) {
220 DLOG("This is the focused container, i need to find another one to focus. I start looking at ws = %p\n", ws);
221 next = con_next_focused(ws);
222 dont_kill_parent = true;
223 DLOG("Alright, focusing %p\n", next);
230 TAILQ_REMOVE(&all_cons, con, all_cons);
233 /* in the case of floating windows, we already focused another container
234 * when closing the parent, so we can exit now. */
236 DLOG("No next container, i will just exit now\n");
240 if (was_mapped || con == focused) {
241 DLOG("focusing %p / %s\n", next, next->name);
242 /* TODO: check if the container (or one of its children) was focused */
245 DLOG("not focusing, was not mapped\n");
248 /* check if the parent container is empty now and close it */
249 if (!dont_kill_parent &&
250 parent->type != CT_WORKSPACE &&
251 TAILQ_EMPTY(&(parent->nodes_head))) {
252 DLOG("Closing empty parent container\n");
253 /* TODO: check if this container would swallow any other client and
254 * don’t close it automatically. */
255 tree_close(parent, false, false);
260 * Closes the current container using tree_close().
263 void tree_close_con() {
264 assert(focused != NULL);
265 if (focused->type == CT_WORKSPACE) {
266 LOG("Cannot close workspace\n");
270 /* There *should* be no possibility to focus outputs / root container */
271 assert(focused->type != CT_OUTPUT);
272 assert(focused->type != CT_ROOT);
275 tree_close(focused, true, false);
279 * Splits (horizontally or vertically) the given container by creating a new
280 * container which contains the old one and the future ones.
283 void tree_split(Con *con, orientation_t orientation) {
284 /* for a workspace, we just need to change orientation */
285 if (con->type == CT_WORKSPACE) {
286 DLOG("Workspace, simply changing orientation to %d\n", orientation);
287 con->orientation = orientation;
291 Con *parent = con->parent;
292 /* if we are in a container whose parent contains only one
293 * child (its split functionality is unused so far), we just change the
294 * orientation (more intuitive than splitting again) */
295 if (con_num_children(parent) == 1) {
296 parent->orientation = orientation;
297 DLOG("Just changing orientation of existing container\n");
301 DLOG("Splitting in orientation %d\n", orientation);
303 /* 2: replace it with a new Con */
304 Con *new = con_new(NULL);
305 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
306 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
307 new->parent = parent;
308 new->orientation = orientation;
310 /* 3: swap 'percent' (resize factor) */
311 new->percent = con->percent;
314 /* 4: add it as a child to the new Con */
315 con_attach(con, new, false);
319 * Moves focus one level up.
323 /* We can focus up to the workspace, but not any higher in the tree */
324 if (focused->parent->type != CT_CON &&
325 focused->parent->type != CT_WORKSPACE) {
326 printf("cannot go up\n");
329 con_focus(focused->parent);
333 * Moves focus one level down.
337 /* Go down the focus stack of the current node */
338 Con *next = TAILQ_FIRST(&(focused->focus_head));
339 if (next == TAILQ_END(&(focused->focus_head))) {
340 printf("cannot go down\n");
346 static void mark_unmapped(Con *con) {
350 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
351 mark_unmapped(current);
352 if (con->type == CT_WORKSPACE) {
353 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
354 current->mapped = false;
355 Con *child = TAILQ_FIRST(&(current->nodes_head));
356 child->mapped = false;
362 * Renders the tree, that is rendering all outputs using render_con() and
363 * pushing the changes to X11 using x_push_changes().
370 printf("-- BEGIN RENDERING --\n");
371 /* Reset map state for all nodes in tree */
372 /* TODO: a nicer method to walk all nodes would be good, maybe? */
373 mark_unmapped(croot);
374 croot->mapped = true;
376 /* We start rendering at an output */
378 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
379 printf("output %p / %s\n", output, output->name);
380 render_con(output, false);
382 x_push_changes(croot);
383 printf("-- END RENDERING --\n");
387 * Changes focus in the given way (next/previous) and given orientation
388 * (horizontal/vertical).
391 void tree_next(char way, orientation_t orientation) {
392 /* 1: get the first parent with the same orientation */
393 Con *parent = focused->parent;
394 while (focused->type != CT_WORKSPACE &&
395 con_orientation(parent) != orientation) {
396 LOG("need to go one level further up\n");
397 /* if the current parent is an output, we are at a workspace
398 * and the orientation still does not match */
399 if (parent->type == CT_WORKSPACE)
401 parent = parent->parent;
403 Con *current = TAILQ_FIRST(&(parent->focus_head));
404 assert(current != TAILQ_END(&(parent->focus_head)));
406 /* 2: chose next (or previous) */
409 next = TAILQ_NEXT(current, nodes);
410 /* if we are at the end of the list, we need to wrap */
411 if (next == TAILQ_END(&(parent->nodes_head)))
412 next = TAILQ_FIRST(&(parent->nodes_head));
414 next = TAILQ_PREV(current, nodes_head, nodes);
415 /* if we are at the end of the list, we need to wrap */
416 if (next == TAILQ_END(&(parent->nodes_head)))
417 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
420 /* 3: focus choice comes in here. at the moment we will go down
421 * until we find a window */
422 /* TODO: check for window, atm we only go down as far as possible */
423 while (!TAILQ_EMPTY(&(next->focus_head)))
424 next = TAILQ_FIRST(&(next->focus_head));
426 DLOG("focusing %p\n", next);
431 * Moves the current container in the given way (next/previous) and given
432 * orientation (horizontal/vertical).
435 void tree_move(char way, orientation_t orientation) {
436 /* 1: get the first parent with the same orientation */
437 Con *parent = focused->parent;
438 Con *old_parent = parent;
439 if (focused->type == CT_WORKSPACE)
441 bool level_changed = false;
442 while (con_orientation(parent) != orientation) {
443 DLOG("need to go one level further up\n");
444 /* If the current parent is an output, we are at a workspace
445 * and the orientation still does not match. In this case, we split the
446 * workspace to have the same look & feel as in older i3 releases. */
447 if (parent->type == CT_WORKSPACE) {
448 DLOG("Arrived at workspace, splitting...\n");
449 /* 1: create a new split container */
450 Con *new = con_new(NULL);
451 new->parent = parent;
453 /* 2: copy layout and orientation from workspace */
454 new->layout = parent->layout;
455 new->orientation = parent->orientation;
457 Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
458 if (old_focused == TAILQ_END(&(parent->focus_head)))
461 /* 3: move the existing cons of this workspace below the new con */
462 DLOG("Moving cons\n");
464 while (!TAILQ_EMPTY(&(parent->nodes_head))) {
465 child = TAILQ_FIRST(&(parent->nodes_head));
467 con_attach(child, new, true);
470 /* 4: switch workspace orientation */
471 parent->orientation = orientation;
473 /* 4: attach the new split container to the workspace */
474 DLOG("Attaching new split to ws\n");
475 con_attach(new, parent, false);
478 con_focus(old_focused);
480 level_changed = true;
484 parent = parent->parent;
485 level_changed = true;
487 Con *current = TAILQ_FIRST(&(parent->focus_head));
488 assert(current != TAILQ_END(&(parent->focus_head)));
490 /* 2: chose next (or previous) */
493 LOG("i would insert it after %p / %s\n", next, next->name);
495 /* Have a look at the next container: If there is no next container or
496 * if it is a leaf node, we move the focused one left to it. However,
497 * for split containers, we descend into it. */
498 next = TAILQ_NEXT(next, nodes);
499 if (next == TAILQ_END(&(next->parent->nodes_head))) {
500 if (focused == current)
504 if (level_changed && con_is_leaf(next)) {
507 /* if this is a split container, we need to go down */
508 while (!TAILQ_EMPTY(&(next->focus_head)))
509 next = TAILQ_FIRST(&(next->focus_head));
514 focused->parent = next->parent;
516 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
517 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
518 /* TODO: don’t influence focus handling? */
520 LOG("i would insert it before %p / %s\n", current, current->name);
521 bool gone_down = false;
522 next = TAILQ_PREV(next, nodes_head, nodes);
523 if (next == TAILQ_END(&(next->parent->nodes_head))) {
524 if (focused == current)
528 if (level_changed && con_is_leaf(next)) {
531 /* if this is a split container, we need to go down */
532 while (!TAILQ_EMPTY(&(next->focus_head))) {
534 next = TAILQ_FIRST(&(next->focus_head));
540 focused->parent = next->parent;
542 /* After going down in the tree, we insert the container *after*
543 * the currently focused one even though the command used "before".
544 * This is to keep the user experience clear, since the before/after
545 * only signifies the direction of the movement on top-level */
547 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
548 else TAILQ_INSERT_BEFORE(next, focused, nodes);
549 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
550 /* TODO: don’t influence focus handling? */
553 /* We need to call con_focus() to fix the focus stack "above" the container
554 * we just inserted the focused container into (otherwise, the parent
555 * container(s) would still point to the old container(s)). */
558 if (con_num_children(old_parent) == 0) {
559 DLOG("Old container empty after moving. Let's close it\n");
560 tree_close(old_parent, false, false);