2 #define I3__FILE__ "con.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
9 * con.c: Functions which deal with containers directly (creating containers,
10 * searching containers, getting specific properties from containers,
29 static void con_on_remove_child(Con *con);
32 * force parent split containers to be redrawn
35 static void con_force_split_parents_redraw(Con *con) {
38 while (parent && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
40 FREE(parent->deco_render_params);
41 parent = parent->parent;
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().
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);
57 new->border_style = config.default_border;
59 DLOG("opening window %d\n", cnt);
61 /* TODO: remove window coloring after test-phase */
62 DLOG("color %s\n", colors[cnt]);
63 new->name = strdup(colors[cnt]);
64 //uint32_t cp = get_colorpixel(colors[cnt]);
66 if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
69 x_con_init(new, window->depth);
71 x_con_init(new, XCB_COPY_FROM_PARENT);
73 TAILQ_INIT(&(new->floating_head));
74 TAILQ_INIT(&(new->nodes_head));
75 TAILQ_INIT(&(new->focus_head));
76 TAILQ_INIT(&(new->swallow_head));
79 con_attach(new, parent, false);
85 * Attaches the given container to the given parent. This happens when moving
86 * a container or when inserting a new container at a specific place in the
89 * ignore_focus is to just insert the Con at the end (useful when creating a
90 * new split container *around* some containers, that is, detaching and
91 * attaching them in order without wanting to mess with the focus in between).
94 void con_attach(Con *con, Con *parent, bool ignore_focus) {
98 struct nodes_head *nodes_head = &(parent->nodes_head);
99 struct focus_head *focus_head = &(parent->focus_head);
101 /* Workspaces are handled differently: they need to be inserted at the
103 if (con->type == CT_WORKSPACE) {
104 DLOG("it's a workspace. num = %d\n", con->num);
105 if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
106 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
108 current = TAILQ_FIRST(nodes_head);
109 if (con->num < current->num) {
110 /* we need to insert the container at the beginning */
111 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
113 while (current->num != -1 && con->num > current->num) {
114 current = TAILQ_NEXT(current, nodes);
115 if (current == TAILQ_END(nodes_head)) {
120 /* we need to insert con after current, if current is not NULL */
122 TAILQ_INSERT_BEFORE(current, con, nodes);
123 else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
126 goto add_to_focus_head;
129 if (con->type == CT_FLOATING_CON) {
130 DLOG("Inserting into floating containers\n");
131 TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
134 /* Get the first tiling container in focus stack */
135 TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
136 if (loop->type == CT_FLOATING_CON)
143 /* When the container is not a split container (but contains a window)
144 * and is attached to a workspace, we check if the user configured a
145 * workspace_layout. This is done in workspace_attach_to, which will
146 * provide us with the container to which we should attach (either the
147 * workspace or a new split container with the configured
150 if (con->window != NULL &&
151 parent->type == CT_WORKSPACE &&
152 parent->workspace_layout != L_DEFAULT) {
153 DLOG("Parent is a workspace. Applying default layout...\n");
154 Con *target = workspace_attach_to(parent);
156 /* Attach the original con to this new split con instead */
157 nodes_head = &(target->nodes_head);
158 focus_head = &(target->focus_head);
159 con->parent = target;
165 /* Insert the container after the tiling container, if found.
166 * When adding to a CT_OUTPUT, just append one after another. */
167 if (current && parent->type != CT_OUTPUT) {
168 DLOG("Inserting con = %p after last focused tiling con %p\n",
170 TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
171 } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
175 /* We insert to the TAIL because con_focus() will correct this.
176 * This way, we have the option to insert Cons without having
178 TAILQ_INSERT_TAIL(focus_head, con, focused);
179 con_force_split_parents_redraw(con);
183 * Detaches the given container from its current parent
186 void con_detach(Con *con) {
187 con_force_split_parents_redraw(con);
188 if (con->type == CT_FLOATING_CON) {
189 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
190 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
192 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
193 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
198 * Sets input focus to the given container. Will be updated in X11 in the next
199 * run of x_push_changes().
202 void con_focus(Con *con) {
204 DLOG("con_focus = %p\n", con);
206 /* 1: set focused-pointer to the new con */
207 /* 2: exchange the position of the container in focus stack of the parent all the way up */
208 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
209 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
210 if (con->parent->parent != NULL)
211 con_focus(con->parent);
214 /* We can't blindly reset non-leaf containers since they might have
215 * other urgent children. Therefore we only reset leafs and propagate
216 * the changes upwards via con_update_parents_urgency() which does proper
217 * checks before resetting the urgency.
219 if (con->urgent && con_is_leaf(con)) {
221 con_update_parents_urgency(con);
222 workspace_update_urgent_flag(con_get_workspace(con));
227 * Returns true when this node is a leaf node (has no children)
230 bool con_is_leaf(Con *con) {
231 return TAILQ_EMPTY(&(con->nodes_head));
235 * Returns true if this node accepts a window (if the node swallows windows,
236 * it might already have swallowed enough and cannot hold any more).
239 bool con_accepts_window(Con *con) {
240 /* 1: workspaces never accept direct windows */
241 if (con->type == CT_WORKSPACE)
245 DLOG("container %p does not accept windows, it is a split container.\n", con);
249 /* TODO: if this is a swallowing container, we need to check its max_clients */
250 return (con->window == NULL);
254 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
258 Con *con_get_output(Con *con) {
260 while (result != NULL && result->type != CT_OUTPUT)
261 result = result->parent;
262 /* We must be able to get an output because focus can never be set higher
263 * in the tree (root node cannot be focused). */
264 assert(result != NULL);
269 * Gets the workspace container this node is on.
272 Con *con_get_workspace(Con *con) {
274 while (result != NULL && result->type != CT_WORKSPACE)
275 result = result->parent;
280 * Searches parenst of the given 'con' until it reaches one with the specified
281 * 'orientation'. Aborts when it comes across a floating_con.
284 Con *con_parent_with_orientation(Con *con, orientation_t orientation) {
285 DLOG("Searching for parent of Con %p with orientation %d\n", con, orientation);
286 Con *parent = con->parent;
287 if (parent->type == CT_FLOATING_CON)
289 while (con_orientation(parent) != orientation) {
290 DLOG("Need to go one level further up\n");
291 parent = parent->parent;
292 /* Abort when we reach a floating con, or an output con */
294 (parent->type == CT_FLOATING_CON ||
295 parent->type == CT_OUTPUT ||
296 (parent->parent && parent->parent->type == CT_OUTPUT)))
301 DLOG("Result: %p\n", parent);
306 * helper data structure for the breadth-first-search in
307 * con_get_fullscreen_con()
313 TAILQ_ENTRY(bfs_entry) entries;
317 * Returns the first fullscreen node below this node.
320 Con *con_get_fullscreen_con(Con *con, int fullscreen_mode) {
321 Con *current, *child;
323 /* TODO: is breadth-first-search really appropriate? (check as soon as
324 * fullscreen levels and fullscreen for containers is implemented) */
325 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
326 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
328 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
330 while (!TAILQ_EMPTY(&bfs_head)) {
331 entry = TAILQ_FIRST(&bfs_head);
332 current = entry->con;
333 if (current != con && current->fullscreen_mode == fullscreen_mode) {
334 /* empty the queue */
335 while (!TAILQ_EMPTY(&bfs_head)) {
336 entry = TAILQ_FIRST(&bfs_head);
337 TAILQ_REMOVE(&bfs_head, entry, entries);
343 TAILQ_REMOVE(&bfs_head, entry, entries);
346 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
347 entry = smalloc(sizeof(struct bfs_entry));
349 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
352 TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
353 entry = smalloc(sizeof(struct bfs_entry));
355 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
363 * Returns true if the container is internal, such as __i3_scratch
366 bool con_is_internal(Con *con) {
367 return (con->name[0] == '_' && con->name[1] == '_');
371 * Returns true if the node is floating.
374 bool con_is_floating(Con *con) {
376 DLOG("checking if con %p is floating\n", con);
377 return (con->floating >= FLOATING_AUTO_ON);
381 * Checks if the given container is either floating or inside some floating
382 * container. It returns the FLOATING_CON container.
385 Con *con_inside_floating(Con *con) {
387 if (con->type == CT_FLOATING_CON)
390 if (con->floating >= FLOATING_AUTO_ON)
393 if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT)
396 return con_inside_floating(con->parent);
400 * Checks if the given container is inside a focused container.
403 bool con_inside_focused(Con *con) {
408 return con_inside_focused(con->parent);
412 * Returns the container with the given client window ID or NULL if no such
416 Con *con_by_window_id(xcb_window_t window) {
418 TAILQ_FOREACH(con, &all_cons, all_cons)
419 if (con->window != NULL && con->window->id == window)
425 * Returns the container with the given frame ID or NULL if no such container
429 Con *con_by_frame_id(xcb_window_t frame) {
431 TAILQ_FOREACH(con, &all_cons, all_cons)
432 if (con->frame == frame)
438 * Returns the first container below 'con' which wants to swallow this window
442 Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
445 //DLOG("searching con for window %p starting at con %p\n", window, con);
446 //DLOG("class == %s\n", window->class_class);
448 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
449 TAILQ_FOREACH(match, &(child->swallow_head), matches) {
450 if (!match_matches_window(match, window))
452 if (store_match != NULL)
453 *store_match = match;
456 Con *result = con_for_window(child, window, store_match);
461 TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
462 TAILQ_FOREACH(match, &(child->swallow_head), matches) {
463 if (!match_matches_window(match, window))
465 if (store_match != NULL)
466 *store_match = match;
469 Con *result = con_for_window(child, window, store_match);
478 * Returns the number of children of this container.
481 int con_num_children(Con *con) {
485 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
492 * Updates the percent attribute of the children of the given container. This
493 * function needs to be called when a window is added or removed from a
497 void con_fix_percent(Con *con) {
499 int children = con_num_children(con);
501 // calculate how much we have distributed and how many containers
502 // with a percentage set we have
504 int children_with_percent = 0;
505 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
506 if (child->percent > 0.0) {
507 total += child->percent;
508 ++children_with_percent;
512 // if there were children without a percentage set, set to a value that
513 // will make those children proportional to all others
514 if (children_with_percent != children) {
515 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
516 if (child->percent <= 0.0) {
517 if (children_with_percent == 0)
518 total += (child->percent = 1.0);
519 else total += (child->percent = total / children_with_percent);
524 // if we got a zero, just distribute the space equally, otherwise
525 // distribute according to the proportions we got
527 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
528 child->percent = 1.0 / children;
529 } else if (total != 1.0) {
530 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
531 child->percent /= total;
536 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
537 * entered when there already is a fullscreen container on this workspace.
540 void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
541 Con *workspace, *fullscreen;
543 if (con->type == CT_WORKSPACE) {
544 DLOG("You cannot make a workspace fullscreen.\n");
548 DLOG("toggling fullscreen for %p / %s\n", con, con->name);
549 if (con->fullscreen_mode == CF_NONE) {
550 /* 1: check if there already is a fullscreen con */
551 if (fullscreen_mode == CF_GLOBAL)
552 fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
554 workspace = con_get_workspace(con);
555 fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
557 if (fullscreen != NULL) {
558 /* Disable fullscreen for the currently fullscreened
559 * container and enable it for the one the user wants
560 * to have in fullscreen mode. */
561 LOG("Disabling fullscreen for (%p/%s) upon user request\n",
562 fullscreen, fullscreen->name);
563 fullscreen->fullscreen_mode = CF_NONE;
566 /* 2: enable fullscreen */
567 con->fullscreen_mode = fullscreen_mode;
569 /* 1: disable fullscreen */
570 con->fullscreen_mode = CF_NONE;
573 DLOG("mode now: %d\n", con->fullscreen_mode);
575 /* update _NET_WM_STATE if this container has a window */
576 /* TODO: when a window is assigned to a container which is already
577 * fullscreened, this state needs to be pushed to the client, too */
578 if (con->window == NULL)
582 unsigned int num = 0;
584 if (con->fullscreen_mode != CF_NONE)
585 values[num++] = A__NET_WM_STATE_FULLSCREEN;
587 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
588 A__NET_WM_STATE, XCB_ATOM_ATOM, 32, num, values);
592 * Moves the given container to the currently focused container on the given
595 * The fix_coordinates flag will translate the current coordinates (offset from
596 * the monitor position basically) to appropriate coordinates on the
597 * destination workspace.
598 * Not enabling this behaviour comes in handy when this function gets called by
599 * floating_maybe_reassign_ws, which will only "move" a floating window when it
600 * *already* changed its coordinates to a different output.
602 * The dont_warp flag disables pointer warping and will be set when this
603 * function is called while dragging a floating window.
605 * TODO: is there a better place for this function?
608 void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp) {
609 if (con->type == CT_WORKSPACE) {
610 DLOG("Moving workspaces is not yet implemented.\n");
614 /* Prevent moving if this would violate the fullscreen focus restrictions. */
615 if (!con_fullscreen_permits_focusing(workspace)) {
616 LOG("Cannot move out of a fullscreen container");
620 if (con_is_floating(con)) {
621 DLOG("Using FLOATINGCON instead\n");
625 Con *source_ws = con_get_workspace(con);
626 if (workspace == source_ws) {
627 DLOG("Not moving, already there\n");
631 /* Save the current workspace. So we can call workspace_show() by the end
632 * of this function. */
633 Con *current_ws = con_get_workspace(focused);
635 Con *source_output = con_get_output(con),
636 *dest_output = con_get_output(workspace);
638 /* 1: save the container which is going to be focused after the current
639 * container is moved away */
640 Con *focus_next = con_next_focused(con);
642 /* 2: get the focused container of this workspace */
643 Con *next = con_descend_focused(workspace);
645 /* 3: we go up one level, but only when next is a normal container */
646 if (next->type != CT_WORKSPACE) {
647 DLOG("next originally = %p / %s / type %d\n", next, next->name, next->type);
651 /* 4: if the target container is floating, we get the workspace instead.
652 * Only tiling windows need to get inserted next to the current container.
654 Con *floatingcon = con_inside_floating(next);
655 if (floatingcon != NULL) {
656 DLOG("floatingcon, going up even further\n");
657 next = floatingcon->parent;
660 if (con->type == CT_FLOATING_CON) {
661 Con *ws = con_get_workspace(next);
662 DLOG("This is a floating window, using workspace %p / %s\n", ws, ws->name);
666 if (source_output != dest_output) {
667 /* Take the relative coordinates of the current output, then add them
668 * to the coordinate space of the correct output */
669 if (fix_coordinates && con->type == CT_FLOATING_CON) {
670 floating_fix_coordinates(con, &(source_output->rect), &(dest_output->rect));
671 } else DLOG("Not fixing coordinates, fix_coordinates flag = %d\n", fix_coordinates);
673 /* If moving to a visible workspace, call show so it can be considered
674 * focused. Must do before attaching because workspace_show checks to see
675 * if focused container is in its area. */
676 if (workspace_is_visible(workspace)) {
677 workspace_show(workspace);
679 /* Don’t warp if told so (when dragging floating windows with the
680 * mouse for example) */
684 x_set_warp_to(&(con->rect));
688 DLOG("Re-attaching container to %p / %s\n", next, next->name);
689 /* 5: re-attach the con to the parent of this focused container */
690 Con *parent = con->parent;
692 con_attach(con, next, false);
694 /* 6: fix the percentages */
695 con_fix_percent(parent);
697 con_fix_percent(next);
699 /* 7: focus the con on the target workspace (the X focus is only updated by
700 * calling tree_render(), so for the "real" focus this is a no-op).
701 * We don’t focus the con for i3 pseudo workspaces like __i3_scratch and
702 * we don’t focus when there is a fullscreen con on that workspace. */
703 if (!con_is_internal(workspace) &&
704 con_get_fullscreen_con(workspace, CF_OUTPUT) == NULL)
705 con_focus(con_descend_focused(con));
707 /* 8: when moving to a visible workspace on a different output, we keep the
708 * con focused. Otherwise, we leave the focus on the current workspace as we
709 * don’t want to focus invisible workspaces */
710 if (source_output != dest_output &&
711 workspace_is_visible(workspace) &&
712 !con_is_internal(workspace)) {
713 DLOG("Moved to a different output, focusing target\n");
715 /* Descend focus stack in case focus_next is a workspace which can
716 * occur if we move to the same workspace. Also show current workspace
717 * to ensure it is focused. */
718 workspace_show(current_ws);
720 /* Set focus only if con was on current workspace before moving.
721 * Otherwise we would give focus to some window on different workspace. */
722 if (source_ws == current_ws)
723 con_focus(con_descend_focused(focus_next));
726 CALL(parent, on_remove_child);
730 * Returns the orientation of the given container (for stacked containers,
731 * vertical orientation is used regardless of the actual orientation of the
735 int con_orientation(Con *con) {
736 switch (con->layout) {
738 /* stacking containers behave like they are in vertical orientation */
743 /* tabbed containers behave like they are in vertical orientation */
748 DLOG("Someone called con_orientation() on a con with L_DEFAULT, this is a bug in the code.\n");
754 DLOG("con_orientation() called on dockarea/output (%d) container %p\n", con->layout, con);
759 DLOG("con_orientation() ran into default\n");
765 * Returns the container which will be focused next when the given container
766 * is not available anymore. Called in tree_close and con_move_to_workspace
767 * to properly restore focus.
770 Con *con_next_focused(Con *con) {
772 /* floating containers are attached to a workspace, so we focus either the
773 * next floating container (if any) or the workspace itself. */
774 if (con->type == CT_FLOATING_CON) {
775 DLOG("selecting next for CT_FLOATING_CON\n");
776 next = TAILQ_NEXT(con, floating_windows);
777 DLOG("next = %p\n", next);
779 next = TAILQ_PREV(con, floating_head, floating_windows);
780 DLOG("using prev, next = %p\n", next);
783 Con *ws = con_get_workspace(con);
785 DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
786 while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
787 next = TAILQ_FIRST(&(next->focus_head));
789 DLOG("skipping container itself, we want the next client\n");
790 next = TAILQ_NEXT(next, focused);
793 if (next == TAILQ_END(&(ws->focus_head))) {
794 DLOG("Focus list empty, returning ws\n");
798 /* Instead of returning the next CT_FLOATING_CON, we descend it to
799 * get an actual window to focus. */
800 next = con_descend_focused(next);
805 /* dock clients cannot be focused, so we focus the workspace instead */
806 if (con->parent->type == CT_DOCKAREA) {
807 DLOG("selecting workspace for dock client\n");
808 return con_descend_focused(output_get_content(con->parent->parent));
811 /* if 'con' is not the first entry in the focus stack, use the first one as
812 * it’s currently focused already */
813 Con *first = TAILQ_FIRST(&(con->parent->focus_head));
815 DLOG("Using first entry %p\n", first);
818 /* try to focus the next container on the same level as this one or fall
819 * back to its parent */
820 if (!(next = TAILQ_NEXT(con, focused)))
824 /* now go down the focus stack as far as
825 * possible, excluding the current container */
826 while (!TAILQ_EMPTY(&(next->focus_head)) &&
827 TAILQ_FIRST(&(next->focus_head)) != con)
828 next = TAILQ_FIRST(&(next->focus_head));
834 * Get the next/previous container in the specified orientation. This may
835 * travel up until it finds a container with suitable orientation.
838 Con *con_get_next(Con *con, char way, orientation_t orientation) {
839 DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
840 /* 1: get the first parent with the same orientation */
842 while (con_orientation(cur->parent) != orientation) {
843 DLOG("need to go one level further up\n");
844 if (cur->parent->type == CT_WORKSPACE) {
845 LOG("that's a workspace, we can't go further up\n");
851 /* 2: chose next (or previous) */
854 next = TAILQ_NEXT(cur, nodes);
855 /* if we are at the end of the list, we need to wrap */
856 if (next == TAILQ_END(&(parent->nodes_head)))
859 next = TAILQ_PREV(cur, nodes_head, nodes);
860 /* if we are at the end of the list, we need to wrap */
861 if (next == TAILQ_END(&(cur->nodes_head)))
864 DLOG("next = %p\n", next);
870 * Returns the focused con inside this client, descending the tree as far as
871 * possible. This comes in handy when attaching a con to a workspace at the
872 * currently focused position, for example.
875 Con *con_descend_focused(Con *con) {
877 while (next != focused && !TAILQ_EMPTY(&(next->focus_head)))
878 next = TAILQ_FIRST(&(next->focus_head));
883 * Returns the focused con inside this client, descending the tree as far as
884 * possible. This comes in handy when attaching a con to a workspace at the
885 * currently focused position, for example.
887 * Works like con_descend_focused but considers only tiling cons.
890 Con *con_descend_tiling_focused(Con *con) {
898 TAILQ_FOREACH(child, &(next->focus_head), focused) {
899 if (child->type == CT_FLOATING_CON)
905 } while (before != next && next != focused);
910 * Returns the leftmost, rightmost, etc. container in sub-tree. For example, if
911 * direction is D_LEFT, then we return the rightmost container and if direction
912 * is D_RIGHT, we return the leftmost container. This is because if we are
913 * moving D_LEFT, and thus want the rightmost container.
916 Con *con_descend_direction(Con *con, direction_t direction) {
918 int orientation = con_orientation(con);
919 DLOG("con_descend_direction(%p, orientation %d, direction %d)\n", con, orientation, direction);
920 if (direction == D_LEFT || direction == D_RIGHT) {
921 if (orientation == HORIZ) {
922 /* If the direction is horizontal, we can use either the first
923 * (D_RIGHT) or the last con (D_LEFT) */
924 if (direction == D_RIGHT)
925 most = TAILQ_FIRST(&(con->nodes_head));
926 else most = TAILQ_LAST(&(con->nodes_head), nodes_head);
927 } else if (orientation == VERT) {
928 /* Wrong orientation. We use the last focused con. Within that con,
929 * we recurse to chose the left/right con or at least the last
931 most = TAILQ_FIRST(&(con->focus_head));
933 /* If the con has no orientation set, it’s not a split container
934 * but a container with a client window, so stop recursing */
939 if (direction == D_UP || direction == D_DOWN) {
940 if (orientation == VERT) {
941 /* If the direction is vertical, we can use either the first
942 * (D_DOWN) or the last con (D_UP) */
943 if (direction == D_UP)
944 most = TAILQ_LAST(&(con->nodes_head), nodes_head);
945 else most = TAILQ_FIRST(&(con->nodes_head));
946 } else if (orientation == HORIZ) {
947 /* Wrong orientation. We use the last focused con. Within that con,
948 * we recurse to chose the top/bottom con or at least the last
950 most = TAILQ_FIRST(&(con->focus_head));
952 /* If the con has no orientation set, it’s not a split container
953 * but a container with a client window, so stop recursing */
960 return con_descend_direction(most, direction);
964 * Returns a "relative" Rect which contains the amount of pixels that need to
965 * be added to the original Rect to get the final position (obviously the
966 * amount of pixels for normal, 1pixel and borderless are different).
969 Rect con_border_style_rect(Con *con) {
970 adjacent_t borders_to_hide = ADJ_NONE;
972 /* Shortcut to avoid calling con_adjacent_borders() on dock containers. */
973 int border_style = con_border_style(con);
974 if (border_style == BS_NONE)
975 return (Rect){ 0, 0, 0, 0 };
976 borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
977 switch (border_style) {
979 result = (Rect){2, 0, -(2 * 2), -2};
980 if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
984 if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
987 /* With normal borders we never hide the upper border */
988 if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
994 result = (Rect){1, 1, -2, -2};
995 if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
999 if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
1002 if (borders_to_hide & ADJ_UPPER_SCREEN_EDGE) {
1006 if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
1012 return (Rect){0, 0, 0, 0};
1020 * Returns adjacent borders of the window. We need this if hide_edge_borders is
1023 adjacent_t con_adjacent_borders(Con *con) {
1024 adjacent_t result = ADJ_NONE;
1025 Con *workspace = con_get_workspace(con);
1026 if (con->rect.x == workspace->rect.x)
1027 result |= ADJ_LEFT_SCREEN_EDGE;
1028 if (con->rect.x + con->rect.width == workspace->rect.x + workspace->rect.width)
1029 result |= ADJ_RIGHT_SCREEN_EDGE;
1030 if (con->rect.y == workspace->rect.y)
1031 result |= ADJ_UPPER_SCREEN_EDGE;
1032 if (con->rect.y + con->rect.height == workspace->rect.y + workspace->rect.height)
1033 result |= ADJ_LOWER_SCREEN_EDGE;
1038 * Use this function to get a container’s border style. This is important
1039 * because when inside a stack, the border style is always BS_NORMAL.
1040 * For tabbed mode, the same applies, with one exception: when the container is
1041 * borderless and the only element in the tabbed container, the border is not
1044 * For children of a CT_DOCKAREA, the border style is always none.
1047 int con_border_style(Con *con) {
1048 Con *fs = con_get_fullscreen_con(con->parent, CF_OUTPUT);
1050 DLOG("this one is fullscreen! overriding BS_NONE\n");
1054 if (con->parent->layout == L_STACKED)
1055 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1057 if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
1058 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1060 if (con->parent->type == CT_DOCKAREA)
1063 return con->border_style;
1067 * Sets the given border style on con, correctly keeping the position/size of a
1071 void con_set_border_style(Con *con, int border_style) {
1072 /* Handle the simple case: non-floating containerns */
1073 if (!con_is_floating(con)) {
1074 con->border_style = border_style;
1078 /* For floating containers, we want to keep the position/size of the
1079 * *window* itself. We first add the border pixels to con->rect to make
1080 * con->rect represent the absolute position of the window. Then, we change
1081 * the border and subtract the new border pixels. Afterwards, we update
1082 * parent->rect to contain con. */
1083 DLOG("This is a floating container\n");
1085 Rect bsr = con_border_style_rect(con);
1086 con->rect.x += bsr.x;
1087 con->rect.y += bsr.y;
1088 con->rect.width += bsr.width;
1089 con->rect.height += bsr.height;
1091 /* Change the border style, get new border/decoration values. */
1092 con->border_style = border_style;
1093 bsr = con_border_style_rect(con);
1095 (con->border_style == BS_NORMAL ? config.font.height + 5 : 0);
1097 con->rect.x -= bsr.x;
1098 con->rect.y -= bsr.y;
1099 con->rect.width -= bsr.width;
1100 con->rect.height -= bsr.height;
1102 Con *parent = con->parent;
1103 parent->rect.x = con->rect.x;
1104 parent->rect.y = con->rect.y - deco_height;
1105 parent->rect.width = con->rect.width;
1106 parent->rect.height = con->rect.height + deco_height;
1110 * This function changes the layout of a given container. Use it to handle
1111 * special cases like changing a whole workspace to stacked/tabbed (creates a
1112 * new split container before).
1115 void con_set_layout(Con *con, int layout) {
1116 DLOG("con_set_layout(%p, %d), con->type = %d\n",
1117 con, layout, con->type);
1119 /* Users can focus workspaces, but not any higher in the hierarchy.
1120 * Focus on the workspace is a special case, since in every other case, the
1121 * user means "change the layout of the parent split container". */
1122 if (con->type != CT_WORKSPACE)
1125 /* We fill in last_split_layout when switching to a different layout
1126 * since there are many places in the code that don’t use
1127 * con_set_layout(). */
1128 if (con->layout == L_SPLITH || con->layout == L_SPLITV)
1129 con->last_split_layout = con->layout;
1131 /* When the container type is CT_WORKSPACE, the user wants to change the
1132 * whole workspace into stacked/tabbed mode. To do this and still allow
1133 * intuitive operations (like level-up and then opening a new window), we
1134 * need to create a new split container. */
1135 if (con->type == CT_WORKSPACE &&
1136 (layout == L_STACKED || layout == L_TABBED)) {
1137 if (con_num_children(con) == 0) {
1138 DLOG("Setting workspace_layout to %d\n", layout);
1139 con->workspace_layout = layout;
1141 DLOG("Creating new split container\n");
1142 /* 1: create a new split container */
1143 Con *new = con_new(NULL, NULL);
1146 /* 2: Set the requested layout on the split container and mark it as
1148 new->layout = layout;
1149 new->last_split_layout = con->last_split_layout;
1152 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
1153 if (old_focused == TAILQ_END(&(con->focus_head)))
1156 /* 3: move the existing cons of this workspace below the new con */
1157 DLOG("Moving cons\n");
1159 while (!TAILQ_EMPTY(&(con->nodes_head))) {
1160 child = TAILQ_FIRST(&(con->nodes_head));
1162 con_attach(child, new, true);
1165 /* 4: attach the new split container to the workspace */
1166 DLOG("Attaching new split to ws\n");
1167 con_attach(new, con, false);
1170 con_focus(old_focused);
1172 tree_flatten(croot);
1174 con_force_split_parents_redraw(con);
1178 if (layout == L_DEFAULT) {
1179 /* Special case: the layout formerly known as "default" (in combination
1180 * with an orientation). Since we switched to splith/splitv layouts,
1181 * using the "default" layout (which "only" should happen when using
1182 * legacy configs) is using the last split layout (either splith or
1183 * splitv) in order to still do the same thing.
1185 * Starting from v4.6 though, we will nag users about using "layout
1186 * default", and in v4.9 we will remove it entirely (with an
1187 * appropriate i3-migrate-config mechanism). */
1188 con->layout = con->last_split_layout;
1189 /* In case last_split_layout was not initialized… */
1190 if (con->layout == L_DEFAULT)
1191 con->layout = L_SPLITH;
1193 con->layout = layout;
1195 con_force_split_parents_redraw(con);
1199 * This function toggles the layout of a given container. toggle_mode can be
1200 * either 'default' (toggle only between stacked/tabbed/last_split_layout),
1201 * 'split' (toggle only between splitv/splith) or 'all' (toggle between all
1205 void con_toggle_layout(Con *con, const char *toggle_mode) {
1207 /* Users can focus workspaces, but not any higher in the hierarchy.
1208 * Focus on the workspace is a special case, since in every other case, the
1209 * user means "change the layout of the parent split container". */
1210 if (con->type != CT_WORKSPACE)
1211 parent = con->parent;
1212 DLOG("con_toggle_layout(%p, %s), parent = %p\n", con, toggle_mode, parent);
1214 if (strcmp(toggle_mode, "split") == 0) {
1215 /* Toggle between splits. When the current layout is not a split
1216 * layout, we just switch back to last_split_layout. Otherwise, we
1217 * change to the opposite split layout. */
1218 if (parent->layout != L_SPLITH && parent->layout != L_SPLITV)
1219 con_set_layout(con, parent->last_split_layout);
1221 if (parent->layout == L_SPLITH)
1222 con_set_layout(con, L_SPLITV);
1223 else con_set_layout(con, L_SPLITH);
1226 if (parent->layout == L_STACKED)
1227 con_set_layout(con, L_TABBED);
1228 else if (parent->layout == L_TABBED) {
1229 if (strcmp(toggle_mode, "all") == 0)
1230 con_set_layout(con, L_SPLITH);
1231 else con_set_layout(con, parent->last_split_layout);
1232 } else if (parent->layout == L_SPLITH || parent->layout == L_SPLITV) {
1233 if (strcmp(toggle_mode, "all") == 0) {
1234 /* When toggling through all modes, we toggle between
1235 * splith/splitv, whereas normally we just directly jump to
1237 if (parent->layout == L_SPLITH)
1238 con_set_layout(con, L_SPLITV);
1239 else con_set_layout(con, L_STACKED);
1241 con_set_layout(con, L_STACKED);
1248 * Callback which will be called when removing a child from the given con.
1249 * Kills the container if it is empty and replaces it with the child if there
1250 * is exactly one child.
1253 static void con_on_remove_child(Con *con) {
1254 DLOG("on_remove_child\n");
1256 /* Every container 'above' (in the hierarchy) the workspace content should
1257 * not be closed when the last child was removed */
1258 if (con->type == CT_OUTPUT ||
1259 con->type == CT_ROOT ||
1260 con->type == CT_DOCKAREA) {
1261 DLOG("not handling, type = %d\n", con->type);
1265 /* For workspaces, close them only if they're not visible anymore */
1266 if (con->type == CT_WORKSPACE) {
1267 if (TAILQ_EMPTY(&(con->focus_head)) && !workspace_is_visible(con)) {
1268 LOG("Closing old workspace (%p / %s), it is empty\n", con, con->name);
1269 tree_close(con, DONT_KILL_WINDOW, false, false);
1270 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
1275 con_force_split_parents_redraw(con);
1277 /* TODO: check if this container would swallow any other client and
1278 * don’t close it automatically. */
1279 int children = con_num_children(con);
1280 if (children == 0) {
1281 DLOG("Container empty, closing\n");
1282 tree_close(con, DONT_KILL_WINDOW, false, false);
1288 * Determines the minimum size of the given con by looking at its children (for
1289 * split/stacked/tabbed cons). Will be called when resizing floating cons
1292 Rect con_minimum_size(Con *con) {
1293 DLOG("Determining minimum size for con %p\n", con);
1295 if (con_is_leaf(con)) {
1296 DLOG("leaf node, returning 75x50\n");
1297 return (Rect){ 0, 0, 75, 50 };
1300 if (con->type == CT_FLOATING_CON) {
1301 DLOG("floating con\n");
1302 Con *child = TAILQ_FIRST(&(con->nodes_head));
1303 return con_minimum_size(child);
1306 if (con->layout == L_STACKED || con->layout == L_TABBED) {
1307 uint32_t max_width = 0, max_height = 0, deco_height = 0;
1309 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1310 Rect min = con_minimum_size(child);
1311 deco_height += child->deco_rect.height;
1312 max_width = max(max_width, min.width);
1313 max_height = max(max_height, min.height);
1315 DLOG("stacked/tabbed now, returning %d x %d + deco_rect = %d\n",
1316 max_width, max_height, deco_height);
1317 return (Rect){ 0, 0, max_width, max_height + deco_height };
1320 /* For horizontal/vertical split containers we sum up the width (h-split)
1321 * or height (v-split) and use the maximum of the height (h-split) or width
1322 * (v-split) as minimum size. */
1324 uint32_t width = 0, height = 0;
1326 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1327 Rect min = con_minimum_size(child);
1328 if (con->layout == L_SPLITH) {
1330 height = max(height, min.height);
1332 height += min.height;
1333 width = max(width, min.width);
1336 DLOG("split container, returning width = %d x height = %d\n", width, height);
1337 return (Rect){ 0, 0, width, height };
1340 ELOG("Unhandled case, type = %d, layout = %d, split = %d\n",
1341 con->type, con->layout, con->split);
1346 * Returns true if changing the focus to con would be allowed considering
1347 * the fullscreen focus constraints. Specifically, if a fullscreen container or
1348 * any of its descendants is focused, this function returns true if and only if
1349 * focusing con would mean that focus would still be visible on screen, i.e.,
1350 * the newly focused container would not be obscured by a fullscreen container.
1352 * In the simplest case, if a fullscreen container or any of its descendants is
1353 * fullscreen, this functions returns true if con is the fullscreen container
1354 * itself or any of its descendants, as this means focus wouldn't escape the
1355 * boundaries of the fullscreen container.
1357 * In case the fullscreen container is of type CF_OUTPUT, this function returns
1358 * true if con is on a different workspace, as focus wouldn't be obscured by
1359 * the fullscreen container that is constrained to a different workspace.
1361 * Note that this same logic can be applied to moving containers. If a
1362 * container can be focused under the fullscreen focus constraints, it can also
1363 * become a parent or sibling to the currently focused container.
1366 bool con_fullscreen_permits_focusing(Con *con) {
1367 /* No focus, no problem. */
1371 /* Find the first fullscreen ascendent. */
1373 while (fs && fs->fullscreen_mode == CF_NONE)
1376 /* fs must be non-NULL since the workspace con doesn’t have CF_NONE and
1377 * there always has to be a workspace con in the hierarchy. */
1379 /* The most common case is we hit the workspace level. In this
1380 * situation, changing focus is also harmless. */
1381 assert(fs->fullscreen_mode != CF_NONE);
1382 if (fs->type == CT_WORKSPACE)
1385 /* Allow it if the container itself is the fullscreen container. */
1389 /* If fullscreen is per-output, the focus being in a different workspace is
1390 * sufficient to guarantee that change won't leave fullscreen in bad shape. */
1391 if (fs->fullscreen_mode == CF_OUTPUT &&
1392 con_get_workspace(con) != con_get_workspace(fs)) {
1396 /* Allow it only if the container to be focused is contained within the
1397 * current fullscreen container. */
1399 if (con->parent == fs)
1404 /* Focusing con would hide it behind a fullscreen window, disallow it. */
1410 * Checks if the given container has an urgent child.
1413 bool con_has_urgent_child(Con *con) {
1416 if (con_is_leaf(con))
1419 /* We are not interested in floating windows since they can only be
1420 * attached to a workspace → nodes_head instead of focus_head */
1421 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1422 if (con_has_urgent_child(child))
1430 * Make all parent containers urgent if con is urgent or clear the urgent flag
1431 * of all parent containers if there are no more urgent children left.
1434 void con_update_parents_urgency(Con *con) {
1435 Con *parent = con->parent;
1437 bool new_urgency_value = con->urgent;
1438 while (parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
1439 if (new_urgency_value) {
1440 parent->urgent = true;
1442 /* We can only reset the urgency when the parent
1443 * has no other urgent children */
1444 if (!con_has_urgent_child(parent))
1445 parent->urgent = false;
1447 parent = parent->parent;
1452 * Create a string representing the subtree under con.
1455 char *con_get_tree_representation(Con *con) {
1456 /* this code works as follows:
1457 * 1) create a string with the layout type (D/V/H/T/S) and an opening bracket
1458 * 2) append the tree representation of the children to the string
1459 * 3) add closing bracket
1461 * The recursion ends when we hit a leaf, in which case we return the
1462 * class_instance of the contained window.
1465 /* end of recursion */
1466 if (con_is_leaf(con)) {
1468 return sstrdup("nowin");
1470 if (!con->window->class_instance)
1471 return sstrdup("noinstance");
1473 return sstrdup(con->window->class_instance);
1477 /* 1) add the Layout type to buf */
1478 if (con->layout == L_DEFAULT)
1479 buf = sstrdup("D[");
1480 else if (con->layout == L_SPLITV)
1481 buf = sstrdup("V[");
1482 else if (con->layout == L_SPLITH)
1483 buf = sstrdup("H[");
1484 else if (con->layout == L_TABBED)
1485 buf = sstrdup("T[");
1486 else if (con->layout == L_STACKED)
1487 buf = sstrdup("S[");
1489 /* 2) append representation of children */
1491 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1492 char *child_txt = con_get_tree_representation(child);
1495 sasprintf(&tmp_buf, "%s%s%s", buf,
1496 (TAILQ_FIRST(&(con->nodes_head)) == child ? "" : " "), child_txt);
1501 /* 3) close the brackets */
1503 sasprintf(&complete_buf, "%s]", buf);
1506 return complete_buf;