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