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 xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &cp);
53 TAILQ_INIT(&(new->floating_head));
54 TAILQ_INIT(&(new->nodes_head));
55 TAILQ_INIT(&(new->focus_head));
56 TAILQ_INIT(&(new->swallow_head));
59 con_attach(new, parent);
65 * Attaches the given container to the given parent. This happens when moving
66 * a container or when inserting a new container at a specific place in the
70 void con_attach(Con *con, Con *parent) {
72 Con *current = TAILQ_FIRST(&(parent->focus_head));
74 if (current == TAILQ_END(&(parent->focus_head)))
75 TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
77 DLOG("inserting after\n");
78 TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
80 /* We insert to the TAIL because con_focus() will correct this.
81 * This way, we have the option to insert Cons without having
83 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
87 * Detaches the given container from its current parent
90 void con_detach(Con *con) {
91 if (con->type == CT_FLOATING_CON) {
92 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
93 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
95 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
96 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
101 * Sets input focus to the given container. Will be updated in X11 in the next
102 * run of x_push_changes().
105 void con_focus(Con *con) {
108 /* 1: set focused-pointer to the new con */
109 /* 2: exchange the position of the container in focus stack of the parent all the way up */
110 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
111 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
112 if (con->parent->parent != NULL)
113 con_focus(con->parent);
118 workspace_update_urgent_flag(con_get_workspace(con));
123 * Returns true when this node is a leaf node (has no children)
126 bool con_is_leaf(Con *con) {
127 return TAILQ_EMPTY(&(con->nodes_head));
131 * Returns true if this node accepts a window (if the node swallows windows,
132 * it might already have swallowed enough and cannot hold any more).
135 bool con_accepts_window(Con *con) {
136 /* 1: workspaces never accept direct windows */
137 if (con->type == CT_WORKSPACE)
140 if (con->orientation != NO_ORIENTATION) {
141 DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
145 /* TODO: if this is a swallowing container, we need to check its max_clients */
146 return (con->window == NULL);
150 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
154 Con *con_get_output(Con *con) {
156 while (result != NULL && result->type != CT_OUTPUT)
157 result = result->parent;
158 /* We must be able to get an output because focus can never be set higher
159 * in the tree (root node cannot be focused). */
160 assert(result != NULL);
165 * Gets the workspace container this node is on.
168 Con *con_get_workspace(Con *con) {
170 while (result != NULL && result->type != CT_WORKSPACE)
171 result = result->parent;
172 assert(result != NULL);
177 * helper data structure for the breadth-first-search in
178 * con_get_fullscreen_con()
184 TAILQ_ENTRY(bfs_entry) entries;
188 * Returns the first fullscreen node below this node.
191 Con *con_get_fullscreen_con(Con *con) {
192 Con *current, *child;
194 LOG("looking for fullscreen node\n");
195 /* TODO: is breadth-first-search really appropriate? (check as soon as
196 * fullscreen levels and fullscreen for containers is implemented) */
197 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
198 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
200 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
202 while (!TAILQ_EMPTY(&bfs_head)) {
203 entry = TAILQ_FIRST(&bfs_head);
204 current = entry->con;
205 LOG("checking %p\n", current);
206 if (current != con && current->fullscreen_mode != CF_NONE) {
207 /* empty the queue */
208 while (!TAILQ_EMPTY(&bfs_head)) {
209 entry = TAILQ_FIRST(&bfs_head);
210 TAILQ_REMOVE(&bfs_head, entry, entries);
216 LOG("deleting from queue\n");
217 TAILQ_REMOVE(&bfs_head, entry, entries);
220 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
221 entry = smalloc(sizeof(struct bfs_entry));
223 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
231 * Returns true if the node is floating.
234 bool con_is_floating(Con *con) {
236 LOG("checking if con %p is floating\n", con);
237 return (con->floating >= FLOATING_AUTO_ON);
241 * Returns the container with the given client window ID or NULL if no such
245 Con *con_by_window_id(xcb_window_t window) {
247 TAILQ_FOREACH(con, &all_cons, all_cons)
248 if (con->window != NULL && con->window->id == window)
254 * Returns the container with the given frame ID or NULL if no such container
258 Con *con_by_frame_id(xcb_window_t frame) {
260 TAILQ_FOREACH(con, &all_cons, all_cons)
261 if (con->frame == frame)
267 * Returns the first container which wants to swallow this window
271 Con *con_for_window(i3Window *window, Match **store_match) {
274 LOG("searching con for window %p\n", window);
275 LOG("class == %s\n", window->class_class);
277 TAILQ_FOREACH(con, &all_cons, all_cons)
278 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
279 if (!match_matches_window(match, window))
281 if (store_match != NULL)
282 *store_match = match;
290 * Updates the percent attribute of the children of the given container. This
291 * function needs to be called when a window is added or removed from a
295 void con_fix_percent(Con *con, int action) {
298 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
300 /* TODO: better document why this math works */
302 if (action == WINDOW_ADD)
303 fix = (1.0 - (1.0 / (children+1)));
305 fix = 1.0 / (1.0 - (1.0 / (children+1)));
307 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
308 if (child->percent <= 0.0)
310 child->percent *= fix;
315 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
316 * entered when there already is a fullscreen container on this workspace.
319 void con_toggle_fullscreen(Con *con) {
320 Con *workspace, *fullscreen;
321 LOG("toggling fullscreen for %p / %s\n", con, con->name);
322 if (con->fullscreen_mode == CF_NONE) {
323 /* 1: check if there already is a fullscreen con */
324 workspace = con_get_workspace(con);
325 if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
326 LOG("Not entering fullscreen mode, container (%p/%s) "
327 "already is in fullscreen mode\n",
328 fullscreen, fullscreen->name);
332 /* 2: enable fullscreen */
333 con->fullscreen_mode = CF_OUTPUT;
335 /* 1: disable fullscreen */
336 con->fullscreen_mode = CF_NONE;
338 LOG("mode now: %d\n", con->fullscreen_mode);
340 /* update _NET_WM_STATE if this container has a window */
341 /* TODO: when a window is assigned to a container which is already
342 * fullscreened, this state needs to be pushed to the client, too */
343 if (con->window == NULL)
347 unsigned int num = 0;
349 if (con->fullscreen_mode != CF_NONE)
350 values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
352 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
353 atoms[_NET_WM_STATE], ATOM, 32, num, values);
357 * Moves the given container to the currently focused container on the given
359 * TODO: is there a better place for this function?
362 void con_move_to_workspace(Con *con, Con *workspace) {
363 /* 1: save the container which is going to be focused after the current
364 * container is moved away */
365 Con *focus_next = con_next_focused(con);
367 /* 2: get the focused container of this workspace by going down as far as
369 Con *next = workspace;
371 while (!TAILQ_EMPTY(&(next->focus_head)))
372 next = TAILQ_FIRST(&(next->focus_head));
374 /* 3: we go up one level, but only when next is a normal container */
375 if (next->type != CT_WORKSPACE)
378 DLOG("Re-attaching container to %p / %s\n", next, next->name);
379 /* 4: re-attach the con to the parent of this focused container */
381 con_attach(con, next);
383 /* 5: keep focus on the current workspace */
384 con_focus(focus_next);
388 * Returns the orientation of the given container (for stacked containers,
389 * vertical orientation is used regardless of the actual orientation of the
393 int con_orientation(Con *con) {
394 /* stacking containers behave like they are in vertical orientation */
395 if (con->layout == L_STACKED)
398 return con->orientation;
402 * Returns the container which will be focused next when the given container
403 * is not available anymore. Called in tree_close and con_move_to_workspace
404 * to properly restore focus.
407 Con *con_next_focused(Con *con) {
409 /* floating containers are attached to a workspace, so we focus either the
410 * next floating container (if any) or the workspace itself. */
411 if (con->type == CT_FLOATING_CON) {
412 next = TAILQ_NEXT(con, floating_windows);
413 if (next == TAILQ_END(&(parent->floating_head)))
414 next = con_get_workspace(con);
418 /* try to focus the next container on the same level as this one */
419 next = TAILQ_NEXT(con, focused);
421 /* if that was not possible, go up to its parent */
422 if (next == TAILQ_END(&(parent->nodes_head)))
425 /* now go down the focus stack as far as
426 * possible, excluding the current container */
427 while (!TAILQ_EMPTY(&(next->focus_head)) &&
428 TAILQ_FIRST(&(next->focus_head)) != con)
429 next = TAILQ_FIRST(&(next->focus_head));