]> git.sur5r.net Git - i3/i3/blob - src/con.c
Bugfix: Close containers which are empty due to a move (Thanks fernando)
[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     uint32_t cp = get_colorpixel(colors[cnt]);
45     cnt++;
46     if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
47         cnt = 0;
48
49     x_con_init(new);
50
51     xcb_change_window_attributes(conn, new->frame, XCB_CW_BACK_PIXEL, &cp);
52
53     TAILQ_INIT(&(new->floating_head));
54     TAILQ_INIT(&(new->nodes_head));
55     TAILQ_INIT(&(new->focus_head));
56     TAILQ_INIT(&(new->swallow_head));
57
58     if (parent != NULL)
59         con_attach(new, parent);
60
61     return new;
62 }
63
64 /*
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
67  * tree.
68  *
69  */
70 void con_attach(Con *con, Con *parent) {
71     con->parent = parent;
72     Con *current = TAILQ_FIRST(&(parent->focus_head));
73
74     if (current == TAILQ_END(&(parent->focus_head)))
75         TAILQ_INSERT_TAIL(&(parent->nodes_head), con, nodes);
76     else {
77         DLOG("inserting after\n");
78         TAILQ_INSERT_AFTER(&(parent->nodes_head), current, con, nodes);
79     }
80     /* We insert to the TAIL because con_focus() will correct this.
81      * This way, we have the option to insert Cons without having
82      * to focus them. */
83     TAILQ_INSERT_TAIL(&(parent->focus_head), con, focused);
84 }
85
86 /*
87  * Detaches the given container from its current parent
88  *
89  */
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);
94     } else {
95         TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
96         TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
97     }
98 }
99
100 /*
101  * Sets input focus to the given container. Will be updated in X11 in the next
102  * run of x_push_changes().
103  *
104  */
105 void con_focus(Con *con) {
106     assert(con != NULL);
107
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);
114
115     focused = con;
116     if (con->urgent) {
117         con->urgent = false;
118         workspace_update_urgent_flag(con_get_workspace(con));
119     }
120 }
121
122 /*
123  * Returns true when this node is a leaf node (has no children)
124  *
125  */
126 bool con_is_leaf(Con *con) {
127     return TAILQ_EMPTY(&(con->nodes_head));
128 }
129
130 /*
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).
133  *
134  */
135 bool con_accepts_window(Con *con) {
136     /* 1: workspaces never accept direct windows */
137     if (con->type == CT_WORKSPACE)
138         return false;
139
140     if (con->orientation != NO_ORIENTATION) {
141         DLOG("container %p does not accepts windows, orientation != NO_ORIENTATION\n", con);
142         return false;
143     }
144
145     /* TODO: if this is a swallowing container, we need to check its max_clients */
146     return (con->window == NULL);
147 }
148
149 /*
150  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
151  * node is on.
152  *
153  */
154 Con *con_get_output(Con *con) {
155     Con *result = 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);
161     return result;
162 }
163
164 /*
165  * Gets the workspace container this node is on.
166  *
167  */
168 Con *con_get_workspace(Con *con) {
169     Con *result = con;
170     while (result != NULL && result->type != CT_WORKSPACE)
171         result = result->parent;
172     assert(result != NULL);
173     return result;
174 }
175
176 /*
177  * helper data structure for the breadth-first-search in
178  * con_get_fullscreen_con()
179  *
180  */
181 struct bfs_entry {
182     Con *con;
183
184     TAILQ_ENTRY(bfs_entry) entries;
185 };
186
187 /*
188  * Returns the first fullscreen node below this node.
189  *
190  */
191 Con *con_get_fullscreen_con(Con *con) {
192     Con *current, *child;
193
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));
199     entry->con = con;
200     TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
201
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);
211                 free(entry);
212             }
213             return current;
214         }
215
216         LOG("deleting from queue\n");
217         TAILQ_REMOVE(&bfs_head, entry, entries);
218         free(entry);
219
220         TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
221             entry = smalloc(sizeof(struct bfs_entry));
222             entry->con = child;
223             TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
224         }
225     }
226
227     return NULL;
228 }
229
230 /*
231  * Returns true if the node is floating.
232  *
233  */
234 bool con_is_floating(Con *con) {
235     assert(con != NULL);
236     LOG("checking if con %p is floating\n", con);
237     return (con->floating >= FLOATING_AUTO_ON);
238 }
239
240 /*
241  * Returns the container with the given client window ID or NULL if no such
242  * container exists.
243  *
244  */
245 Con *con_by_window_id(xcb_window_t window) {
246     Con *con;
247     TAILQ_FOREACH(con, &all_cons, all_cons)
248         if (con->window != NULL && con->window->id == window)
249             return con;
250     return NULL;
251 }
252
253 /*
254  * Returns the container with the given frame ID or NULL if no such container
255  * exists.
256  *
257  */
258 Con *con_by_frame_id(xcb_window_t frame) {
259     Con *con;
260     TAILQ_FOREACH(con, &all_cons, all_cons)
261         if (con->frame == frame)
262             return con;
263     return NULL;
264 }
265
266 /*
267  * Returns the first container which wants to swallow this window
268  * TODO: priority
269  *
270  */
271 Con *con_for_window(i3Window *window, Match **store_match) {
272     Con *con;
273     Match *match;
274     LOG("searching con for window %p\n", window);
275     LOG("class == %s\n", window->class_class);
276
277     TAILQ_FOREACH(con, &all_cons, all_cons)
278         TAILQ_FOREACH(match, &(con->swallow_head), matches) {
279             if (!match_matches_window(match, window))
280                 continue;
281             if (store_match != NULL)
282                 *store_match = match;
283             return con;
284         }
285
286     return NULL;
287 }
288
289 /*
290  * Returns the number of children of this container.
291  *
292  */
293 int con_num_children(Con *con) {
294     Con *child;
295     int children = 0;
296
297     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
298         children++;
299
300     return children;
301 }
302
303 /*
304  * Updates the percent attribute of the children of the given container. This
305  * function needs to be called when a window is added or removed from a
306  * container.
307  *
308  */
309 void con_fix_percent(Con *con, int action) {
310     Con *child;
311     int children = con_num_children(con);
312     /* TODO: better document why this math works */
313     double fix;
314     if (action == WINDOW_ADD)
315         fix = (1.0 - (1.0 / (children+1)));
316     else
317         fix = 1.0 / (1.0 - (1.0 / (children+1)));
318
319     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
320         if (child->percent <= 0.0)
321             continue;
322         child->percent *= fix;
323     }
324 }
325
326 /*
327  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
328  * entered when there already is a fullscreen container on this workspace.
329  *
330  */
331 void con_toggle_fullscreen(Con *con) {
332     Con *workspace, *fullscreen;
333     LOG("toggling fullscreen for %p / %s\n", con, con->name);
334     if (con->fullscreen_mode == CF_NONE) {
335         /* 1: check if there already is a fullscreen con */
336         workspace = con_get_workspace(con);
337         if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
338             LOG("Not entering fullscreen mode, container (%p/%s) "
339                 "already is in fullscreen mode\n",
340                 fullscreen, fullscreen->name);
341             return;
342         }
343
344         /* 2: enable fullscreen */
345         con->fullscreen_mode = CF_OUTPUT;
346     } else {
347         /* 1: disable fullscreen */
348         con->fullscreen_mode = CF_NONE;
349     }
350     LOG("mode now: %d\n", con->fullscreen_mode);
351
352     /* update _NET_WM_STATE if this container has a window */
353     /* TODO: when a window is assigned to a container which is already
354      * fullscreened, this state needs to be pushed to the client, too */
355     if (con->window == NULL)
356         return;
357
358     uint32_t values[1];
359     unsigned int num = 0;
360
361     if (con->fullscreen_mode != CF_NONE)
362         values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
363
364     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
365                         atoms[_NET_WM_STATE], ATOM, 32, num, values);
366 }
367
368 /*
369  * Moves the given container to the currently focused container on the given
370  * workspace.
371  * TODO: is there a better place for this function?
372  *
373  */
374 void con_move_to_workspace(Con *con, Con *workspace) {
375     /* 1: save the container which is going to be focused after the current
376      * container is moved away */
377     Con *focus_next = con_next_focused(con);
378
379     /* 2: get the focused container of this workspace by going down as far as
380      * possible */
381     Con *next = workspace;
382
383     while (!TAILQ_EMPTY(&(next->focus_head)))
384         next = TAILQ_FIRST(&(next->focus_head));
385
386     /* 3: we go up one level, but only when next is a normal container */
387     if (next->type != CT_WORKSPACE)
388         next = next->parent;
389
390     DLOG("Re-attaching container to %p / %s\n", next, next->name);
391     /* 4: re-attach the con to the parent of this focused container */
392     con_detach(con);
393     con_attach(con, next);
394
395     /* 5: keep focus on the current workspace */
396     con_focus(focus_next);
397 }
398
399 /*
400  * Returns the orientation of the given container (for stacked containers,
401  * vertical orientation is used regardless of the actual orientation of the
402  * container).
403  *
404  */
405 int con_orientation(Con *con) {
406     /* stacking containers behave like they are in vertical orientation */
407     if (con->layout == L_STACKED)
408         return VERT;
409
410     return con->orientation;
411 }
412
413 /*
414  * Returns the container which will be focused next when the given container
415  * is not available anymore. Called in tree_close and con_move_to_workspace
416  * to properly restore focus.
417  *
418  */
419 Con *con_next_focused(Con *con) {
420     Con *next;
421     /* floating containers are attached to a workspace, so we focus either the
422      * next floating container (if any) or the workspace itself. */
423     if (con->type == CT_FLOATING_CON) {
424         next = TAILQ_NEXT(con, floating_windows);
425         if (next == TAILQ_END(&(parent->floating_head)))
426             next = con_get_workspace(con);
427         return next;
428     }
429
430     /* try to focus the next container on the same level as this one */
431     next = TAILQ_NEXT(con, focused);
432
433     /* if that was not possible, go up to its parent */
434     if (next == TAILQ_END(&(parent->nodes_head)))
435         next = con->parent;
436
437     /* now go down the focus stack as far as
438      * possible, excluding the current container */
439     while (!TAILQ_EMPTY(&(next->focus_head)) &&
440            TAILQ_FIRST(&(next->focus_head)) != con)
441         next = TAILQ_FIRST(&(next->focus_head));
442
443     return next;
444 }
445
446 /*
447  * Returns a "relative" Rect which contains the amount of pixels that need to
448  * be added to the original Rect to get the final position (obviously the
449  * amount of pixels for normal, 1pixel and borderless are different).
450  *
451  */
452 Rect con_border_style_rect(Con *con) {
453     if (con->border_style == BS_NORMAL)
454         return (Rect){2, 0, -(2 * 2), -2};
455
456     if (con->border_style == BS_1PIXEL)
457         return (Rect){1, 1, -2, -2};
458
459     if (con->border_style == BS_NONE)
460         return (Rect){0, 0, 0, 0};
461
462     assert(false);
463 }