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]);
45 uint32_t cp = get_colorpixel(colors[cnt]);
47 if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
53 // TODO: this needs to be integrated into src/x.c and updated on config file reloads
54 xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &config.client.background);
56 TAILQ_INIT(&(new->floating_head));
57 TAILQ_INIT(&(new->nodes_head));
58 TAILQ_INIT(&(new->focus_head));
59 TAILQ_INIT(&(new->swallow_head));
62 con_attach(new, parent);
68 * Attaches the given container to the given parent. This happens when moving
69 * a container or when inserting a new container at a specific place in the
73 void con_attach(Con *con, Con *parent) {
75 Con *current = TAILQ_FIRST(&(parent->focus_head));
77 if (current == TAILQ_END(&(parent->focus_head)))
78 TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
80 DLOG("inserting after\n");
81 TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
83 /* We insert to the TAIL because con_focus() will correct this.
84 * This way, we have the option to insert Cons without having
86 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
90 * Detaches the given container from its current parent
93 void con_detach(Con *con) {
94 if (con->type == CT_FLOATING_CON) {
95 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
96 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
98 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
99 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
104 * Sets input focus to the given container. Will be updated in X11 in the next
105 * run of x_push_changes().
108 void con_focus(Con *con) {
111 /* 1: set focused-pointer to the new con */
112 /* 2: exchange the position of the container in focus stack of the parent all the way up */
113 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
114 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
115 if (con->parent->parent != NULL)
116 con_focus(con->parent);
121 workspace_update_urgent_flag(con_get_workspace(con));
126 * Returns true when this node is a leaf node (has no children)
129 bool con_is_leaf(Con *con) {
130 return TAILQ_EMPTY(&(con->nodes_head));
134 * Returns true if this node accepts a window (if the node swallows windows,
135 * it might already have swallowed enough and cannot hold any more).
138 bool con_accepts_window(Con *con) {
139 /* 1: workspaces never accept direct windows */
140 if (con->type == CT_WORKSPACE)
143 if (con->orientation != NO_ORIENTATION) {
144 DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
148 /* TODO: if this is a swallowing container, we need to check its max_clients */
149 return (con->window == NULL);
153 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
157 Con *con_get_output(Con *con) {
159 while (result != NULL && result->type != CT_OUTPUT)
160 result = result->parent;
161 /* We must be able to get an output because focus can never be set higher
162 * in the tree (root node cannot be focused). */
163 assert(result != NULL);
168 * Gets the workspace container this node is on.
171 Con *con_get_workspace(Con *con) {
173 while (result != NULL && result->type != CT_WORKSPACE)
174 result = result->parent;
175 assert(result != NULL);
180 * helper data structure for the breadth-first-search in
181 * con_get_fullscreen_con()
187 TAILQ_ENTRY(bfs_entry) entries;
191 * Returns the first fullscreen node below this node.
194 Con *con_get_fullscreen_con(Con *con) {
195 Con *current, *child;
197 LOG("looking for fullscreen node\n");
198 /* TODO: is breadth-first-search really appropriate? (check as soon as
199 * fullscreen levels and fullscreen for containers is implemented) */
200 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
201 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
203 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
205 while (!TAILQ_EMPTY(&bfs_head)) {
206 entry = TAILQ_FIRST(&bfs_head);
207 current = entry->con;
208 LOG("checking %p\n", current);
209 if (current != con && current->fullscreen_mode != CF_NONE) {
210 /* empty the queue */
211 while (!TAILQ_EMPTY(&bfs_head)) {
212 entry = TAILQ_FIRST(&bfs_head);
213 TAILQ_REMOVE(&bfs_head, entry, entries);
219 LOG("deleting from queue\n");
220 TAILQ_REMOVE(&bfs_head, entry, entries);
223 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
224 entry = smalloc(sizeof(struct bfs_entry));
226 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
234 * Returns true if the node is floating.
237 bool con_is_floating(Con *con) {
239 LOG("checking if con %p is floating\n", con);
240 return (con->floating >= FLOATING_AUTO_ON);
244 * Returns the container with the given client window ID or NULL if no such
248 Con *con_by_window_id(xcb_window_t window) {
250 TAILQ_FOREACH(con, &all_cons, all_cons)
251 if (con->window != NULL && con->window->id == window)
257 * Returns the container with the given frame ID or NULL if no such container
261 Con *con_by_frame_id(xcb_window_t frame) {
263 TAILQ_FOREACH(con, &all_cons, all_cons)
264 if (con->frame == frame)
270 * Returns the first container which wants to swallow this window
274 Con *con_for_window(i3Window *window, Match **store_match) {
277 LOG("searching con for window %p\n", window);
278 LOG("class == %s\n", window->class_class);
280 TAILQ_FOREACH(con, &all_cons, all_cons)
281 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
282 if (!match_matches_window(match, window))
284 if (store_match != NULL)
285 *store_match = match;
293 * Returns the number of children of this container.
296 int con_num_children(Con *con) {
300 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
307 * Updates the percent attribute of the children of the given container. This
308 * function needs to be called when a window is added or removed from a
312 void con_fix_percent(Con *con, int action) {
314 int children = con_num_children(con);
315 /* TODO: better document why this math works */
317 if (action == WINDOW_ADD)
318 fix = (1.0 - (1.0 / (children+1)));
320 fix = 1.0 / (1.0 - (1.0 / (children+1)));
322 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
323 if (child->percent <= 0.0)
325 child->percent *= fix;
330 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
331 * entered when there already is a fullscreen container on this workspace.
334 void con_toggle_fullscreen(Con *con) {
335 Con *workspace, *fullscreen;
336 LOG("toggling fullscreen for %p / %s\n", con, con->name);
337 if (con->fullscreen_mode == CF_NONE) {
338 /* 1: check if there already is a fullscreen con */
339 workspace = con_get_workspace(con);
340 if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
341 LOG("Not entering fullscreen mode, container (%p/%s) "
342 "already is in fullscreen mode\n",
343 fullscreen, fullscreen->name);
347 /* 2: enable fullscreen */
348 con->fullscreen_mode = CF_OUTPUT;
350 /* 1: disable fullscreen */
351 con->fullscreen_mode = CF_NONE;
353 LOG("mode now: %d\n", con->fullscreen_mode);
355 /* update _NET_WM_STATE if this container has a window */
356 /* TODO: when a window is assigned to a container which is already
357 * fullscreened, this state needs to be pushed to the client, too */
358 if (con->window == NULL)
362 unsigned int num = 0;
364 if (con->fullscreen_mode != CF_NONE)
365 values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
367 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
368 atoms[_NET_WM_STATE], ATOM, 32, num, values);
372 * Moves the given container to the currently focused container on the given
374 * TODO: is there a better place for this function?
377 void con_move_to_workspace(Con *con, Con *workspace) {
378 /* 1: save the container which is going to be focused after the current
379 * container is moved away */
380 Con *focus_next = con_next_focused(con);
382 /* 2: get the focused container of this workspace by going down as far as
384 Con *next = workspace;
386 while (!TAILQ_EMPTY(&(next->focus_head)))
387 next = TAILQ_FIRST(&(next->focus_head));
389 /* 3: we go up one level, but only when next is a normal container */
390 if (next->type != CT_WORKSPACE)
393 DLOG("Re-attaching container to %p / %s\n", next, next->name);
394 /* 4: re-attach the con to the parent of this focused container */
396 con_attach(con, next);
398 /* 5: keep focus on the current workspace */
399 con_focus(focus_next);
403 * Returns the orientation of the given container (for stacked containers,
404 * vertical orientation is used regardless of the actual orientation of the
408 int con_orientation(Con *con) {
409 /* stacking containers behave like they are in vertical orientation */
410 if (con->layout == L_STACKED)
413 return con->orientation;
417 * Returns the container which will be focused next when the given container
418 * is not available anymore. Called in tree_close and con_move_to_workspace
419 * to properly restore focus.
422 Con *con_next_focused(Con *con) {
424 /* floating containers are attached to a workspace, so we focus either the
425 * next floating container (if any) or the workspace itself. */
426 if (con->type == CT_FLOATING_CON) {
427 next = TAILQ_NEXT(con, floating_windows);
428 if (next == TAILQ_END(&(parent->floating_head)))
429 next = con_get_workspace(con);
433 /* try to focus the next container on the same level as this one */
434 next = TAILQ_NEXT(con, focused);
436 /* if that was not possible, go up to its parent */
437 if (next == TAILQ_END(&(parent->nodes_head)))
440 /* now go down the focus stack as far as
441 * possible, excluding the current container */
442 while (!TAILQ_EMPTY(&(next->focus_head)) &&
443 TAILQ_FIRST(&(next->focus_head)) != con)
444 next = TAILQ_FIRST(&(next->focus_head));
450 * Returns a "relative" Rect which contains the amount of pixels that need to
451 * be added to the original Rect to get the final position (obviously the
452 * amount of pixels for normal, 1pixel and borderless are different).
455 Rect con_border_style_rect(Con *con) {
456 if (con->border_style == BS_NORMAL)
457 return (Rect){2, 0, -(2 * 2), -2};
459 if (con->border_style == BS_1PIXEL)
460 return (Rect){1, 1, -2, -2};
462 if (con->border_style == BS_NONE)
463 return (Rect){0, 0, 0, 0};