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);
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
71 void con_attach(Con *con, Con *parent) {
76 /* Get the first tiling container in focus stack */
77 TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
78 if (loop->type == CT_FLOATING_CON)
84 /* Insert the container after the tiling container, if found */
86 DLOG("Inserting con = %p after last focused tiling con %p\n",
88 TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
89 } else TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
91 /* We insert to the TAIL because con_focus() will correct this.
92 * This way, we have the option to insert Cons without having
94 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
98 * Detaches the given container from its current parent
101 void con_detach(Con *con) {
102 if (con->type == CT_FLOATING_CON) {
103 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
104 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
106 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
107 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
112 * Sets input focus to the given container. Will be updated in X11 in the next
113 * run of x_push_changes().
116 void con_focus(Con *con) {
119 /* 1: set focused-pointer to the new con */
120 /* 2: exchange the position of the container in focus stack of the parent all the way up */
121 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
122 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
123 if (con->parent->parent != NULL)
124 con_focus(con->parent);
129 workspace_update_urgent_flag(con_get_workspace(con));
134 * Returns true when this node is a leaf node (has no children)
137 bool con_is_leaf(Con *con) {
138 return TAILQ_EMPTY(&(con->nodes_head));
142 * Returns true if this node accepts a window (if the node swallows windows,
143 * it might already have swallowed enough and cannot hold any more).
146 bool con_accepts_window(Con *con) {
147 /* 1: workspaces never accept direct windows */
148 if (con->type == CT_WORKSPACE)
151 if (con->orientation != NO_ORIENTATION) {
152 DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
156 /* TODO: if this is a swallowing container, we need to check its max_clients */
157 return (con->window == NULL);
161 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
165 Con *con_get_output(Con *con) {
167 while (result != NULL && result->type != CT_OUTPUT)
168 result = result->parent;
169 /* We must be able to get an output because focus can never be set higher
170 * in the tree (root node cannot be focused). */
171 assert(result != NULL);
176 * Gets the workspace container this node is on.
179 Con *con_get_workspace(Con *con) {
181 while (result != NULL && result->type != CT_WORKSPACE)
182 result = result->parent;
183 assert(result != NULL);
188 * helper data structure for the breadth-first-search in
189 * con_get_fullscreen_con()
195 TAILQ_ENTRY(bfs_entry) entries;
199 * Returns the first fullscreen node below this node.
202 Con *con_get_fullscreen_con(Con *con) {
203 Con *current, *child;
205 LOG("looking for fullscreen node\n");
206 /* TODO: is breadth-first-search really appropriate? (check as soon as
207 * fullscreen levels and fullscreen for containers is implemented) */
208 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
209 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
211 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
213 while (!TAILQ_EMPTY(&bfs_head)) {
214 entry = TAILQ_FIRST(&bfs_head);
215 current = entry->con;
216 LOG("checking %p\n", current);
217 if (current != con && current->fullscreen_mode != CF_NONE) {
218 /* empty the queue */
219 while (!TAILQ_EMPTY(&bfs_head)) {
220 entry = TAILQ_FIRST(&bfs_head);
221 TAILQ_REMOVE(&bfs_head, entry, entries);
227 LOG("deleting from queue\n");
228 TAILQ_REMOVE(&bfs_head, entry, entries);
231 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
232 entry = smalloc(sizeof(struct bfs_entry));
234 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
242 * Returns true if the node is floating.
245 bool con_is_floating(Con *con) {
247 LOG("checking if con %p is floating\n", con);
248 return (con->floating >= FLOATING_AUTO_ON);
252 * Returns the container with the given client window ID or NULL if no such
256 Con *con_by_window_id(xcb_window_t window) {
258 TAILQ_FOREACH(con, &all_cons, all_cons)
259 if (con->window != NULL && con->window->id == window)
265 * Returns the container with the given frame ID or NULL if no such container
269 Con *con_by_frame_id(xcb_window_t frame) {
271 TAILQ_FOREACH(con, &all_cons, all_cons)
272 if (con->frame == frame)
278 * Returns the first container which wants to swallow this window
282 Con *con_for_window(i3Window *window, Match **store_match) {
285 LOG("searching con for window %p\n", window);
286 LOG("class == %s\n", window->class_class);
288 TAILQ_FOREACH(con, &all_cons, all_cons)
289 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
290 if (!match_matches_window(match, window))
292 if (store_match != NULL)
293 *store_match = match;
301 * Returns the number of children of this container.
304 int con_num_children(Con *con) {
308 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
315 * Updates the percent attribute of the children of the given container. This
316 * function needs to be called when a window is added or removed from a
320 void con_fix_percent(Con *con, int action) {
322 int children = con_num_children(con);
323 /* TODO: better document why this math works */
325 if (action == WINDOW_ADD)
326 fix = (1.0 - (1.0 / (children+1)));
328 fix = 1.0 / (1.0 - (1.0 / (children+1)));
330 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
331 if (child->percent <= 0.0)
333 child->percent *= fix;
338 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
339 * entered when there already is a fullscreen container on this workspace.
342 void con_toggle_fullscreen(Con *con) {
343 Con *workspace, *fullscreen;
344 LOG("toggling fullscreen for %p / %s\n", con, con->name);
345 if (con->fullscreen_mode == CF_NONE) {
346 /* 1: check if there already is a fullscreen con */
347 workspace = con_get_workspace(con);
348 if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
349 LOG("Not entering fullscreen mode, container (%p/%s) "
350 "already is in fullscreen mode\n",
351 fullscreen, fullscreen->name);
355 /* 2: enable fullscreen */
356 con->fullscreen_mode = CF_OUTPUT;
358 /* 1: disable fullscreen */
359 con->fullscreen_mode = CF_NONE;
361 LOG("mode now: %d\n", con->fullscreen_mode);
363 /* update _NET_WM_STATE if this container has a window */
364 /* TODO: when a window is assigned to a container which is already
365 * fullscreened, this state needs to be pushed to the client, too */
366 if (con->window == NULL)
370 unsigned int num = 0;
372 if (con->fullscreen_mode != CF_NONE)
373 values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
375 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
376 atoms[_NET_WM_STATE], ATOM, 32, num, values);
380 * Moves the given container to the currently focused container on the given
382 * TODO: is there a better place for this function?
385 void con_move_to_workspace(Con *con, Con *workspace) {
386 /* 1: save the container which is going to be focused after the current
387 * container is moved away */
388 Con *focus_next = con_next_focused(con);
390 /* 2: get the focused container of this workspace by going down as far as
392 Con *next = workspace;
394 while (!TAILQ_EMPTY(&(next->focus_head)))
395 next = TAILQ_FIRST(&(next->focus_head));
397 /* 3: we go up one level, but only when next is a normal container */
398 if (next->type != CT_WORKSPACE)
401 DLOG("Re-attaching container to %p / %s\n", next, next->name);
402 /* 4: re-attach the con to the parent of this focused container */
404 con_attach(con, next);
406 /* 5: keep focus on the current workspace */
407 con_focus(focus_next);
411 * Returns the orientation of the given container (for stacked containers,
412 * vertical orientation is used regardless of the actual orientation of the
416 int con_orientation(Con *con) {
417 /* stacking containers behave like they are in vertical orientation */
418 if (con->layout == L_STACKED)
421 if (con->layout == L_TABBED)
424 return con->orientation;
428 * Returns the container which will be focused next when the given container
429 * is not available anymore. Called in tree_close and con_move_to_workspace
430 * to properly restore focus.
433 Con *con_next_focused(Con *con) {
435 /* floating containers are attached to a workspace, so we focus either the
436 * next floating container (if any) or the workspace itself. */
437 if (con->type == CT_FLOATING_CON) {
438 DLOG("selecting next for CT_FLOATING_CON\n");
439 next = TAILQ_NEXT(con, floating_windows);
440 if (next == TAILQ_END(&(parent->floating_head))) {
441 Con *ws = con_get_workspace(con);
443 DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
444 while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
445 next = TAILQ_FIRST(&(next->focus_head));
447 DLOG("skipping container itself, we want the next client\n");
448 next = TAILQ_NEXT(next, focused);
451 if (next == TAILQ_END(&(ws->focus_head))) {
452 DLOG("Focus list empty, returning ws\n");
459 /* try to focus the next container on the same level as this one */
460 next = TAILQ_NEXT(con, focused);
462 /* if that was not possible, go up to its parent */
463 if (next == TAILQ_END(&(parent->nodes_head)))
466 /* now go down the focus stack as far as
467 * possible, excluding the current container */
468 while (!TAILQ_EMPTY(&(next->focus_head)) &&
469 TAILQ_FIRST(&(next->focus_head)) != con)
470 next = TAILQ_FIRST(&(next->focus_head));
476 * Returns a "relative" Rect which contains the amount of pixels that need to
477 * be added to the original Rect to get the final position (obviously the
478 * amount of pixels for normal, 1pixel and borderless are different).
481 Rect con_border_style_rect(Con *con) {
482 switch (con_border_style(con)) {
484 return (Rect){2, 0, -(2 * 2), -2};
487 return (Rect){1, 1, -2, -2};
490 return (Rect){0, 0, 0, 0};
498 * Use this function to get a container’s border style. This is important
499 * because when inside a stack, the border style is always BS_NORMAL.
500 * For tabbed mode, the same applies, with one exception: when the container is
501 * borderless and the only element in the tabbed container, the border is not
505 int con_border_style(Con *con) {
506 if (con->parent->layout == L_STACKED)
509 return con->border_style;