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