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) {
73 Con *current = TAILQ_FIRST(&(parent->focus_head));
75 if (current == TAILQ_END(&(parent->focus_head)))
76 TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
78 DLOG("inserting after\n");
79 TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
81 /* We insert to the TAIL because con_focus() will correct this.
82 * This way, we have the option to insert Cons without having
84 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
88 * Detaches the given container from its current parent
91 void con_detach(Con *con) {
92 if (con->type == CT_FLOATING_CON) {
93 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
94 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
96 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
97 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
102 * Sets input focus to the given container. Will be updated in X11 in the next
103 * run of x_push_changes().
106 void con_focus(Con *con) {
109 /* 1: set focused-pointer to the new con */
110 /* 2: exchange the position of the container in focus stack of the parent all the way up */
111 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
112 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
113 if (con->parent->parent != NULL)
114 con_focus(con->parent);
119 workspace_update_urgent_flag(con_get_workspace(con));
124 * Returns true when this node is a leaf node (has no children)
127 bool con_is_leaf(Con *con) {
128 return TAILQ_EMPTY(&(con->nodes_head));
132 * Returns true if this node accepts a window (if the node swallows windows,
133 * it might already have swallowed enough and cannot hold any more).
136 bool con_accepts_window(Con *con) {
137 /* 1: workspaces never accept direct windows */
138 if (con->type == CT_WORKSPACE)
141 if (con->orientation != NO_ORIENTATION) {
142 DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
146 /* TODO: if this is a swallowing container, we need to check its max_clients */
147 return (con->window == NULL);
151 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
155 Con *con_get_output(Con *con) {
157 while (result != NULL && result->type != CT_OUTPUT)
158 result = result->parent;
159 /* We must be able to get an output because focus can never be set higher
160 * in the tree (root node cannot be focused). */
161 assert(result != NULL);
166 * Gets the workspace container this node is on.
169 Con *con_get_workspace(Con *con) {
171 while (result != NULL && result->type != CT_WORKSPACE)
172 result = result->parent;
173 assert(result != NULL);
178 * helper data structure for the breadth-first-search in
179 * con_get_fullscreen_con()
185 TAILQ_ENTRY(bfs_entry) entries;
189 * Returns the first fullscreen node below this node.
192 Con *con_get_fullscreen_con(Con *con) {
193 Con *current, *child;
195 LOG("looking for fullscreen node\n");
196 /* TODO: is breadth-first-search really appropriate? (check as soon as
197 * fullscreen levels and fullscreen for containers is implemented) */
198 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
199 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
201 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
203 while (!TAILQ_EMPTY(&bfs_head)) {
204 entry = TAILQ_FIRST(&bfs_head);
205 current = entry->con;
206 LOG("checking %p\n", current);
207 if (current != con && current->fullscreen_mode != CF_NONE) {
208 /* empty the queue */
209 while (!TAILQ_EMPTY(&bfs_head)) {
210 entry = TAILQ_FIRST(&bfs_head);
211 TAILQ_REMOVE(&bfs_head, entry, entries);
217 LOG("deleting from queue\n");
218 TAILQ_REMOVE(&bfs_head, entry, entries);
221 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
222 entry = smalloc(sizeof(struct bfs_entry));
224 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
232 * Returns true if the node is floating.
235 bool con_is_floating(Con *con) {
237 LOG("checking if con %p is floating\n", con);
238 return (con->floating >= FLOATING_AUTO_ON);
242 * Returns the container with the given client window ID or NULL if no such
246 Con *con_by_window_id(xcb_window_t window) {
248 TAILQ_FOREACH(con, &all_cons, all_cons)
249 if (con->window != NULL && con->window->id == window)
255 * Returns the container with the given frame ID or NULL if no such container
259 Con *con_by_frame_id(xcb_window_t frame) {
261 TAILQ_FOREACH(con, &all_cons, all_cons)
262 if (con->frame == frame)
268 * Returns the first container which wants to swallow this window
272 Con *con_for_window(i3Window *window, Match **store_match) {
275 LOG("searching con for window %p\n", window);
276 LOG("class == %s\n", window->class_class);
278 TAILQ_FOREACH(con, &all_cons, all_cons)
279 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
280 if (!match_matches_window(match, window))
282 if (store_match != NULL)
283 *store_match = match;
291 * Returns the number of children of this container.
294 int con_num_children(Con *con) {
298 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
305 * Updates the percent attribute of the children of the given container. This
306 * function needs to be called when a window is added or removed from a
310 void con_fix_percent(Con *con, int action) {
312 int children = con_num_children(con);
313 /* TODO: better document why this math works */
315 if (action == WINDOW_ADD)
316 fix = (1.0 - (1.0 / (children+1)));
318 fix = 1.0 / (1.0 - (1.0 / (children+1)));
320 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
321 if (child->percent <= 0.0)
323 child->percent *= fix;
328 * Toggles fullscreen mode for the given container. Fullscreen mode will not be
329 * entered when there already is a fullscreen container on this workspace.
332 void con_toggle_fullscreen(Con *con) {
333 Con *workspace, *fullscreen;
334 LOG("toggling fullscreen for %p / %s\n", con, con->name);
335 if (con->fullscreen_mode == CF_NONE) {
336 /* 1: check if there already is a fullscreen con */
337 workspace = con_get_workspace(con);
338 if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
339 LOG("Not entering fullscreen mode, container (%p/%s) "
340 "already is in fullscreen mode\n",
341 fullscreen, fullscreen->name);
345 /* 2: enable fullscreen */
346 con->fullscreen_mode = CF_OUTPUT;
348 /* 1: disable fullscreen */
349 con->fullscreen_mode = CF_NONE;
351 LOG("mode now: %d\n", con->fullscreen_mode);
353 /* update _NET_WM_STATE if this container has a window */
354 /* TODO: when a window is assigned to a container which is already
355 * fullscreened, this state needs to be pushed to the client, too */
356 if (con->window == NULL)
360 unsigned int num = 0;
362 if (con->fullscreen_mode != CF_NONE)
363 values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
365 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
366 atoms[_NET_WM_STATE], ATOM, 32, num, values);
370 * Moves the given container to the currently focused container on the given
372 * TODO: is there a better place for this function?
375 void con_move_to_workspace(Con *con, Con *workspace) {
376 /* 1: save the container which is going to be focused after the current
377 * container is moved away */
378 Con *focus_next = con_next_focused(con);
380 /* 2: get the focused container of this workspace by going down as far as
382 Con *next = workspace;
384 while (!TAILQ_EMPTY(&(next->focus_head)))
385 next = TAILQ_FIRST(&(next->focus_head));
387 /* 3: we go up one level, but only when next is a normal container */
388 if (next->type != CT_WORKSPACE)
391 DLOG("Re-attaching container to %p / %s\n", next, next->name);
392 /* 4: re-attach the con to the parent of this focused container */
394 con_attach(con, next);
396 /* 5: keep focus on the current workspace */
397 con_focus(focus_next);
401 * Returns the orientation of the given container (for stacked containers,
402 * vertical orientation is used regardless of the actual orientation of the
406 int con_orientation(Con *con) {
407 /* stacking containers behave like they are in vertical orientation */
408 if (con->layout == L_STACKED)
411 if (con->layout == L_TABBED)
414 return con->orientation;
418 * Returns the container which will be focused next when the given container
419 * is not available anymore. Called in tree_close and con_move_to_workspace
420 * to properly restore focus.
423 Con *con_next_focused(Con *con) {
425 /* floating containers are attached to a workspace, so we focus either the
426 * next floating container (if any) or the workspace itself. */
427 if (con->type == CT_FLOATING_CON) {
428 DLOG("selecting next for CT_FLOATING_CON\n");
429 next = TAILQ_NEXT(con, floating_windows);
430 if (next == TAILQ_END(&(parent->floating_head))) {
431 Con *ws = con_get_workspace(con);
433 DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
434 while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
435 next = TAILQ_FIRST(&(next->focus_head));
437 DLOG("skipping container itself, we want the next client\n");
438 next = TAILQ_NEXT(next, focused);
441 if (next == TAILQ_END(&(ws->focus_head))) {
442 DLOG("Focus list empty, returning NULL\n");
449 /* try to focus the next container on the same level as this one */
450 next = TAILQ_NEXT(con, focused);
452 /* if that was not possible, go up to its parent */
453 if (next == TAILQ_END(&(parent->nodes_head)))
456 /* now go down the focus stack as far as
457 * possible, excluding the current container */
458 while (!TAILQ_EMPTY(&(next->focus_head)) &&
459 TAILQ_FIRST(&(next->focus_head)) != con)
460 next = TAILQ_FIRST(&(next->focus_head));
466 * Returns a "relative" Rect which contains the amount of pixels that need to
467 * be added to the original Rect to get the final position (obviously the
468 * amount of pixels for normal, 1pixel and borderless are different).
471 Rect con_border_style_rect(Con *con) {
472 switch (con_border_style(con)) {
474 return (Rect){2, 0, -(2 * 2), -2};
477 return (Rect){1, 1, -2, -2};
480 return (Rect){0, 0, 0, 0};
488 * Use this function to get a container’s border style. This is important
489 * because when inside a stack, the border style is always BS_NORMAL.
490 * For tabbed mode, the same applies, with one exception: when the container is
491 * borderless and the only element in the tabbed container, the border is not
495 int con_border_style(Con *con) {
496 if (con->parent->layout == L_STACKED)
499 return con->border_style;