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;
75 asprintf(&name, "[i3 con] output %s", oc->name);
79 /* add a workspace to this output */
81 ws->type = CT_WORKSPACE;
83 asprintf(&(ws->name), "%d", c);
87 asprintf(&name, "[i3 con] workspace %s", ws->name);
91 ws->fullscreen_mode = CF_OUTPUT;
92 ws->orientation = HORIZ;
99 * Opens an empty container in the current container
102 Con *tree_open_con(Con *con) {
104 /* every focusable Con has a parent (outputs have parent root) */
105 con = focused->parent;
106 /* If the parent is an output, we are on a workspace. In this case,
107 * the new container needs to be opened as a leaf of the workspace. */
108 if (con->type == CT_OUTPUT)
110 /* If the currently focused container is a floating container, we
111 * attach the new container to the workspace */
112 if (con->type == CT_FLOATING_CON)
118 /* 3: re-calculate child->percent for each child */
119 con_fix_percent(con, WINDOW_ADD);
121 /* 4: add a new container leaf to this con */
122 Con *new = con_new(con);
129 * vanishing is the container that is about to be closed (so any floating
130 * client which has old_parent == vanishing needs to be "re-parented").
133 static void fix_floating_parent(Con *con, Con *vanishing) {
136 if (con->old_parent == vanishing) {
137 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
138 vanishing, con, vanishing->parent);
139 con->old_parent = vanishing->parent;
142 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
143 fix_floating_parent(child, vanishing);
145 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
146 fix_floating_parent(child, vanishing);
150 * Closes the given container including all children
153 void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
154 Con *parent = con->parent;
156 /* check floating clients and adjust old_parent if necessary */
157 fix_floating_parent(croot, con);
159 /* Get the container which is next focused */
160 Con *next = con_next_focused(con);
162 DLOG("closing %p, kill_window = %d\n", con, kill_window);
164 /* We cannot use TAILQ_FOREACH because the children get deleted
165 * in their parent’s nodes_head */
166 while (!TAILQ_EMPTY(&(con->nodes_head))) {
167 child = TAILQ_FIRST(&(con->nodes_head));
168 DLOG("killing child=%p\n", child);
169 tree_close(child, kill_window, true);
172 if (con->window != NULL) {
174 x_window_kill(con->window->id);
176 /* un-parent the window */
177 xcb_reparent_window(conn, con->window->id, root, 0, 0);
178 /* TODO: client_unmap to set state to withdrawn */
184 /* kill the X11 part of this container */
188 con_fix_percent(parent, WINDOW_REMOVE);
190 if (con_is_floating(con)) {
191 DLOG("Container was floating, killing floating container\n");
192 tree_close(parent, false, false);
197 TAILQ_REMOVE(&all_cons, con, all_cons);
200 /* in the case of floating windows, we already focused another container
201 * when closing the parent, so we can exit now. */
205 DLOG("focusing %p / %s\n", next, next->name);
206 /* TODO: check if the container (or one of its children) was focused */
209 /* check if the parent container is empty now and close it */
210 if (!dont_kill_parent &&
211 parent->type != CT_WORKSPACE &&
212 TAILQ_EMPTY(&(parent->nodes_head))) {
213 DLOG("Closing empty parent container\n");
214 /* TODO: check if this container would swallow any other client and
215 * don’t close it automatically. */
216 tree_close(parent, false, false);
221 * Closes the current container using tree_close().
224 void tree_close_con() {
225 assert(focused != NULL);
226 if (focused->type == CT_WORKSPACE) {
227 LOG("Cannot close workspace\n");
232 tree_close(focused, true, false);
236 * Splits (horizontally or vertically) the given container by creating a new
237 * container which contains the old one and the future ones.
240 void tree_split(Con *con, orientation_t orientation) {
241 /* for a workspace, we just need to change orientation */
242 if (con->type == CT_WORKSPACE) {
243 DLOG("Workspace, simply changing orientation to %d\n", orientation);
244 con->orientation = orientation;
248 Con *parent = con->parent;
249 /* if we are in a container whose parent contains only one
250 * child (its split functionality is unused so far), we just change the
251 * orientation (more intuitive than splitting again) */
252 if (con_num_children(parent) == 1) {
253 parent->orientation = orientation;
254 DLOG("Just changing orientation of existing container\n");
258 DLOG("Splitting in orientation %d\n", orientation);
260 /* 2: replace it with a new Con */
261 Con *new = con_new(NULL);
262 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
263 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
264 new->parent = parent;
265 new->orientation = orientation;
267 /* 3: add it as a child to the new Con */
268 con_attach(con, new);
272 * Moves focus one level up.
276 /* We can focus up to the workspace, but not any higher in the tree */
277 if (focused->parent->type != CT_CON &&
278 focused->parent->type != CT_WORKSPACE) {
279 printf("cannot go up\n");
282 con_focus(focused->parent);
286 * Moves focus one level down.
290 /* Go down the focus stack of the current node */
291 Con *next = TAILQ_FIRST(&(focused->focus_head));
292 if (next == TAILQ_END(&(focused->focus_head))) {
293 printf("cannot go down\n");
299 static void mark_unmapped(Con *con) {
303 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
304 mark_unmapped(current);
305 if (con->type == CT_WORKSPACE) {
306 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
307 current->mapped = false;
308 Con *child = TAILQ_FIRST(&(current->nodes_head));
309 child->mapped = false;
315 * Renders the tree, that is rendering all outputs using render_con() and
316 * pushing the changes to X11 using x_push_changes().
323 printf("-- BEGIN RENDERING --\n");
324 /* Reset map state for all nodes in tree */
325 /* TODO: a nicer method to walk all nodes would be good, maybe? */
326 mark_unmapped(croot);
327 croot->mapped = true;
329 /* We start rendering at an output */
331 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
332 printf("output %p / %s\n", output, output->name);
333 render_con(output, false);
335 x_push_changes(croot);
336 printf("-- END RENDERING --\n");
340 * Changes focus in the given way (next/previous) and given orientation
341 * (horizontal/vertical).
344 void tree_next(char way, orientation_t orientation) {
345 /* 1: get the first parent with the same orientation */
346 Con *parent = focused->parent;
347 while (focused->type != CT_WORKSPACE &&
348 con_orientation(parent) != orientation) {
349 LOG("need to go one level further up\n");
350 /* if the current parent is an output, we are at a workspace
351 * and the orientation still does not match */
352 if (parent->type == CT_WORKSPACE)
354 parent = parent->parent;
356 Con *current = TAILQ_FIRST(&(parent->focus_head));
357 assert(current != TAILQ_END(&(parent->focus_head)));
359 /* 2: chose next (or previous) */
362 next = TAILQ_NEXT(current, nodes);
363 /* if we are at the end of the list, we need to wrap */
364 if (next == TAILQ_END(&(parent->nodes_head)))
365 next = TAILQ_FIRST(&(parent->nodes_head));
367 next = TAILQ_PREV(current, nodes_head, nodes);
368 /* if we are at the end of the list, we need to wrap */
369 if (next == TAILQ_END(&(parent->nodes_head)))
370 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
373 /* 3: focus choice comes in here. at the moment we will go down
374 * until we find a window */
375 /* TODO: check for window, atm we only go down as far as possible */
376 while (!TAILQ_EMPTY(&(next->focus_head)))
377 next = TAILQ_FIRST(&(next->focus_head));
379 DLOG("focusing %p\n", next);
384 * Moves the current container in the given way (next/previous) and given
385 * orientation (horizontal/vertical).
388 void tree_move(char way, orientation_t orientation) {
389 /* 1: get the first parent with the same orientation */
390 Con *parent = focused->parent;
391 Con *old_parent = parent;
392 if (focused->type == CT_WORKSPACE)
394 bool level_changed = false;
395 while (con_orientation(parent) != orientation) {
396 LOG("need to go one level further up\n");
397 /* if the current parent is an output, we are at a workspace
398 * and the orientation still does not match */
399 if (parent->type == CT_WORKSPACE)
401 parent = parent->parent;
402 level_changed = true;
404 Con *current = TAILQ_FIRST(&(parent->focus_head));
405 assert(current != TAILQ_END(&(parent->focus_head)));
407 /* 2: chose next (or previous) */
410 LOG("i would insert it after %p / %s\n", next, next->name);
411 if (!level_changed) {
412 next = TAILQ_NEXT(next, nodes);
413 if (next == TAILQ_END(&(next->parent->nodes_head))) {
414 LOG("cannot move further to the right\n");
418 /* if this is a split container, we need to go down */
419 while (!TAILQ_EMPTY(&(next->focus_head)))
420 next = TAILQ_FIRST(&(next->focus_head));
424 focused->parent = next->parent;
426 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
427 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
428 /* TODO: don’t influence focus handling? */
430 LOG("i would insert it before %p / %s\n", current, current->name);
431 bool gone_down = false;
432 if (!level_changed) {
433 next = TAILQ_PREV(next, nodes_head, nodes);
434 if (next == TAILQ_END(&(next->parent->nodes_head))) {
435 LOG("cannot move further\n");
439 /* if this is a split container, we need to go down */
440 while (!TAILQ_EMPTY(&(next->focus_head))) {
442 next = TAILQ_FIRST(&(next->focus_head));
447 focused->parent = next->parent;
449 /* After going down in the tree, we insert the container *after*
450 * the currently focused one even though the command used "before".
451 * This is to keep the user experience clear, since the before/after
452 * only signifies the direction of the movement on top-level */
454 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
455 else TAILQ_INSERT_BEFORE(next, focused, nodes);
456 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
457 /* TODO: don’t influence focus handling? */
460 if (con_num_children(old_parent) == 0) {
461 DLOG("Old container empty after moving. Let's close it\n");
462 tree_close(old_parent, false, false);