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);
53 * Initializes the tree by creating the root node, adding all RandR outputs
54 * to the tree (that means randr_init() has to be called before) and
55 * assigning a workspace to each RandR output.
61 croot = con_new(NULL);
63 croot->type = CT_ROOT;
68 TAILQ_FOREACH(output, &outputs, outputs) {
72 Con *oc = con_new(croot);
73 oc->name = strdup(output->name);
75 oc->rect = output->rect;
79 asprintf(&name, "[i3 con] output %s", oc->name);
83 /* add a workspace to this output */
85 ws->type = CT_WORKSPACE;
87 asprintf(&(ws->name), "%d", c);
89 con_attach(ws, oc, false);
91 asprintf(&name, "[i3 con] workspace %s", ws->name);
95 ws->fullscreen_mode = CF_OUTPUT;
96 ws->orientation = HORIZ;
103 * Opens an empty container in the current container
106 Con *tree_open_con(Con *con) {
108 /* every focusable Con has a parent (outputs have parent root) */
109 con = focused->parent;
110 /* If the parent is an output, we are on a workspace. In this case,
111 * the new container needs to be opened as a leaf of the workspace. */
112 if (con->type == CT_OUTPUT)
114 /* If the currently focused container is a floating container, we
115 * attach the new container to the workspace */
116 if (con->type == CT_FLOATING_CON)
122 /* 3: re-calculate child->percent for each child */
123 con_fix_percent(con, WINDOW_ADD);
125 /* 4: add a new container leaf to this con */
126 Con *new = con_new(con);
133 * vanishing is the container that is about to be closed (so any floating
134 * client which has old_parent == vanishing needs to be "re-parented").
137 static void fix_floating_parent(Con *con, Con *vanishing) {
140 if (con->old_parent == vanishing) {
141 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
142 vanishing, con, vanishing->parent);
143 con->old_parent = vanishing->parent;
146 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
147 fix_floating_parent(child, vanishing);
149 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
150 fix_floating_parent(child, vanishing);
154 * Closes the given container including all children
157 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
158 Con *parent = con->parent;
160 /* check floating clients and adjust old_parent if necessary */
161 fix_floating_parent(croot, con);
163 /* Get the container which is next focused */
164 Con *next = con_next_focused(con);
166 DLOG("closing %p, kill_window = %d\n", con, kill_window);
168 /* We cannot use TAILQ_FOREACH because the children get deleted
169 * in their parent’s nodes_head */
170 while (!TAILQ_EMPTY(&(con->nodes_head))) {
171 child = TAILQ_FIRST(&(con->nodes_head));
172 DLOG("killing child=%p\n", child);
173 tree_close(child, kill_window, true);
176 if (con->window != NULL) {
178 x_window_kill(con->window->id);
180 /* un-parent the window */
181 xcb_reparent_window(conn, con->window->id, root, 0, 0);
182 /* TODO: client_unmap to set state to withdrawn */
188 /* kill the X11 part of this container */
192 con_fix_percent(parent, WINDOW_REMOVE);
194 if (con_is_floating(con)) {
195 DLOG("Container was floating, killing floating container\n");
196 tree_close(parent, false, false);
201 TAILQ_REMOVE(&all_cons, con, all_cons);
204 /* in the case of floating windows, we already focused another container
205 * when closing the parent, so we can exit now. */
209 DLOG("focusing %p / %s\n", next, next->name);
210 /* TODO: check if the container (or one of its children) was focused */
213 /* check if the parent container is empty now and close it */
214 if (!dont_kill_parent &&
215 parent->type != CT_WORKSPACE &&
216 TAILQ_EMPTY(&(parent->nodes_head))) {
217 DLOG("Closing empty parent container\n");
218 /* TODO: check if this container would swallow any other client and
219 * don’t close it automatically. */
220 tree_close(parent, false, false);
225 * Closes the current container using tree_close().
228 void tree_close_con() {
229 assert(focused != NULL);
230 if (focused->type == CT_WORKSPACE) {
231 LOG("Cannot close workspace\n");
236 tree_close(focused, true, false);
240 * Splits (horizontally or vertically) the given container by creating a new
241 * container which contains the old one and the future ones.
244 void tree_split(Con *con, orientation_t orientation) {
245 /* for a workspace, we just need to change orientation */
246 if (con->type == CT_WORKSPACE) {
247 DLOG("Workspace, simply changing orientation to %d\n", orientation);
248 con->orientation = orientation;
252 Con *parent = con->parent;
253 /* if we are in a container whose parent contains only one
254 * child (its split functionality is unused so far), we just change the
255 * orientation (more intuitive than splitting again) */
256 if (con_num_children(parent) == 1) {
257 parent->orientation = orientation;
258 DLOG("Just changing orientation of existing container\n");
262 DLOG("Splitting in orientation %d\n", orientation);
264 /* 2: replace it with a new Con */
265 Con *new = con_new(NULL);
266 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
267 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
268 new->parent = parent;
269 new->orientation = orientation;
271 /* 3: add it as a child to the new Con */
272 con_attach(con, new, false);
276 * Moves focus one level up.
280 /* We can focus up to the workspace, but not any higher in the tree */
281 if (focused->parent->type != CT_CON &&
282 focused->parent->type != CT_WORKSPACE) {
283 printf("cannot go up\n");
286 con_focus(focused->parent);
290 * Moves focus one level down.
294 /* Go down the focus stack of the current node */
295 Con *next = TAILQ_FIRST(&(focused->focus_head));
296 if (next == TAILQ_END(&(focused->focus_head))) {
297 printf("cannot go down\n");
303 static void mark_unmapped(Con *con) {
307 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
308 mark_unmapped(current);
309 if (con->type == CT_WORKSPACE) {
310 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
311 current->mapped = false;
312 Con *child = TAILQ_FIRST(&(current->nodes_head));
313 child->mapped = false;
319 * Renders the tree, that is rendering all outputs using render_con() and
320 * pushing the changes to X11 using x_push_changes().
327 printf("-- BEGIN RENDERING --\n");
328 /* Reset map state for all nodes in tree */
329 /* TODO: a nicer method to walk all nodes would be good, maybe? */
330 mark_unmapped(croot);
331 croot->mapped = true;
333 /* We start rendering at an output */
335 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
336 printf("output %p / %s\n", output, output->name);
337 render_con(output, false);
339 x_push_changes(croot);
340 printf("-- END RENDERING --\n");
344 * Changes focus in the given way (next/previous) and given orientation
345 * (horizontal/vertical).
348 void tree_next(char way, orientation_t orientation) {
349 /* 1: get the first parent with the same orientation */
350 Con *parent = focused->parent;
351 while (focused->type != CT_WORKSPACE &&
352 con_orientation(parent) != orientation) {
353 LOG("need to go one level further up\n");
354 /* if the current parent is an output, we are at a workspace
355 * and the orientation still does not match */
356 if (parent->type == CT_WORKSPACE)
358 parent = parent->parent;
360 Con *current = TAILQ_FIRST(&(parent->focus_head));
361 assert(current != TAILQ_END(&(parent->focus_head)));
363 /* 2: chose next (or previous) */
366 next = TAILQ_NEXT(current, nodes);
367 /* if we are at the end of the list, we need to wrap */
368 if (next == TAILQ_END(&(parent->nodes_head)))
369 next = TAILQ_FIRST(&(parent->nodes_head));
371 next = TAILQ_PREV(current, nodes_head, nodes);
372 /* if we are at the end of the list, we need to wrap */
373 if (next == TAILQ_END(&(parent->nodes_head)))
374 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
377 /* 3: focus choice comes in here. at the moment we will go down
378 * until we find a window */
379 /* TODO: check for window, atm we only go down as far as possible */
380 while (!TAILQ_EMPTY(&(next->focus_head)))
381 next = TAILQ_FIRST(&(next->focus_head));
383 DLOG("focusing %p\n", next);
388 * Moves the current container in the given way (next/previous) and given
389 * orientation (horizontal/vertical).
392 void tree_move(char way, orientation_t orientation) {
393 /* 1: get the first parent with the same orientation */
394 Con *parent = focused->parent;
395 Con *old_parent = parent;
396 if (focused->type == CT_WORKSPACE)
398 bool level_changed = false;
399 while (con_orientation(parent) != orientation) {
400 LOG("need to go one level further up\n");
401 /* if the current parent is an output, we are at a workspace
402 * and the orientation still does not match */
403 if (parent->type == CT_WORKSPACE)
405 parent = parent->parent;
406 level_changed = true;
408 Con *current = TAILQ_FIRST(&(parent->focus_head));
409 assert(current != TAILQ_END(&(parent->focus_head)));
411 /* 2: chose next (or previous) */
414 LOG("i would insert it after %p / %s\n", next, next->name);
415 if (!level_changed) {
416 next = TAILQ_NEXT(next, nodes);
417 if (next == TAILQ_END(&(next->parent->nodes_head))) {
418 LOG("cannot move further to the right\n");
422 /* if this is a split container, we need to go down */
423 while (!TAILQ_EMPTY(&(next->focus_head)))
424 next = TAILQ_FIRST(&(next->focus_head));
428 focused->parent = next->parent;
430 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
431 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
432 /* TODO: don’t influence focus handling? */
434 LOG("i would insert it before %p / %s\n", current, current->name);
435 bool gone_down = false;
436 if (!level_changed) {
437 next = TAILQ_PREV(next, nodes_head, nodes);
438 if (next == TAILQ_END(&(next->parent->nodes_head))) {
439 LOG("cannot move further\n");
443 /* if this is a split container, we need to go down */
444 while (!TAILQ_EMPTY(&(next->focus_head))) {
446 next = TAILQ_FIRST(&(next->focus_head));
451 focused->parent = next->parent;
453 /* After going down in the tree, we insert the container *after*
454 * the currently focused one even though the command used "before".
455 * This is to keep the user experience clear, since the before/after
456 * only signifies the direction of the movement on top-level */
458 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
459 else TAILQ_INSERT_BEFORE(next, focused, nodes);
460 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
461 /* TODO: don’t influence focus handling? */
464 if (con_num_children(old_parent) == 0) {
465 DLOG("Old container empty after moving. Let's close it\n");
466 tree_close(old_parent, false, false);