]> git.sur5r.net Git - i3/i3/blob - src/con.c
acc5e8afc072da4557f7ab0040c63964d4f8a7f6
[i3/i3] / src / con.c
1 #undef I3__FILE__
2 #define I3__FILE__ "con.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * con.c: Functions which deal with containers directly (creating containers,
10  *        searching containers, getting specific properties from containers,
11  *        …).
12  *
13  */
14 #include "all.h"
15
16 char *colors[] = {
17     "#ff0000",
18     "#00FF00",
19     "#0000FF",
20     "#ff00ff",
21     "#00ffff",
22     "#ffff00",
23     "#aa0000",
24     "#00aa00",
25     "#0000aa",
26     "#aa00aa"
27 };
28
29 static void con_on_remove_child(Con *con);
30
31 /*
32  * force parent split containers to be redrawn
33  *
34  */
35 static void con_force_split_parents_redraw(Con *con) {
36     Con *parent = con;
37
38     while (parent && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
39         if (parent->split)
40             FREE(parent->deco_render_params);
41         parent = parent->parent;
42     }
43 }
44
45 /*
46  * Create a new container (and attach it to the given parent, if not NULL).
47  * This function initializes the data structures and creates the appropriate
48  * X11 IDs using x_con_init().
49  *
50  */
51 Con *con_new(Con *parent, i3Window *window) {
52     Con *new = scalloc(sizeof(Con));
53     new->on_remove_child = con_on_remove_child;
54     TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
55     new->type = CT_CON;
56     new->window = window;
57     new->border_style = config.default_border;
58     static int cnt = 0;
59     DLOG("opening window %d\n", cnt);
60
61     /* TODO: remove window coloring after test-phase */
62     DLOG("color %s\n", colors[cnt]);
63     new->name = strdup(colors[cnt]);
64     //uint32_t cp = get_colorpixel(colors[cnt]);
65     cnt++;
66     if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
67         cnt = 0;
68     if (window)
69         x_con_init(new, window->depth);
70     else
71         x_con_init(new, XCB_COPY_FROM_PARENT);
72
73     TAILQ_INIT(&(new->floating_head));
74     TAILQ_INIT(&(new->nodes_head));
75     TAILQ_INIT(&(new->focus_head));
76     TAILQ_INIT(&(new->swallow_head));
77
78     if (parent != NULL)
79         con_attach(new, parent, false);
80
81     return new;
82 }
83
84 /*
85  * Attaches the given container to the given parent. This happens when moving
86  * a container or when inserting a new container at a specific place in the
87  * tree.
88  *
89  * ignore_focus is to just insert the Con at the end (useful when creating a
90  * new split container *around* some containers, that is, detaching and
91  * attaching them in order without wanting to mess with the focus in between).
92  *
93  */
94 void con_attach(Con *con, Con *parent, bool ignore_focus) {
95     con->parent = parent;
96     Con *loop;
97     Con *current = NULL;
98     struct nodes_head *nodes_head = &(parent->nodes_head);
99     struct focus_head *focus_head = &(parent->focus_head);
100
101     /* Workspaces are handled differently: they need to be inserted at the
102      * right position. */
103     if (con->type == CT_WORKSPACE) {
104         DLOG("it's a workspace. num = %d\n", con->num);
105         if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
106             TAILQ_INSERT_TAIL(nodes_head, con, nodes);
107         } else {
108             current = TAILQ_FIRST(nodes_head);
109             if (con->num < current->num) {
110                 /* we need to insert the container at the beginning */
111                 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
112             } else {
113                 while (current->num != -1 && con->num > current->num) {
114                     current = TAILQ_NEXT(current, nodes);
115                     if (current == TAILQ_END(nodes_head)) {
116                         current = NULL;
117                         break;
118                     }
119                 }
120                 /* we need to insert con after current, if current is not NULL */
121                 if (current)
122                     TAILQ_INSERT_BEFORE(current, con, nodes);
123                 else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
124             }
125         }
126         goto add_to_focus_head;
127     }
128
129     if (con->type == CT_FLOATING_CON) {
130         DLOG("Inserting into floating containers\n");
131         TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
132     } else {
133         if (!ignore_focus) {
134             /* Get the first tiling container in focus stack */
135             TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
136                 if (loop->type == CT_FLOATING_CON)
137                     continue;
138                 current = loop;
139                 break;
140             }
141         }
142
143         /* When the container is not a split container (but contains a window)
144          * and is attached to a workspace, we check if the user configured a
145          * workspace_layout. This is done in workspace_attach_to, which will
146          * provide us with the container to which we should attach (either the
147          * workspace or a new split container with the configured
148          * workspace_layout).
149          */
150         if (con->window != NULL &&
151             parent->type == CT_WORKSPACE &&
152             parent->workspace_layout != L_DEFAULT) {
153             DLOG("Parent is a workspace. Applying default layout...\n");
154             Con *target = workspace_attach_to(parent);
155
156             /* Attach the original con to this new split con instead */
157             nodes_head = &(target->nodes_head);
158             focus_head = &(target->focus_head);
159             con->parent = target;
160             current = NULL;
161
162             DLOG("done\n");
163         }
164
165         /* Insert the container after the tiling container, if found.
166          * When adding to a CT_OUTPUT, just append one after another. */
167         if (current && parent->type != CT_OUTPUT) {
168             DLOG("Inserting con = %p after last focused tiling con %p\n",
169                  con, current);
170             TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
171         } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
172     }
173
174 add_to_focus_head:
175     /* We insert to the TAIL because con_focus() will correct this.
176      * This way, we have the option to insert Cons without having
177      * to focus them. */
178     TAILQ_INSERT_TAIL(focus_head, con, focused);
179     con_force_split_parents_redraw(con);
180 }
181
182 /*
183  * Detaches the given container from its current parent
184  *
185  */
186 void con_detach(Con *con) {
187     con_force_split_parents_redraw(con);
188     if (con->type == CT_FLOATING_CON) {
189         TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
190         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
191     } else {
192         TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
193         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
194     }
195 }
196
197 /*
198  * Sets input focus to the given container. Will be updated in X11 in the next
199  * run of x_push_changes().
200  *
201  */
202 void con_focus(Con *con) {
203     assert(con != NULL);
204     DLOG("con_focus = %p\n", con);
205
206     /* 1: set focused-pointer to the new con */
207     /* 2: exchange the position of the container in focus stack of the parent all the way up */
208     TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
209     TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
210     if (con->parent->parent != NULL)
211         con_focus(con->parent);
212
213     focused = con;
214     /* We can't blindly reset non-leaf containers since they might have
215      * other urgent children. Therefore we only reset leafs and propagate
216      * the changes upwards via con_update_parents_urgency() which does proper
217      * checks before resetting the urgency.
218      */
219     if (con->urgent && con_is_leaf(con)) {
220         con->urgent = false;
221         con_update_parents_urgency(con);
222         workspace_update_urgent_flag(con_get_workspace(con));
223     }
224 }
225
226 /*
227  * Returns true when this node is a leaf node (has no children)
228  *
229  */
230 bool con_is_leaf(Con *con) {
231     return TAILQ_EMPTY(&(con->nodes_head));
232 }
233
234 /*
235  * Returns true if this node accepts a window (if the node swallows windows,
236  * it might already have swallowed enough and cannot hold any more).
237  *
238  */
239 bool con_accepts_window(Con *con) {
240     /* 1: workspaces never accept direct windows */
241     if (con->type == CT_WORKSPACE)
242         return false;
243
244     if (con->split) {
245         DLOG("container %p does not accept windows, it is a split container.\n", con);
246         return false;
247     }
248
249     /* TODO: if this is a swallowing container, we need to check its max_clients */
250     return (con->window == NULL);
251 }
252
253 /*
254  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
255  * node is on.
256  *
257  */
258 Con *con_get_output(Con *con) {
259     Con *result = con;
260     while (result != NULL && result->type != CT_OUTPUT)
261         result = result->parent;
262     /* We must be able to get an output because focus can never be set higher
263      * in the tree (root node cannot be focused). */
264     assert(result != NULL);
265     return result;
266 }
267
268 /*
269  * Gets the workspace container this node is on.
270  *
271  */
272 Con *con_get_workspace(Con *con) {
273     Con *result = con;
274     while (result != NULL && result->type != CT_WORKSPACE)
275         result = result->parent;
276     return result;
277 }
278
279 /*
280  * Searches parenst of the given 'con' until it reaches one with the specified
281  * 'orientation'. Aborts when it comes across a floating_con.
282  *
283  */
284 Con *con_parent_with_orientation(Con *con, orientation_t orientation) {
285     DLOG("Searching for parent of Con %p with orientation %d\n", con, orientation);
286     Con *parent = con->parent;
287     if (parent->type == CT_FLOATING_CON)
288         return NULL;
289     while (con_orientation(parent) != orientation) {
290         DLOG("Need to go one level further up\n");
291         parent = parent->parent;
292         /* Abort when we reach a floating con, or an output con */
293         if (parent &&
294             (parent->type == CT_FLOATING_CON ||
295              parent->type == CT_OUTPUT ||
296              (parent->parent && parent->parent->type == CT_OUTPUT)))
297             parent = NULL;
298         if (parent == NULL)
299             break;
300     }
301     DLOG("Result: %p\n", parent);
302     return parent;
303 }
304
305 /*
306  * helper data structure for the breadth-first-search in
307  * con_get_fullscreen_con()
308  *
309  */
310 struct bfs_entry {
311     Con *con;
312
313     TAILQ_ENTRY(bfs_entry) entries;
314 };
315
316 /*
317  * Returns the first fullscreen node below this node.
318  *
319  */
320 Con *con_get_fullscreen_con(Con *con, int fullscreen_mode) {
321     Con *current, *child;
322
323     /* TODO: is breadth-first-search really appropriate? (check as soon as
324      * fullscreen levels and fullscreen for containers is implemented) */
325     TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
326     struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
327     entry->con = con;
328     TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
329
330     while (!TAILQ_EMPTY(&bfs_head)) {
331         entry = TAILQ_FIRST(&bfs_head);
332         current = entry->con;
333         if (current != con && current->fullscreen_mode == fullscreen_mode) {
334             /* empty the queue */
335             while (!TAILQ_EMPTY(&bfs_head)) {
336                 entry = TAILQ_FIRST(&bfs_head);
337                 TAILQ_REMOVE(&bfs_head, entry, entries);
338                 free(entry);
339             }
340             return current;
341         }
342
343         TAILQ_REMOVE(&bfs_head, entry, entries);
344         free(entry);
345
346         TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
347             entry = smalloc(sizeof(struct bfs_entry));
348             entry->con = child;
349             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
350         }
351
352         TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
353             entry = smalloc(sizeof(struct bfs_entry));
354             entry->con = child;
355             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
356         }
357     }
358
359     return NULL;
360 }
361
362 /**
363  * Returns true if the container is internal, such as __i3_scratch
364  *
365  */
366 bool con_is_internal(Con *con) {
367     return (con->name[0] == '_' && con->name[1] == '_');
368 }
369
370 /*
371  * Returns true if the node is floating.
372  *
373  */
374 bool con_is_floating(Con *con) {
375     assert(con != NULL);
376     DLOG("checking if con %p is floating\n", con);
377     return (con->floating >= FLOATING_AUTO_ON);
378 }
379
380 /*
381  * Checks if the given container is either floating or inside some floating
382  * container. It returns the FLOATING_CON container.
383  *
384  */
385 Con *con_inside_floating(Con *con) {
386     assert(con != NULL);
387     if (con->type == CT_FLOATING_CON)
388         return con;
389
390     if (con->floating >= FLOATING_AUTO_ON)
391         return con->parent;
392
393     if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT)
394         return NULL;
395
396     return con_inside_floating(con->parent);
397 }
398
399 /*
400  * Checks if the given container is inside a focused container.
401  *
402  */
403 bool con_inside_focused(Con *con) {
404     if (con == focused)
405         return true;
406     if (!con->parent)
407         return false;
408     return con_inside_focused(con->parent);
409 }
410
411 /*
412  * Returns the container with the given client window ID or NULL if no such
413  * container exists.
414  *
415  */
416 Con *con_by_window_id(xcb_window_t window) {
417     Con *con;
418     TAILQ_FOREACH(con, &all_cons, all_cons)
419         if (con->window != NULL && con->window->id == window)
420             return con;
421     return NULL;
422 }
423
424 /*
425  * Returns the container with the given frame ID or NULL if no such container
426  * exists.
427  *
428  */
429 Con *con_by_frame_id(xcb_window_t frame) {
430     Con *con;
431     TAILQ_FOREACH(con, &all_cons, all_cons)
432         if (con->frame == frame)
433             return con;
434     return NULL;
435 }
436
437 /*
438  * Returns the first container below 'con' which wants to swallow this window
439  * TODO: priority
440  *
441  */
442 Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
443     Con *child;
444     Match *match;
445     //DLOG("searching con for window %p starting at con %p\n", window, con);
446     //DLOG("class == %s\n", window->class_class);
447
448     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
449         TAILQ_FOREACH(match, &(child->swallow_head), matches) {
450             if (!match_matches_window(match, window))
451                 continue;
452             if (store_match != NULL)
453                 *store_match = match;
454             return child;
455         }
456         Con *result = con_for_window(child, window, store_match);
457         if (result != NULL)
458             return result;
459     }
460
461     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
462         TAILQ_FOREACH(match, &(child->swallow_head), matches) {
463             if (!match_matches_window(match, window))
464                 continue;
465             if (store_match != NULL)
466                 *store_match = match;
467             return child;
468         }
469         Con *result = con_for_window(child, window, store_match);
470         if (result != NULL)
471             return result;
472     }
473
474     return NULL;
475 }
476
477 /*
478  * Returns the number of children of this container.
479  *
480  */
481 int con_num_children(Con *con) {
482     Con *child;
483     int children = 0;
484
485     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
486         children++;
487
488     return children;
489 }
490
491 /*
492  * Updates the percent attribute of the children of the given container. This
493  * function needs to be called when a window is added or removed from a
494  * container.
495  *
496  */
497 void con_fix_percent(Con *con) {
498     Con *child;
499     int children = con_num_children(con);
500
501     // calculate how much we have distributed and how many containers
502     // with a percentage set we have
503     double total = 0.0;
504     int children_with_percent = 0;
505     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
506         if (child->percent > 0.0) {
507             total += child->percent;
508             ++children_with_percent;
509         }
510     }
511
512     // if there were children without a percentage set, set to a value that
513     // will make those children proportional to all others
514     if (children_with_percent != children) {
515         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
516             if (child->percent <= 0.0) {
517                 if (children_with_percent == 0)
518                     total += (child->percent = 1.0);
519                 else total += (child->percent = total / children_with_percent);
520             }
521         }
522     }
523
524     // if we got a zero, just distribute the space equally, otherwise
525     // distribute according to the proportions we got
526     if (total == 0.0) {
527         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
528             child->percent = 1.0 / children;
529     } else if (total != 1.0) {
530         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
531             child->percent /= total;
532     }
533 }
534
535 /*
536  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
537  * entered when there already is a fullscreen container on this workspace.
538  *
539  */
540 void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
541     Con *workspace, *fullscreen;
542
543     if (con->type == CT_WORKSPACE) {
544         DLOG("You cannot make a workspace fullscreen.\n");
545         return;
546     }
547
548     DLOG("toggling fullscreen for %p / %s\n", con, con->name);
549     if (con->fullscreen_mode == CF_NONE) {
550         /* 1: check if there already is a fullscreen con */
551         if (fullscreen_mode == CF_GLOBAL)
552             fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
553         else {
554             workspace = con_get_workspace(con);
555             fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
556         }
557         if (fullscreen != NULL) {
558             /* Disable fullscreen for the currently fullscreened
559              * container and enable it for the one the user wants
560              * to have in fullscreen mode. */
561             LOG("Disabling fullscreen for (%p/%s) upon user request\n",
562                 fullscreen, fullscreen->name);
563             fullscreen->fullscreen_mode = CF_NONE;
564         }
565
566         /* 2: enable fullscreen */
567         con->fullscreen_mode = fullscreen_mode;
568     } else {
569         /* 1: disable fullscreen */
570         con->fullscreen_mode = CF_NONE;
571     }
572
573     DLOG("mode now: %d\n", con->fullscreen_mode);
574
575     /* update _NET_WM_STATE if this container has a window */
576     /* TODO: when a window is assigned to a container which is already
577      * fullscreened, this state needs to be pushed to the client, too */
578     if (con->window == NULL)
579         return;
580
581     uint32_t values[1];
582     unsigned int num = 0;
583
584     if (con->fullscreen_mode != CF_NONE)
585         values[num++] = A__NET_WM_STATE_FULLSCREEN;
586
587     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
588                         A__NET_WM_STATE, XCB_ATOM_ATOM, 32, num, values);
589 }
590
591 /*
592  * Moves the given container to the currently focused container on the given
593  * workspace.
594  *
595  * The fix_coordinates flag will translate the current coordinates (offset from
596  * the monitor position basically) to appropriate coordinates on the
597  * destination workspace.
598  * Not enabling this behaviour comes in handy when this function gets called by
599  * floating_maybe_reassign_ws, which will only "move" a floating window when it
600  * *already* changed its coordinates to a different output.
601  *
602  * The dont_warp flag disables pointer warping and will be set when this
603  * function is called while dragging a floating window.
604  *
605  * TODO: is there a better place for this function?
606  *
607  */
608 void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp) {
609     if (con->type == CT_WORKSPACE) {
610         DLOG("Moving workspaces is not yet implemented.\n");
611         return;
612     }
613
614     /* Prevent moving if this would violate the fullscreen focus restrictions. */
615     if (!con_fullscreen_permits_focusing(workspace)) {
616         LOG("Cannot move out of a fullscreen container");
617         return;
618     }
619
620     if (con_is_floating(con)) {
621         DLOG("Using FLOATINGCON instead\n");
622         con = con->parent;
623     }
624
625     Con *source_ws = con_get_workspace(con);
626     if (workspace == source_ws) {
627         DLOG("Not moving, already there\n");
628         return;
629     }
630
631     /* Save the current workspace. So we can call workspace_show() by the end
632      * of this function. */
633     Con *current_ws = con_get_workspace(focused);
634
635     Con *source_output = con_get_output(con),
636         *dest_output = con_get_output(workspace);
637
638     /* 1: save the container which is going to be focused after the current
639      * container is moved away */
640     Con *focus_next = con_next_focused(con);
641
642     /* 2: get the focused container of this workspace */
643     Con *next = con_descend_focused(workspace);
644
645     /* 3: we go up one level, but only when next is a normal container */
646     if (next->type != CT_WORKSPACE) {
647         DLOG("next originally = %p / %s / type %d\n", next, next->name, next->type);
648         next = next->parent;
649     }
650
651     /* 4: if the target container is floating, we get the workspace instead.
652      * Only tiling windows need to get inserted next to the current container.
653      * */
654     Con *floatingcon = con_inside_floating(next);
655     if (floatingcon != NULL) {
656         DLOG("floatingcon, going up even further\n");
657         next = floatingcon->parent;
658     }
659
660     if (con->type == CT_FLOATING_CON) {
661         Con *ws = con_get_workspace(next);
662         DLOG("This is a floating window, using workspace %p / %s\n", ws, ws->name);
663         next = ws;
664     }
665
666     if (source_output != dest_output) {
667         /* Take the relative coordinates of the current output, then add them
668          * to the coordinate space of the correct output */
669         if (fix_coordinates && con->type == CT_FLOATING_CON) {
670             floating_fix_coordinates(con, &(source_output->rect), &(dest_output->rect));
671         } else DLOG("Not fixing coordinates, fix_coordinates flag = %d\n", fix_coordinates);
672
673         /* If moving to a visible workspace, call show so it can be considered
674          * focused. Must do before attaching because workspace_show checks to see
675          * if focused container is in its area. */
676         if (workspace_is_visible(workspace)) {
677             workspace_show(workspace);
678
679             /* Don’t warp if told so (when dragging floating windows with the
680              * mouse for example) */
681             if (dont_warp)
682                 x_set_warp_to(NULL);
683             else
684                 x_set_warp_to(&(con->rect));
685         }
686     }
687
688     DLOG("Re-attaching container to %p / %s\n", next, next->name);
689     /* 5: re-attach the con to the parent of this focused container */
690     Con *parent = con->parent;
691     con_detach(con);
692     con_attach(con, next, false);
693
694     /* 6: fix the percentages */
695     con_fix_percent(parent);
696     con->percent = 0.0;
697     con_fix_percent(next);
698
699     /* 7: focus the con on the target workspace (the X focus is only updated by
700      * calling tree_render(), so for the "real" focus this is a no-op).
701      * We don’t focus the con for i3 pseudo workspaces like __i3_scratch and
702      * we don’t focus when there is a fullscreen con on that workspace. */
703     if (!con_is_internal(workspace) &&
704         con_get_fullscreen_con(workspace, CF_OUTPUT) == NULL)
705         con_focus(con_descend_focused(con));
706
707     /* 8: when moving to a visible workspace on a different output, we keep the
708      * con focused. Otherwise, we leave the focus on the current workspace as we
709      * don’t want to focus invisible workspaces */
710     if (source_output != dest_output &&
711         workspace_is_visible(workspace) &&
712         !con_is_internal(workspace)) {
713         DLOG("Moved to a different output, focusing target\n");
714     } else {
715         /* Descend focus stack in case focus_next is a workspace which can
716          * occur if we move to the same workspace.  Also show current workspace
717          * to ensure it is focused. */
718         workspace_show(current_ws);
719
720         /* Set focus only if con was on current workspace before moving.
721          * Otherwise we would give focus to some window on different workspace. */
722         if (source_ws == current_ws)
723             con_focus(con_descend_focused(focus_next));
724     }
725
726     CALL(parent, on_remove_child);
727 }
728
729 /*
730  * Returns the orientation of the given container (for stacked containers,
731  * vertical orientation is used regardless of the actual orientation of the
732  * container).
733  *
734  */
735 int con_orientation(Con *con) {
736     switch (con->layout) {
737         case L_SPLITV:
738         /* stacking containers behave like they are in vertical orientation */
739         case L_STACKED:
740             return VERT;
741
742         case L_SPLITH:
743         /* tabbed containers behave like they are in vertical orientation */
744         case L_TABBED:
745             return HORIZ;
746
747         case L_DEFAULT:
748             DLOG("Someone called con_orientation() on a con with L_DEFAULT, this is a bug in the code.\n");
749             assert(false);
750             return HORIZ;
751
752         case L_DOCKAREA:
753         case L_OUTPUT:
754             DLOG("con_orientation() called on dockarea/output (%d) container %p\n", con->layout, con);
755             assert(false);
756             return HORIZ;
757
758         default:
759             DLOG("con_orientation() ran into default\n");
760             assert(false);
761     }
762 }
763
764 /*
765  * Returns the container which will be focused next when the given container
766  * is not available anymore. Called in tree_close and con_move_to_workspace
767  * to properly restore focus.
768  *
769  */
770 Con *con_next_focused(Con *con) {
771     Con *next;
772     /* floating containers are attached to a workspace, so we focus either the
773      * next floating container (if any) or the workspace itself. */
774     if (con->type == CT_FLOATING_CON) {
775         DLOG("selecting next for CT_FLOATING_CON\n");
776         next = TAILQ_NEXT(con, floating_windows);
777         DLOG("next = %p\n", next);
778         if (!next) {
779             next = TAILQ_PREV(con, floating_head, floating_windows);
780             DLOG("using prev, next = %p\n", next);
781         }
782         if (!next) {
783             Con *ws = con_get_workspace(con);
784             next = ws;
785             DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
786             while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
787                 next = TAILQ_FIRST(&(next->focus_head));
788                 if (next == con) {
789                     DLOG("skipping container itself, we want the next client\n");
790                     next = TAILQ_NEXT(next, focused);
791                 }
792             }
793             if (next == TAILQ_END(&(ws->focus_head))) {
794                 DLOG("Focus list empty, returning ws\n");
795                 next = ws;
796             }
797         } else {
798             /* Instead of returning the next CT_FLOATING_CON, we descend it to
799              * get an actual window to focus. */
800             next = con_descend_focused(next);
801         }
802         return next;
803     }
804
805     /* dock clients cannot be focused, so we focus the workspace instead */
806     if (con->parent->type == CT_DOCKAREA) {
807         DLOG("selecting workspace for dock client\n");
808         return con_descend_focused(output_get_content(con->parent->parent));
809     }
810
811     /* if 'con' is not the first entry in the focus stack, use the first one as
812      * it’s currently focused already */
813     Con *first = TAILQ_FIRST(&(con->parent->focus_head));
814     if (first != con) {
815         DLOG("Using first entry %p\n", first);
816         next = first;
817     } else {
818         /* try to focus the next container on the same level as this one or fall
819          * back to its parent */
820         if (!(next = TAILQ_NEXT(con, focused)))
821             next = con->parent;
822     }
823
824     /* now go down the focus stack as far as
825      * possible, excluding the current container */
826     while (!TAILQ_EMPTY(&(next->focus_head)) &&
827            TAILQ_FIRST(&(next->focus_head)) != con)
828         next = TAILQ_FIRST(&(next->focus_head));
829
830     return next;
831 }
832
833 /*
834  * Get the next/previous container in the specified orientation. This may
835  * travel up until it finds a container with suitable orientation.
836  *
837  */
838 Con *con_get_next(Con *con, char way, orientation_t orientation) {
839     DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
840     /* 1: get the first parent with the same orientation */
841     Con *cur = con;
842     while (con_orientation(cur->parent) != orientation) {
843         DLOG("need to go one level further up\n");
844         if (cur->parent->type == CT_WORKSPACE) {
845             LOG("that's a workspace, we can't go further up\n");
846             return NULL;
847         }
848         cur = cur->parent;
849     }
850
851     /* 2: chose next (or previous) */
852     Con *next;
853     if (way == 'n') {
854         next = TAILQ_NEXT(cur, nodes);
855         /* if we are at the end of the list, we need to wrap */
856         if (next == TAILQ_END(&(parent->nodes_head)))
857             return NULL;
858     } else {
859         next = TAILQ_PREV(cur, nodes_head, nodes);
860         /* if we are at the end of the list, we need to wrap */
861         if (next == TAILQ_END(&(cur->nodes_head)))
862             return NULL;
863     }
864     DLOG("next = %p\n", next);
865
866     return next;
867 }
868
869 /*
870  * Returns the focused con inside this client, descending the tree as far as
871  * possible. This comes in handy when attaching a con to a workspace at the
872  * currently focused position, for example.
873  *
874  */
875 Con *con_descend_focused(Con *con) {
876     Con *next = con;
877     while (next != focused && !TAILQ_EMPTY(&(next->focus_head)))
878         next = TAILQ_FIRST(&(next->focus_head));
879     return next;
880 }
881
882 /*
883  * Returns the focused con inside this client, descending the tree as far as
884  * possible. This comes in handy when attaching a con to a workspace at the
885  * currently focused position, for example.
886  *
887  * Works like con_descend_focused but considers only tiling cons.
888  *
889  */
890 Con *con_descend_tiling_focused(Con *con) {
891     Con *next = con;
892     Con *before;
893     Con *child;
894     if (next == focused)
895         return next;
896     do {
897         before = next;
898         TAILQ_FOREACH(child, &(next->focus_head), focused) {
899             if (child->type == CT_FLOATING_CON)
900                 continue;
901
902             next = child;
903             break;
904         }
905     } while (before != next && next != focused);
906     return next;
907 }
908
909 /*
910  * Returns the leftmost, rightmost, etc. container in sub-tree. For example, if
911  * direction is D_LEFT, then we return the rightmost container and if direction
912  * is D_RIGHT, we return the leftmost container.  This is because if we are
913  * moving D_LEFT, and thus want the rightmost container.
914  *
915  */
916 Con *con_descend_direction(Con *con, direction_t direction) {
917     Con *most = NULL;
918     int orientation = con_orientation(con);
919     DLOG("con_descend_direction(%p, orientation %d, direction %d)\n", con, orientation, direction);
920     if (direction == D_LEFT || direction == D_RIGHT) {
921         if (orientation == HORIZ) {
922             /* If the direction is horizontal, we can use either the first
923              * (D_RIGHT) or the last con (D_LEFT) */
924             if (direction == D_RIGHT)
925                 most = TAILQ_FIRST(&(con->nodes_head));
926             else most = TAILQ_LAST(&(con->nodes_head), nodes_head);
927         } else if (orientation == VERT) {
928             /* Wrong orientation. We use the last focused con. Within that con,
929              * we recurse to chose the left/right con or at least the last
930              * focused one. */
931             most = TAILQ_FIRST(&(con->focus_head));
932         } else {
933             /* If the con has no orientation set, it’s not a split container
934              * but a container with a client window, so stop recursing */
935             return con;
936         }
937     }
938
939     if (direction == D_UP || direction == D_DOWN) {
940         if (orientation == VERT) {
941             /* If the direction is vertical, we can use either the first
942              * (D_DOWN) or the last con (D_UP) */
943             if (direction == D_UP)
944                 most = TAILQ_LAST(&(con->nodes_head), nodes_head);
945             else most = TAILQ_FIRST(&(con->nodes_head));
946         } else if (orientation == HORIZ) {
947             /* Wrong orientation. We use the last focused con. Within that con,
948              * we recurse to chose the top/bottom con or at least the last
949              * focused one. */
950             most = TAILQ_FIRST(&(con->focus_head));
951         } else {
952             /* If the con has no orientation set, it’s not a split container
953              * but a container with a client window, so stop recursing */
954             return con;
955         }
956     }
957
958     if (!most)
959         return con;
960     return con_descend_direction(most, direction);
961 }
962
963 /*
964  * Returns a "relative" Rect which contains the amount of pixels that need to
965  * be added to the original Rect to get the final position (obviously the
966  * amount of pixels for normal, 1pixel and borderless are different).
967  *
968  */
969 Rect con_border_style_rect(Con *con) {
970     adjacent_t borders_to_hide = ADJ_NONE;
971     Rect result;
972     /* Shortcut to avoid calling con_adjacent_borders() on dock containers. */
973     int border_style = con_border_style(con);
974     if (border_style == BS_NONE)
975         return (Rect){ 0, 0, 0, 0 };
976     borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
977     switch (border_style) {
978     case BS_NORMAL:
979         result = (Rect){2, 0, -(2 * 2), -2};
980         if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
981             result.x -= 2;
982             result.width += 2;
983         }
984         if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
985             result.width += 2;
986         }
987         /* With normal borders we never hide the upper border */
988         if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
989             result.height += 2;
990         }
991         return result;
992
993     case BS_1PIXEL:
994         result = (Rect){1, 1, -2, -2};
995         if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
996             result.x -= 1;
997             result.width += 1;
998         }
999         if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
1000             result.width += 1;
1001         }
1002         if (borders_to_hide & ADJ_UPPER_SCREEN_EDGE) {
1003             result.y -= 1;
1004             result.height += 1;
1005         }
1006         if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
1007             result.height += 1;
1008         }
1009         return result;
1010
1011     case BS_NONE:
1012         return (Rect){0, 0, 0, 0};
1013
1014     default:
1015         assert(false);
1016     }
1017 }
1018
1019 /*
1020  * Returns adjacent borders of the window. We need this if hide_edge_borders is
1021  * enabled.
1022  */
1023 adjacent_t con_adjacent_borders(Con *con) {
1024     adjacent_t result = ADJ_NONE;
1025     Con *workspace = con_get_workspace(con);
1026     if (con->rect.x == workspace->rect.x)
1027         result |= ADJ_LEFT_SCREEN_EDGE;
1028     if (con->rect.x + con->rect.width == workspace->rect.x + workspace->rect.width)
1029         result |= ADJ_RIGHT_SCREEN_EDGE;
1030     if (con->rect.y == workspace->rect.y)
1031         result |= ADJ_UPPER_SCREEN_EDGE;
1032     if (con->rect.y + con->rect.height == workspace->rect.y + workspace->rect.height)
1033         result |= ADJ_LOWER_SCREEN_EDGE;
1034     return result;
1035 }
1036
1037 /*
1038  * Use this function to get a container’s border style. This is important
1039  * because when inside a stack, the border style is always BS_NORMAL.
1040  * For tabbed mode, the same applies, with one exception: when the container is
1041  * borderless and the only element in the tabbed container, the border is not
1042  * rendered.
1043  *
1044  * For children of a CT_DOCKAREA, the border style is always none.
1045  *
1046  */
1047 int con_border_style(Con *con) {
1048     Con *fs = con_get_fullscreen_con(con->parent, CF_OUTPUT);
1049     if (fs == con) {
1050         DLOG("this one is fullscreen! overriding BS_NONE\n");
1051         return BS_NONE;
1052     }
1053
1054     if (con->parent->layout == L_STACKED)
1055         return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1056
1057     if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
1058         return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1059
1060     if (con->parent->type == CT_DOCKAREA)
1061         return BS_NONE;
1062
1063     return con->border_style;
1064 }
1065
1066 /*
1067  * Sets the given border style on con, correctly keeping the position/size of a
1068  * floating window.
1069  *
1070  */
1071 void con_set_border_style(Con *con, int border_style) {
1072     /* Handle the simple case: non-floating containerns */
1073     if (!con_is_floating(con)) {
1074         con->border_style = border_style;
1075         return;
1076     }
1077
1078     /* For floating containers, we want to keep the position/size of the
1079      * *window* itself. We first add the border pixels to con->rect to make
1080      * con->rect represent the absolute position of the window. Then, we change
1081      * the border and subtract the new border pixels. Afterwards, we update
1082      * parent->rect to contain con. */
1083     DLOG("This is a floating container\n");
1084
1085     Rect bsr = con_border_style_rect(con);
1086     con->rect.x += bsr.x;
1087     con->rect.y += bsr.y;
1088     con->rect.width += bsr.width;
1089     con->rect.height += bsr.height;
1090
1091     /* Change the border style, get new border/decoration values. */
1092     con->border_style = border_style;
1093     bsr = con_border_style_rect(con);
1094     int deco_height =
1095         (con->border_style == BS_NORMAL ? config.font.height + 5 : 0);
1096
1097     con->rect.x -= bsr.x;
1098     con->rect.y -= bsr.y;
1099     con->rect.width -= bsr.width;
1100     con->rect.height -= bsr.height;
1101
1102     Con *parent = con->parent;
1103     parent->rect.x = con->rect.x;
1104     parent->rect.y = con->rect.y - deco_height;
1105     parent->rect.width = con->rect.width;
1106     parent->rect.height = con->rect.height + deco_height;
1107 }
1108
1109 /*
1110  * This function changes the layout of a given container. Use it to handle
1111  * special cases like changing a whole workspace to stacked/tabbed (creates a
1112  * new split container before).
1113  *
1114  */
1115 void con_set_layout(Con *con, int layout) {
1116     DLOG("con_set_layout(%p, %d), con->type = %d\n",
1117          con, layout, con->type);
1118
1119     /* Users can focus workspaces, but not any higher in the hierarchy.
1120      * Focus on the workspace is a special case, since in every other case, the
1121      * user means "change the layout of the parent split container". */
1122     if (con->type != CT_WORKSPACE)
1123         con = con->parent;
1124
1125     /* We fill in last_split_layout when switching to a different layout
1126      * since there are many places in the code that don’t use
1127      * con_set_layout(). */
1128     if (con->layout == L_SPLITH || con->layout == L_SPLITV)
1129         con->last_split_layout = con->layout;
1130
1131     /* When the container type is CT_WORKSPACE, the user wants to change the
1132      * whole workspace into stacked/tabbed mode. To do this and still allow
1133      * intuitive operations (like level-up and then opening a new window), we
1134      * need to create a new split container. */
1135     if (con->type == CT_WORKSPACE &&
1136         (layout == L_STACKED || layout == L_TABBED)) {
1137         if (con_num_children(con) == 0) {
1138             DLOG("Setting workspace_layout to %d\n", layout);
1139             con->workspace_layout = layout;
1140         } else {
1141             DLOG("Creating new split container\n");
1142             /* 1: create a new split container */
1143             Con *new = con_new(NULL, NULL);
1144             new->parent = con;
1145
1146             /* 2: Set the requested layout on the split container and mark it as
1147              * split. */
1148             new->layout = layout;
1149             new->last_split_layout = con->last_split_layout;
1150             new->split = true;
1151
1152             Con *old_focused = TAILQ_FIRST(&(con->focus_head));
1153             if (old_focused == TAILQ_END(&(con->focus_head)))
1154                 old_focused = NULL;
1155
1156             /* 3: move the existing cons of this workspace below the new con */
1157             DLOG("Moving cons\n");
1158             Con *child;
1159             while (!TAILQ_EMPTY(&(con->nodes_head))) {
1160                 child = TAILQ_FIRST(&(con->nodes_head));
1161                 con_detach(child);
1162                 con_attach(child, new, true);
1163             }
1164
1165             /* 4: attach the new split container to the workspace */
1166             DLOG("Attaching new split to ws\n");
1167             con_attach(new, con, false);
1168
1169             if (old_focused)
1170                 con_focus(old_focused);
1171
1172             tree_flatten(croot);
1173         }
1174         con_force_split_parents_redraw(con);
1175         return;
1176     }
1177
1178     if (layout == L_DEFAULT) {
1179         /* Special case: the layout formerly known as "default" (in combination
1180          * with an orientation). Since we switched to splith/splitv layouts,
1181          * using the "default" layout (which "only" should happen when using
1182          * legacy configs) is using the last split layout (either splith or
1183          * splitv) in order to still do the same thing.
1184          *
1185          * Starting from v4.6 though, we will nag users about using "layout
1186          * default", and in v4.9 we will remove it entirely (with an
1187          * appropriate i3-migrate-config mechanism). */
1188         con->layout = con->last_split_layout;
1189         /* In case last_split_layout was not initialized… */
1190         if (con->layout == L_DEFAULT)
1191             con->layout = L_SPLITH;
1192     } else {
1193         con->layout = layout;
1194     }
1195     con_force_split_parents_redraw(con);
1196 }
1197
1198 /*
1199  * This function toggles the layout of a given container. toggle_mode can be
1200  * either 'default' (toggle only between stacked/tabbed/last_split_layout),
1201  * 'split' (toggle only between splitv/splith) or 'all' (toggle between all
1202  * layouts).
1203  *
1204  */
1205 void con_toggle_layout(Con *con, const char *toggle_mode) {
1206     Con *parent = con;
1207     /* Users can focus workspaces, but not any higher in the hierarchy.
1208      * Focus on the workspace is a special case, since in every other case, the
1209      * user means "change the layout of the parent split container". */
1210     if (con->type != CT_WORKSPACE)
1211         parent = con->parent;
1212     DLOG("con_toggle_layout(%p, %s), parent = %p\n", con, toggle_mode, parent);
1213
1214     if (strcmp(toggle_mode, "split") == 0) {
1215         /* Toggle between splits. When the current layout is not a split
1216          * layout, we just switch back to last_split_layout. Otherwise, we
1217          * change to the opposite split layout. */
1218         if (parent->layout != L_SPLITH && parent->layout != L_SPLITV)
1219             con_set_layout(con, parent->last_split_layout);
1220         else {
1221             if (parent->layout == L_SPLITH)
1222                 con_set_layout(con, L_SPLITV);
1223             else con_set_layout(con, L_SPLITH);
1224         }
1225     } else {
1226         if (parent->layout == L_STACKED)
1227             con_set_layout(con, L_TABBED);
1228         else if (parent->layout == L_TABBED) {
1229             if (strcmp(toggle_mode, "all") == 0)
1230                 con_set_layout(con, L_SPLITH);
1231             else con_set_layout(con, parent->last_split_layout);
1232         } else if (parent->layout == L_SPLITH || parent->layout == L_SPLITV) {
1233             if (strcmp(toggle_mode, "all") == 0) {
1234                 /* When toggling through all modes, we toggle between
1235                  * splith/splitv, whereas normally we just directly jump to
1236                  * stacked. */
1237                 if (parent->layout == L_SPLITH)
1238                     con_set_layout(con, L_SPLITV);
1239                 else con_set_layout(con, L_STACKED);
1240             } else {
1241                 con_set_layout(con, L_STACKED);
1242             }
1243         }
1244     }
1245 }
1246
1247 /*
1248  * Callback which will be called when removing a child from the given con.
1249  * Kills the container if it is empty and replaces it with the child if there
1250  * is exactly one child.
1251  *
1252  */
1253 static void con_on_remove_child(Con *con) {
1254     DLOG("on_remove_child\n");
1255
1256     /* Every container 'above' (in the hierarchy) the workspace content should
1257      * not be closed when the last child was removed */
1258     if (con->type == CT_OUTPUT ||
1259         con->type == CT_ROOT ||
1260         con->type == CT_DOCKAREA) {
1261         DLOG("not handling, type = %d\n", con->type);
1262         return;
1263     }
1264
1265     /* For workspaces, close them only if they're not visible anymore */
1266     if (con->type == CT_WORKSPACE) {
1267         if (TAILQ_EMPTY(&(con->focus_head)) && !workspace_is_visible(con)) {
1268             LOG("Closing old workspace (%p / %s), it is empty\n", con, con->name);
1269             tree_close(con, DONT_KILL_WINDOW, false, false);
1270             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
1271         }
1272         return;
1273     }
1274
1275     con_force_split_parents_redraw(con);
1276
1277     /* TODO: check if this container would swallow any other client and
1278      * don’t close it automatically. */
1279     int children = con_num_children(con);
1280     if (children == 0) {
1281         DLOG("Container empty, closing\n");
1282         tree_close(con, DONT_KILL_WINDOW, false, false);
1283         return;
1284     }
1285 }
1286
1287 /*
1288  * Determines the minimum size of the given con by looking at its children (for
1289  * split/stacked/tabbed cons). Will be called when resizing floating cons
1290  *
1291  */
1292 Rect con_minimum_size(Con *con) {
1293     DLOG("Determining minimum size for con %p\n", con);
1294
1295     if (con_is_leaf(con)) {
1296         DLOG("leaf node, returning 75x50\n");
1297         return (Rect){ 0, 0, 75, 50 };
1298     }
1299
1300     if (con->type == CT_FLOATING_CON) {
1301         DLOG("floating con\n");
1302         Con *child = TAILQ_FIRST(&(con->nodes_head));
1303         return con_minimum_size(child);
1304     }
1305
1306     if (con->layout == L_STACKED || con->layout == L_TABBED) {
1307         uint32_t max_width = 0, max_height = 0, deco_height = 0;
1308         Con *child;
1309         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1310             Rect min = con_minimum_size(child);
1311             deco_height += child->deco_rect.height;
1312             max_width = max(max_width, min.width);
1313             max_height = max(max_height, min.height);
1314         }
1315         DLOG("stacked/tabbed now, returning %d x %d + deco_rect = %d\n",
1316              max_width, max_height, deco_height);
1317         return (Rect){ 0, 0, max_width, max_height + deco_height };
1318     }
1319
1320     /* For horizontal/vertical split containers we sum up the width (h-split)
1321      * or height (v-split) and use the maximum of the height (h-split) or width
1322      * (v-split) as minimum size. */
1323     if (con->split) {
1324         uint32_t width = 0, height = 0;
1325         Con *child;
1326         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1327             Rect min = con_minimum_size(child);
1328             if (con->layout == L_SPLITH) {
1329                 width += min.width;
1330                 height = max(height, min.height);
1331             } else {
1332                 height += min.height;
1333                 width = max(width, min.width);
1334             }
1335         }
1336         DLOG("split container, returning width = %d x height = %d\n", width, height);
1337         return (Rect){ 0, 0, width, height };
1338     }
1339
1340     ELOG("Unhandled case, type = %d, layout = %d, split = %d\n",
1341          con->type, con->layout, con->split);
1342     assert(false);
1343 }
1344
1345 /*
1346  * Returns true if changing the focus to con would be allowed considering
1347  * the fullscreen focus constraints. Specifically, if a fullscreen container or
1348  * any of its descendants is focused, this function returns true if and only if
1349  * focusing con would mean that focus would still be visible on screen, i.e.,
1350  * the newly focused container would not be obscured by a fullscreen container.
1351  *
1352  * In the simplest case, if a fullscreen container or any of its descendants is
1353  * fullscreen, this functions returns true if con is the fullscreen container
1354  * itself or any of its descendants, as this means focus wouldn't escape the
1355  * boundaries of the fullscreen container.
1356  *
1357  * In case the fullscreen container is of type CF_OUTPUT, this function returns
1358  * true if con is on a different workspace, as focus wouldn't be obscured by
1359  * the fullscreen container that is constrained to a different workspace.
1360  *
1361  * Note that this same logic can be applied to moving containers. If a
1362  * container can be focused under the fullscreen focus constraints, it can also
1363  * become a parent or sibling to the currently focused container.
1364  *
1365  */
1366 bool con_fullscreen_permits_focusing(Con *con) {
1367     /* No focus, no problem. */
1368     if (!focused)
1369         return true;
1370
1371     /* Find the first fullscreen ascendent. */
1372     Con *fs = focused;
1373     while (fs && fs->fullscreen_mode == CF_NONE)
1374         fs = fs->parent;
1375
1376     /* fs must be non-NULL since the workspace con doesn’t have CF_NONE and
1377      * there always has to be a workspace con in the hierarchy. */
1378     assert(fs != NULL);
1379     /* The most common case is we hit the workspace level. In this
1380      * situation, changing focus is also harmless. */
1381     assert(fs->fullscreen_mode != CF_NONE);
1382     if (fs->type == CT_WORKSPACE)
1383         return true;
1384
1385     /* Allow it if the container itself is the fullscreen container. */
1386     if (con == fs)
1387         return true;
1388
1389     /* If fullscreen is per-output, the focus being in a different workspace is
1390      * sufficient to guarantee that change won't leave fullscreen in bad shape. */
1391     if (fs->fullscreen_mode == CF_OUTPUT &&
1392         con_get_workspace(con) != con_get_workspace(fs)) {
1393             return true;
1394     }
1395
1396     /* Allow it only if the container to be focused is contained within the
1397      * current fullscreen container. */
1398     do {
1399         if (con->parent == fs)
1400             return true;
1401         con = con->parent;
1402     } while (con);
1403
1404     /* Focusing con would hide it behind a fullscreen window, disallow it. */
1405     return false;
1406 }
1407
1408 /*
1409  *
1410  * Checks if the given container has an urgent child.
1411  *
1412  */
1413 bool con_has_urgent_child(Con *con) {
1414     Con *child;
1415
1416     if (con_is_leaf(con))
1417         return con->urgent;
1418
1419     /* We are not interested in floating windows since they can only be
1420      * attached to a workspace → nodes_head instead of focus_head */
1421     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1422         if (con_has_urgent_child(child))
1423             return true;
1424     }
1425
1426     return false;
1427 }
1428
1429 /*
1430  * Make all parent containers urgent if con is urgent or clear the urgent flag
1431  * of all parent containers if there are no more urgent children left.
1432  *
1433  */
1434 void con_update_parents_urgency(Con *con) {
1435     Con *parent = con->parent;
1436
1437     bool new_urgency_value = con->urgent;
1438     while (parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
1439         if (new_urgency_value) {
1440             parent->urgent = true;
1441         } else {
1442             /* We can only reset the urgency when the parent
1443              * has no other urgent children */
1444             if (!con_has_urgent_child(parent))
1445                 parent->urgent = false;
1446         }
1447         parent = parent->parent;
1448     }
1449 }
1450
1451 /*
1452  * Create a string representing the subtree under con.
1453  *
1454  */
1455 char *con_get_tree_representation(Con *con) {
1456     /* this code works as follows:
1457      *  1) create a string with the layout type (D/V/H/T/S) and an opening bracket
1458      *  2) append the tree representation of the children to the string
1459      *  3) add closing bracket
1460      *
1461      * The recursion ends when we hit a leaf, in which case we return the
1462      * class_instance of the contained window.
1463      */
1464
1465     /* end of recursion */
1466     if (con_is_leaf(con)) {
1467         if (!con->window)
1468             return sstrdup("nowin");
1469
1470         if (!con->window->class_instance)
1471             return sstrdup("noinstance");
1472
1473         return sstrdup(con->window->class_instance);
1474     }
1475
1476     char *buf;
1477     /* 1) add the Layout type to buf */
1478     if (con->layout == L_DEFAULT)
1479         buf = sstrdup("D[");
1480     else if (con->layout == L_SPLITV)
1481         buf = sstrdup("V[");
1482     else if (con->layout == L_SPLITH)
1483         buf = sstrdup("H[");
1484     else if (con->layout == L_TABBED)
1485         buf = sstrdup("T[");
1486     else if (con->layout == L_STACKED)
1487         buf = sstrdup("S[");
1488
1489     /* 2) append representation of children */
1490     Con *child;
1491     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1492         char *child_txt = con_get_tree_representation(child);
1493
1494         char *tmp_buf;
1495         sasprintf(&tmp_buf, "%s%s%s", buf,
1496                 (TAILQ_FIRST(&(con->nodes_head)) == child ? "" : " "), child_txt);
1497         free(buf);
1498         buf = tmp_buf;
1499     }
1500
1501     /* 3) close the brackets */
1502     char *complete_buf;
1503     sasprintf(&complete_buf, "%s]", buf);
1504     free(buf);
1505
1506     return complete_buf;
1507 }