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->name = strdup("");
39 LOG("opening window %d\n", cnt);
41 /* TODO: remove window coloring after test-phase */
42 LOG("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;
110 /* Get the first tiling container in focus stack */
111 TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
112 if (loop->type == CT_FLOATING_CON)
119 /* Insert the container after the tiling container, if found */
121 DLOG("Inserting con = %p after last focused tiling con %p\n",
123 TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
124 } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
127 /* We insert to the TAIL because con_focus() will correct this.
128 * This way, we have the option to insert Cons without having
130 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
134 * Detaches the given container from its current parent
137 void con_detach(Con *con) {
138 if (con->type == CT_FLOATING_CON) {
139 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
140 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
142 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
143 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
148 * Sets input focus to the given container. Will be updated in X11 in the next
149 * run of x_push_changes().
152 void con_focus(Con *con) {
155 /* 1: set focused-pointer to the new con */
156 /* 2: exchange the position of the container in focus stack of the parent all the way up */
157 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
158 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
159 if (con->parent->parent != NULL)
160 con_focus(con->parent);
165 workspace_update_urgent_flag(con_get_workspace(con));
170 * Returns true when this node is a leaf node (has no children)
173 bool con_is_leaf(Con *con) {
174 return TAILQ_EMPTY(&(con->nodes_head));
178 * Returns true if this node accepts a window (if the node swallows windows,
179 * it might already have swallowed enough and cannot hold any more).
182 bool con_accepts_window(Con *con) {
183 /* 1: workspaces never accept direct windows */
184 if (con->type == CT_WORKSPACE)
187 if (con->orientation != NO_ORIENTATION) {
188 DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
192 /* TODO: if this is a swallowing container, we need to check its max_clients */
193 return (con->window == NULL);
197 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
201 Con *con_get_output(Con *con) {
203 while (result != NULL && result->type != CT_OUTPUT)
204 result = result->parent;
205 /* We must be able to get an output because focus can never be set higher
206 * in the tree (root node cannot be focused). */
207 assert(result != NULL);
212 * Gets the workspace container this node is on.
215 Con *con_get_workspace(Con *con) {
217 while (result != NULL && result->type != CT_WORKSPACE)
218 result = result->parent;
219 assert(result != NULL);
224 * helper data structure for the breadth-first-search in
225 * con_get_fullscreen_con()
231 TAILQ_ENTRY(bfs_entry) entries;
235 * Returns the first fullscreen node below this node.
238 Con *con_get_fullscreen_con(Con *con) {
239 Con *current, *child;
241 /* TODO: is breadth-first-search really appropriate? (check as soon as
242 * fullscreen levels and fullscreen for containers is implemented) */
243 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
244 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
246 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
248 while (!TAILQ_EMPTY(&bfs_head)) {
249 entry = TAILQ_FIRST(&bfs_head);
250 current = entry->con;
251 if (current != con && current->fullscreen_mode != CF_NONE) {
252 /* empty the queue */
253 while (!TAILQ_EMPTY(&bfs_head)) {
254 entry = TAILQ_FIRST(&bfs_head);
255 TAILQ_REMOVE(&bfs_head, entry, entries);
261 TAILQ_REMOVE(&bfs_head, entry, entries);
264 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
265 entry = smalloc(sizeof(struct bfs_entry));
267 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
275 * Returns true if the node is floating.
278 bool con_is_floating(Con *con) {
280 LOG("checking if con %p is floating\n", con);
281 return (con->floating >= FLOATING_AUTO_ON);
285 * Returns the container with the given client window ID or NULL if no such
289 Con *con_by_window_id(xcb_window_t window) {
291 TAILQ_FOREACH(con, &all_cons, all_cons)
292 if (con->window != NULL && con->window->id == window)
298 * Returns the container with the given frame ID or NULL if no such container
302 Con *con_by_frame_id(xcb_window_t frame) {
304 TAILQ_FOREACH(con, &all_cons, all_cons)
305 if (con->frame == frame)
311 * Returns the first container which wants to swallow this window
315 Con *con_for_window(i3Window *window, Match **store_match) {
318 LOG("searching con for window %p\n", window);
319 LOG("class == %s\n", window->class_class);
321 TAILQ_FOREACH(con, &all_cons, all_cons)
322 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
323 if (!match_matches_window(match, window))
325 if (store_match != NULL)
326 *store_match = match;
334 * Returns the number of children of this container.
337 int con_num_children(Con *con) {
341 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
348 * Updates the percent attribute of the children of the given container. This
349 * function needs to be called when a window is added or removed from a
353 void con_fix_percent(Con *con, int action) {
355 int children = con_num_children(con);
356 /* TODO: better document why this math works */
358 if (action == WINDOW_ADD)
359 fix = (1.0 - (1.0 / (children+1)));
361 fix = 1.0 / (1.0 - (1.0 / (children+1)));
363 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
364 if (child->percent <= 0.0)
366 child->percent *= fix;
371 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
372 * entered when there already is a fullscreen container on this workspace.
375 void con_toggle_fullscreen(Con *con) {
376 Con *workspace, *fullscreen;
377 LOG("toggling fullscreen for %p / %s\n", con, con->name);
378 if (con->fullscreen_mode == CF_NONE) {
379 /* 1: check if there already is a fullscreen con */
380 workspace = con_get_workspace(con);
381 if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
382 LOG("Not entering fullscreen mode, container (%p/%s) "
383 "already is in fullscreen mode\n",
384 fullscreen, fullscreen->name);
386 /* 2: enable fullscreen */
387 con->fullscreen_mode = CF_OUTPUT;
390 /* 1: disable fullscreen */
391 con->fullscreen_mode = CF_NONE;
393 LOG("mode now: %d\n", con->fullscreen_mode);
395 /* update _NET_WM_STATE if this container has a window */
396 /* TODO: when a window is assigned to a container which is already
397 * fullscreened, this state needs to be pushed to the client, too */
398 if (con->window == NULL)
402 unsigned int num = 0;
404 if (con->fullscreen_mode != CF_NONE)
405 values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
407 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
408 atoms[_NET_WM_STATE], ATOM, 32, num, values);
412 * Moves the given container to the currently focused container on the given
414 * TODO: is there a better place for this function?
417 void con_move_to_workspace(Con *con, Con *workspace) {
418 /* 1: save the container which is going to be focused after the current
419 * container is moved away */
420 Con *focus_next = con_next_focused(con);
422 /* 2: get the focused container of this workspace by going down as far as
424 Con *next = workspace;
426 while (!TAILQ_EMPTY(&(next->focus_head)))
427 next = TAILQ_FIRST(&(next->focus_head));
429 /* 3: we go up one level, but only when next is a normal container */
430 if (next->type != CT_WORKSPACE)
433 DLOG("Re-attaching container to %p / %s\n", next, next->name);
434 /* 4: re-attach the con to the parent of this focused container */
436 con_attach(con, next, false);
438 /* 5: keep focus on the current workspace */
439 con_focus(focus_next);
443 * Returns the orientation of the given container (for stacked containers,
444 * vertical orientation is used regardless of the actual orientation of the
448 int con_orientation(Con *con) {
449 /* stacking containers behave like they are in vertical orientation */
450 if (con->layout == L_STACKED)
453 if (con->layout == L_TABBED)
456 return con->orientation;
460 * Returns the container which will be focused next when the given container
461 * is not available anymore. Called in tree_close and con_move_to_workspace
462 * to properly restore focus.
465 Con *con_next_focused(Con *con) {
467 /* floating containers are attached to a workspace, so we focus either the
468 * next floating container (if any) or the workspace itself. */
469 if (con->type == CT_FLOATING_CON) {
470 DLOG("selecting next for CT_FLOATING_CON\n");
471 next = TAILQ_NEXT(con, floating_windows);
472 if (next == TAILQ_END(&(parent->floating_head))) {
473 Con *ws = con_get_workspace(con);
475 DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
476 while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
477 next = TAILQ_FIRST(&(next->focus_head));
479 DLOG("skipping container itself, we want the next client\n");
480 next = TAILQ_NEXT(next, focused);
483 if (next == TAILQ_END(&(ws->focus_head))) {
484 DLOG("Focus list empty, returning ws\n");
491 /* try to focus the next container on the same level as this one */
492 next = TAILQ_NEXT(con, focused);
494 /* if that was not possible, go up to its parent */
495 if (next == TAILQ_END(&(parent->nodes_head)))
498 /* now go down the focus stack as far as
499 * possible, excluding the current container */
500 while (!TAILQ_EMPTY(&(next->focus_head)) &&
501 TAILQ_FIRST(&(next->focus_head)) != con)
502 next = TAILQ_FIRST(&(next->focus_head));
508 * Returns a "relative" Rect which contains the amount of pixels that need to
509 * be added to the original Rect to get the final position (obviously the
510 * amount of pixels for normal, 1pixel and borderless are different).
513 Rect con_border_style_rect(Con *con) {
514 switch (con_border_style(con)) {
516 return (Rect){2, 0, -(2 * 2), -2};
519 return (Rect){1, 1, -2, -2};
522 return (Rect){0, 0, 0, 0};
530 * Use this function to get a container’s border style. This is important
531 * because when inside a stack, the border style is always BS_NORMAL.
532 * For tabbed mode, the same applies, with one exception: when the container is
533 * borderless and the only element in the tabbed container, the border is not
537 int con_border_style(Con *con) {
538 Con *fs = con_get_fullscreen_con(con->parent);
540 DLOG("this one is fullscreen! overriding BS_NONE\n");
544 if (con->parent->layout == L_STACKED)
547 return con->border_style;
551 * This function changes the layout of a given container. Use it to handle
552 * special cases like changing a whole workspace to stacked/tabbed (creates a
553 * new split container before).
556 void con_set_layout(Con *con, int layout) {
557 /* When the container type is CT_WORKSPACE, the user wants to change the
558 * whole workspace into stacked/tabbed mode. To do this and still allow
559 * intuitive operations (like level-up and then opening a new window), we
560 * need to create a new split container. */
561 if (con->type == CT_WORKSPACE) {
562 DLOG("Creating new split container\n");
563 /* 1: create a new split container */
564 Con *new = con_new(NULL);
567 /* 2: set the requested layout on the split con */
568 new->layout = layout;
570 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
571 * to be set. Otherwise, this con will not be interpreted as a split
573 new->orientation = HORIZ;
575 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
576 if (old_focused == TAILQ_END(&(con->focus_head)))
579 /* 4: move the existing cons of this workspace below the new con */
580 DLOG("Moving cons\n");
582 while (!TAILQ_EMPTY(&(con->nodes_head))) {
583 child = TAILQ_FIRST(&(con->nodes_head));
585 con_attach(child, new, true);
588 /* 4: attach the new split container to the workspace */
589 DLOG("Attaching new split to ws\n");
590 con_attach(new, con, false);
593 con_focus(old_focused);
598 con->layout = layout;