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