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
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;
63 TAILQ_FOREACH(output, &outputs, outputs) {
67 Con *oc = con_new(croot);
68 oc->name = strdup(output->name);
70 oc->rect = output->rect;
72 /* add a workspace to this output */
74 ws->type = CT_WORKSPACE;
75 ws->name = strdup("1");
76 ws->fullscreen_mode = CF_OUTPUT;
77 ws->orientation = HORIZ;
84 * Opens an empty container in the current container
87 Con *tree_open_con(Con *con) {
89 /* every focusable Con has a parent (outputs have parent root) */
90 con = focused->parent;
91 /* If the parent is an output, we are on a workspace. In this case,
92 * the new container needs to be opened as a leaf of the workspace. */
93 if (con->type == CT_OUTPUT)
99 /* 3: re-calculate child->percent for each child */
100 con_fix_percent(con, WINDOW_ADD);
102 /* 4: add a new container leaf to this con */
103 Con *new = con_new(con);
110 * vanishing is the container that is about to be closed (so any floating
111 * client which has old_parent == vanishing needs to be "re-parented").
114 static void fix_floating_parent(Con *con, Con *vanishing) {
117 if (con->old_parent == vanishing) {
118 LOG("Fixing vanishing old_parent (%p) of container %p to be %p\n",
119 vanishing, con, vanishing->parent);
120 con->old_parent = vanishing->parent;
123 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
124 fix_floating_parent(child, vanishing);
126 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
127 fix_floating_parent(child, vanishing);
131 * Closes the given container including all children
134 void tree_close(Con *con, bool kill_window) {
135 /* check floating clients and adjust old_parent if necessary */
136 fix_floating_parent(croot, con);
138 /* Get the container which is next focused */
140 if (con->type == CT_FLOATING_CON) {
141 next = TAILQ_NEXT(con, floating_windows);
142 if (next == TAILQ_END(&(con->parent->floating_head)))
145 next = TAILQ_NEXT(con, focused);
146 if (next == TAILQ_END(&(con->parent->nodes_head)))
150 DLOG("closing %p, kill_window = %d\n", con, kill_window);
152 /* We cannot use TAILQ_FOREACH because the children get deleted
153 * in their parent’s nodes_head */
154 while (!TAILQ_EMPTY(&(con->nodes_head))) {
155 child = TAILQ_FIRST(&(con->nodes_head));
156 tree_close(child, kill_window);
159 if (con->window != NULL) {
161 x_window_kill(con->window->id);
163 /* un-parent the window */
164 xcb_reparent_window(conn, con->window->id, root, 0, 0);
165 /* TODO: client_unmap to set state to withdrawn */
171 /* kill the X11 part of this container */
175 con_fix_percent(con->parent, WINDOW_REMOVE);
178 TAILQ_REMOVE(&all_cons, con, all_cons);
181 /* TODO: check if the container (or one of its children) was focused */
185 void tree_close_con() {
186 assert(focused != NULL);
187 if (focused->type == CT_WORKSPACE) {
188 LOG("Cannot close workspace\n");
193 tree_close(focused, true);
197 * Splits (horizontally or vertically) the given container by creating a new
198 * container which contains the old one and the future ones.
201 void tree_split(Con *con, orientation_t orientation) {
202 /* for a workspace, we just need to change orientation */
203 if (con->type == CT_WORKSPACE) {
204 DLOG("Workspace, simply changing orientation to %d\n", orientation);
205 con->orientation = orientation;
209 Con *parent = con->parent;
210 /* if we are in a container whose parent contains only one
211 * child and has the same orientation like we are trying to
212 * set, this operation is a no-op to not confuse the user */
213 if (parent->orientation == orientation &&
214 TAILQ_NEXT(con, nodes) == TAILQ_END(&(parent->nodes_head))) {
215 DLOG("Not splitting the same way again\n");
219 DLOG("Splitting in orientation %d\n", orientation);
221 /* 2: replace it with a new Con */
222 Con *new = con_new(NULL);
223 TAILQ_REPLACE(&(parent->nodes_head), con, new, nodes);
224 TAILQ_REPLACE(&(parent->focus_head), con, new, focused);
225 new->parent = parent;
226 new->orientation = orientation;
228 /* 3: add it as a child to the new Con */
229 con_attach(con, new);
233 /* We can focus up to the workspace, but not any higher in the tree */
234 if (focused->parent->type != CT_CON &&
235 focused->parent->type != CT_WORKSPACE) {
236 printf("cannot go up\n");
239 con_focus(focused->parent);
243 /* Go down the focus stack of the current node */
244 Con *next = TAILQ_FIRST(&(focused->focus_head));
245 if (next == TAILQ_END(&(focused->focus_head))) {
246 printf("cannot go down\n");
252 static void mark_unmapped(Con *con) {
256 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
257 mark_unmapped(current);
264 printf("-- BEGIN RENDERING --\n");
265 /* Reset map state for all nodes in tree */
266 /* TODO: a nicer method to walk all nodes would be good, maybe? */
267 mark_unmapped(croot);
268 croot->mapped = true;
270 /* We start rendering at an output */
272 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
273 printf("output %p / %s\n", output, output->name);
276 x_push_changes(croot);
277 printf("-- END RENDERING --\n");
280 void tree_next(char way, orientation_t orientation) {
281 /* 1: get the first parent with the same orientation */
282 Con *parent = focused->parent;
283 while (parent->orientation != orientation) {
284 LOG("need to go one level further up\n");
285 /* if the current parent is an output, we are at a workspace
286 * and the orientation still does not match */
287 if (parent->type == CT_WORKSPACE)
289 parent = parent->parent;
291 Con *current = TAILQ_FIRST(&(parent->focus_head));
292 assert(current != TAILQ_END(&(parent->focus_head)));
294 /* 2: chose next (or previous) */
297 next = TAILQ_NEXT(current, nodes);
298 /* if we are at the end of the list, we need to wrap */
299 if (next == TAILQ_END(&(parent->nodes_head)))
300 next = TAILQ_FIRST(&(parent->nodes_head));
302 next = TAILQ_PREV(current, nodes_head, nodes);
303 /* if we are at the end of the list, we need to wrap */
304 if (next == TAILQ_END(&(parent->nodes_head)))
305 next = TAILQ_LAST(&(parent->nodes_head), nodes_head);
308 /* 3: focus choice comes in here. at the moment we will go down
309 * until we find a window */
310 /* TODO: check for window, atm we only go down as far as possible */
311 while (!TAILQ_EMPTY(&(next->focus_head)))
312 next = TAILQ_FIRST(&(next->focus_head));
317 void tree_move(char way, orientation_t orientation) {
318 /* 1: get the first parent with the same orientation */
319 Con *parent = focused->parent;
320 if (focused->type == CT_WORKSPACE)
322 bool level_changed = false;
323 while (parent->orientation != orientation) {
324 LOG("need to go one level further up\n");
325 /* if the current parent is an output, we are at a workspace
326 * and the orientation still does not match */
327 if (parent->type == CT_WORKSPACE)
329 parent = parent->parent;
330 level_changed = true;
332 Con *current = TAILQ_FIRST(&(parent->focus_head));
333 assert(current != TAILQ_END(&(parent->focus_head)));
335 /* 2: chose next (or previous) */
338 LOG("i would insert it after %p / %s\n", next, next->name);
339 if (!level_changed) {
340 next = TAILQ_NEXT(next, nodes);
341 if (next == TAILQ_END(&(next->parent->nodes_head))) {
342 LOG("cannot move further to the right\n");
348 focused->parent = next->parent;
350 TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, focused, nodes);
351 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
352 /* TODO: don’t influence focus handling? */
354 LOG("i would insert it before %p / %s\n", current, current->name);
355 if (!level_changed) {
356 next = TAILQ_PREV(next, nodes_head, nodes);
357 if (next == TAILQ_END(&(next->parent->nodes_head))) {
358 LOG("cannot move further\n");
364 focused->parent = next->parent;
366 TAILQ_INSERT_BEFORE(next, focused, nodes);
367 TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
368 /* TODO: don’t influence focus handling? */