]> git.sur5r.net Git - i3/i3/blob - src/render.c
Merge branch 'master' into next
[i3/i3] / src / render.c
1 #undef I3__FILE__
2 #define I3__FILE__ "render.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * render.c: Renders (determines position/sizes) the layout tree, updating the
10  *           various rects. Needs to be pushed to X11 (see x.c) to be visible.
11  *
12  */
13 #include "all.h"
14
15 /* change this to 'true' if you want to have additional borders around every
16  * container (for debugging purposes) */
17 static bool show_debug_borders = false;
18
19 /*
20  * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
21  * get the height of their content and the remaining CT_CON gets the rest.
22  *
23  */
24 static void render_l_output(Con *con) {
25     Con *child, *dockchild;
26
27     int x = con->rect.x;
28     int y = con->rect.y;
29     int height = con->rect.height;
30
31     /* Find the content container and ensure that there is exactly one. Also
32      * check for any non-CT_DOCKAREA clients. */
33     Con *content = NULL;
34     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
35         if (child->type == CT_CON) {
36             if (content != NULL) {
37                 DLOG("More than one CT_CON on output container\n");
38                 assert(false);
39             }
40             content = child;
41         } else if (child->type != CT_DOCKAREA) {
42             DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
43             assert(false);
44         }
45     }
46
47     assert(content != NULL);
48
49     /* We need to find out if there is a fullscreen con on the current workspace
50      * and take the short-cut to render it directly (the user does not want to
51      * see the dockareas in that case) */
52     Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
53     Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
54     if (fullscreen) {
55         fullscreen->rect = con->rect;
56         x_raise_con(fullscreen);
57         render_con(fullscreen, true);
58         return;
59     }
60
61     /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
62      * children) and figure out how many pixels we have left for the rest */
63     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
64         if (child->type != CT_DOCKAREA)
65             continue;
66
67         child->rect.height = 0;
68         TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
69             child->rect.height += dockchild->geometry.height;
70
71         height -= child->rect.height;
72     }
73
74     /* Second pass: Set the widths/heights */
75     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
76         if (child->type == CT_CON) {
77             child->rect.x = x;
78             child->rect.y = y;
79             child->rect.width = con->rect.width;
80             child->rect.height = height;
81         }
82
83         child->rect.x = x;
84         child->rect.y = y;
85         child->rect.width = con->rect.width;
86
87         child->deco_rect.x = 0;
88         child->deco_rect.y = 0;
89         child->deco_rect.width = 0;
90         child->deco_rect.height = 0;
91
92         y += child->rect.height;
93
94         DLOG("child at (%d, %d) with (%d x %d)\n",
95                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
96         x_raise_con(child);
97         render_con(child, false);
98     }
99 }
100
101 /*
102  * "Renders" the given container (and its children), meaning that all rects are
103  * updated correctly. Note that this function does not call any xcb_*
104  * functions, so the changes are completely done in memory only (and
105  * side-effect free). As soon as you call x_push_changes(), the changes will be
106  * updated in X11.
107  *
108  */
109 void render_con(Con *con, bool render_fullscreen) {
110     int children = con_num_children(con);
111     DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
112          (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
113          children);
114
115     /* Copy container rect, subtract container border */
116     /* This is the actually usable space inside this container for clients */
117     Rect rect = con->rect;
118
119     /* Display a border if this is a leaf node. For container nodes, we don’t
120      * draw borders (except when in debug mode) */
121     if (show_debug_borders) {
122         rect.x += 2;
123         rect.y += 2;
124         rect.width -= 2 * 2;
125         rect.height -= 2 * 2;
126     }
127
128     int x = rect.x;
129     int y = rect.y;
130
131     int i = 0;
132
133     con->mapped = true;
134
135     /* if this container contains a window, set the coordinates */
136     if (con->window) {
137         /* depending on the border style, the rect of the child window
138          * needs to be smaller */
139         Rect *inset = &(con->window_rect);
140         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
141         if (!render_fullscreen)
142             *inset = rect_add(*inset, con_border_style_rect(con));
143
144         /* Obey x11 border */
145         inset->width -= (2 * con->border_width);
146         inset->height -= (2 * con->border_width);
147
148         /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
149          *
150          * The spec isn’t explicit on whether the aspect ratio hints should be
151          * respected during fullscreen mode. Other WMs such as Openbox don’t do
152          * that, and this post suggests that this is the correct way to do it:
153          * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
154          *
155          * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
156          * subtitle rendering, see http://bugs.i3wm.org/594 */
157         if (!render_fullscreen &&
158             con->proportional_height != 0 &&
159             con->proportional_width != 0) {
160             double new_height = inset->height + 1;
161             int new_width = inset->width;
162
163             while (new_height > inset->height) {
164                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
165
166                 if (new_height > inset->height)
167                     new_width--;
168             }
169             /* Center the window */
170             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
171             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
172
173             inset->height = new_height;
174             inset->width = new_width;
175         }
176
177         if (con->height_increment > 1) {
178             int old_height = inset->height;
179             inset->height -= (inset->height - con->base_height) % con->height_increment;
180             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
181                 old_height - inset->height, con->height_increment, con->base_height);
182         }
183
184         if (con->width_increment > 1) {
185             int old_width = inset->width;
186             inset->width -= (inset->width - con->base_width) % con->width_increment;
187             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
188                 old_width - inset->width, con->width_increment, con->base_width);
189         }
190
191         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
192     }
193
194     /* Check for fullscreen nodes */
195     Con *fullscreen = NULL;
196     if (con->type != CT_OUTPUT) {
197         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
198     }
199     if (fullscreen) {
200         fullscreen->rect = rect;
201         x_raise_con(fullscreen);
202         render_con(fullscreen, true);
203         return;
204     }
205
206     /* find the height for the decorations */
207     int deco_height = config.font.height + 4;
208     if (config.font.height & 0x01)
209         ++deco_height;
210
211     /* precalculate the sizes to be able to correct rounding errors */
212     int sizes[children];
213     if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && children > 0) {
214         assert(!TAILQ_EMPTY(&con->nodes_head));
215         Con *child;
216         int i = 0, assigned = 0;
217         int total = con_orientation(con) == HORIZ ? rect.width : rect.height;
218         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
219             double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
220             assigned += sizes[i++] = percentage * total;
221         }
222         assert(assigned == total ||
223                 (assigned > total && assigned - total <= children * 2) ||
224                 (assigned < total && total - assigned <= children * 2));
225         int signal = assigned < total ? 1 : -1;
226         while (assigned != total) {
227             for (i = 0; i < children && assigned != total; ++i) {
228                 sizes[i] += signal;
229                 assigned += signal;
230             }
231         }
232     }
233
234     if (con->layout == L_OUTPUT) {
235         /* Skip i3-internal outputs */
236         if (con->name[0] == '_' && con->name[1] == '_')
237             return;
238         render_l_output(con);
239     } else if (con->type == CT_ROOT) {
240         Con *output;
241         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
242             render_con(output, false);
243         }
244
245         /* We need to render floating windows after rendering all outputs’
246          * tiling windows because they need to be on top of *every* output at
247          * all times. This is important when the user places floating
248          * windows/containers so that they overlap on another output. */
249         DLOG("Rendering floating windows:\n");
250         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
251             if (output->name[0] == '_' && output->name[1] == '_')
252                 continue;
253             /* Get the active workspace of that output */
254             Con *content = output_get_content(output);
255             Con *workspace = TAILQ_FIRST(&(content->focus_head));
256
257             /* Check for (floating!) fullscreen nodes */
258             /* XXX: This code duplication is unfortunate. Keep in mind to fix
259              * this when we clean up the whole render.c */
260             Con *fullscreen = NULL;
261             fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
262             if (fullscreen) {
263                 /* Either the fullscreen window is inside the floating
264                  * container, then we need to render and raise it now… */
265                 if (con_inside_floating(fullscreen)) {
266                     fullscreen->rect = output->rect;
267                     x_raise_con(fullscreen);
268                     render_con(fullscreen, true);
269                     continue;
270                 } else {
271                     /* …or it’s a tiling window, in which case the floating
272                      * windows should not overlap it, so we skip rendering this
273                      * output. */
274                     continue;
275                 }
276             }
277
278             Con *child;
279             TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
280                 DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
281                 x_raise_con(child);
282                 render_con(child, false);
283             }
284         }
285
286     } else {
287
288         /* FIXME: refactor this into separate functions: */
289     Con *child;
290     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
291         assert(children > 0);
292
293         /* default layout */
294         if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
295             if (con->layout == L_SPLITH) {
296                 child->rect.x = x;
297                 child->rect.y = y;
298                 child->rect.width = sizes[i];
299                 child->rect.height = rect.height;
300                 x += child->rect.width;
301             } else {
302                 child->rect.x = x;
303                 child->rect.y = y;
304                 child->rect.width = rect.width;
305                 child->rect.height = sizes[i];
306                 y += child->rect.height;
307             }
308
309             /* first we have the decoration, if this is a leaf node */
310             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
311                 /* TODO: make a function for relative coords? */
312                 child->deco_rect.x = child->rect.x - con->rect.x;
313                 child->deco_rect.y = child->rect.y - con->rect.y;
314
315                 child->rect.y += deco_height;
316                 child->rect.height -= deco_height;
317
318                 child->deco_rect.width = child->rect.width;
319                 child->deco_rect.height = deco_height;
320             }
321         }
322
323         /* stacked layout */
324         else if (con->layout == L_STACKED) {
325             child->rect.x = x;
326             child->rect.y = y;
327             child->rect.width = rect.width;
328             child->rect.height = rect.height;
329
330             child->deco_rect.x = x - con->rect.x;
331             child->deco_rect.y = y - con->rect.y + (i * deco_height);
332             child->deco_rect.width = child->rect.width;
333             child->deco_rect.height = deco_height;
334
335             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
336                 child->rect.y += (deco_height * children);
337                 child->rect.height -= (deco_height * children);
338             }
339         }
340
341         /* tabbed layout */
342         else if (con->layout == L_TABBED) {
343             child->rect.x = x;
344             child->rect.y = y;
345             child->rect.width = rect.width;
346             child->rect.height = rect.height;
347
348             child->deco_rect.width = child->rect.width / children;
349             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
350             child->deco_rect.y = y - con->rect.y;
351
352             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
353                 child->rect.y += deco_height;
354                 child->rect.height -= deco_height;
355                 child->deco_rect.height = deco_height;
356             } else {
357                 child->deco_rect.height = (child->border_style == BS_1PIXEL ? 1 : 0);
358             }
359         }
360
361         /* dockarea layout */
362         else if (con->layout == L_DOCKAREA) {
363             child->rect.x = x;
364             child->rect.y = y;
365             child->rect.width = rect.width;
366             child->rect.height = child->geometry.height;
367
368             child->deco_rect.x = 0;
369             child->deco_rect.y = 0;
370             child->deco_rect.width = 0;
371             child->deco_rect.height = 0;
372             y += child->rect.height;
373         }
374
375         DLOG("child at (%d, %d) with (%d x %d)\n",
376                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
377         x_raise_con(child);
378         render_con(child, false);
379         i++;
380     }
381
382     /* in a stacking or tabbed container, we ensure the focused client is raised */
383     if (con->layout == L_STACKED || con->layout == L_TABBED) {
384         TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
385             x_raise_con(child);
386         if ((child = TAILQ_FIRST(&(con->focus_head)))) {
387             /* By rendering the stacked container again, we handle the case
388              * that we have a non-leaf-container inside the stack. In that
389              * case, the children of the non-leaf-container need to be raised
390              * aswell. */
391             render_con(child, false);
392         }
393
394         if (children != 1)
395             /* Raise the stack con itself. This will put the stack decoration on
396              * top of every stack window. That way, when a new window is opened in
397              * the stack, the old window will not obscure part of the decoration
398              * (it’s unmapped afterwards). */
399             x_raise_con(con);
400     }
401     }
402 }