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