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 Con *con_new(Con *parent) {
29 Con *new = scalloc(sizeof(Con));
30 TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
32 new->name = strdup("");
34 LOG("opening window %d\n", cnt);
36 /* TODO: remove window coloring after test-phase */
37 LOG("color %s\n", colors[cnt]);
38 new->name = strdup(colors[cnt]);
39 uint32_t cp = get_colorpixel(colors[cnt]);
41 if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
46 xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &cp);
48 TAILQ_INIT(&(new->floating_head));
49 TAILQ_INIT(&(new->nodes_head));
50 TAILQ_INIT(&(new->focus_head));
51 TAILQ_INIT(&(new->swallow_head));
54 con_attach(new, parent);
59 void con_attach(Con *con, Con *parent) {
61 TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
62 /* We insert to the TAIL because con_focus() will correct this.
63 * This way, we have the option to insert Cons without having
65 TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
68 void con_detach(Con *con) {
69 if (con->type == CT_FLOATING_CON) {
72 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
73 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
78 * Returns true when this node is a leaf node (has no children)
81 bool con_is_leaf(Con *con) {
82 return TAILQ_EMPTY(&(con->nodes_head));
86 * Returns true if this node accepts a window (if the node swallows windows,
87 * it might already have swallowed enough and cannot hold any more).
90 bool con_accepts_window(Con *con) {
91 /* 1: workspaces never accept direct windows */
92 if (con->parent->type == CT_OUTPUT)
95 /* TODO: if this is a swallowing container, we need to check its max_clients */
96 return (con->window == NULL);
100 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
104 Con *con_get_output(Con *con) {
106 while (result != NULL && result->type != CT_OUTPUT)
107 result = result->parent;
108 /* We must be able to get an output because focus can never be set higher
109 * in the tree (root node cannot be focused). */
110 assert(result != NULL);
115 * Gets the workspace container this node is on.
118 Con *con_get_workspace(Con *con) {
120 while (result != NULL && result->parent->type != CT_OUTPUT)
121 result = result->parent;
122 assert(result != NULL);
127 * helper data structure for the breadth-first-search in
128 * con_get_fullscreen_con()
134 TAILQ_ENTRY(bfs_entry) entries;
138 * Returns the first fullscreen node below this node.
141 Con *con_get_fullscreen_con(Con *con) {
142 Con *current, *child;
144 LOG("looking for fullscreen node\n");
145 /* TODO: is breadth-first-search really appropriate? (check as soon as
146 * fullscreen levels and fullscreen for containers is implemented) */
147 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
148 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
150 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
152 while (!TAILQ_EMPTY(&bfs_head)) {
153 entry = TAILQ_FIRST(&bfs_head);
154 current = entry->con;
155 LOG("checking %p\n", current);
156 if (current != con && current->fullscreen_mode != CF_NONE) {
157 /* empty the queue */
158 while (!TAILQ_EMPTY(&bfs_head)) {
159 entry = TAILQ_FIRST(&bfs_head);
160 TAILQ_REMOVE(&bfs_head, entry, entries);
166 LOG("deleting from queue\n");
167 TAILQ_REMOVE(&bfs_head, entry, entries);
170 TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
171 entry = smalloc(sizeof(struct bfs_entry));
173 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
181 * Returns true if the node is floating.
184 bool con_is_floating(Con *con) {
186 LOG("checking if con %p is floating\n", con);
187 return (con->floating >= FLOATING_AUTO_ON);
190 Con *con_by_window_id(xcb_window_t window) {
192 TAILQ_FOREACH(con, &all_cons, all_cons)
193 if (con->window != NULL && con->window->id == window)
198 Con *con_by_frame_id(xcb_window_t frame) {
200 TAILQ_FOREACH(con, &all_cons, all_cons)
201 if (con->frame == frame)
206 bool match_matches_window(Match *match, i3Window *window) {
207 /* TODO: pcre, full matching, … */
208 if (match->class != NULL && strcasecmp(match->class, window->class_class) == 0) {
209 LOG("match made by window class (%s)\n", window->class_class);
213 if (match->instance != NULL && strcasecmp(match->instance, window->class_instance) == 0) {
214 LOG("match made by window instance (%s)\n", window->class_instance);
219 if (match->id != XCB_NONE && window->id == match->id) {
220 LOG("match made by window id (%d)\n", window->id);
224 LOG("window %d (%s) could not be matched\n", window->id, window->class_class);
230 * Returns the first container which wants to swallow this window
234 Con *con_for_window(i3Window *window, Match **store_match) {
237 LOG("searching con for window %p\n", window);
238 LOG("class == %s\n", window->class_class);
240 TAILQ_FOREACH(con, &all_cons, all_cons)
241 TAILQ_FOREACH(match, &(con->swallow_head), matches) {
242 if (!match_matches_window(match, window))
244 if (store_match != NULL)
245 *store_match = match;
253 * Updates the percent attribute of the children of the given container. This
254 * function needs to be called when a window is added or removed from a
258 void con_fix_percent(Con *con, int action) {
261 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
263 /* TODO: better document why this math works */
265 if (action == WINDOW_ADD)
266 fix = (1.0 - (1.0 / (children+1)));
268 fix = 1.0 / (1.0 - (1.0 / (children+1)));
270 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
271 if (child->percent <= 0.0)
273 child->percent *= fix;