]> git.sur5r.net Git - i3/i3/blob - src/con.c
draw empty title if WM_NAME is empty
[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 node is floating.
364  *
365  */
366 bool con_is_floating(Con *con) {
367     assert(con != NULL);
368     DLOG("checking if con %p is floating\n", con);
369     return (con->floating >= FLOATING_AUTO_ON);
370 }
371
372 /*
373  * Checks if the given container is either floating or inside some floating
374  * container. It returns the FLOATING_CON container.
375  *
376  */
377 Con *con_inside_floating(Con *con) {
378     assert(con != NULL);
379     if (con->type == CT_FLOATING_CON)
380         return con;
381
382     if (con->floating >= FLOATING_AUTO_ON)
383         return con->parent;
384
385     if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT)
386         return NULL;
387
388     return con_inside_floating(con->parent);
389 }
390
391 /*
392  * Checks if the given container is inside a focused container.
393  *
394  */
395 bool con_inside_focused(Con *con) {
396     if (con == focused)
397         return true;
398     if (!con->parent)
399         return false;
400     return con_inside_focused(con->parent);
401 }
402
403 /*
404  * Returns the container with the given client window ID or NULL if no such
405  * container exists.
406  *
407  */
408 Con *con_by_window_id(xcb_window_t window) {
409     Con *con;
410     TAILQ_FOREACH(con, &all_cons, all_cons)
411         if (con->window != NULL && con->window->id == window)
412             return con;
413     return NULL;
414 }
415
416 /*
417  * Returns the container with the given frame ID or NULL if no such container
418  * exists.
419  *
420  */
421 Con *con_by_frame_id(xcb_window_t frame) {
422     Con *con;
423     TAILQ_FOREACH(con, &all_cons, all_cons)
424         if (con->frame == frame)
425             return con;
426     return NULL;
427 }
428
429 /*
430  * Returns the first container below 'con' which wants to swallow this window
431  * TODO: priority
432  *
433  */
434 Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
435     Con *child;
436     Match *match;
437     //DLOG("searching con for window %p starting at con %p\n", window, con);
438     //DLOG("class == %s\n", window->class_class);
439
440     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
441         TAILQ_FOREACH(match, &(child->swallow_head), matches) {
442             if (!match_matches_window(match, window))
443                 continue;
444             if (store_match != NULL)
445                 *store_match = match;
446             return child;
447         }
448         Con *result = con_for_window(child, window, store_match);
449         if (result != NULL)
450             return result;
451     }
452
453     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
454         TAILQ_FOREACH(match, &(child->swallow_head), matches) {
455             if (!match_matches_window(match, window))
456                 continue;
457             if (store_match != NULL)
458                 *store_match = match;
459             return child;
460         }
461         Con *result = con_for_window(child, window, store_match);
462         if (result != NULL)
463             return result;
464     }
465
466     return NULL;
467 }
468
469 /*
470  * Returns the number of children of this container.
471  *
472  */
473 int con_num_children(Con *con) {
474     Con *child;
475     int children = 0;
476
477     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
478         children++;
479
480     return children;
481 }
482
483 /*
484  * Updates the percent attribute of the children of the given container. This
485  * function needs to be called when a window is added or removed from a
486  * container.
487  *
488  */
489 void con_fix_percent(Con *con) {
490     Con *child;
491     int children = con_num_children(con);
492
493     // calculate how much we have distributed and how many containers
494     // with a percentage set we have
495     double total = 0.0;
496     int children_with_percent = 0;
497     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
498         if (child->percent > 0.0) {
499             total += child->percent;
500             ++children_with_percent;
501         }
502     }
503
504     // if there were children without a percentage set, set to a value that
505     // will make those children proportional to all others
506     if (children_with_percent != children) {
507         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
508             if (child->percent <= 0.0) {
509                 if (children_with_percent == 0)
510                     total += (child->percent = 1.0);
511                 else total += (child->percent = total / children_with_percent);
512             }
513         }
514     }
515
516     // if we got a zero, just distribute the space equally, otherwise
517     // distribute according to the proportions we got
518     if (total == 0.0) {
519         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
520             child->percent = 1.0 / children;
521     } else if (total != 1.0) {
522         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
523             child->percent /= total;
524     }
525 }
526
527 /*
528  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
529  * entered when there already is a fullscreen container on this workspace.
530  *
531  */
532 void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
533     Con *workspace, *fullscreen;
534
535     if (con->type == CT_WORKSPACE) {
536         DLOG("You cannot make a workspace fullscreen.\n");
537         return;
538     }
539
540     DLOG("toggling fullscreen for %p / %s\n", con, con->name);
541     if (con->fullscreen_mode == CF_NONE) {
542         /* 1: check if there already is a fullscreen con */
543         if (fullscreen_mode == CF_GLOBAL)
544             fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
545         else {
546             workspace = con_get_workspace(con);
547             fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
548         }
549         if (fullscreen != NULL) {
550             /* Disable fullscreen for the currently fullscreened
551              * container and enable it for the one the user wants
552              * to have in fullscreen mode. */
553             LOG("Disabling fullscreen for (%p/%s) upon user request\n",
554                 fullscreen, fullscreen->name);
555             fullscreen->fullscreen_mode = CF_NONE;
556         }
557
558         /* 2: enable fullscreen */
559         con->fullscreen_mode = fullscreen_mode;
560     } else {
561         /* 1: disable fullscreen */
562         con->fullscreen_mode = CF_NONE;
563     }
564
565     DLOG("mode now: %d\n", con->fullscreen_mode);
566
567     /* update _NET_WM_STATE if this container has a window */
568     /* TODO: when a window is assigned to a container which is already
569      * fullscreened, this state needs to be pushed to the client, too */
570     if (con->window == NULL)
571         return;
572
573     uint32_t values[1];
574     unsigned int num = 0;
575
576     if (con->fullscreen_mode != CF_NONE)
577         values[num++] = A__NET_WM_STATE_FULLSCREEN;
578
579     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
580                         A__NET_WM_STATE, XCB_ATOM_ATOM, 32, num, values);
581 }
582
583 /*
584  * Moves the given container to the currently focused container on the given
585  * workspace.
586  *
587  * The fix_coordinates flag will translate the current coordinates (offset from
588  * the monitor position basically) to appropriate coordinates on the
589  * destination workspace.
590  * Not enabling this behaviour comes in handy when this function gets called by
591  * floating_maybe_reassign_ws, which will only "move" a floating window when it
592  * *already* changed its coordinates to a different output.
593  *
594  * The dont_warp flag disables pointer warping and will be set when this
595  * function is called while dragging a floating window.
596  *
597  * TODO: is there a better place for this function?
598  *
599  */
600 void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp) {
601     if (con->type == CT_WORKSPACE) {
602         DLOG("Moving workspaces is not yet implemented.\n");
603         return;
604     }
605
606     /* Prevent moving if this would violate the fullscreen focus restrictions. */
607     if (!con_fullscreen_permits_focusing(workspace)) {
608         LOG("Cannot move out of a fullscreen container");
609         return;
610     }
611
612     if (con_is_floating(con)) {
613         DLOG("Using FLOATINGCON instead\n");
614         con = con->parent;
615     }
616
617     Con *source_ws = con_get_workspace(con);
618     if (workspace == source_ws) {
619         DLOG("Not moving, already there\n");
620         return;
621     }
622
623     /* Save the current workspace. So we can call workspace_show() by the end
624      * of this function. */
625     Con *current_ws = con_get_workspace(focused);
626
627     Con *source_output = con_get_output(con),
628         *dest_output = con_get_output(workspace);
629
630     /* 1: save the container which is going to be focused after the current
631      * container is moved away */
632     Con *focus_next = con_next_focused(con);
633
634     /* 2: get the focused container of this workspace */
635     Con *next = con_descend_focused(workspace);
636
637     /* 3: we go up one level, but only when next is a normal container */
638     if (next->type != CT_WORKSPACE) {
639         DLOG("next originally = %p / %s / type %d\n", next, next->name, next->type);
640         next = next->parent;
641     }
642
643     /* 4: if the target container is floating, we get the workspace instead.
644      * Only tiling windows need to get inserted next to the current container.
645      * */
646     Con *floatingcon = con_inside_floating(next);
647     if (floatingcon != NULL) {
648         DLOG("floatingcon, going up even further\n");
649         next = floatingcon->parent;
650     }
651
652     if (con->type == CT_FLOATING_CON) {
653         Con *ws = con_get_workspace(next);
654         DLOG("This is a floating window, using workspace %p / %s\n", ws, ws->name);
655         next = ws;
656     }
657
658     if (source_output != dest_output) {
659         /* Take the relative coordinates of the current output, then add them
660          * to the coordinate space of the correct output */
661         if (fix_coordinates && con->type == CT_FLOATING_CON) {
662             floating_fix_coordinates(con, &(source_output->rect), &(dest_output->rect));
663         } else DLOG("Not fixing coordinates, fix_coordinates flag = %d\n", fix_coordinates);
664
665         /* If moving to a visible workspace, call show so it can be considered
666          * focused. Must do before attaching because workspace_show checks to see
667          * if focused container is in its area. */
668         if (workspace_is_visible(workspace)) {
669             workspace_show(workspace);
670
671             /* Don’t warp if told so (when dragging floating windows with the
672              * mouse for example) */
673             if (dont_warp)
674                 x_set_warp_to(NULL);
675             else
676                 x_set_warp_to(&(con->rect));
677         }
678     }
679
680     DLOG("Re-attaching container to %p / %s\n", next, next->name);
681     /* 5: re-attach the con to the parent of this focused container */
682     Con *parent = con->parent;
683     con_detach(con);
684     con_attach(con, next, false);
685
686     /* 6: fix the percentages */
687     con_fix_percent(parent);
688     con->percent = 0.0;
689     con_fix_percent(next);
690
691     /* 7: focus the con on the target workspace (the X focus is only updated by
692      * calling tree_render(), so for the "real" focus this is a no-op).
693      * We don’t focus the con for i3 pseudo workspaces like __i3_scratch and
694      * we don’t focus when there is a fullscreen con on that workspace. */
695     if ((workspace->name[0] != '_' || workspace->name[1] != '_') &&
696         con_get_fullscreen_con(workspace, CF_OUTPUT) == NULL)
697         con_focus(con_descend_focused(con));
698
699     /* 8: when moving to a visible workspace on a different output, we keep the
700      * con focused. Otherwise, we leave the focus on the current workspace as we
701      * don’t want to focus invisible workspaces */
702     if (source_output != dest_output &&
703         workspace_is_visible(workspace) &&
704         workspace->name[0] != '_' &&
705         workspace->name[1] != '_') {
706         DLOG("Moved to a different output, focusing target\n");
707     } else {
708         /* Descend focus stack in case focus_next is a workspace which can
709          * occur if we move to the same workspace.  Also show current workspace
710          * to ensure it is focused. */
711         workspace_show(current_ws);
712
713         /* Set focus only if con was on current workspace before moving.
714          * Otherwise we would give focus to some window on different workspace. */
715         if (source_ws == current_ws)
716             con_focus(con_descend_focused(focus_next));
717     }
718
719     CALL(parent, on_remove_child);
720 }
721
722 /*
723  * Returns the orientation of the given container (for stacked containers,
724  * vertical orientation is used regardless of the actual orientation of the
725  * container).
726  *
727  */
728 int con_orientation(Con *con) {
729     switch (con->layout) {
730         case L_SPLITV:
731         /* stacking containers behave like they are in vertical orientation */
732         case L_STACKED:
733             return VERT;
734
735         case L_SPLITH:
736         /* tabbed containers behave like they are in vertical orientation */
737         case L_TABBED:
738             return HORIZ;
739
740         case L_DEFAULT:
741             DLOG("Someone called con_orientation() on a con with L_DEFAULT, this is a bug in the code.\n");
742             assert(false);
743             return HORIZ;
744
745         case L_DOCKAREA:
746         case L_OUTPUT:
747             DLOG("con_orientation() called on dockarea/output (%d) container %p\n", con->layout, con);
748             assert(false);
749             return HORIZ;
750
751         default:
752             DLOG("con_orientation() ran into default\n");
753             assert(false);
754     }
755 }
756
757 /*
758  * Returns the container which will be focused next when the given container
759  * is not available anymore. Called in tree_close and con_move_to_workspace
760  * to properly restore focus.
761  *
762  */
763 Con *con_next_focused(Con *con) {
764     Con *next;
765     /* floating containers are attached to a workspace, so we focus either the
766      * next floating container (if any) or the workspace itself. */
767     if (con->type == CT_FLOATING_CON) {
768         DLOG("selecting next for CT_FLOATING_CON\n");
769         next = TAILQ_NEXT(con, floating_windows);
770         DLOG("next = %p\n", next);
771         if (!next) {
772             next = TAILQ_PREV(con, floating_head, floating_windows);
773             DLOG("using prev, next = %p\n", next);
774         }
775         if (!next) {
776             Con *ws = con_get_workspace(con);
777             next = ws;
778             DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
779             while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
780                 next = TAILQ_FIRST(&(next->focus_head));
781                 if (next == con) {
782                     DLOG("skipping container itself, we want the next client\n");
783                     next = TAILQ_NEXT(next, focused);
784                 }
785             }
786             if (next == TAILQ_END(&(ws->focus_head))) {
787                 DLOG("Focus list empty, returning ws\n");
788                 next = ws;
789             }
790         } else {
791             /* Instead of returning the next CT_FLOATING_CON, we descend it to
792              * get an actual window to focus. */
793             next = con_descend_focused(next);
794         }
795         return next;
796     }
797
798     /* dock clients cannot be focused, so we focus the workspace instead */
799     if (con->parent->type == CT_DOCKAREA) {
800         DLOG("selecting workspace for dock client\n");
801         return con_descend_focused(output_get_content(con->parent->parent));
802     }
803
804     /* if 'con' is not the first entry in the focus stack, use the first one as
805      * it’s currently focused already */
806     Con *first = TAILQ_FIRST(&(con->parent->focus_head));
807     if (first != con) {
808         DLOG("Using first entry %p\n", first);
809         next = first;
810     } else {
811         /* try to focus the next container on the same level as this one or fall
812          * back to its parent */
813         if (!(next = TAILQ_NEXT(con, focused)))
814             next = con->parent;
815     }
816
817     /* now go down the focus stack as far as
818      * possible, excluding the current container */
819     while (!TAILQ_EMPTY(&(next->focus_head)) &&
820            TAILQ_FIRST(&(next->focus_head)) != con)
821         next = TAILQ_FIRST(&(next->focus_head));
822
823     return next;
824 }
825
826 /*
827  * Get the next/previous container in the specified orientation. This may
828  * travel up until it finds a container with suitable orientation.
829  *
830  */
831 Con *con_get_next(Con *con, char way, orientation_t orientation) {
832     DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
833     /* 1: get the first parent with the same orientation */
834     Con *cur = con;
835     while (con_orientation(cur->parent) != orientation) {
836         DLOG("need to go one level further up\n");
837         if (cur->parent->type == CT_WORKSPACE) {
838             LOG("that's a workspace, we can't go further up\n");
839             return NULL;
840         }
841         cur = cur->parent;
842     }
843
844     /* 2: chose next (or previous) */
845     Con *next;
846     if (way == 'n') {
847         next = TAILQ_NEXT(cur, nodes);
848         /* if we are at the end of the list, we need to wrap */
849         if (next == TAILQ_END(&(parent->nodes_head)))
850             return NULL;
851     } else {
852         next = TAILQ_PREV(cur, nodes_head, nodes);
853         /* if we are at the end of the list, we need to wrap */
854         if (next == TAILQ_END(&(cur->nodes_head)))
855             return NULL;
856     }
857     DLOG("next = %p\n", next);
858
859     return next;
860 }
861
862 /*
863  * Returns the focused con inside this client, descending the tree as far as
864  * possible. This comes in handy when attaching a con to a workspace at the
865  * currently focused position, for example.
866  *
867  */
868 Con *con_descend_focused(Con *con) {
869     Con *next = con;
870     while (next != focused && !TAILQ_EMPTY(&(next->focus_head)))
871         next = TAILQ_FIRST(&(next->focus_head));
872     return next;
873 }
874
875 /*
876  * Returns the focused con inside this client, descending the tree as far as
877  * possible. This comes in handy when attaching a con to a workspace at the
878  * currently focused position, for example.
879  *
880  * Works like con_descend_focused but considers only tiling cons.
881  *
882  */
883 Con *con_descend_tiling_focused(Con *con) {
884     Con *next = con;
885     Con *before;
886     Con *child;
887     if (next == focused)
888         return next;
889     do {
890         before = next;
891         TAILQ_FOREACH(child, &(next->focus_head), focused) {
892             if (child->type == CT_FLOATING_CON)
893                 continue;
894
895             next = child;
896             break;
897         }
898     } while (before != next && next != focused);
899     return next;
900 }
901
902 /*
903  * Returns the leftmost, rightmost, etc. container in sub-tree. For example, if
904  * direction is D_LEFT, then we return the rightmost container and if direction
905  * is D_RIGHT, we return the leftmost container.  This is because if we are
906  * moving D_LEFT, and thus want the rightmost container.
907  *
908  */
909 Con *con_descend_direction(Con *con, direction_t direction) {
910     Con *most = NULL;
911     int orientation = con_orientation(con);
912     DLOG("con_descend_direction(%p, orientation %d, direction %d)\n", con, orientation, direction);
913     if (direction == D_LEFT || direction == D_RIGHT) {
914         if (orientation == HORIZ) {
915             /* If the direction is horizontal, we can use either the first
916              * (D_RIGHT) or the last con (D_LEFT) */
917             if (direction == D_RIGHT)
918                 most = TAILQ_FIRST(&(con->nodes_head));
919             else most = TAILQ_LAST(&(con->nodes_head), nodes_head);
920         } else if (orientation == VERT) {
921             /* Wrong orientation. We use the last focused con. Within that con,
922              * we recurse to chose the left/right con or at least the last
923              * focused one. */
924             most = TAILQ_FIRST(&(con->focus_head));
925         } else {
926             /* If the con has no orientation set, it’s not a split container
927              * but a container with a client window, so stop recursing */
928             return con;
929         }
930     }
931
932     if (direction == D_UP || direction == D_DOWN) {
933         if (orientation == VERT) {
934             /* If the direction is vertical, we can use either the first
935              * (D_DOWN) or the last con (D_UP) */
936             if (direction == D_UP)
937                 most = TAILQ_LAST(&(con->nodes_head), nodes_head);
938             else most = TAILQ_FIRST(&(con->nodes_head));
939         } else if (orientation == HORIZ) {
940             /* Wrong orientation. We use the last focused con. Within that con,
941              * we recurse to chose the top/bottom con or at least the last
942              * focused one. */
943             most = TAILQ_FIRST(&(con->focus_head));
944         } else {
945             /* If the con has no orientation set, it’s not a split container
946              * but a container with a client window, so stop recursing */
947             return con;
948         }
949     }
950
951     if (!most)
952         return con;
953     return con_descend_direction(most, direction);
954 }
955
956 /*
957  * Returns a "relative" Rect which contains the amount of pixels that need to
958  * be added to the original Rect to get the final position (obviously the
959  * amount of pixels for normal, 1pixel and borderless are different).
960  *
961  */
962 Rect con_border_style_rect(Con *con) {
963     adjacent_t borders_to_hide = ADJ_NONE;
964     Rect result;
965     /* Shortcut to avoid calling con_adjacent_borders() on dock containers. */
966     int border_style = con_border_style(con);
967     if (border_style == BS_NONE)
968         return (Rect){ 0, 0, 0, 0 };
969     borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
970     switch (border_style) {
971     case BS_NORMAL:
972         result = (Rect){2, 0, -(2 * 2), -2};
973         if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
974             result.x -= 2;
975             result.width += 2;
976         }
977         if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
978             result.width += 2;
979         }
980         /* With normal borders we never hide the upper border */
981         if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
982             result.height += 2;
983         }
984         return result;
985
986     case BS_1PIXEL:
987         result = (Rect){1, 1, -2, -2};
988         if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
989             result.x -= 1;
990             result.width += 1;
991         }
992         if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
993             result.width += 1;
994         }
995         if (borders_to_hide & ADJ_UPPER_SCREEN_EDGE) {
996             result.y -= 1;
997             result.height += 1;
998         }
999         if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
1000             result.height += 1;
1001         }
1002         return result;
1003
1004     case BS_NONE:
1005         return (Rect){0, 0, 0, 0};
1006
1007     default:
1008         assert(false);
1009     }
1010 }
1011
1012 /*
1013  * Returns adjacent borders of the window. We need this if hide_edge_borders is
1014  * enabled.
1015  */
1016 adjacent_t con_adjacent_borders(Con *con) {
1017     adjacent_t result = ADJ_NONE;
1018     Con *workspace = con_get_workspace(con);
1019     if (con->rect.x == workspace->rect.x)
1020         result |= ADJ_LEFT_SCREEN_EDGE;
1021     if (con->rect.x + con->rect.width == workspace->rect.x + workspace->rect.width)
1022         result |= ADJ_RIGHT_SCREEN_EDGE;
1023     if (con->rect.y == workspace->rect.y)
1024         result |= ADJ_UPPER_SCREEN_EDGE;
1025     if (con->rect.y + con->rect.height == workspace->rect.y + workspace->rect.height)
1026         result |= ADJ_LOWER_SCREEN_EDGE;
1027     return result;
1028 }
1029
1030 /*
1031  * Use this function to get a container’s border style. This is important
1032  * because when inside a stack, the border style is always BS_NORMAL.
1033  * For tabbed mode, the same applies, with one exception: when the container is
1034  * borderless and the only element in the tabbed container, the border is not
1035  * rendered.
1036  *
1037  * For children of a CT_DOCKAREA, the border style is always none.
1038  *
1039  */
1040 int con_border_style(Con *con) {
1041     Con *fs = con_get_fullscreen_con(con->parent, CF_OUTPUT);
1042     if (fs == con) {
1043         DLOG("this one is fullscreen! overriding BS_NONE\n");
1044         return BS_NONE;
1045     }
1046
1047     if (con->parent->layout == L_STACKED)
1048         return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1049
1050     if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
1051         return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1052
1053     if (con->parent->type == CT_DOCKAREA)
1054         return BS_NONE;
1055
1056     return con->border_style;
1057 }
1058
1059 /*
1060  * Sets the given border style on con, correctly keeping the position/size of a
1061  * floating window.
1062  *
1063  */
1064 void con_set_border_style(Con *con, int border_style) {
1065     /* Handle the simple case: non-floating containerns */
1066     if (!con_is_floating(con)) {
1067         con->border_style = border_style;
1068         return;
1069     }
1070
1071     /* For floating containers, we want to keep the position/size of the
1072      * *window* itself. We first add the border pixels to con->rect to make
1073      * con->rect represent the absolute position of the window. Then, we change
1074      * the border and subtract the new border pixels. Afterwards, we update
1075      * parent->rect to contain con. */
1076     DLOG("This is a floating container\n");
1077
1078     Rect bsr = con_border_style_rect(con);
1079     con->rect.x += bsr.x;
1080     con->rect.y += bsr.y;
1081     con->rect.width += bsr.width;
1082     con->rect.height += bsr.height;
1083
1084     /* Change the border style, get new border/decoration values. */
1085     con->border_style = border_style;
1086     bsr = con_border_style_rect(con);
1087     int deco_height =
1088         (con->border_style == BS_NORMAL ? config.font.height + 5 : 0);
1089
1090     con->rect.x -= bsr.x;
1091     con->rect.y -= bsr.y;
1092     con->rect.width -= bsr.width;
1093     con->rect.height -= bsr.height;
1094
1095     Con *parent = con->parent;
1096     parent->rect.x = con->rect.x;
1097     parent->rect.y = con->rect.y - deco_height;
1098     parent->rect.width = con->rect.width;
1099     parent->rect.height = con->rect.height + deco_height;
1100 }
1101
1102 /*
1103  * This function changes the layout of a given container. Use it to handle
1104  * special cases like changing a whole workspace to stacked/tabbed (creates a
1105  * new split container before).
1106  *
1107  */
1108 void con_set_layout(Con *con, int layout) {
1109     DLOG("con_set_layout(%p, %d), con->type = %d\n",
1110          con, layout, con->type);
1111
1112     /* Users can focus workspaces, but not any higher in the hierarchy.
1113      * Focus on the workspace is a special case, since in every other case, the
1114      * user means "change the layout of the parent split container". */
1115     if (con->type != CT_WORKSPACE)
1116         con = con->parent;
1117
1118     /* We fill in last_split_layout when switching to a different layout
1119      * since there are many places in the code that don’t use
1120      * con_set_layout(). */
1121     if (con->layout == L_SPLITH || con->layout == L_SPLITV)
1122         con->last_split_layout = con->layout;
1123
1124     /* When the container type is CT_WORKSPACE, the user wants to change the
1125      * whole workspace into stacked/tabbed mode. To do this and still allow
1126      * intuitive operations (like level-up and then opening a new window), we
1127      * need to create a new split container. */
1128     if (con->type == CT_WORKSPACE &&
1129         (layout == L_STACKED || layout == L_TABBED)) {
1130         if (con_num_children(con) == 0) {
1131             DLOG("Setting workspace_layout to %d\n", layout);
1132             con->workspace_layout = layout;
1133         } else {
1134             DLOG("Creating new split container\n");
1135             /* 1: create a new split container */
1136             Con *new = con_new(NULL, NULL);
1137             new->parent = con;
1138
1139             /* 2: Set the requested layout on the split container and mark it as
1140              * split. */
1141             new->layout = layout;
1142             new->last_split_layout = con->last_split_layout;
1143             new->split = true;
1144
1145             Con *old_focused = TAILQ_FIRST(&(con->focus_head));
1146             if (old_focused == TAILQ_END(&(con->focus_head)))
1147                 old_focused = NULL;
1148
1149             /* 3: move the existing cons of this workspace below the new con */
1150             DLOG("Moving cons\n");
1151             Con *child;
1152             while (!TAILQ_EMPTY(&(con->nodes_head))) {
1153                 child = TAILQ_FIRST(&(con->nodes_head));
1154                 con_detach(child);
1155                 con_attach(child, new, true);
1156             }
1157
1158             /* 4: attach the new split container to the workspace */
1159             DLOG("Attaching new split to ws\n");
1160             con_attach(new, con, false);
1161
1162             if (old_focused)
1163                 con_focus(old_focused);
1164
1165             tree_flatten(croot);
1166         }
1167         con_force_split_parents_redraw(con);
1168         return;
1169     }
1170
1171     if (layout == L_DEFAULT) {
1172         /* Special case: the layout formerly known as "default" (in combination
1173          * with an orientation). Since we switched to splith/splitv layouts,
1174          * using the "default" layout (which "only" should happen when using
1175          * legacy configs) is using the last split layout (either splith or
1176          * splitv) in order to still do the same thing.
1177          *
1178          * Starting from v4.6 though, we will nag users about using "layout
1179          * default", and in v4.9 we will remove it entirely (with an
1180          * appropriate i3-migrate-config mechanism). */
1181         con->layout = con->last_split_layout;
1182         /* In case last_split_layout was not initialized… */
1183         if (con->layout == L_DEFAULT)
1184             con->layout = L_SPLITH;
1185     } else {
1186         con->layout = layout;
1187     }
1188     con_force_split_parents_redraw(con);
1189 }
1190
1191 /*
1192  * This function toggles the layout of a given container. toggle_mode can be
1193  * either 'default' (toggle only between stacked/tabbed/last_split_layout),
1194  * 'split' (toggle only between splitv/splith) or 'all' (toggle between all
1195  * layouts).
1196  *
1197  */
1198 void con_toggle_layout(Con *con, const char *toggle_mode) {
1199     Con *parent = con;
1200     /* Users can focus workspaces, but not any higher in the hierarchy.
1201      * Focus on the workspace is a special case, since in every other case, the
1202      * user means "change the layout of the parent split container". */
1203     if (con->type != CT_WORKSPACE)
1204         parent = con->parent;
1205     DLOG("con_toggle_layout(%p, %s), parent = %p\n", con, toggle_mode, parent);
1206
1207     if (strcmp(toggle_mode, "split") == 0) {
1208         /* Toggle between splits. When the current layout is not a split
1209          * layout, we just switch back to last_split_layout. Otherwise, we
1210          * change to the opposite split layout. */
1211         if (parent->layout != L_SPLITH && parent->layout != L_SPLITV)
1212             con_set_layout(con, parent->last_split_layout);
1213         else {
1214             if (parent->layout == L_SPLITH)
1215                 con_set_layout(con, L_SPLITV);
1216             else con_set_layout(con, L_SPLITH);
1217         }
1218     } else {
1219         if (parent->layout == L_STACKED)
1220             con_set_layout(con, L_TABBED);
1221         else if (parent->layout == L_TABBED) {
1222             if (strcmp(toggle_mode, "all") == 0)
1223                 con_set_layout(con, L_SPLITH);
1224             else con_set_layout(con, parent->last_split_layout);
1225         } else if (parent->layout == L_SPLITH || parent->layout == L_SPLITV) {
1226             if (strcmp(toggle_mode, "all") == 0) {
1227                 /* When toggling through all modes, we toggle between
1228                  * splith/splitv, whereas normally we just directly jump to
1229                  * stacked. */
1230                 if (parent->layout == L_SPLITH)
1231                     con_set_layout(con, L_SPLITV);
1232                 else con_set_layout(con, L_STACKED);
1233             } else {
1234                 con_set_layout(con, L_STACKED);
1235             }
1236         }
1237     }
1238 }
1239
1240 /*
1241  * Callback which will be called when removing a child from the given con.
1242  * Kills the container if it is empty and replaces it with the child if there
1243  * is exactly one child.
1244  *
1245  */
1246 static void con_on_remove_child(Con *con) {
1247     DLOG("on_remove_child\n");
1248
1249     /* Every container 'above' (in the hierarchy) the workspace content should
1250      * not be closed when the last child was removed */
1251     if (con->type == CT_OUTPUT ||
1252         con->type == CT_ROOT ||
1253         con->type == CT_DOCKAREA) {
1254         DLOG("not handling, type = %d\n", con->type);
1255         return;
1256     }
1257
1258     /* For workspaces, close them only if they're not visible anymore */
1259     if (con->type == CT_WORKSPACE) {
1260         if (TAILQ_EMPTY(&(con->focus_head)) && !workspace_is_visible(con)) {
1261             LOG("Closing old workspace (%p / %s), it is empty\n", con, con->name);
1262             tree_close(con, DONT_KILL_WINDOW, false, false);
1263             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
1264         }
1265         return;
1266     }
1267
1268     con_force_split_parents_redraw(con);
1269
1270     /* TODO: check if this container would swallow any other client and
1271      * don’t close it automatically. */
1272     int children = con_num_children(con);
1273     if (children == 0) {
1274         DLOG("Container empty, closing\n");
1275         tree_close(con, DONT_KILL_WINDOW, false, false);
1276         return;
1277     }
1278 }
1279
1280 /*
1281  * Determines the minimum size of the given con by looking at its children (for
1282  * split/stacked/tabbed cons). Will be called when resizing floating cons
1283  *
1284  */
1285 Rect con_minimum_size(Con *con) {
1286     DLOG("Determining minimum size for con %p\n", con);
1287
1288     if (con_is_leaf(con)) {
1289         DLOG("leaf node, returning 75x50\n");
1290         return (Rect){ 0, 0, 75, 50 };
1291     }
1292
1293     if (con->type == CT_FLOATING_CON) {
1294         DLOG("floating con\n");
1295         Con *child = TAILQ_FIRST(&(con->nodes_head));
1296         return con_minimum_size(child);
1297     }
1298
1299     if (con->layout == L_STACKED || con->layout == L_TABBED) {
1300         uint32_t max_width = 0, max_height = 0, deco_height = 0;
1301         Con *child;
1302         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1303             Rect min = con_minimum_size(child);
1304             deco_height += child->deco_rect.height;
1305             max_width = max(max_width, min.width);
1306             max_height = max(max_height, min.height);
1307         }
1308         DLOG("stacked/tabbed now, returning %d x %d + deco_rect = %d\n",
1309              max_width, max_height, deco_height);
1310         return (Rect){ 0, 0, max_width, max_height + deco_height };
1311     }
1312
1313     /* For horizontal/vertical split containers we sum up the width (h-split)
1314      * or height (v-split) and use the maximum of the height (h-split) or width
1315      * (v-split) as minimum size. */
1316     if (con->split) {
1317         uint32_t width = 0, height = 0;
1318         Con *child;
1319         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1320             Rect min = con_minimum_size(child);
1321             if (con->layout == L_SPLITH) {
1322                 width += min.width;
1323                 height = max(height, min.height);
1324             } else {
1325                 height += min.height;
1326                 width = max(width, min.width);
1327             }
1328         }
1329         DLOG("split container, returning width = %d x height = %d\n", width, height);
1330         return (Rect){ 0, 0, width, height };
1331     }
1332
1333     ELOG("Unhandled case, type = %d, layout = %d, split = %d\n",
1334          con->type, con->layout, con->split);
1335     assert(false);
1336 }
1337
1338 /*
1339  * Returns true if changing the focus to con would be allowed considering
1340  * the fullscreen focus constraints. Specifically, if a fullscreen container or
1341  * any of its descendants is focused, this function returns true if and only if
1342  * focusing con would mean that focus would still be visible on screen, i.e.,
1343  * the newly focused container would not be obscured by a fullscreen container.
1344  *
1345  * In the simplest case, if a fullscreen container or any of its descendants is
1346  * fullscreen, this functions returns true if con is the fullscreen container
1347  * itself or any of its descendants, as this means focus wouldn't escape the
1348  * boundaries of the fullscreen container.
1349  *
1350  * In case the fullscreen container is of type CF_OUTPUT, this function returns
1351  * true if con is on a different workspace, as focus wouldn't be obscured by
1352  * the fullscreen container that is constrained to a different workspace.
1353  *
1354  * Note that this same logic can be applied to moving containers. If a
1355  * container can be focused under the fullscreen focus constraints, it can also
1356  * become a parent or sibling to the currently focused container.
1357  *
1358  */
1359 bool con_fullscreen_permits_focusing(Con *con) {
1360     /* No focus, no problem. */
1361     if (!focused)
1362         return true;
1363
1364     /* Find the first fullscreen ascendent. */
1365     Con *fs = focused;
1366     while (fs && fs->fullscreen_mode == CF_NONE)
1367         fs = fs->parent;
1368
1369     /* fs must be non-NULL since the workspace con doesn’t have CF_NONE and
1370      * there always has to be a workspace con in the hierarchy. */
1371     assert(fs != NULL);
1372     /* The most common case is we hit the workspace level. In this
1373      * situation, changing focus is also harmless. */
1374     assert(fs->fullscreen_mode != CF_NONE);
1375     if (fs->type == CT_WORKSPACE)
1376         return true;
1377
1378     /* Allow it if the container itself is the fullscreen container. */
1379     if (con == fs)
1380         return true;
1381
1382     /* If fullscreen is per-output, the focus being in a different workspace is
1383      * sufficient to guarantee that change won't leave fullscreen in bad shape. */
1384     if (fs->fullscreen_mode == CF_OUTPUT &&
1385         con_get_workspace(con) != con_get_workspace(fs)) {
1386             return true;
1387     }
1388
1389     /* Allow it only if the container to be focused is contained within the
1390      * current fullscreen container. */
1391     do {
1392         if (con->parent == fs)
1393             return true;
1394         con = con->parent;
1395     } while (con);
1396
1397     /* Focusing con would hide it behind a fullscreen window, disallow it. */
1398     return false;
1399 }
1400
1401 /*
1402  *
1403  * Checks if the given container has an urgent child.
1404  *
1405  */
1406 bool con_has_urgent_child(Con *con) {
1407     Con *child;
1408
1409     if (con_is_leaf(con))
1410         return con->urgent;
1411
1412     /* We are not interested in floating windows since they can only be
1413      * attached to a workspace → nodes_head instead of focus_head */
1414     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1415         if (con_has_urgent_child(child))
1416             return true;
1417     }
1418
1419     return false;
1420 }
1421
1422 /*
1423  * Make all parent containers urgent if con is urgent or clear the urgent flag
1424  * of all parent containers if there are no more urgent children left.
1425  *
1426  */
1427 void con_update_parents_urgency(Con *con) {
1428     Con *parent = con->parent;
1429
1430     bool new_urgency_value = con->urgent;
1431     while (parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
1432         if (new_urgency_value) {
1433             parent->urgent = true;
1434         } else {
1435             /* We can only reset the urgency when the parent
1436              * has no other urgent children */
1437             if (!con_has_urgent_child(parent))
1438                 parent->urgent = false;
1439         }
1440         parent = parent->parent;
1441     }
1442 }
1443
1444 /*
1445  * Create a string representing the subtree under con.
1446  *
1447  */
1448 char *con_get_tree_representation(Con *con) {
1449     /* this code works as follows:
1450      *  1) create a string with the layout type (D/V/H/T/S) and an opening bracket
1451      *  2) append the tree representation of the children to the string
1452      *  3) add closing bracket
1453      *
1454      * The recursion ends when we hit a leaf, in which case we return the
1455      * class_instance of the contained window.
1456      */
1457
1458     /* end of recursion */
1459     if (con_is_leaf(con)) {
1460         if (!con->window)
1461             return sstrdup("nowin");
1462
1463         if (!con->window->class_instance)
1464             return sstrdup("noinstance");
1465
1466         return sstrdup(con->window->class_instance);
1467     }
1468
1469     char *buf;
1470     /* 1) add the Layout type to buf */
1471     if (con->layout == L_DEFAULT)
1472         buf = sstrdup("D[");
1473     else if (con->layout == L_SPLITV)
1474         buf = sstrdup("V[");
1475     else if (con->layout == L_SPLITH)
1476         buf = sstrdup("H[");
1477     else if (con->layout == L_TABBED)
1478         buf = sstrdup("T[");
1479     else if (con->layout == L_STACKED)
1480         buf = sstrdup("S[");
1481
1482     /* 2) append representation of children */
1483     Con *child;
1484     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1485         char *child_txt = con_get_tree_representation(child);
1486
1487         char *tmp_buf;
1488         sasprintf(&tmp_buf, "%s%s%s", buf,
1489                 (TAILQ_FIRST(&(con->nodes_head)) == child ? "" : " "), child_txt);
1490         free(buf);
1491         buf = tmp_buf;
1492     }
1493
1494     /* 3) close the brackets */
1495     char *complete_buf;
1496     sasprintf(&complete_buf, "%s]", buf);
1497     free(buf);
1498
1499     return complete_buf;
1500 }