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