]> 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     if (content == NULL) {
48         DLOG("Skipping this output because it is currently being destroyed.\n");
49         return;
50     }
51
52     /* We need to find out if there is a fullscreen con on the current workspace
53      * and take the short-cut to render it directly (the user does not want to
54      * see the dockareas in that case) */
55     Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
56     if (!ws) {
57         DLOG("Skipping this output because it is currently being destroyed.\n");
58         return;
59     }
60     Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
61     if (fullscreen) {
62         fullscreen->rect = con->rect;
63         x_raise_con(fullscreen);
64         render_con(fullscreen, true);
65         return;
66     }
67
68     /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
69      * children) and figure out how many pixels we have left for the rest */
70     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
71         if (child->type != CT_DOCKAREA)
72             continue;
73
74         child->rect.height = 0;
75         TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
76             child->rect.height += dockchild->geometry.height;
77
78         height -= child->rect.height;
79     }
80
81     /* Second pass: Set the widths/heights */
82     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
83         if (child->type == CT_CON) {
84             child->rect.x = x;
85             child->rect.y = y;
86             child->rect.width = con->rect.width;
87             child->rect.height = height;
88         }
89
90         child->rect.x = x;
91         child->rect.y = y;
92         child->rect.width = con->rect.width;
93
94         child->deco_rect.x = 0;
95         child->deco_rect.y = 0;
96         child->deco_rect.width = 0;
97         child->deco_rect.height = 0;
98
99         y += child->rect.height;
100
101         DLOG("child at (%d, %d) with (%d x %d)\n",
102                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
103         x_raise_con(child);
104         render_con(child, false);
105     }
106 }
107
108 /*
109  * "Renders" the given container (and its children), meaning that all rects are
110  * updated correctly. Note that this function does not call any xcb_*
111  * functions, so the changes are completely done in memory only (and
112  * side-effect free). As soon as you call x_push_changes(), the changes will be
113  * updated in X11.
114  *
115  */
116 void render_con(Con *con, bool render_fullscreen) {
117     int children = con_num_children(con);
118     DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
119          (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
120          children);
121
122     /* Copy container rect, subtract container border */
123     /* This is the actually usable space inside this container for clients */
124     Rect rect = con->rect;
125
126     /* Display a border if this is a leaf node. For container nodes, we don’t
127      * draw borders (except when in debug mode) */
128     if (show_debug_borders) {
129         rect.x += 2;
130         rect.y += 2;
131         rect.width -= 2 * 2;
132         rect.height -= 2 * 2;
133     }
134
135     int x = rect.x;
136     int y = rect.y;
137
138     int i = 0;
139
140     con->mapped = true;
141
142     /* if this container contains a window, set the coordinates */
143     if (con->window) {
144         /* depending on the border style, the rect of the child window
145          * needs to be smaller */
146         Rect *inset = &(con->window_rect);
147         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
148         if (!render_fullscreen)
149             *inset = rect_add(*inset, con_border_style_rect(con));
150
151         /* Obey x11 border */
152         inset->width -= (2 * con->border_width);
153         inset->height -= (2 * con->border_width);
154
155         /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
156          *
157          * The spec isn’t explicit on whether the aspect ratio hints should be
158          * respected during fullscreen mode. Other WMs such as Openbox don’t do
159          * that, and this post suggests that this is the correct way to do it:
160          * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
161          *
162          * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
163          * subtitle rendering, see http://bugs.i3wm.org/594 */
164         if (!render_fullscreen &&
165             con->proportional_height != 0 &&
166             con->proportional_width != 0) {
167             double new_height = inset->height + 1;
168             int new_width = inset->width;
169
170             while (new_height > inset->height) {
171                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
172
173                 if (new_height > inset->height)
174                     new_width--;
175             }
176             /* Center the window */
177             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
178             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
179
180             inset->height = new_height;
181             inset->width = new_width;
182         }
183
184         /* NB: We used to respect resize increment size hints for tiling
185          * windows up until commit 0db93d9 here. However, since all terminal
186          * emulators cope with ignoring the size hints in a better way than we
187          * can (by providing their fake-transparency or background color), this
188          * code was removed. See also http://bugs.i3wm.org/540 */
189
190         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
191     }
192
193     /* Check for fullscreen nodes */
194     Con *fullscreen = NULL;
195     if (con->type != CT_OUTPUT) {
196         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
197     }
198     if (fullscreen) {
199         fullscreen->rect = rect;
200         x_raise_con(fullscreen);
201         render_con(fullscreen, true);
202         return;
203     }
204
205     /* find the height for the decorations */
206     int deco_height = config.font.height + 4;
207     if (config.font.height & 0x01)
208         ++deco_height;
209
210     /* precalculate the sizes to be able to correct rounding errors */
211     int sizes[children];
212     memset(sizes, 0, children*sizeof(int));
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_is_internal(con))
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 (con_is_internal(output))
252                 continue;
253             /* Get the active workspace of that output */
254             Con *content = output_get_content(output);
255             if (!content || TAILQ_EMPTY(&(content->focus_head))) {
256                 DLOG("Skipping this output because it is currently being destroyed.\n");
257                 continue;
258             }
259             Con *workspace = TAILQ_FIRST(&(content->focus_head));
260             Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
261             Con *child;
262             TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
263                 /* Don’t render floating windows when there is a fullscreen window
264                  * on that workspace. Necessary to make floating fullscreen work
265                  * correctly (ticket #564). */
266                 if (fullscreen != NULL) {
267                     Con *floating_child = con_descend_focused(child);
268                     /* Exception to the above rule: smart
269                      * popup_during_fullscreen handling (popups belonging to
270                      * the fullscreen app will be rendered). */
271                     if (floating_child->window == NULL ||
272                         fullscreen->window == NULL ||
273                         floating_child->window->transient_for != fullscreen->window->id)
274                         continue;
275                     else {
276                         DLOG("Rendering floating child even though in fullscreen mode: "
277                              "floating->transient_for (0x%08x) == fullscreen->id (0x%08x)\n",
278                              floating_child->window->transient_for, fullscreen->window->id);
279                     }
280                 }
281                 DLOG("floating child at (%d,%d) with %d x %d\n",
282                      child->rect.x, child->rect.y, child->rect.width, child->rect.height);
283                 x_raise_con(child);
284                 render_con(child, false);
285             }
286         }
287
288     } else {
289
290         /* FIXME: refactor this into separate functions: */
291     Con *child;
292     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
293         assert(children > 0);
294
295         /* default layout */
296         if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
297             if (con->layout == L_SPLITH) {
298                 child->rect.x = x;
299                 child->rect.y = y;
300                 child->rect.width = sizes[i];
301                 child->rect.height = rect.height;
302                 x += child->rect.width;
303             } else {
304                 child->rect.x = x;
305                 child->rect.y = y;
306                 child->rect.width = rect.width;
307                 child->rect.height = sizes[i];
308                 y += child->rect.height;
309             }
310
311             /* first we have the decoration, if this is a leaf node */
312             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
313                 /* TODO: make a function for relative coords? */
314                 child->deco_rect.x = child->rect.x - con->rect.x;
315                 child->deco_rect.y = child->rect.y - con->rect.y;
316
317                 child->rect.y += deco_height;
318                 child->rect.height -= deco_height;
319
320                 child->deco_rect.width = child->rect.width;
321                 child->deco_rect.height = deco_height;
322             }
323         }
324
325         /* stacked layout */
326         else if (con->layout == L_STACKED) {
327             child->rect.x = x;
328             child->rect.y = y;
329             child->rect.width = rect.width;
330             child->rect.height = rect.height;
331
332             child->deco_rect.x = x - con->rect.x;
333             child->deco_rect.y = y - con->rect.y + (i * deco_height);
334             child->deco_rect.width = child->rect.width;
335             child->deco_rect.height = deco_height;
336
337             if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
338                 child->rect.y += (deco_height * children);
339                 child->rect.height -= (deco_height * children);
340             }
341         }
342
343         /* tabbed layout */
344         else if (con->layout == L_TABBED) {
345             child->rect.x = x;
346             child->rect.y = y;
347             child->rect.width = rect.width;
348             child->rect.height = rect.height;
349
350             child->deco_rect.width = ceil((float)child->rect.width / children);
351             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
352             child->deco_rect.y = y - con->rect.y;
353
354             if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
355                 child->rect.y += deco_height;
356                 child->rect.height -= deco_height;
357                 child->deco_rect.height = deco_height;
358             } else {
359                 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
360             }
361         }
362
363         /* dockarea layout */
364         else if (con->layout == L_DOCKAREA) {
365             child->rect.x = x;
366             child->rect.y = y;
367             child->rect.width = rect.width;
368             child->rect.height = child->geometry.height;
369
370             child->deco_rect.x = 0;
371             child->deco_rect.y = 0;
372             child->deco_rect.width = 0;
373             child->deco_rect.height = 0;
374             y += child->rect.height;
375         }
376
377         DLOG("child at (%d, %d) with (%d x %d)\n",
378                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
379         x_raise_con(child);
380         render_con(child, false);
381         i++;
382     }
383
384     /* in a stacking or tabbed container, we ensure the focused client is raised */
385     if (con->layout == L_STACKED || con->layout == L_TABBED) {
386         TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
387             x_raise_con(child);
388         if ((child = TAILQ_FIRST(&(con->focus_head)))) {
389             /* By rendering the stacked container again, we handle the case
390              * that we have a non-leaf-container inside the stack. In that
391              * case, the children of the non-leaf-container need to be raised
392              * aswell. */
393             render_con(child, false);
394         }
395
396         if (children != 1)
397             /* Raise the stack con itself. This will put the stack decoration on
398              * top of every stack window. That way, when a new window is opened in
399              * the stack, the old window will not obscure part of the decoration
400              * (it’s unmapped afterwards). */
401             x_raise_con(con);
402     }
403     }
404 }