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