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 * Create a new container (and attach it to the given parent, if not NULL).
33 * This function initializes the data structures and creates the appropriate
34 * X11 IDs using x_con_init().
37 Con *con_new(Con *parent, i3Window *window) {
38 Con *new = scalloc(sizeof(Con));
39 new->on_remove_child = con_on_remove_child;
40 TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
43 new->border_style = config.default_border;
45 DLOG("opening window %d\n", cnt);
47 /* TODO: remove window coloring after test-phase */
48 DLOG("color %s\n", colors[cnt]);
49 new->name = strdup(colors[cnt]);
50 //uint32_t cp = get_colorpixel(colors[cnt]);
52 if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
55 x_con_init(new, window->depth);
57 x_con_init(new, XCB_COPY_FROM_PARENT);
59 TAILQ_INIT(&(new->floating_head));
60 TAILQ_INIT(&(new->nodes_head));
61 TAILQ_INIT(&(new->focus_head));
62 TAILQ_INIT(&(new->swallow_head));
65 con_attach(new, parent, false);
71 * Attaches the given container to the given parent. This happens when moving
72 * a container or when inserting a new container at a specific place in the
75 * ignore_focus is to just insert the Con at the end (useful when creating a
76 * new split container *around* some containers, that is, detaching and
77 * attaching them in order without wanting to mess with the focus in between).
80 void con_attach(Con *con, Con *parent, bool ignore_focus) {
84 struct nodes_head *nodes_head = &(parent->nodes_head);
85 struct focus_head *focus_head = &(parent->focus_head);
87 /* Workspaces are handled differently: they need to be inserted at the
89 if (con->type == CT_WORKSPACE) {
90 DLOG("it's a workspace. num = %d\n", con->num);
91 if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
92 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
94 current = TAILQ_FIRST(nodes_head);
95 if (con->num < current->num) {
96 /* we need to insert the container at the beginning */
97 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
99 while (current->num != -1 && con->num > current->num) {
100 current = TAILQ_NEXT(current, nodes);
101 if (current == TAILQ_END(nodes_head)) {
106 /* we need to insert con after current, if current is not NULL */
108 TAILQ_INSERT_BEFORE(current, con, nodes);
109 else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
112 goto add_to_focus_head;
115 if (con->type == CT_FLOATING_CON) {
116 DLOG("Inserting into floating containers\n");
117 TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
120 /* Get the first tiling container in focus stack */
121 TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
122 if (loop->type == CT_FLOATING_CON)
129 /* When the container is not a split container (but contains a window)
130 * and is attached to a workspace, we check if the user configured a
131 * workspace_layout. This is done in workspace_attach_to, which will
132 * provide us with the container to which we should attach (either the
133 * workspace or a new split container with the configured
136 if (con->window != NULL &&
137 parent->type == CT_WORKSPACE &&
138 parent->workspace_layout != L_DEFAULT) {
139 DLOG("Parent is a workspace. Applying default layout...\n");
140 Con *target = workspace_attach_to(parent);
142 /* Attach the original con to this new split con instead */
143 nodes_head = &(target->nodes_head);
144 focus_head = &(target->focus_head);
145 con->parent = target;
151 /* Insert the container after the tiling container, if found.
152 * When adding to a CT_OUTPUT, just append one after another. */
153 if (current && parent->type != CT_OUTPUT) {
154 DLOG("Inserting con = %p after last focused tiling con %p\n",
156 TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
157 } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
161 /* We insert to the TAIL because con_focus() will correct this.
162 * This way, we have the option to insert Cons without having
164 TAILQ_INSERT_TAIL(focus_head, con, focused);
168 * Detaches the given container from its current parent
171 void con_detach(Con *con) {
172 if (con->type == CT_FLOATING_CON) {
173 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
174 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
176 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
177 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
182 * Sets input focus to the given container. Will be updated in X11 in the next
183 * run of x_push_changes().
186 void con_focus(Con *con) {
188 DLOG("con_focus = %p\n", con);
190 /* 1: set focused-pointer to the new con */
191 /* 2: exchange the position of the container in focus stack of the parent all the way up */
192 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
193 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
194 if (con->parent->parent != NULL)
195 con_focus(con->parent);
200 workspace_update_urgent_flag(con_get_workspace(con));
205 * Returns true when this node is a leaf node (has no children)
208 bool con_is_leaf(Con *con) {
209 return TAILQ_EMPTY(&(con->nodes_head));
213 * Returns true if this node accepts a window (if the node swallows windows,
214 * it might already have swallowed enough and cannot hold any more).
217 bool con_accepts_window(Con *con) {
218 /* 1: workspaces never accept direct windows */
219 if (con->type == CT_WORKSPACE)
223 DLOG("container %p does not accept windows, it is a split container.\n", con);
227 /* TODO: if this is a swallowing container, we need to check its max_clients */
228 return (con->window == NULL);
232 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
236 Con *con_get_output(Con *con) {
238 while (result != NULL && result->type != CT_OUTPUT)
239 result = result->parent;
240 /* We must be able to get an output because focus can never be set higher
241 * in the tree (root node cannot be focused). */
242 assert(result != NULL);
247 * Gets the workspace container this node is on.
250 Con *con_get_workspace(Con *con) {
252 while (result != NULL && result->type != CT_WORKSPACE)
253 result = result->parent;
258 * Searches parenst of the given 'con' until it reaches one with the specified
259 * 'orientation'. Aborts when it comes across a floating_con.
262 Con *con_parent_with_orientation(Con *con, orientation_t orientation) {
263 DLOG("Searching for parent of Con %p with orientation %d\n", con, orientation);
264 Con *parent = con->parent;
265 if (parent->type == CT_FLOATING_CON)
267 while (con_orientation(parent) != orientation) {
268 DLOG("Need to go one level further up\n");
269 parent = parent->parent;
270 /* Abort when we reach a floating con, or an output con */
272 (parent->type == CT_FLOATING_CON ||
273 parent->type == CT_OUTPUT ||
274 (parent->parent && parent->parent->type == CT_OUTPUT)))
279 DLOG("Result: %p\n", parent);
284 * helper data structure for the breadth-first-search in
285 * con_get_fullscreen_con()
291 TAILQ_ENTRY(bfs_entry) entries;
295 * Returns the first fullscreen node below this node.
298 Con *con_get_fullscreen_con(Con *con, int fullscreen_mode) {
299 Con *current, *child;
301 /* TODO: is breadth-first-search really appropriate? (check as soon as
302 * fullscreen levels and fullscreen for containers is implemented) */
303 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
304 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
306 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
308 while (!TAILQ_EMPTY(&bfs_head)) {
309 entry = TAILQ_FIRST(&bfs_head);
310 current = entry->con;
311 if (current != con && current->fullscreen_mode == fullscreen_mode) {
312 /* empty the queue */
313 while (!TAILQ_EMPTY(&bfs_head)) {
314 entry = TAILQ_FIRST(&bfs_head);
315 TAILQ_REMOVE(&bfs_head, entry, entries);
321 TAILQ_REMOVE(&bfs_head, entry, entries);
324 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
325 entry = smalloc(sizeof(struct bfs_entry));
327 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
330 TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
331 entry = smalloc(sizeof(struct bfs_entry));
333 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
341 * Returns true if the node is floating.
344 bool con_is_floating(Con *con) {
346 DLOG("checking if con %p is floating\n", con);
347 return (con->floating >= FLOATING_AUTO_ON);
351 * Checks if the given container is either floating or inside some floating
352 * container. It returns the FLOATING_CON container.
355 Con *con_inside_floating(Con *con) {
357 if (con->type == CT_FLOATING_CON)
360 if (con->floating >= FLOATING_AUTO_ON)
363 if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT)
366 return con_inside_floating(con->parent);
370 * Checks if the given container is inside a focused container.
373 bool con_inside_focused(Con *con) {
378 return con_inside_focused(con->parent);
382 * Returns the container with the given client window ID or NULL if no such
386 Con *con_by_window_id(xcb_window_t window) {
388 TAILQ_FOREACH(con, &all_cons, all_cons)
389 if (con->window != NULL && con->window->id == window)
395 * Returns the container with the given frame ID or NULL if no such container
399 Con *con_by_frame_id(xcb_window_t frame) {
401 TAILQ_FOREACH(con, &all_cons, all_cons)
402 if (con->frame == frame)
408 * Returns the first container below 'con' which wants to swallow this window
412 Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
415 //DLOG("searching con for window %p starting at con %p\n", window, con);
416 //DLOG("class == %s\n", window->class_class);
418 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
419 TAILQ_FOREACH(match, &(child->swallow_head), matches) {
420 if (!match_matches_window(match, window))
422 if (store_match != NULL)
423 *store_match = match;
426 Con *result = con_for_window(child, window, store_match);
431 TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
432 TAILQ_FOREACH(match, &(child->swallow_head), matches) {
433 if (!match_matches_window(match, window))
435 if (store_match != NULL)
436 *store_match = match;
439 Con *result = con_for_window(child, window, store_match);
448 * Returns the number of children of this container.
451 int con_num_children(Con *con) {
455 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
462 * Updates the percent attribute of the children of the given container. This
463 * function needs to be called when a window is added or removed from a
467 void con_fix_percent(Con *con) {
469 int children = con_num_children(con);
471 // calculate how much we have distributed and how many containers
472 // with a percentage set we have
474 int children_with_percent = 0;
475 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
476 if (child->percent > 0.0) {
477 total += child->percent;
478 ++children_with_percent;
482 // if there were children without a percentage set, set to a value that
483 // will make those children proportional to all others
484 if (children_with_percent != children) {
485 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
486 if (child->percent <= 0.0) {
487 if (children_with_percent == 0)
488 total += (child->percent = 1.0);
489 else total += (child->percent = total / children_with_percent);
494 // if we got a zero, just distribute the space equally, otherwise
495 // distribute according to the proportions we got
497 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
498 child->percent = 1.0 / children;
499 } else if (total != 1.0) {
500 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
501 child->percent /= total;
506 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
507 * entered when there already is a fullscreen container on this workspace.
510 void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
511 Con *workspace, *fullscreen;
513 if (con->type == CT_WORKSPACE) {
514 DLOG("You cannot make a workspace fullscreen.\n");
518 DLOG("toggling fullscreen for %p / %s\n", con, con->name);
519 if (con->fullscreen_mode == CF_NONE) {
520 /* 1: check if there already is a fullscreen con */
521 if (fullscreen_mode == CF_GLOBAL)
522 fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
524 workspace = con_get_workspace(con);
525 fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
527 if (fullscreen != NULL) {
528 /* Disable fullscreen for the currently fullscreened
529 * container and enable it for the one the user wants
530 * to have in fullscreen mode. */
531 LOG("Disabling fullscreen for (%p/%s) upon user request\n",
532 fullscreen, fullscreen->name);
533 fullscreen->fullscreen_mode = CF_NONE;
536 /* 2: enable fullscreen */
537 con->fullscreen_mode = fullscreen_mode;
539 /* 1: disable fullscreen */
540 con->fullscreen_mode = CF_NONE;
543 DLOG("mode now: %d\n", con->fullscreen_mode);
545 /* update _NET_WM_STATE if this container has a window */
546 /* TODO: when a window is assigned to a container which is already
547 * fullscreened, this state needs to be pushed to the client, too */
548 if (con->window == NULL)
552 unsigned int num = 0;
554 if (con->fullscreen_mode != CF_NONE)
555 values[num++] = A__NET_WM_STATE_FULLSCREEN;
557 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
558 A__NET_WM_STATE, XCB_ATOM_ATOM, 32, num, values);
562 * Moves the given container to the currently focused container on the given
565 * The fix_coordinates flag will translate the current coordinates (offset from
566 * the monitor position basically) to appropriate coordinates on the
567 * destination workspace.
568 * Not enabling this behaviour comes in handy when this function gets called by
569 * floating_maybe_reassign_ws, which will only "move" a floating window when it
570 * *already* changed its coordinates to a different output.
572 * The dont_warp flag disables pointer warping and will be set when this
573 * function is called while dragging a floating window.
575 * TODO: is there a better place for this function?
578 void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp) {
579 if (con->type == CT_WORKSPACE) {
580 DLOG("Moving workspaces is not yet implemented.\n");
584 /* Prevent moving if this would violate the fullscreen focus restrictions. */
585 if (!con_fullscreen_permits_focusing(workspace)) {
586 LOG("Cannot move out of a fullscreen container");
590 if (con_is_floating(con)) {
591 DLOG("Using FLOATINGCON instead\n");
595 Con *source_ws = con_get_workspace(con);
596 if (workspace == source_ws) {
597 DLOG("Not moving, already there\n");
601 /* Save the current workspace. So we can call workspace_show() by the end
602 * of this function. */
603 Con *current_ws = con_get_workspace(focused);
605 Con *source_output = con_get_output(con),
606 *dest_output = con_get_output(workspace);
608 /* 1: save the container which is going to be focused after the current
609 * container is moved away */
610 Con *focus_next = con_next_focused(con);
612 /* 2: get the focused container of this workspace */
613 Con *next = con_descend_focused(workspace);
615 /* 3: we go up one level, but only when next is a normal container */
616 if (next->type != CT_WORKSPACE) {
617 DLOG("next originally = %p / %s / type %d\n", next, next->name, next->type);
621 /* 4: if the target container is floating, we get the workspace instead.
622 * Only tiling windows need to get inserted next to the current container.
624 Con *floatingcon = con_inside_floating(next);
625 if (floatingcon != NULL) {
626 DLOG("floatingcon, going up even further\n");
627 next = floatingcon->parent;
630 if (con->type == CT_FLOATING_CON) {
631 Con *ws = con_get_workspace(next);
632 DLOG("This is a floating window, using workspace %p / %s\n", ws, ws->name);
636 if (source_output != dest_output) {
637 /* Take the relative coordinates of the current output, then add them
638 * to the coordinate space of the correct output */
639 if (fix_coordinates && con->type == CT_FLOATING_CON) {
640 floating_fix_coordinates(con, &(source_output->rect), &(dest_output->rect));
641 } else DLOG("Not fixing coordinates, fix_coordinates flag = %d\n", fix_coordinates);
643 /* If moving to a visible workspace, call show so it can be considered
644 * focused. Must do before attaching because workspace_show checks to see
645 * if focused container is in its area. */
646 if (workspace_is_visible(workspace)) {
647 workspace_show(workspace);
649 /* Don’t warp if told so (when dragging floating windows with the
650 * mouse for example) */
654 x_set_warp_to(&(con->rect));
658 DLOG("Re-attaching container to %p / %s\n", next, next->name);
659 /* 5: re-attach the con to the parent of this focused container */
660 Con *parent = con->parent;
662 con_attach(con, next, false);
664 /* 6: fix the percentages */
665 con_fix_percent(parent);
667 con_fix_percent(next);
669 /* 7: focus the con on the target workspace, but only within that
670 * workspace, that is, don’t move focus away if the target workspace is
672 * We don’t focus the con for i3 pseudo workspaces like __i3_scratch and
673 * we don’t focus when there is a fullscreen con on that workspace. */
674 if ((workspace->name[0] != '_' || workspace->name[1] != '_') &&
675 con_get_fullscreen_con(workspace, CF_OUTPUT) == NULL) {
676 /* We need to save focus on workspace level and restore it afterwards.
677 * Otherwise, we might focus a different workspace without actually
678 * switching workspaces. */
679 Con *old_focus = TAILQ_FIRST(&(output_get_content(dest_output)->focus_head));
680 con_focus(con_descend_focused(con));
681 con_focus(old_focus);
684 /* 8: when moving to a visible workspace on a different output, we keep the
685 * con focused. Otherwise, we leave the focus on the current workspace as we
686 * don’t want to focus invisible workspaces */
687 if (source_output != dest_output &&
688 workspace_is_visible(workspace) &&
689 workspace->name[0] != '_' &&
690 workspace->name[1] != '_') {
691 DLOG("Moved to a different output, focusing target\n");
693 /* Descend focus stack in case focus_next is a workspace which can
694 * occur if we move to the same workspace. Also show current workspace
695 * to ensure it is focused. */
696 workspace_show(current_ws);
698 /* Set focus only if con was on current workspace before moving.
699 * Otherwise we would give focus to some window on different workspace. */
700 if (source_ws == current_ws)
701 con_focus(con_descend_focused(focus_next));
704 CALL(parent, on_remove_child);
708 * Returns the orientation of the given container (for stacked containers,
709 * vertical orientation is used regardless of the actual orientation of the
713 int con_orientation(Con *con) {
714 switch (con->layout) {
716 /* stacking containers behave like they are in vertical orientation */
721 /* tabbed containers behave like they are in vertical orientation */
726 DLOG("Someone called con_orientation() on a con with L_DEFAULT, this is a bug in the code.\n");
732 DLOG("con_orientation() called on dockarea/output (%d) container %p\n", con->layout, con);
737 DLOG("con_orientation() ran into default\n");
743 * Returns the container which will be focused next when the given container
744 * is not available anymore. Called in tree_close and con_move_to_workspace
745 * to properly restore focus.
748 Con *con_next_focused(Con *con) {
750 /* floating containers are attached to a workspace, so we focus either the
751 * next floating container (if any) or the workspace itself. */
752 if (con->type == CT_FLOATING_CON) {
753 DLOG("selecting next for CT_FLOATING_CON\n");
754 next = TAILQ_NEXT(con, floating_windows);
755 DLOG("next = %p\n", next);
757 next = TAILQ_PREV(con, floating_head, floating_windows);
758 DLOG("using prev, next = %p\n", next);
761 Con *ws = con_get_workspace(con);
763 DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
764 while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
765 next = TAILQ_FIRST(&(next->focus_head));
767 DLOG("skipping container itself, we want the next client\n");
768 next = TAILQ_NEXT(next, focused);
771 if (next == TAILQ_END(&(ws->focus_head))) {
772 DLOG("Focus list empty, returning ws\n");
776 /* Instead of returning the next CT_FLOATING_CON, we descend it to
777 * get an actual window to focus. */
778 next = con_descend_focused(next);
783 /* dock clients cannot be focused, so we focus the workspace instead */
784 if (con->parent->type == CT_DOCKAREA) {
785 DLOG("selecting workspace for dock client\n");
786 return con_descend_focused(output_get_content(con->parent->parent));
789 /* if 'con' is not the first entry in the focus stack, use the first one as
790 * it’s currently focused already */
791 Con *first = TAILQ_FIRST(&(con->parent->focus_head));
793 DLOG("Using first entry %p\n", first);
796 /* try to focus the next container on the same level as this one or fall
797 * back to its parent */
798 if (!(next = TAILQ_NEXT(con, focused)))
802 /* now go down the focus stack as far as
803 * possible, excluding the current container */
804 while (!TAILQ_EMPTY(&(next->focus_head)) &&
805 TAILQ_FIRST(&(next->focus_head)) != con)
806 next = TAILQ_FIRST(&(next->focus_head));
812 * Get the next/previous container in the specified orientation. This may
813 * travel up until it finds a container with suitable orientation.
816 Con *con_get_next(Con *con, char way, orientation_t orientation) {
817 DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
818 /* 1: get the first parent with the same orientation */
820 while (con_orientation(cur->parent) != orientation) {
821 DLOG("need to go one level further up\n");
822 if (cur->parent->type == CT_WORKSPACE) {
823 LOG("that's a workspace, we can't go further up\n");
829 /* 2: chose next (or previous) */
832 next = TAILQ_NEXT(cur, nodes);
833 /* if we are at the end of the list, we need to wrap */
834 if (next == TAILQ_END(&(parent->nodes_head)))
837 next = TAILQ_PREV(cur, nodes_head, nodes);
838 /* if we are at the end of the list, we need to wrap */
839 if (next == TAILQ_END(&(cur->nodes_head)))
842 DLOG("next = %p\n", next);
848 * Returns the focused con inside this client, descending the tree as far as
849 * possible. This comes in handy when attaching a con to a workspace at the
850 * currently focused position, for example.
853 Con *con_descend_focused(Con *con) {
855 while (next != focused && !TAILQ_EMPTY(&(next->focus_head)))
856 next = TAILQ_FIRST(&(next->focus_head));
861 * Returns the focused con inside this client, descending the tree as far as
862 * possible. This comes in handy when attaching a con to a workspace at the
863 * currently focused position, for example.
865 * Works like con_descend_focused but considers only tiling cons.
868 Con *con_descend_tiling_focused(Con *con) {
876 TAILQ_FOREACH(child, &(next->focus_head), focused) {
877 if (child->type == CT_FLOATING_CON)
883 } while (before != next && next != focused);
888 * Returns the leftmost, rightmost, etc. container in sub-tree. For example, if
889 * direction is D_LEFT, then we return the rightmost container and if direction
890 * is D_RIGHT, we return the leftmost container. This is because if we are
891 * moving D_LEFT, and thus want the rightmost container.
894 Con *con_descend_direction(Con *con, direction_t direction) {
896 int orientation = con_orientation(con);
897 DLOG("con_descend_direction(%p, orientation %d, direction %d)\n", con, orientation, direction);
898 if (direction == D_LEFT || direction == D_RIGHT) {
899 if (orientation == HORIZ) {
900 /* If the direction is horizontal, we can use either the first
901 * (D_RIGHT) or the last con (D_LEFT) */
902 if (direction == D_RIGHT)
903 most = TAILQ_FIRST(&(con->nodes_head));
904 else most = TAILQ_LAST(&(con->nodes_head), nodes_head);
905 } else if (orientation == VERT) {
906 /* Wrong orientation. We use the last focused con. Within that con,
907 * we recurse to chose the left/right con or at least the last
909 most = TAILQ_FIRST(&(con->focus_head));
911 /* If the con has no orientation set, it’s not a split container
912 * but a container with a client window, so stop recursing */
917 if (direction == D_UP || direction == D_DOWN) {
918 if (orientation == VERT) {
919 /* If the direction is vertical, we can use either the first
920 * (D_DOWN) or the last con (D_UP) */
921 if (direction == D_UP)
922 most = TAILQ_LAST(&(con->nodes_head), nodes_head);
923 else most = TAILQ_FIRST(&(con->nodes_head));
924 } else if (orientation == HORIZ) {
925 /* Wrong orientation. We use the last focused con. Within that con,
926 * we recurse to chose the top/bottom con or at least the last
928 most = TAILQ_FIRST(&(con->focus_head));
930 /* If the con has no orientation set, it’s not a split container
931 * but a container with a client window, so stop recursing */
938 return con_descend_direction(most, direction);
942 * Returns a "relative" Rect which contains the amount of pixels that need to
943 * be added to the original Rect to get the final position (obviously the
944 * amount of pixels for normal, 1pixel and borderless are different).
947 Rect con_border_style_rect(Con *con) {
948 adjacent_t borders_to_hide = ADJ_NONE;
950 /* Shortcut to avoid calling con_adjacent_borders() on dock containers. */
951 int border_style = con_border_style(con);
952 if (border_style == BS_NONE)
953 return (Rect){ 0, 0, 0, 0 };
954 borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
955 switch (border_style) {
957 result = (Rect){2, 0, -(2 * 2), -2};
958 if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
962 if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
965 /* With normal borders we never hide the upper border */
966 if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
972 result = (Rect){1, 1, -2, -2};
973 if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
977 if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
980 if (borders_to_hide & ADJ_UPPER_SCREEN_EDGE) {
984 if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
990 return (Rect){0, 0, 0, 0};
998 * Returns adjacent borders of the window. We need this if hide_edge_borders is
1001 adjacent_t con_adjacent_borders(Con *con) {
1002 adjacent_t result = ADJ_NONE;
1003 Con *workspace = con_get_workspace(con);
1004 if (con->rect.x == workspace->rect.x)
1005 result |= ADJ_LEFT_SCREEN_EDGE;
1006 if (con->rect.x + con->rect.width == workspace->rect.x + workspace->rect.width)
1007 result |= ADJ_RIGHT_SCREEN_EDGE;
1008 if (con->rect.y == workspace->rect.y)
1009 result |= ADJ_UPPER_SCREEN_EDGE;
1010 if (con->rect.y + con->rect.height == workspace->rect.y + workspace->rect.height)
1011 result |= ADJ_LOWER_SCREEN_EDGE;
1016 * Use this function to get a container’s border style. This is important
1017 * because when inside a stack, the border style is always BS_NORMAL.
1018 * For tabbed mode, the same applies, with one exception: when the container is
1019 * borderless and the only element in the tabbed container, the border is not
1022 * For children of a CT_DOCKAREA, the border style is always none.
1025 int con_border_style(Con *con) {
1026 Con *fs = con_get_fullscreen_con(con->parent, CF_OUTPUT);
1028 DLOG("this one is fullscreen! overriding BS_NONE\n");
1032 if (con->parent->layout == L_STACKED)
1033 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1035 if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
1036 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1038 if (con->parent->type == CT_DOCKAREA)
1041 return con->border_style;
1045 * Sets the given border style on con, correctly keeping the position/size of a
1049 void con_set_border_style(Con *con, int border_style) {
1050 /* Handle the simple case: non-floating containerns */
1051 if (!con_is_floating(con)) {
1052 con->border_style = border_style;
1056 /* For floating containers, we want to keep the position/size of the
1057 * *window* itself. We first add the border pixels to con->rect to make
1058 * con->rect represent the absolute position of the window. Then, we change
1059 * the border and subtract the new border pixels. Afterwards, we update
1060 * parent->rect to contain con. */
1061 DLOG("This is a floating container\n");
1063 Rect bsr = con_border_style_rect(con);
1064 con->rect.x += bsr.x;
1065 con->rect.y += bsr.y;
1066 con->rect.width += bsr.width;
1067 con->rect.height += bsr.height;
1069 /* Change the border style, get new border/decoration values. */
1070 con->border_style = border_style;
1071 bsr = con_border_style_rect(con);
1073 (con->border_style == BS_NORMAL ? config.font.height + 5 : 0);
1075 con->rect.x -= bsr.x;
1076 con->rect.y -= bsr.y;
1077 con->rect.width -= bsr.width;
1078 con->rect.height -= bsr.height;
1080 Con *parent = con->parent;
1081 parent->rect.x = con->rect.x;
1082 parent->rect.y = con->rect.y - deco_height;
1083 parent->rect.width = con->rect.width;
1084 parent->rect.height = con->rect.height + deco_height;
1088 * This function changes the layout of a given container. Use it to handle
1089 * special cases like changing a whole workspace to stacked/tabbed (creates a
1090 * new split container before).
1093 void con_set_layout(Con *con, int layout) {
1094 DLOG("con_set_layout(%p, %d), con->type = %d\n",
1095 con, layout, con->type);
1097 /* Users can focus workspaces, but not any higher in the hierarchy.
1098 * Focus on the workspace is a special case, since in every other case, the
1099 * user means "change the layout of the parent split container". */
1100 if (con->type != CT_WORKSPACE)
1103 /* We fill in last_split_layout when switching to a different layout
1104 * since there are many places in the code that don’t use
1105 * con_set_layout(). */
1106 if (con->layout == L_SPLITH || con->layout == L_SPLITV)
1107 con->last_split_layout = con->layout;
1109 /* When the container type is CT_WORKSPACE, the user wants to change the
1110 * whole workspace into stacked/tabbed mode. To do this and still allow
1111 * intuitive operations (like level-up and then opening a new window), we
1112 * need to create a new split container. */
1113 if (con->type == CT_WORKSPACE &&
1114 (layout == L_STACKED || layout == L_TABBED)) {
1115 if (con_num_children(con) == 0) {
1116 DLOG("Setting workspace_layout to %d\n", layout);
1117 con->workspace_layout = layout;
1119 DLOG("Creating new split container\n");
1120 /* 1: create a new split container */
1121 Con *new = con_new(NULL, NULL);
1124 /* 2: Set the requested layout on the split container and mark it as
1126 new->layout = layout;
1127 new->last_split_layout = con->last_split_layout;
1130 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
1131 if (old_focused == TAILQ_END(&(con->focus_head)))
1134 /* 3: move the existing cons of this workspace below the new con */
1135 DLOG("Moving cons\n");
1137 while (!TAILQ_EMPTY(&(con->nodes_head))) {
1138 child = TAILQ_FIRST(&(con->nodes_head));
1140 con_attach(child, new, true);
1143 /* 4: attach the new split container to the workspace */
1144 DLOG("Attaching new split to ws\n");
1145 con_attach(new, con, false);
1148 con_focus(old_focused);
1150 tree_flatten(croot);
1155 if (layout == L_DEFAULT) {
1156 /* Special case: the layout formerly known as "default" (in combination
1157 * with an orientation). Since we switched to splith/splitv layouts,
1158 * using the "default" layout (which "only" should happen when using
1159 * legacy configs) is using the last split layout (either splith or
1160 * splitv) in order to still do the same thing.
1162 * Starting from v4.6 though, we will nag users about using "layout
1163 * default", and in v4.9 we will remove it entirely (with an
1164 * appropriate i3-migrate-config mechanism). */
1165 con->layout = con->last_split_layout;
1166 /* In case last_split_layout was not initialized… */
1167 if (con->layout == L_DEFAULT)
1168 con->layout = L_SPLITH;
1170 con->layout = layout;
1175 * This function toggles the layout of a given container. toggle_mode can be
1176 * either 'default' (toggle only between stacked/tabbed/last_split_layout),
1177 * 'split' (toggle only between splitv/splith) or 'all' (toggle between all
1181 void con_toggle_layout(Con *con, const char *toggle_mode) {
1183 /* Users can focus workspaces, but not any higher in the hierarchy.
1184 * Focus on the workspace is a special case, since in every other case, the
1185 * user means "change the layout of the parent split container". */
1186 if (con->type != CT_WORKSPACE)
1187 parent = con->parent;
1188 DLOG("con_toggle_layout(%p, %s), parent = %p\n", con, toggle_mode, parent);
1190 if (strcmp(toggle_mode, "split") == 0) {
1191 /* Toggle between splits. When the current layout is not a split
1192 * layout, we just switch back to last_split_layout. Otherwise, we
1193 * change to the opposite split layout. */
1194 if (parent->layout != L_SPLITH && parent->layout != L_SPLITV)
1195 con_set_layout(con, parent->last_split_layout);
1197 if (parent->layout == L_SPLITH)
1198 con_set_layout(con, L_SPLITV);
1199 else con_set_layout(con, L_SPLITH);
1202 if (parent->layout == L_STACKED)
1203 con_set_layout(con, L_TABBED);
1204 else if (parent->layout == L_TABBED) {
1205 if (strcmp(toggle_mode, "all") == 0)
1206 con_set_layout(con, L_SPLITH);
1207 else con_set_layout(con, parent->last_split_layout);
1208 } else if (parent->layout == L_SPLITH || parent->layout == L_SPLITV) {
1209 if (strcmp(toggle_mode, "all") == 0) {
1210 /* When toggling through all modes, we toggle between
1211 * splith/splitv, whereas normally we just directly jump to
1213 if (parent->layout == L_SPLITH)
1214 con_set_layout(con, L_SPLITV);
1215 else con_set_layout(con, L_STACKED);
1217 con_set_layout(con, L_STACKED);
1224 * Callback which will be called when removing a child from the given con.
1225 * Kills the container if it is empty and replaces it with the child if there
1226 * is exactly one child.
1229 static void con_on_remove_child(Con *con) {
1230 DLOG("on_remove_child\n");
1232 /* Every container 'above' (in the hierarchy) the workspace content should
1233 * not be closed when the last child was removed */
1234 if (con->type == CT_OUTPUT ||
1235 con->type == CT_ROOT ||
1236 con->type == CT_DOCKAREA) {
1237 DLOG("not handling, type = %d\n", con->type);
1241 /* For workspaces, close them only if they're not visible anymore */
1242 if (con->type == CT_WORKSPACE) {
1243 if (TAILQ_EMPTY(&(con->focus_head)) && !workspace_is_visible(con)) {
1244 LOG("Closing old workspace (%p / %s), it is empty\n", con, con->name);
1245 tree_close(con, DONT_KILL_WINDOW, false, false);
1246 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
1251 /* TODO: check if this container would swallow any other client and
1252 * don’t close it automatically. */
1253 int children = con_num_children(con);
1254 if (children == 0) {
1255 DLOG("Container empty, closing\n");
1256 tree_close(con, DONT_KILL_WINDOW, false, false);
1262 * Determines the minimum size of the given con by looking at its children (for
1263 * split/stacked/tabbed cons). Will be called when resizing floating cons
1266 Rect con_minimum_size(Con *con) {
1267 DLOG("Determining minimum size for con %p\n", con);
1269 if (con_is_leaf(con)) {
1270 DLOG("leaf node, returning 75x50\n");
1271 return (Rect){ 0, 0, 75, 50 };
1274 if (con->type == CT_FLOATING_CON) {
1275 DLOG("floating con\n");
1276 Con *child = TAILQ_FIRST(&(con->nodes_head));
1277 return con_minimum_size(child);
1280 if (con->layout == L_STACKED || con->layout == L_TABBED) {
1281 uint32_t max_width = 0, max_height = 0, deco_height = 0;
1283 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1284 Rect min = con_minimum_size(child);
1285 deco_height += child->deco_rect.height;
1286 max_width = max(max_width, min.width);
1287 max_height = max(max_height, min.height);
1289 DLOG("stacked/tabbed now, returning %d x %d + deco_rect = %d\n",
1290 max_width, max_height, deco_height);
1291 return (Rect){ 0, 0, max_width, max_height + deco_height };
1294 /* For horizontal/vertical split containers we sum up the width (h-split)
1295 * or height (v-split) and use the maximum of the height (h-split) or width
1296 * (v-split) as minimum size. */
1298 uint32_t width = 0, height = 0;
1300 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1301 Rect min = con_minimum_size(child);
1302 if (con->layout == L_SPLITH) {
1304 height = max(height, min.height);
1306 height += min.height;
1307 width = max(width, min.width);
1310 DLOG("split container, returning width = %d x height = %d\n", width, height);
1311 return (Rect){ 0, 0, width, height };
1314 ELOG("Unhandled case, type = %d, layout = %d, split = %d\n",
1315 con->type, con->layout, con->split);
1320 * Returns true if changing the focus to con would be allowed considering
1321 * the fullscreen focus constraints. Specifically, if a fullscreen container or
1322 * any of its descendants is focused, this function returns true if and only if
1323 * focusing con would mean that focus would still be visible on screen, i.e.,
1324 * the newly focused container would not be obscured by a fullscreen container.
1326 * In the simplest case, if a fullscreen container or any of its descendants is
1327 * fullscreen, this functions returns true if con is the fullscreen container
1328 * itself or any of its descendants, as this means focus wouldn't escape the
1329 * boundaries of the fullscreen container.
1331 * In case the fullscreen container is of type CF_OUTPUT, this function returns
1332 * true if con is on a different workspace, as focus wouldn't be obscured by
1333 * the fullscreen container that is constrained to a different workspace.
1335 * Note that this same logic can be applied to moving containers. If a
1336 * container can be focused under the fullscreen focus constraints, it can also
1337 * become a parent or sibling to the currently focused container.
1340 bool con_fullscreen_permits_focusing(Con *con) {
1341 /* No focus, no problem. */
1345 /* Find the first fullscreen ascendent. */
1347 while (fs && fs->fullscreen_mode == CF_NONE)
1350 /* fs must be non-NULL since the workspace con doesn’t have CF_NONE and
1351 * there always has to be a workspace con in the hierarchy. */
1353 /* The most common case is we hit the workspace level. In this
1354 * situation, changing focus is also harmless. */
1355 assert(fs->fullscreen_mode != CF_NONE);
1356 if (fs->type == CT_WORKSPACE)
1359 /* Allow it if the container itself is the fullscreen container. */
1363 /* If fullscreen is per-output, the focus being in a different workspace is
1364 * sufficient to guarantee that change won't leave fullscreen in bad shape. */
1365 if (fs->fullscreen_mode == CF_OUTPUT &&
1366 con_get_workspace(con) != con_get_workspace(fs)) {
1370 /* Allow it only if the container to be focused is contained within the
1371 * current fullscreen container. */
1373 if (con->parent == fs)
1378 /* Focusing con would hide it behind a fullscreen window, disallow it. */