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 = glob_path("~/.i3/_restart.json");
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);
30 char *old_restart = glob_path("~/.i3/_restart.json.old");
32 rename(globbed, old_restart);
36 printf("appended tree, using new root\n");
37 croot = TAILQ_FIRST(&(croot->nodes_head));
38 printf("new root = %p\n", croot);
39 Con *out = TAILQ_FIRST(&(croot->nodes_head));
40 printf("out = %p\n", out);
41 Con *ws = TAILQ_FIRST(&(out->nodes_head));
42 printf("ws = %p\n", ws);
49 * Initializes the tree by creating the root node, adding all RandR outputs
50 * to the tree (that means randr_init() has to be called before) and
51 * assigning a workspace to each RandR output.
57 croot = con_new(NULL);
59 croot->type = CT_ROOT;
64 TAILQ_FOREACH(output, &outputs, outputs) {
68 Con *oc = con_new(croot);
69 oc->name = strdup(output->name);
71 oc->rect = output->rect;
74 asprintf(&name, "[i3 con] output %s", oc->name);
78 /* add a workspace to this output */
80 ws->type = CT_WORKSPACE;
81 asprintf(&(ws->name), "%d", c);
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)
111 /* 3: re-calculate child->percent for each child */
112 con_fix_percent(con, WINDOW_ADD);
114 /* 4: add a new container leaf to this con */
115 Con *new = con_new(con);
122 * vanishing is the container that is about to be closed (so any floating
123 * client which has old_parent == vanishing needs to be "re-parented").
126 static void fix_floating_parent(Con *con, Con *vanishing) {
129 if (con->old_parent == vanishing) {
130 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
131 vanishing, con, vanishing->parent);
132 con->old_parent = vanishing->parent;
135 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
136 fix_floating_parent(child, vanishing);
138 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
139 fix_floating_parent(child, vanishing);
143 * Closes the given container including all children
146 void tree_close(Con *con, bool kill_window) {
147 Con *parent = con->parent;
149 /* check floating clients and adjust old_parent if necessary */
150 fix_floating_parent(croot, con);
152 /* Get the container which is next focused */
153 Con *next = con_next_focused(con);
155 DLOG("closing %p, kill_window = %d\n", con, kill_window);
157 /* We cannot use TAILQ_FOREACH because the children get deleted
158 * in their parent’s nodes_head */
159 while (!TAILQ_EMPTY(&(con->nodes_head))) {
160 child = TAILQ_FIRST(&(con->nodes_head));
161 tree_close(child, kill_window);
164 if (con->window != NULL) {
166 x_window_kill(con->window->id);
168 /* un-parent the window */
169 xcb_reparent_window(conn, con->window->id, root, 0, 0);
170 /* TODO: client_unmap to set state to withdrawn */
176 /* kill the X11 part of this container */
180 con_fix_percent(parent, WINDOW_REMOVE);
182 if (con_is_floating(con)) {
183 DLOG("Container was floating, killing floating container\n");
185 TAILQ_REMOVE(&(parent->parent->floating_head), parent, floating_windows);
186 TAILQ_REMOVE(&(parent->parent->focus_head), parent, focused);
187 tree_close(parent, false);
192 TAILQ_REMOVE(&all_cons, con, all_cons);
195 /* in the case of floating windows, we already focused another container
196 * when closing the parent, so we can exit now. */
200 DLOG("focusing %p / %s\n", next, next->name);
201 /* TODO: check if the container (or one of its children) was focused */
204 /* check if the parent container is empty now and close it */
205 if (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);
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);
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 and has the same orientation like we are trying to
245 * set, this operation is a no-op to not confuse the user */
246 if (con_orientation(parent) == orientation &&
247 TAILQ_NEXT(con, nodes) == TAILQ_END(&(parent->nodes_head))) {
248 DLOG("Not splitting the same way again\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: add it as a child to the new Con */
262 con_attach(con, new);
266 * Moves focus one level up.
270 /* We can focus up to the workspace, but not any higher in the tree */
271 if (focused->parent->type != CT_CON &&
272 focused->parent->type != CT_WORKSPACE) {
273 printf("cannot go up\n");
276 con_focus(focused->parent);
280 * Moves focus one level down.
284 /* Go down the focus stack of the current node */
285 Con *next = TAILQ_FIRST(&(focused->focus_head));
286 if (next == TAILQ_END(&(focused->focus_head))) {
287 printf("cannot go down\n");
293 static void mark_unmapped(Con *con) {
297 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
298 mark_unmapped(current);
302 * Renders the tree, that is rendering all outputs using render_con() and
303 * pushing the changes to X11 using x_push_changes().
310 printf("-- BEGIN RENDERING --\n");
311 /* Reset map state for all nodes in tree */
312 /* TODO: a nicer method to walk all nodes would be good, maybe? */
313 mark_unmapped(croot);
314 croot->mapped = true;
316 /* We start rendering at an output */
318 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
319 printf("output %p / %s\n", output, output->name);
322 x_push_changes(croot);
323 printf("-- END RENDERING --\n");
327 * Changes focus in the given way (next/previous) and given orientation
328 * (horizontal/vertical).
331 void tree_next(char way, orientation_t orientation) {
332 /* 1: get the first parent with the same orientation */
333 Con *parent = focused->parent;
334 while (focused->type != CT_WORKSPACE &&
335 con_orientation(parent) != orientation) {
336 LOG("need to go one level further up\n");
337 /* if the current parent is an output, we are at a workspace
338 * and the orientation still does not match */
339 if (parent->type == CT_WORKSPACE)
341 parent = parent->parent;
343 Con *current = TAILQ_FIRST(&(parent->focus_head));
344 assert(current != TAILQ_END(&(parent->focus_head)));
346 /* 2: chose next (or previous) */
349 next = TAILQ_NEXT(current, nodes);
350 /* if we are at the end of the list, we need to wrap */
351 if (next == TAILQ_END(&(parent->nodes_head)))
352 next = TAILQ_FIRST(&(parent->nodes_head));
354 next = TAILQ_PREV(current, nodes_head, nodes);
355 /* if we are at the end of the list, we need to wrap */
356 if (next == TAILQ_END(&(parent->nodes_head)))
357 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
360 /* 3: focus choice comes in here. at the moment we will go down
361 * until we find a window */
362 /* TODO: check for window, atm we only go down as far as possible */
363 while (!TAILQ_EMPTY(&(next->focus_head)))
364 next = TAILQ_FIRST(&(next->focus_head));
366 DLOG("focusing %p\n", next);
371 * Moves the current container in the given way (next/previous) and given
372 * orientation (horizontal/vertical).
375 void tree_move(char way, orientation_t orientation) {
376 /* 1: get the first parent with the same orientation */
377 Con *parent = focused->parent;
378 Con *old_parent = parent;
379 if (focused->type == CT_WORKSPACE)
381 bool level_changed = false;
382 while (con_orientation(parent) != orientation) {
383 LOG("need to go one level further up\n");
384 /* if the current parent is an output, we are at a workspace
385 * and the orientation still does not match */
386 if (parent->type == CT_WORKSPACE)
388 parent = parent->parent;
389 level_changed = true;
391 Con *current = TAILQ_FIRST(&(parent->focus_head));
392 assert(current != TAILQ_END(&(parent->focus_head)));
394 /* 2: chose next (or previous) */
397 LOG("i would insert it after %p / %s\n", next, next->name);
398 if (!level_changed) {
399 next = TAILQ_NEXT(next, nodes);
400 if (next == TAILQ_END(&(next->parent->nodes_head))) {
401 LOG("cannot move further to the right\n");
405 /* if this is a split container, we need to go down */
406 while (!TAILQ_EMPTY(&(next->focus_head)))
407 next = TAILQ_FIRST(&(next->focus_head));
411 focused->parent = next->parent;
413 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
414 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
415 /* TODO: don’t influence focus handling? */
417 LOG("i would insert it before %p / %s\n", current, current->name);
418 bool gone_down = false;
419 if (!level_changed) {
420 next = TAILQ_PREV(next, nodes_head, nodes);
421 if (next == TAILQ_END(&(next->parent->nodes_head))) {
422 LOG("cannot move further\n");
426 /* if this is a split container, we need to go down */
427 while (!TAILQ_EMPTY(&(next->focus_head))) {
429 next = TAILQ_FIRST(&(next->focus_head));
434 focused->parent = next->parent;
436 /* After going down in the tree, we insert the container *after*
437 * the currently focused one even though the command used "before".
438 * This is to keep the user experience clear, since the before/after
439 * only signifies the direction of the movement on top-level */
441 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
442 else TAILQ_INSERT_BEFORE(next, focused, nodes);
443 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
444 /* TODO: don’t influence focus handling? */
447 if (con_num_children(old_parent) == 0) {
448 DLOG("Old container empty after moving. Let's close it\n");
449 tree_close(old_parent, false);