]> git.sur5r.net Git - i3/i3/blob - src/con.c
8121bf0e8eccbab2b296417c47d28c75f208a24b
[i3/i3] / src / con.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * con.c contains all functions which deal with containers directly (creating
8  * containers, searching containers, getting specific properties from
9  * containers, …).
10  *
11  */
12 #include "all.h"
13
14 char *colors[] = {
15     "#ff0000",
16     "#00FF00",
17     "#0000FF",
18     "#ff00ff",
19     "#00ffff",
20     "#ffff00",
21     "#aa0000",
22     "#00aa00",
23     "#0000aa",
24     "#aa00aa"
25 };
26
27 /*
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().
31  *
32  */
33 Con *con_new(Con *parent) {
34     Con *new = scalloc(sizeof(Con));
35     TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
36     new->type = CT_CON;
37     new->name = strdup("");
38     static int cnt = 0;
39     LOG("opening window %d\n", cnt);
40
41     /* TODO: remove window coloring after test-phase */
42     LOG("color %s\n", colors[cnt]);
43     new->name = strdup(colors[cnt]);
44 #if 0
45     uint32_t cp = get_colorpixel(colors[cnt]);
46     cnt++;
47     if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
48         cnt = 0;
49 #endif
50
51     x_con_init(new);
52
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);
55
56     TAILQ_INIT(&(new->floating_head));
57     TAILQ_INIT(&(new->nodes_head));
58     TAILQ_INIT(&(new->focus_head));
59     TAILQ_INIT(&(new->swallow_head));
60
61     if (parent != NULL)
62         con_attach(new, parent);
63
64     return new;
65 }
66
67 /*
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
70  * tree.
71  *
72  */
73 void con_attach(Con *con, Con *parent) {
74     con->parent = parent;
75     Con *current = TAILQ_FIRST(&(parent->focus_head));
76
77     if (current == TAILQ_END(&(parent->focus_head)))
78         TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
79     else {
80         DLOG("inserting after\n");
81         TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
82     }
83     /* We insert to the TAIL because con_focus() will correct this.
84      * This way, we have the option to insert Cons without having
85      * to focus them. */
86     TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
87 }
88
89 /*
90  * Detaches the given container from its current parent
91  *
92  */
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);
97     } else {
98         TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
99         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
100     }
101 }
102
103 /*
104  * Sets input focus to the given container. Will be updated in X11 in the next
105  * run of x_push_changes().
106  *
107  */
108 void con_focus(Con *con) {
109     assert(con != NULL);
110
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);
117
118     focused = con;
119     if (con->urgent) {
120         con->urgent = false;
121         workspace_update_urgent_flag(con_get_workspace(con));
122     }
123 }
124
125 /*
126  * Returns true when this node is a leaf node (has no children)
127  *
128  */
129 bool con_is_leaf(Con *con) {
130     return TAILQ_EMPTY(&(con->nodes_head));
131 }
132
133 /*
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).
136  *
137  */
138 bool con_accepts_window(Con *con) {
139     /* 1: workspaces never accept direct windows */
140     if (con->type == CT_WORKSPACE)
141         return false;
142
143     if (con->orientation != NO_ORIENTATION) {
144         DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
145         return false;
146     }
147
148     /* TODO: if this is a swallowing container, we need to check its max_clients */
149     return (con->window == NULL);
150 }
151
152 /*
153  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
154  * node is on.
155  *
156  */
157 Con *con_get_output(Con *con) {
158     Con *result = 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);
164     return result;
165 }
166
167 /*
168  * Gets the workspace container this node is on.
169  *
170  */
171 Con *con_get_workspace(Con *con) {
172     Con *result = con;
173     while (result != NULL && result->type != CT_WORKSPACE)
174         result = result->parent;
175     assert(result != NULL);
176     return result;
177 }
178
179 /*
180  * helper data structure for the breadth-first-search in
181  * con_get_fullscreen_con()
182  *
183  */
184 struct bfs_entry {
185     Con *con;
186
187     TAILQ_ENTRY(bfs_entry) entries;
188 };
189
190 /*
191  * Returns the first fullscreen node below this node.
192  *
193  */
194 Con *con_get_fullscreen_con(Con *con) {
195     Con *current, *child;
196
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));
202     entry->con = con;
203     TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
204
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);
214                 free(entry);
215             }
216             return current;
217         }
218
219         LOG("deleting from queue\n");
220         TAILQ_REMOVE(&bfs_head, entry, entries);
221         free(entry);
222
223         TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
224             entry = smalloc(sizeof(struct bfs_entry));
225             entry->con = child;
226             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
227         }
228     }
229
230     return NULL;
231 }
232
233 /*
234  * Returns true if the node is floating.
235  *
236  */
237 bool con_is_floating(Con *con) {
238     assert(con != NULL);
239     LOG("checking if con %p is floating\n", con);
240     return (con->floating >= FLOATING_AUTO_ON);
241 }
242
243 /*
244  * Returns the container with the given client window ID or NULL if no such
245  * container exists.
246  *
247  */
248 Con *con_by_window_id(xcb_window_t window) {
249     Con *con;
250     TAILQ_FOREACH(con, &all_cons, all_cons)
251         if (con->window != NULL && con->window->id == window)
252             return con;
253     return NULL;
254 }
255
256 /*
257  * Returns the container with the given frame ID or NULL if no such container
258  * exists.
259  *
260  */
261 Con *con_by_frame_id(xcb_window_t frame) {
262     Con *con;
263     TAILQ_FOREACH(con, &all_cons, all_cons)
264         if (con->frame == frame)
265             return con;
266     return NULL;
267 }
268
269 /*
270  * Returns the first container which wants to swallow this window
271  * TODO: priority
272  *
273  */
274 Con *con_for_window(i3Window *window, Match **store_match) {
275     Con *con;
276     Match *match;
277     LOG("searching con for window %p\n", window);
278     LOG("class == %s\n", window->class_class);
279
280     TAILQ_FOREACH(con, &all_cons, all_cons)
281         TAILQ_FOREACH(match, &(con->swallow_head), matches) {
282             if (!match_matches_window(match, window))
283                 continue;
284             if (store_match != NULL)
285                 *store_match = match;
286             return con;
287         }
288
289     return NULL;
290 }
291
292 /*
293  * Returns the number of children of this container.
294  *
295  */
296 int con_num_children(Con *con) {
297     Con *child;
298     int children = 0;
299
300     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
301         children++;
302
303     return children;
304 }
305
306 /*
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
309  * container.
310  *
311  */
312 void con_fix_percent(Con *con, int action) {
313     Con *child;
314     int children = con_num_children(con);
315     /* TODO: better document why this math works */
316     double fix;
317     if (action == WINDOW_ADD)
318         fix = (1.0 - (1.0 / (children+1)));
319     else
320         fix = 1.0 / (1.0 - (1.0 / (children+1)));
321
322     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
323         if (child->percent <= 0.0)
324             continue;
325         child->percent *= fix;
326     }
327 }
328
329 /*
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.
332  *
333  */
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);
344             return;
345         }
346
347         /* 2: enable fullscreen */
348         con->fullscreen_mode = CF_OUTPUT;
349     } else {
350         /* 1: disable fullscreen */
351         con->fullscreen_mode = CF_NONE;
352     }
353     LOG("mode now: %d\n", con->fullscreen_mode);
354
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)
359         return;
360
361     uint32_t values[1];
362     unsigned int num = 0;
363
364     if (con->fullscreen_mode != CF_NONE)
365         values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
366
367     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
368                         atoms[_NET_WM_STATE], ATOM, 32, num, values);
369 }
370
371 /*
372  * Moves the given container to the currently focused container on the given
373  * workspace.
374  * TODO: is there a better place for this function?
375  *
376  */
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);
381
382     /* 2: get the focused container of this workspace by going down as far as
383      * possible */
384     Con *next = workspace;
385
386     while (!TAILQ_EMPTY(&(next->focus_head)))
387         next = TAILQ_FIRST(&(next->focus_head));
388
389     /* 3: we go up one level, but only when next is a normal container */
390     if (next->type != CT_WORKSPACE)
391         next = next->parent;
392
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 */
395     con_detach(con);
396     con_attach(con, next);
397
398     /* 5: keep focus on the current workspace */
399     con_focus(focus_next);
400 }
401
402 /*
403  * Returns the orientation of the given container (for stacked containers,
404  * vertical orientation is used regardless of the actual orientation of the
405  * container).
406  *
407  */
408 int con_orientation(Con *con) {
409     /* stacking containers behave like they are in vertical orientation */
410     if (con->layout == L_STACKED)
411         return VERT;
412
413     return con->orientation;
414 }
415
416 /*
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.
420  *
421  */
422 Con *con_next_focused(Con *con) {
423     Con *next;
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);
430         return next;
431     }
432
433     /* try to focus the next container on the same level as this one */
434     next = TAILQ_NEXT(con, focused);
435
436     /* if that was not possible, go up to its parent */
437     if (next == TAILQ_END(&(parent->nodes_head)))
438         next = con->parent;
439
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));
445
446     return next;
447 }
448
449 /*
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).
453  *
454  */
455 Rect con_border_style_rect(Con *con) {
456     if (con->border_style == BS_NORMAL)
457         return (Rect){2, 0, -(2 * 2), -2};
458
459     if (con->border_style == BS_1PIXEL)
460         return (Rect){1, 1, -2, -2};
461
462     if (con->border_style == BS_NONE)
463         return (Rect){0, 0, 0, 0};
464
465     assert(false);
466 }