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