2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
7 * con.c contains all functions which deal with containers directly (creating
8 * containers, searching containers, getting specific properties from
28 * Create a new container (and attach it to the given parent, if not NULL).
29 * This function initializes the data structures and creates the appropriate
30 * X11 IDs using x_con_init().
33 Con *con_new(Con *parent) {
34 Con *new = scalloc(sizeof(Con));
35 TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
37 new->border_style = config.default_border;
39 DLOG("opening window %d\n", cnt);
41 /* TODO: remove window coloring after test-phase */
42 DLOG("color %s\n", colors[cnt]);
43 new->name = strdup(colors[cnt]);
44 //uint32_t cp = get_colorpixel(colors[cnt]);
46 if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
51 // TODO: this needs to be integrated into src/x.c and updated on config file reloads
52 xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &config.client.background);
54 TAILQ_INIT(&(new->floating_head));
55 TAILQ_INIT(&(new->nodes_head));
56 TAILQ_INIT(&(new->focus_head));
57 TAILQ_INIT(&(new->swallow_head));
60 con_attach(new, parent, false);
66 * Attaches the given container to the given parent. This happens when moving
67 * a container or when inserting a new container at a specific place in the
70 * ignore_focus is to just insert the Con at the end (useful when creating a
71 * new split container *around* some containers, that is, detaching and
72 * attaching them in order without wanting to mess with the focus in between).
75 void con_attach(Con *con, Con *parent, bool ignore_focus) {
79 struct nodes_head *nodes_head = &(parent->nodes_head);
81 /* Workspaces are handled differently: they need to be inserted at the
83 if (con->type == CT_WORKSPACE) {
84 DLOG("it's a workspace. num = %d\n", con->num);
85 if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
86 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
88 current = TAILQ_FIRST(nodes_head);
89 if (con->num < current->num) {
90 /* we need to insert the container at the beginning */
91 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
93 while (current->num != -1 && con->num > current->num) {
94 current = TAILQ_NEXT(current, nodes);
95 if (current == TAILQ_END(nodes_head)) {
100 /* we need to insert con after current, if current is not NULL */
102 TAILQ_INSERT_BEFORE(current, con, nodes);
103 else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
106 goto add_to_focus_head;
109 if (con->type == CT_FLOATING_CON) {
110 DLOG("Inserting into floating containers\n");
111 TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
114 /* Get the first tiling container in focus stack */
115 TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
116 if (loop->type == CT_FLOATING_CON)
123 /* Insert the container after the tiling container, if found */
125 DLOG("Inserting con = %p after last focused tiling con %p\n",
127 TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
128 } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
132 /* We insert to the TAIL because con_focus() will correct this.
133 * This way, we have the option to insert Cons without having
135 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
139 * Detaches the given container from its current parent
142 void con_detach(Con *con) {
143 if (con->type == CT_FLOATING_CON) {
144 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
145 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
147 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
148 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
153 * Sets input focus to the given container. Will be updated in X11 in the next
154 * run of x_push_changes().
157 void con_focus(Con *con) {
159 DLOG("con_focus = %p\n", con);
161 /* 1: set focused-pointer to the new con */
162 /* 2: exchange the position of the container in focus stack of the parent all the way up */
163 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
164 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
165 if (con->parent->parent != NULL)
166 con_focus(con->parent);
171 workspace_update_urgent_flag(con_get_workspace(con));
173 DLOG("con_focus done = %p\n", con);
177 * Returns true when this node is a leaf node (has no children)
180 bool con_is_leaf(Con *con) {
181 return TAILQ_EMPTY(&(con->nodes_head));
185 * Returns true if this node accepts a window (if the node swallows windows,
186 * it might already have swallowed enough and cannot hold any more).
189 bool con_accepts_window(Con *con) {
190 /* 1: workspaces never accept direct windows */
191 if (con->type == CT_WORKSPACE)
194 if (con->orientation != NO_ORIENTATION) {
195 DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
199 /* TODO: if this is a swallowing container, we need to check its max_clients */
200 return (con->window == NULL);
204 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
208 Con *con_get_output(Con *con) {
210 while (result != NULL && result->type != CT_OUTPUT)
211 result = result->parent;
212 /* We must be able to get an output because focus can never be set higher
213 * in the tree (root node cannot be focused). */
214 assert(result != NULL);
219 * Gets the workspace container this node is on.
222 Con *con_get_workspace(Con *con) {
224 while (result != NULL && result->type != CT_WORKSPACE)
225 result = result->parent;
226 assert(result != NULL);
231 * helper data structure for the breadth-first-search in
232 * con_get_fullscreen_con()
238 TAILQ_ENTRY(bfs_entry) entries;
242 * Returns the first fullscreen node below this node.
245 Con *con_get_fullscreen_con(Con *con) {
246 Con *current, *child;
248 /* TODO: is breadth-first-search really appropriate? (check as soon as
249 * fullscreen levels and fullscreen for containers is implemented) */
250 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
251 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
253 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
255 while (!TAILQ_EMPTY(&bfs_head)) {
256 entry = TAILQ_FIRST(&bfs_head);
257 current = entry->con;
258 if (current != con && current->fullscreen_mode != CF_NONE) {
259 /* empty the queue */
260 while (!TAILQ_EMPTY(&bfs_head)) {
261 entry = TAILQ_FIRST(&bfs_head);
262 TAILQ_REMOVE(&bfs_head, entry, entries);
268 TAILQ_REMOVE(&bfs_head, entry, entries);
271 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
272 entry = smalloc(sizeof(struct bfs_entry));
274 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
277 TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
278 entry = smalloc(sizeof(struct bfs_entry));
280 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
288 * Returns true if the node is floating.
291 bool con_is_floating(Con *con) {
293 DLOG("checking if con %p is floating\n", con);
294 return (con->floating >= FLOATING_AUTO_ON);
298 * Checks if the given container is either floating or inside some floating
299 * container. It returns the FLOATING_CON container.
302 Con *con_inside_floating(Con *con) {
304 if (con->type == CT_FLOATING_CON)
307 if (con->floating >= FLOATING_AUTO_ON)
310 if (con->type == CT_WORKSPACE)
313 return con_inside_floating(con->parent);
317 * Returns the container with the given client window ID or NULL if no such
321 Con *con_by_window_id(xcb_window_t window) {
323 TAILQ_FOREACH(con, &all_cons, all_cons)
324 if (con->window != NULL && con->window->id == window)
330 * Returns the container with the given frame ID or NULL if no such container
334 Con *con_by_frame_id(xcb_window_t frame) {
336 TAILQ_FOREACH(con, &all_cons, all_cons)
337 if (con->frame == frame)
343 * Returns the first container which wants to swallow this window
347 Con *con_for_window(i3Window *window, Match **store_match) {
350 DLOG("searching con for window %p\n", window);
351 DLOG("class == %s\n", window->class_class);
353 TAILQ_FOREACH(con, &all_cons, all_cons)
354 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
355 if (!match_matches_window(match, window))
357 if (store_match != NULL)
358 *store_match = match;
366 * Returns the number of children of this container.
369 int con_num_children(Con *con) {
373 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
380 * Updates the percent attribute of the children of the given container. This
381 * function needs to be called when a window is added or removed from a
385 void con_fix_percent(Con *con) {
387 int children = con_num_children(con);
389 // calculate how much we have distributed and how many containers
390 // with a percentage set we have
392 int children_with_percent = 0;
393 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
394 if (child->percent > 0.0) {
395 total += child->percent;
396 ++children_with_percent;
400 // if there were children without a percentage set, set to a value that
401 // will make those children proportional to all others
402 if (children_with_percent != children) {
403 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
404 if (child->percent <= 0.0) {
405 if (children_with_percent == 0)
406 total += (child->percent = 1.0);
407 else total += (child->percent = total / children_with_percent);
412 // if we got a zero, just distribute the space equally, otherwise
413 // distribute according to the proportions we got
415 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
416 child->percent = 1.0 / children;
417 } else if (total != 1.0) {
418 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
419 child->percent /= total;
424 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
425 * entered when there already is a fullscreen container on this workspace.
428 void con_toggle_fullscreen(Con *con) {
429 Con *workspace, *fullscreen;
431 if (con->type == CT_WORKSPACE) {
432 DLOG("You cannot make a workspace fullscreen.\n");
436 DLOG("toggling fullscreen for %p / %s\n", con, con->name);
437 if (con->fullscreen_mode == CF_NONE) {
438 /* 1: check if there already is a fullscreen con */
439 workspace = con_get_workspace(con);
440 if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
441 LOG("Not entering fullscreen mode, container (%p/%s) "
442 "already is in fullscreen mode\n",
443 fullscreen, fullscreen->name);
445 /* 2: enable fullscreen */
446 con->fullscreen_mode = CF_OUTPUT;
449 /* 1: disable fullscreen */
450 con->fullscreen_mode = CF_NONE;
452 DLOG("mode now: %d\n", con->fullscreen_mode);
454 /* update _NET_WM_STATE if this container has a window */
455 /* TODO: when a window is assigned to a container which is already
456 * fullscreened, this state needs to be pushed to the client, too */
457 if (con->window == NULL)
461 unsigned int num = 0;
463 if (con->fullscreen_mode != CF_NONE)
464 values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
466 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
467 atoms[_NET_WM_STATE], ATOM, 32, num, values);
471 * Moves the given container to the currently focused container on the given
473 * TODO: is there a better place for this function?
476 void con_move_to_workspace(Con *con, Con *workspace) {
477 if (con->type == CT_WORKSPACE) {
478 DLOG("Moving workspaces is not yet implemented.\n");
482 if (con_is_floating(con)) {
483 DLOG("Using FLOATINGCON instead\n");
487 /* 1: save the container which is going to be focused after the current
488 * container is moved away */
489 Con *focus_next = con_next_focused(con);
491 /* 2: get the focused container of this workspace */
492 Con *next = con_descend_focused(workspace);
494 /* 3: we go up one level, but only when next is a normal container */
495 if (next->type != CT_WORKSPACE)
498 /* 4: if the target container is floating, we get the workspace instead.
499 * Only tiling windows need to get inserted next to the current container.
501 Con *floatingcon = con_inside_floating(next);
502 if (floatingcon != NULL) {
503 DLOG("floatingcon, going up even further\n");
504 next = floatingcon->parent;
507 DLOG("Re-attaching container to %p / %s\n", next, next->name);
508 /* 5: re-attach the con to the parent of this focused container */
509 Con *parent = con->parent;
511 con_attach(con, next, false);
513 /* 6: fix the percentages */
514 con_fix_percent(parent);
516 con_fix_percent(next);
518 /* 7: focus the con on the target workspace (the X focus is only updated by
519 * calling tree_render(), so for the "real" focus this is a no-op) */
522 /* 8: keep focus on the current workspace */
523 con_focus(focus_next);
525 /* 9: check if the parent container is empty now and close it */
526 if (parent->type != CT_WORKSPACE &&
527 TAILQ_EMPTY(&(parent->nodes_head))) {
528 DLOG("Closing empty parent container\n");
529 /* TODO: check if this container would swallow any other client and
530 * don’t close it automatically. */
531 tree_close(parent, false, false);
536 * Returns the orientation of the given container (for stacked containers,
537 * vertical orientation is used regardless of the actual orientation of the
541 int con_orientation(Con *con) {
542 /* stacking containers behave like they are in vertical orientation */
543 if (con->layout == L_STACKED)
546 if (con->layout == L_TABBED)
549 return con->orientation;
553 * Returns the container which will be focused next when the given container
554 * is not available anymore. Called in tree_close and con_move_to_workspace
555 * to properly restore focus.
558 Con *con_next_focused(Con *con) {
560 /* floating containers are attached to a workspace, so we focus either the
561 * next floating container (if any) or the workspace itself. */
562 if (con->type == CT_FLOATING_CON) {
563 DLOG("selecting next for CT_FLOATING_CON\n");
564 next = TAILQ_NEXT(con, floating_windows);
565 if (next == TAILQ_END(&(parent->floating_head))) {
566 Con *ws = con_get_workspace(con);
568 DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
569 while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
570 next = TAILQ_FIRST(&(next->focus_head));
572 DLOG("skipping container itself, we want the next client\n");
573 next = TAILQ_NEXT(next, focused);
576 if (next == TAILQ_END(&(ws->focus_head))) {
577 DLOG("Focus list empty, returning ws\n");
584 /* try to focus the next container on the same level as this one */
585 next = TAILQ_NEXT(con, focused);
587 /* if that was not possible, go up to its parent */
588 if (next == TAILQ_END(&(parent->nodes_head)))
591 /* now go down the focus stack as far as
592 * possible, excluding the current container */
593 while (!TAILQ_EMPTY(&(next->focus_head)) &&
594 TAILQ_FIRST(&(next->focus_head)) != con)
595 next = TAILQ_FIRST(&(next->focus_head));
601 * Get the next/previous container in the specified orientation. This may
602 * travel up until it finds a container with suitable orientation.
605 Con *con_get_next(Con *con, char way, orientation_t orientation) {
606 DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
607 /* 1: get the first parent with the same orientation */
609 while (con_orientation(cur->parent) != orientation) {
610 DLOG("need to go one level further up\n");
611 if (cur->parent->type == CT_WORKSPACE) {
612 LOG("that's a workspace, we can't go further up\n");
618 /* 2: chose next (or previous) */
621 next = TAILQ_NEXT(cur, nodes);
622 /* if we are at the end of the list, we need to wrap */
623 if (next == TAILQ_END(&(parent->nodes_head)))
626 next = TAILQ_PREV(cur, nodes_head, nodes);
627 /* if we are at the end of the list, we need to wrap */
628 if (next == TAILQ_END(&(cur->nodes_head)))
631 DLOG("next = %p\n", next);
637 * Returns the focused con inside this client, descending the tree as far as
638 * possible. This comes in handy when attaching a con to a workspace at the
639 * currently focused position, for example.
642 Con *con_descend_focused(Con *con) {
644 while (!TAILQ_EMPTY(&(next->focus_head)))
645 next = TAILQ_FIRST(&(next->focus_head));
650 * Returns a "relative" Rect which contains the amount of pixels that need to
651 * be added to the original Rect to get the final position (obviously the
652 * amount of pixels for normal, 1pixel and borderless are different).
655 Rect con_border_style_rect(Con *con) {
656 switch (con_border_style(con)) {
658 return (Rect){2, 0, -(2 * 2), -2};
661 return (Rect){1, 1, -2, -2};
664 return (Rect){0, 0, 0, 0};
672 * Use this function to get a container’s border style. This is important
673 * because when inside a stack, the border style is always BS_NORMAL.
674 * For tabbed mode, the same applies, with one exception: when the container is
675 * borderless and the only element in the tabbed container, the border is not
679 int con_border_style(Con *con) {
680 Con *fs = con_get_fullscreen_con(con->parent);
682 DLOG("this one is fullscreen! overriding BS_NONE\n");
686 if (con->parent->layout == L_STACKED)
687 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
689 if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
690 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
692 return con->border_style;
696 * This function changes the layout of a given container. Use it to handle
697 * special cases like changing a whole workspace to stacked/tabbed (creates a
698 * new split container before).
701 void con_set_layout(Con *con, int layout) {
702 /* When the container type is CT_WORKSPACE, the user wants to change the
703 * whole workspace into stacked/tabbed mode. To do this and still allow
704 * intuitive operations (like level-up and then opening a new window), we
705 * need to create a new split container. */
706 if (con->type == CT_WORKSPACE) {
707 DLOG("Creating new split container\n");
708 /* 1: create a new split container */
709 Con *new = con_new(NULL);
712 /* 2: set the requested layout on the split con */
713 new->layout = layout;
715 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
716 * to be set. Otherwise, this con will not be interpreted as a split
718 new->orientation = HORIZ;
720 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
721 if (old_focused == TAILQ_END(&(con->focus_head)))
724 /* 4: move the existing cons of this workspace below the new con */
725 DLOG("Moving cons\n");
727 while (!TAILQ_EMPTY(&(con->nodes_head))) {
728 child = TAILQ_FIRST(&(con->nodes_head));
730 con_attach(child, new, true);
733 /* 4: attach the new split container to the workspace */
734 DLOG("Attaching new split to ws\n");
735 con_attach(new, con, false);
738 con_focus(old_focused);
745 con->layout = layout;