]> git.sur5r.net Git - i3/i3/blob - src/con.c
02d46db556d0ba13367738f150b4379dd89b3043
[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  * Updates the percent attribute of the children of the given container. This
291  * function needs to be called when a window is added or removed from a
292  * container.
293  *
294  */
295 void con_fix_percent(Con *con, int action) {
296     Con *child;
297     int children = 0;
298     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
299         children++;
300     /* TODO: better document why this math works */
301     double fix;
302     if (action == WINDOW_ADD)
303         fix = (1.0 - (1.0 / (children+1)));
304     else
305         fix = 1.0 / (1.0 - (1.0 / (children+1)));
306
307     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
308         if (child->percent <= 0.0)
309             continue;
310         child->percent *= fix;
311     }
312 }
313
314 /*
315  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
316  * entered when there already is a fullscreen container on this workspace.
317  *
318  */
319 void con_toggle_fullscreen(Con *con) {
320     Con *workspace, *fullscreen;
321     LOG("toggling fullscreen for %p / %s\n", con, con->name);
322     if (con->fullscreen_mode == CF_NONE) {
323         /* 1: check if there already is a fullscreen con */
324         workspace = con_get_workspace(con);
325         if ((fullscreen = con_get_fullscreen_con(workspace)) != NULL) {
326             LOG("Not entering fullscreen mode, container (%p/%s) "
327                 "already is in fullscreen mode\n",
328                 fullscreen, fullscreen->name);
329             return;
330         }
331
332         /* 2: enable fullscreen */
333         con->fullscreen_mode = CF_OUTPUT;
334     } else {
335         /* 1: disable fullscreen */
336         con->fullscreen_mode = CF_NONE;
337     }
338     LOG("mode now: %d\n", con->fullscreen_mode);
339
340     /* update _NET_WM_STATE if this container has a window */
341     /* TODO: when a window is assigned to a container which is already
342      * fullscreened, this state needs to be pushed to the client, too */
343     if (con->window == NULL)
344         return;
345
346     uint32_t values[1];
347     unsigned int num = 0;
348
349     if (con->fullscreen_mode != CF_NONE)
350         values[num++] = atoms[_NET_WM_STATE_FULLSCREEN];
351
352     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
353                         atoms[_NET_WM_STATE], ATOM, 32, num, values);
354 }
355
356 /*
357  * Moves the given container to the currently focused container on the given
358  * workspace.
359  * TODO: is there a better place for this function?
360  *
361  */
362 void con_move_to_workspace(Con *con, Con *workspace) {
363     /* 1: save the container which is going to be focused after the current
364      * container is moved away */
365     Con *focus_next = con_next_focused(con);
366
367     /* 2: get the focused container of this workspace by going down as far as
368      * possible */
369     Con *next = workspace;
370
371     while (!TAILQ_EMPTY(&(next->focus_head)))
372         next = TAILQ_FIRST(&(next->focus_head));
373
374     /* 3: we go up one level, but only when next is a normal container */
375     if (next->type != CT_WORKSPACE)
376         next = next->parent;
377
378     DLOG("Re-attaching container to %p / %s\n", next, next->name);
379     /* 4: re-attach the con to the parent of this focused container */
380     con_detach(con);
381     con_attach(con, next);
382
383     /* 5: keep focus on the current workspace */
384     con_focus(focus_next);
385 }
386
387 /*
388  * Returns the orientation of the given container (for stacked containers,
389  * vertical orientation is used regardless of the actual orientation of the
390  * container).
391  *
392  */
393 int con_orientation(Con *con) {
394     /* stacking containers behave like they are in vertical orientation */
395     if (con->layout == L_STACKED)
396         return VERT;
397
398     return con->orientation;
399 }
400
401 /*
402  * Returns the container which will be focused next when the given container
403  * is not available anymore. Called in tree_close and con_move_to_workspace
404  * to properly restore focus.
405  *
406  */
407 Con *con_next_focused(Con *con) {
408     Con *next;
409     /* floating containers are attached to a workspace, so we focus either the
410      * next floating container (if any) or the workspace itself. */
411     if (con->type == CT_FLOATING_CON) {
412         next = TAILQ_NEXT(con, floating_windows);
413         if (next == TAILQ_END(&(parent->floating_head)))
414             next = con_get_workspace(con);
415         return next;
416     }
417
418     /* try to focus the next container on the same level as this one */
419     next = TAILQ_NEXT(con, focused);
420
421     /* if that was not possible, go up to its parent */
422     if (next == TAILQ_END(&(parent->nodes_head)))
423         next = con->parent;
424
425     /* now go down the focus stack as far as
426      * possible, excluding the current container */
427     while (!TAILQ_EMPTY(&(next->focus_head)) &&
428            TAILQ_FIRST(&(next->focus_head)) != con)
429         next = TAILQ_FIRST(&(next->focus_head));
430
431     return next;
432 }
433
434 /*
435  * Returns a "relative" Rect which contains the amount of pixels that need to
436  * be added to the original Rect to get the final position (obviously the
437  * amount of pixels for normal, 1pixel and borderless are different).
438  *
439  */
440 Rect con_border_style_rect(Con *con) {
441     if (con->border_style == BS_NORMAL)
442         return (Rect){2, 0, -(2 * 2), -2};
443
444     if (con->border_style == BS_1PIXEL)
445         return (Rect){1, 1, -2, -2};
446
447     if (con->border_style == BS_NONE)
448         return (Rect){0, 0, 0, 0};
449 }