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);
53 croot->type = CT_ROOT;
58 TAILQ_FOREACH(output, &outputs, outputs) {
62 Con *oc = con_new(croot);
63 oc->name = strdup(output->name);
65 oc->rect = output->rect;
69 asprintf(&name, "[i3 con] output %s", oc->name);
73 /* add a workspace to this output */
75 ws->type = CT_WORKSPACE;
77 asprintf(&(ws->name), "%d", c);
79 con_attach(ws, oc, false);
81 asprintf(&name, "[i3 con] workspace %s", ws->name);
85 ws->fullscreen_mode = CF_OUTPUT;
86 ws->orientation = HORIZ;
93 * Opens an empty container in the current container
96 Con *tree_open_con(Con *con) {
98 /* every focusable Con has a parent (outputs have parent root) */
99 con = focused->parent;
100 /* If the parent is an output, we are on a workspace. In this case,
101 * the new container needs to be opened as a leaf of the workspace. */
102 if (con->type == CT_OUTPUT)
104 /* If the currently focused container is a floating container, we
105 * attach the new container to the workspace */
106 if (con->type == CT_FLOATING_CON)
112 /* 3: re-calculate child->percent for each child */
113 con_fix_percent(con, WINDOW_ADD);
115 /* 4: add a new container leaf to this con */
116 Con *new = con_new(con);
123 * vanishing is the container that is about to be closed (so any floating
124 * client which has old_parent == vanishing needs to be "re-parented").
127 static void fix_floating_parent(Con *con, Con *vanishing) {
130 if (con->old_parent == vanishing) {
131 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
132 vanishing, con, vanishing->parent);
133 con->old_parent = vanishing->parent;
136 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
137 fix_floating_parent(child, vanishing);
139 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
140 fix_floating_parent(child, vanishing);
144 * Closes the given container including all children
147 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
148 Con *parent = con->parent;
150 /* check floating clients and adjust old_parent if necessary */
151 fix_floating_parent(croot, con);
153 /* Get the container which is next focused */
154 Con *next = con_next_focused(con);
156 DLOG("closing %p, kill_window = %d\n", con, kill_window);
158 /* We cannot use TAILQ_FOREACH because the children get deleted
159 * in their parent’s nodes_head */
160 while (!TAILQ_EMPTY(&(con->nodes_head))) {
161 child = TAILQ_FIRST(&(con->nodes_head));
162 DLOG("killing child=%p\n", child);
163 tree_close(child, kill_window, true);
166 if (con->window != NULL) {
168 x_window_kill(con->window->id);
170 /* un-parent the window */
171 xcb_reparent_window(conn, con->window->id, root, 0, 0);
172 /* TODO: client_unmap to set state to withdrawn */
178 /* kill the X11 part of this container */
182 con_fix_percent(parent, WINDOW_REMOVE);
184 if (con_is_floating(con)) {
185 DLOG("Container was floating, killing floating container\n");
186 tree_close(parent, false, false);
191 TAILQ_REMOVE(&all_cons, con, all_cons);
194 /* in the case of floating windows, we already focused another container
195 * when closing the parent, so we can exit now. */
199 DLOG("focusing %p / %s\n", next, next->name);
200 /* TODO: check if the container (or one of its children) was focused */
203 /* check if the parent container is empty now and close it */
204 if (!dont_kill_parent &&
205 parent->type != CT_WORKSPACE &&
206 TAILQ_EMPTY(&(parent->nodes_head))) {
207 DLOG("Closing empty parent container\n");
208 /* TODO: check if this container would swallow any other client and
209 * don’t close it automatically. */
210 tree_close(parent, false, false);
215 * Closes the current container using tree_close().
218 void tree_close_con() {
219 assert(focused != NULL);
220 if (focused->type == CT_WORKSPACE) {
221 LOG("Cannot close workspace\n");
226 tree_close(focused, true, false);
230 * Splits (horizontally or vertically) the given container by creating a new
231 * container which contains the old one and the future ones.
234 void tree_split(Con *con, orientation_t orientation) {
235 /* for a workspace, we just need to change orientation */
236 if (con->type == CT_WORKSPACE) {
237 DLOG("Workspace, simply changing orientation to %d\n", orientation);
238 con->orientation = orientation;
242 Con *parent = con->parent;
243 /* if we are in a container whose parent contains only one
244 * child (its split functionality is unused so far), we just change the
245 * orientation (more intuitive than splitting again) */
246 if (con_num_children(parent) == 1) {
247 parent->orientation = orientation;
248 DLOG("Just changing orientation of existing container\n");
252 DLOG("Splitting in orientation %d\n", orientation);
254 /* 2: replace it with a new Con */
255 Con *new = con_new(NULL);
256 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
257 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
258 new->parent = parent;
259 new->orientation = orientation;
261 /* 3: swap 'percent' (resize factor) */
262 new->percent = con->percent;
265 /* 4: add it as a child to the new Con */
266 con_attach(con, new, false);
270 * Moves focus one level up.
274 /* We can focus up to the workspace, but not any higher in the tree */
275 if (focused->parent->type != CT_CON &&
276 focused->parent->type != CT_WORKSPACE) {
277 printf("cannot go up\n");
280 con_focus(focused->parent);
284 * Moves focus one level down.
288 /* Go down the focus stack of the current node */
289 Con *next = TAILQ_FIRST(&(focused->focus_head));
290 if (next == TAILQ_END(&(focused->focus_head))) {
291 printf("cannot go down\n");
297 static void mark_unmapped(Con *con) {
301 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
302 mark_unmapped(current);
303 if (con->type == CT_WORKSPACE) {
304 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
305 current->mapped = false;
306 Con *child = TAILQ_FIRST(&(current->nodes_head));
307 child->mapped = false;
313 * Renders the tree, that is rendering all outputs using render_con() and
314 * pushing the changes to X11 using x_push_changes().
321 printf("-- BEGIN RENDERING --\n");
322 /* Reset map state for all nodes in tree */
323 /* TODO: a nicer method to walk all nodes would be good, maybe? */
324 mark_unmapped(croot);
325 croot->mapped = true;
327 /* We start rendering at an output */
329 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
330 printf("output %p / %s\n", output, output->name);
331 render_con(output, false);
333 x_push_changes(croot);
334 printf("-- END RENDERING --\n");
338 * Changes focus in the given way (next/previous) and given orientation
339 * (horizontal/vertical).
342 void tree_next(char way, orientation_t orientation) {
343 /* 1: get the first parent with the same orientation */
344 Con *parent = focused->parent;
345 while (focused->type != CT_WORKSPACE &&
346 con_orientation(parent) != orientation) {
347 LOG("need to go one level further up\n");
348 /* if the current parent is an output, we are at a workspace
349 * and the orientation still does not match */
350 if (parent->type == CT_WORKSPACE)
352 parent = parent->parent;
354 Con *current = TAILQ_FIRST(&(parent->focus_head));
355 assert(current != TAILQ_END(&(parent->focus_head)));
357 /* 2: chose next (or previous) */
360 next = TAILQ_NEXT(current, nodes);
361 /* if we are at the end of the list, we need to wrap */
362 if (next == TAILQ_END(&(parent->nodes_head)))
363 next = TAILQ_FIRST(&(parent->nodes_head));
365 next = TAILQ_PREV(current, nodes_head, nodes);
366 /* if we are at the end of the list, we need to wrap */
367 if (next == TAILQ_END(&(parent->nodes_head)))
368 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
371 /* 3: focus choice comes in here. at the moment we will go down
372 * until we find a window */
373 /* TODO: check for window, atm we only go down as far as possible */
374 while (!TAILQ_EMPTY(&(next->focus_head)))
375 next = TAILQ_FIRST(&(next->focus_head));
377 DLOG("focusing %p\n", next);
382 * Moves the current container in the given way (next/previous) and given
383 * orientation (horizontal/vertical).
386 void tree_move(char way, orientation_t orientation) {
387 /* 1: get the first parent with the same orientation */
388 Con *parent = focused->parent;
389 Con *old_parent = parent;
390 if (focused->type == CT_WORKSPACE)
392 bool level_changed = false;
393 while (con_orientation(parent) != orientation) {
394 DLOG("need to go one level further up\n");
395 /* If the current parent is an output, we are at a workspace
396 * and the orientation still does not match. In this case, we split the
397 * workspace to have the same look & feel as in older i3 releases. */
398 if (parent->type == CT_WORKSPACE) {
399 DLOG("Arrived at workspace, splitting...\n");
400 /* 1: create a new split container */
401 Con *new = con_new(NULL);
402 new->parent = parent;
404 /* 2: copy layout and orientation from workspace */
405 new->layout = parent->layout;
406 new->orientation = parent->orientation;
408 Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
409 if (old_focused == TAILQ_END(&(parent->focus_head)))
412 /* 3: move the existing cons of this workspace below the new con */
413 DLOG("Moving cons\n");
415 while (!TAILQ_EMPTY(&(parent->nodes_head))) {
416 child = TAILQ_FIRST(&(parent->nodes_head));
418 con_attach(child, new, true);
421 /* 4: switch workspace orientation */
422 parent->orientation = orientation;
424 /* 4: attach the new split container to the workspace */
425 DLOG("Attaching new split to ws\n");
426 con_attach(new, parent, false);
429 con_focus(old_focused);
431 level_changed = true;
435 parent = parent->parent;
436 level_changed = true;
438 Con *current = TAILQ_FIRST(&(parent->focus_head));
439 assert(current != TAILQ_END(&(parent->focus_head)));
441 /* 2: chose next (or previous) */
444 LOG("i would insert it after %p / %s\n", next, next->name);
446 /* Have a look at the next container: If there is no next container or
447 * if it is a leaf node, we move the focused one left to it. However,
448 * for split containers, we descend into it. */
449 next = TAILQ_NEXT(next, nodes);
450 if (next == TAILQ_END(&(next->parent->nodes_head))) {
451 if (focused == current)
455 if (level_changed && con_is_leaf(next)) {
458 /* if this is a split container, we need to go down */
459 while (!TAILQ_EMPTY(&(next->focus_head)))
460 next = TAILQ_FIRST(&(next->focus_head));
465 focused->parent = next->parent;
467 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
468 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
469 /* TODO: don’t influence focus handling? */
471 LOG("i would insert it before %p / %s\n", current, current->name);
472 bool gone_down = false;
473 next = TAILQ_PREV(next, nodes_head, nodes);
474 if (next == TAILQ_END(&(next->parent->nodes_head))) {
475 if (focused == current)
479 if (level_changed && con_is_leaf(next)) {
482 /* if this is a split container, we need to go down */
483 while (!TAILQ_EMPTY(&(next->focus_head))) {
485 next = TAILQ_FIRST(&(next->focus_head));
491 focused->parent = next->parent;
493 /* After going down in the tree, we insert the container *after*
494 * the currently focused one even though the command used "before".
495 * This is to keep the user experience clear, since the before/after
496 * only signifies the direction of the movement on top-level */
498 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
499 else TAILQ_INSERT_BEFORE(next, focused, nodes);
500 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
501 /* TODO: don’t influence focus handling? */
504 /* We need to call con_focus() to fix the focus stack "above" the container
505 * we just inserted the focused container into (otherwise, the parent
506 * container(s) would still point to the old container(s)). */
509 if (con_num_children(old_parent) == 0) {
510 DLOG("Old container empty after moving. Let's close it\n");
511 tree_close(old_parent, false, false);