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).
17 char *globbed = resolve_tilde(config.restart_state_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 size_t path_len = strlen(config.restart_state_path);
32 char *old_restart = smalloc(path_len + strlen(".old") + 1);
33 strncpy(old_restart, config.restart_state_path, path_len + strlen(".old") + 1);
34 strncat(old_restart, ".old", strlen(".old") + 1);
36 rename(globbed, old_restart);
40 printf("appended tree, using new root\n");
41 croot = TAILQ_FIRST(&(croot->nodes_head));
42 printf("new root = %p\n", croot);
43 Con *out = TAILQ_FIRST(&(croot->nodes_head));
44 printf("out = %p\n", out);
45 Con *ws = TAILQ_FIRST(&(out->nodes_head));
46 printf("ws = %p\n", ws);
52 * Initializes the tree by creating the root node, adding all RandR outputs
53 * to the tree (that means randr_init() has to be called before) and
54 * assigning a workspace to each RandR output.
60 croot = con_new(NULL);
62 croot->type = CT_ROOT;
67 TAILQ_FOREACH(output, &outputs, outputs) {
71 Con *oc = con_new(croot);
72 oc->name = strdup(output->name);
74 oc->rect = output->rect;
78 asprintf(&name, "[i3 con] output %s", oc->name);
82 /* add a workspace to this output */
84 ws->type = CT_WORKSPACE;
86 asprintf(&(ws->name), "%d", c);
88 con_attach(ws, oc, false);
90 asprintf(&name, "[i3 con] workspace %s", ws->name);
94 ws->fullscreen_mode = CF_OUTPUT;
95 ws->orientation = HORIZ;
102 * Opens an empty container in the current container
105 Con *tree_open_con(Con *con) {
107 /* every focusable Con has a parent (outputs have parent root) */
108 con = focused->parent;
109 /* If the parent is an output, we are on a workspace. In this case,
110 * the new container needs to be opened as a leaf of the workspace. */
111 if (con->type == CT_OUTPUT)
113 /* If the currently focused container is a floating container, we
114 * attach the new container to the workspace */
115 if (con->type == CT_FLOATING_CON)
121 /* 3: re-calculate child->percent for each child */
122 con_fix_percent(con, WINDOW_ADD);
124 /* 4: add a new container leaf to this con */
125 Con *new = con_new(con);
132 * vanishing is the container that is about to be closed (so any floating
133 * client which has old_parent == vanishing needs to be "re-parented").
136 static void fix_floating_parent(Con *con, Con *vanishing) {
139 if (con->old_parent == vanishing) {
140 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
141 vanishing, con, vanishing->parent);
142 con->old_parent = vanishing->parent;
145 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
146 fix_floating_parent(child, vanishing);
148 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
149 fix_floating_parent(child, vanishing);
153 * Closes the given container including all children
156 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
157 Con *parent = con->parent;
159 /* check floating clients and adjust old_parent if necessary */
160 fix_floating_parent(croot, con);
162 /* Get the container which is next focused */
163 Con *next = con_next_focused(con);
165 DLOG("closing %p, kill_window = %d\n", con, kill_window);
167 /* We cannot use TAILQ_FOREACH because the children get deleted
168 * in their parent’s nodes_head */
169 while (!TAILQ_EMPTY(&(con->nodes_head))) {
170 child = TAILQ_FIRST(&(con->nodes_head));
171 DLOG("killing child=%p\n", child);
172 tree_close(child, kill_window, true);
175 if (con->window != NULL) {
177 x_window_kill(con->window->id);
179 /* un-parent the window */
180 xcb_reparent_window(conn, con->window->id, root, 0, 0);
181 /* TODO: client_unmap to set state to withdrawn */
187 /* kill the X11 part of this container */
191 con_fix_percent(parent, WINDOW_REMOVE);
193 if (con_is_floating(con)) {
194 DLOG("Container was floating, killing floating container\n");
195 tree_close(parent, false, false);
200 TAILQ_REMOVE(&all_cons, con, all_cons);
203 /* in the case of floating windows, we already focused another container
204 * when closing the parent, so we can exit now. */
208 DLOG("focusing %p / %s\n", next, next->name);
209 /* TODO: check if the container (or one of its children) was focused */
212 /* check if the parent container is empty now and close it */
213 if (!dont_kill_parent &&
214 parent->type != CT_WORKSPACE &&
215 TAILQ_EMPTY(&(parent->nodes_head))) {
216 DLOG("Closing empty parent container\n");
217 /* TODO: check if this container would swallow any other client and
218 * don’t close it automatically. */
219 tree_close(parent, false, false);
224 * Closes the current container using tree_close().
227 void tree_close_con() {
228 assert(focused != NULL);
229 if (focused->type == CT_WORKSPACE) {
230 LOG("Cannot close workspace\n");
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: add it as a child to the new Con */
271 con_attach(con, new, false);
275 * Moves focus one level up.
279 /* We can focus up to the workspace, but not any higher in the tree */
280 if (focused->parent->type != CT_CON &&
281 focused->parent->type != CT_WORKSPACE) {
282 printf("cannot go up\n");
285 con_focus(focused->parent);
289 * Moves focus one level down.
293 /* Go down the focus stack of the current node */
294 Con *next = TAILQ_FIRST(&(focused->focus_head));
295 if (next == TAILQ_END(&(focused->focus_head))) {
296 printf("cannot go down\n");
302 static void mark_unmapped(Con *con) {
306 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
307 mark_unmapped(current);
308 if (con->type == CT_WORKSPACE) {
309 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
310 current->mapped = false;
311 Con *child = TAILQ_FIRST(&(current->nodes_head));
312 child->mapped = false;
318 * Renders the tree, that is rendering all outputs using render_con() and
319 * pushing the changes to X11 using x_push_changes().
326 printf("-- BEGIN RENDERING --\n");
327 /* Reset map state for all nodes in tree */
328 /* TODO: a nicer method to walk all nodes would be good, maybe? */
329 mark_unmapped(croot);
330 croot->mapped = true;
332 /* We start rendering at an output */
334 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
335 printf("output %p / %s\n", output, output->name);
336 render_con(output, false);
338 x_push_changes(croot);
339 printf("-- END RENDERING --\n");
343 * Changes focus in the given way (next/previous) and given orientation
344 * (horizontal/vertical).
347 void tree_next(char way, orientation_t orientation) {
348 /* 1: get the first parent with the same orientation */
349 Con *parent = focused->parent;
350 while (focused->type != CT_WORKSPACE &&
351 con_orientation(parent) != orientation) {
352 LOG("need to go one level further up\n");
353 /* if the current parent is an output, we are at a workspace
354 * and the orientation still does not match */
355 if (parent->type == CT_WORKSPACE)
357 parent = parent->parent;
359 Con *current = TAILQ_FIRST(&(parent->focus_head));
360 assert(current != TAILQ_END(&(parent->focus_head)));
362 /* 2: chose next (or previous) */
365 next = TAILQ_NEXT(current, 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_FIRST(&(parent->nodes_head));
370 next = TAILQ_PREV(current, nodes_head, nodes);
371 /* if we are at the end of the list, we need to wrap */
372 if (next == TAILQ_END(&(parent->nodes_head)))
373 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
376 /* 3: focus choice comes in here. at the moment we will go down
377 * until we find a window */
378 /* TODO: check for window, atm we only go down as far as possible */
379 while (!TAILQ_EMPTY(&(next->focus_head)))
380 next = TAILQ_FIRST(&(next->focus_head));
382 DLOG("focusing %p\n", next);
387 * Moves the current container in the given way (next/previous) and given
388 * orientation (horizontal/vertical).
391 void tree_move(char way, orientation_t orientation) {
392 /* 1: get the first parent with the same orientation */
393 Con *parent = focused->parent;
394 Con *old_parent = parent;
395 if (focused->type == CT_WORKSPACE)
397 bool level_changed = false;
398 while (con_orientation(parent) != orientation) {
399 DLOG("need to go one level further up\n");
400 /* If the current parent is an output, we are at a workspace
401 * and the orientation still does not match. In this case, we split the
402 * workspace to have the same look & feel as in older i3 releases. */
403 if (parent->type == CT_WORKSPACE) {
404 DLOG("Arrived at workspace, splitting...\n");
405 /* 1: create a new split container */
406 Con *new = con_new(NULL);
407 new->parent = parent;
409 /* 2: copy layout and orientation from workspace */
410 new->layout = parent->layout;
411 new->orientation = parent->orientation;
413 Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
414 if (old_focused == TAILQ_END(&(parent->focus_head)))
417 /* 3: move the existing cons of this workspace below the new con */
418 DLOG("Moving cons\n");
420 while (!TAILQ_EMPTY(&(parent->nodes_head))) {
421 child = TAILQ_FIRST(&(parent->nodes_head));
423 con_attach(child, new, true);
426 /* 4: switch workspace orientation */
427 parent->orientation = orientation;
429 /* 4: attach the new split container to the workspace */
430 DLOG("Attaching new split to ws\n");
431 con_attach(new, parent, false);
434 con_focus(old_focused);
436 level_changed = true;
440 parent = parent->parent;
441 level_changed = true;
443 Con *current = TAILQ_FIRST(&(parent->focus_head));
444 assert(current != TAILQ_END(&(parent->focus_head)));
446 /* 2: chose next (or previous) */
449 LOG("i would insert it after %p / %s\n", next, next->name);
450 if (!level_changed) {
451 next = TAILQ_NEXT(next, nodes);
452 if (next == TAILQ_END(&(next->parent->nodes_head))) {
453 LOG("cannot move further to the right\n");
457 /* if this is a split container, we need to go down */
458 while (!TAILQ_EMPTY(&(next->focus_head)))
459 next = TAILQ_FIRST(&(next->focus_head));
463 focused->parent = next->parent;
465 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
466 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
467 /* TODO: don’t influence focus handling? */
469 LOG("i would insert it before %p / %s\n", current, current->name);
470 bool gone_down = false;
471 if (!level_changed) {
472 next = TAILQ_PREV(next, nodes_head, nodes);
473 if (next == TAILQ_END(&(next->parent->nodes_head))) {
474 LOG("cannot move further\n");
478 /* if this is a split container, we need to go down */
479 while (!TAILQ_EMPTY(&(next->focus_head))) {
481 next = TAILQ_FIRST(&(next->focus_head));
486 focused->parent = next->parent;
488 /* After going down in the tree, we insert the container *after*
489 * the currently focused one even though the command used "before".
490 * This is to keep the user experience clear, since the before/after
491 * only signifies the direction of the movement on top-level */
493 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
494 else TAILQ_INSERT_BEFORE(next, focused, nodes);
495 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
496 /* TODO: don’t influence focus handling? */
499 if (con_num_children(old_parent) == 0) {
500 DLOG("Old container empty after moving. Let's close it\n");
501 tree_close(old_parent, false, false);