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("~/.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 = resolve_tilde("~/.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, bool dont_kill_parent) {
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 DLOG("killing child=%p\n", child);
162 tree_close(child, kill_window, true);
165 if (con->window != NULL) {
167 x_window_kill(con->window->id);
169 /* un-parent the window */
170 xcb_reparent_window(conn, con->window->id, root, 0, 0);
171 /* TODO: client_unmap to set state to withdrawn */
177 /* kill the X11 part of this container */
181 con_fix_percent(parent, WINDOW_REMOVE);
183 if (con_is_floating(con)) {
184 DLOG("Container was floating, killing floating container\n");
185 tree_close(parent, false, false);
190 TAILQ_REMOVE(&all_cons, con, all_cons);
193 /* in the case of floating windows, we already focused another container
194 * when closing the parent, so we can exit now. */
198 DLOG("focusing %p / %s\n", next, next->name);
199 /* TODO: check if the container (or one of its children) was focused */
202 /* check if the parent container is empty now and close it */
203 if (!dont_kill_parent &&
204 parent->type != CT_WORKSPACE &&
205 TAILQ_EMPTY(&(parent->nodes_head))) {
206 DLOG("Closing empty parent container\n");
207 /* TODO: check if this container would swallow any other client and
208 * don’t close it automatically. */
209 tree_close(parent, false, false);
214 * Closes the current container using tree_close().
217 void tree_close_con() {
218 assert(focused != NULL);
219 if (focused->type == CT_WORKSPACE) {
220 LOG("Cannot close workspace\n");
225 tree_close(focused, true, false);
229 * Splits (horizontally or vertically) the given container by creating a new
230 * container which contains the old one and the future ones.
233 void tree_split(Con *con, orientation_t orientation) {
234 /* for a workspace, we just need to change orientation */
235 if (con->type == CT_WORKSPACE) {
236 DLOG("Workspace, simply changing orientation to %d\n", orientation);
237 con->orientation = orientation;
241 Con *parent = con->parent;
242 /* if we are in a container whose parent contains only one
243 * child (its split functionality is unused so far), we just change the
244 * orientation (more intuitive than splitting again) */
245 if (con_num_children(parent) == 1) {
246 parent->orientation = orientation;
247 DLOG("Just changing orientation of existing container\n");
251 DLOG("Splitting in orientation %d\n", orientation);
253 /* 2: replace it with a new Con */
254 Con *new = con_new(NULL);
255 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
256 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
257 new->parent = parent;
258 new->orientation = orientation;
260 /* 3: add it as a child to the new Con */
261 con_attach(con, new);
265 * Moves focus one level up.
269 /* We can focus up to the workspace, but not any higher in the tree */
270 if (focused->parent->type != CT_CON &&
271 focused->parent->type != CT_WORKSPACE) {
272 printf("cannot go up\n");
275 con_focus(focused->parent);
279 * Moves focus one level down.
283 /* Go down the focus stack of the current node */
284 Con *next = TAILQ_FIRST(&(focused->focus_head));
285 if (next == TAILQ_END(&(focused->focus_head))) {
286 printf("cannot go down\n");
292 static void mark_unmapped(Con *con) {
294 DLOG("marking container %p unmapped\n", con);
297 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
298 mark_unmapped(current);
299 if (con->type == CT_WORKSPACE) {
300 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
301 DLOG("Marking unmapped for floating %p\n", current);
302 current->mapped = false;
303 Con *child = TAILQ_FIRST(&(current->nodes_head));
304 DLOG(" unmapping floating child %p\n", child);
305 child->mapped = false;
308 DLOG("mark_unmapped done\n");
312 * Renders the tree, that is rendering all outputs using render_con() and
313 * pushing the changes to X11 using x_push_changes().
320 printf("-- BEGIN RENDERING --\n");
321 /* Reset map state for all nodes in tree */
322 /* TODO: a nicer method to walk all nodes would be good, maybe? */
323 mark_unmapped(croot);
324 croot->mapped = true;
326 /* We start rendering at an output */
328 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
329 printf("output %p / %s\n", output, output->name);
332 x_push_changes(croot);
333 printf("-- END RENDERING --\n");
337 * Changes focus in the given way (next/previous) and given orientation
338 * (horizontal/vertical).
341 void tree_next(char way, orientation_t orientation) {
342 /* 1: get the first parent with the same orientation */
343 Con *parent = focused->parent;
344 while (focused->type != CT_WORKSPACE &&
345 con_orientation(parent) != orientation) {
346 LOG("need to go one level further up\n");
347 /* if the current parent is an output, we are at a workspace
348 * and the orientation still does not match */
349 if (parent->type == CT_WORKSPACE)
351 parent = parent->parent;
353 Con *current = TAILQ_FIRST(&(parent->focus_head));
354 assert(current != TAILQ_END(&(parent->focus_head)));
356 /* 2: chose next (or previous) */
359 next = TAILQ_NEXT(current, nodes);
360 /* if we are at the end of the list, we need to wrap */
361 if (next == TAILQ_END(&(parent->nodes_head)))
362 next = TAILQ_FIRST(&(parent->nodes_head));
364 next = TAILQ_PREV(current, nodes_head, nodes);
365 /* if we are at the end of the list, we need to wrap */
366 if (next == TAILQ_END(&(parent->nodes_head)))
367 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
370 /* 3: focus choice comes in here. at the moment we will go down
371 * until we find a window */
372 /* TODO: check for window, atm we only go down as far as possible */
373 while (!TAILQ_EMPTY(&(next->focus_head)))
374 next = TAILQ_FIRST(&(next->focus_head));
376 DLOG("focusing %p\n", next);
381 * Moves the current container in the given way (next/previous) and given
382 * orientation (horizontal/vertical).
385 void tree_move(char way, orientation_t orientation) {
386 /* 1: get the first parent with the same orientation */
387 Con *parent = focused->parent;
388 Con *old_parent = parent;
389 if (focused->type == CT_WORKSPACE)
391 bool level_changed = false;
392 while (con_orientation(parent) != orientation) {
393 LOG("need to go one level further up\n");
394 /* if the current parent is an output, we are at a workspace
395 * and the orientation still does not match */
396 if (parent->type == CT_WORKSPACE)
398 parent = parent->parent;
399 level_changed = true;
401 Con *current = TAILQ_FIRST(&(parent->focus_head));
402 assert(current != TAILQ_END(&(parent->focus_head)));
404 /* 2: chose next (or previous) */
407 LOG("i would insert it after %p / %s\n", next, next->name);
408 if (!level_changed) {
409 next = TAILQ_NEXT(next, nodes);
410 if (next == TAILQ_END(&(next->parent->nodes_head))) {
411 LOG("cannot move further to the right\n");
415 /* if this is a split container, we need to go down */
416 while (!TAILQ_EMPTY(&(next->focus_head)))
417 next = TAILQ_FIRST(&(next->focus_head));
421 focused->parent = next->parent;
423 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
424 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
425 /* TODO: don’t influence focus handling? */
427 LOG("i would insert it before %p / %s\n", current, current->name);
428 bool gone_down = false;
429 if (!level_changed) {
430 next = TAILQ_PREV(next, nodes_head, nodes);
431 if (next == TAILQ_END(&(next->parent->nodes_head))) {
432 LOG("cannot move further\n");
436 /* if this is a split container, we need to go down */
437 while (!TAILQ_EMPTY(&(next->focus_head))) {
439 next = TAILQ_FIRST(&(next->focus_head));
444 focused->parent = next->parent;
446 /* After going down in the tree, we insert the container *after*
447 * the currently focused one even though the command used "before".
448 * This is to keep the user experience clear, since the before/after
449 * only signifies the direction of the movement on top-level */
451 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
452 else TAILQ_INSERT_BEFORE(next, focused, nodes);
453 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
454 /* TODO: don’t influence focus handling? */
457 if (con_num_children(old_parent) == 0) {
458 DLOG("Old container empty after moving. Let's close it\n");
459 tree_close(old_parent, false, false);