]> git.sur5r.net Git - i3/i3/blob - src/render.c
Refactor out some individual functions in render_con() to make the code more readable.
[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 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 /* Forward declarations */
16 static void render_con_split(Con *con, Con *child, render_params *p, int i);
17 static void render_con_stacked(Con *con, Con *child, render_params *p, int i);
18 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i);
19 static void render_con_dockarea(Con *con, Con *child, render_params *p);
20
21 /* change this to 'true' if you want to have additional borders around every
22  * container (for debugging purposes) */
23 static bool show_debug_borders = false;
24
25 /*
26  * Returns the height for the decorations
27  */
28 int render_deco_height(void) {
29     int deco_height = config.font.height + 4;
30     if (config.font.height & 0x01)
31         ++deco_height;
32     return deco_height;
33 }
34
35 /*
36  * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
37  * get the height of their content and the remaining CT_CON gets the rest.
38  *
39  */
40 static void render_l_output(Con *con) {
41     Con *child, *dockchild;
42
43     int x = con->rect.x;
44     int y = con->rect.y;
45     int height = con->rect.height;
46
47     /* Find the content container and ensure that there is exactly one. Also
48      * check for any non-CT_DOCKAREA clients. */
49     Con *content = NULL;
50     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
51         if (child->type == CT_CON) {
52             if (content != NULL) {
53                 DLOG("More than one CT_CON on output container\n");
54                 assert(false);
55             }
56             content = child;
57         } else if (child->type != CT_DOCKAREA) {
58             DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
59             assert(false);
60         }
61     }
62
63     if (content == NULL) {
64         DLOG("Skipping this output because it is currently being destroyed.\n");
65         return;
66     }
67
68     /* We need to find out if there is a fullscreen con on the current workspace
69      * and take the short-cut to render it directly (the user does not want to
70      * see the dockareas in that case) */
71     Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
72     if (!ws) {
73         DLOG("Skipping this output because it is currently being destroyed.\n");
74         return;
75     }
76     Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
77     if (fullscreen) {
78         fullscreen->rect = con->rect;
79         x_raise_con(fullscreen);
80         render_con(fullscreen, true);
81         return;
82     }
83
84     /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
85      * children) and figure out how many pixels we have left for the rest */
86     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
87         if (child->type != CT_DOCKAREA)
88             continue;
89
90         child->rect.height = 0;
91         TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
92         child->rect.height += dockchild->geometry.height;
93
94         height -= child->rect.height;
95     }
96
97     /* Second pass: Set the widths/heights */
98     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
99         if (child->type == CT_CON) {
100             child->rect.x = x;
101             child->rect.y = y;
102             child->rect.width = con->rect.width;
103             child->rect.height = height;
104         }
105
106         child->rect.x = x;
107         child->rect.y = y;
108         child->rect.width = con->rect.width;
109
110         child->deco_rect.x = 0;
111         child->deco_rect.y = 0;
112         child->deco_rect.width = 0;
113         child->deco_rect.height = 0;
114
115         y += child->rect.height;
116
117         DLOG("child at (%d, %d) with (%d x %d)\n",
118              child->rect.x, child->rect.y, child->rect.width, child->rect.height);
119         x_raise_con(child);
120         render_con(child, false);
121     }
122 }
123
124 /*
125  * "Renders" the given container (and its children), meaning that all rects are
126  * updated correctly. Note that this function does not call any xcb_*
127  * functions, so the changes are completely done in memory only (and
128  * side-effect free). As soon as you call x_push_changes(), the changes will be
129  * updated in X11.
130  *
131  */
132 void render_con(Con *con, bool render_fullscreen) {
133     render_params params = {
134         .rect = con->rect,
135         .x = con->rect.x,
136         .y = con->rect.y,
137         .children = con_num_children(con)};
138
139     DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
140          (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
141          params.children);
142
143     /* Display a border if this is a leaf node. For container nodes, we don’t
144      * draw borders (except when in debug mode) */
145     if (show_debug_borders) {
146         params.rect.x += 2;
147         params.rect.y += 2;
148         params.rect.width -= 2 * 2;
149         params.rect.height -= 2 * 2;
150     }
151
152     int i = 0;
153     con->mapped = true;
154
155     /* if this container contains a window, set the coordinates */
156     if (con->window) {
157         /* depending on the border style, the rect of the child window
158          * needs to be smaller */
159         Rect *inset = &(con->window_rect);
160         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
161         if (!render_fullscreen)
162             *inset = rect_add(*inset, con_border_style_rect(con));
163
164         /* Obey x11 border */
165         inset->width -= (2 * con->border_width);
166         inset->height -= (2 * con->border_width);
167
168         /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
169          *
170          * The spec isn’t explicit on whether the aspect ratio hints should be
171          * respected during fullscreen mode. Other WMs such as Openbox don’t do
172          * that, and this post suggests that this is the correct way to do it:
173          * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
174          *
175          * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
176          * subtitle rendering, see http://bugs.i3wm.org/594 */
177         if (!render_fullscreen &&
178             con->window->aspect_ratio > 0.0) {
179             DLOG("aspect_ratio = %f, current width/height are %d/%d\n",
180                  con->window->aspect_ratio, inset->width, inset->height);
181             double new_height = inset->height + 1;
182             int new_width = inset->width;
183
184             while (new_height > inset->height) {
185                 new_height = (1.0 / con->window->aspect_ratio) * new_width;
186
187                 if (new_height > inset->height)
188                     new_width--;
189             }
190             /* Center the window */
191             inset->y += ceil(inset->height / 2) - floor((new_height + .5) / 2);
192             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
193
194             inset->height = new_height + .5;
195             inset->width = new_width;
196         }
197
198         /* NB: We used to respect resize increment size hints for tiling
199          * windows up until commit 0db93d9 here. However, since all terminal
200          * emulators cope with ignoring the size hints in a better way than we
201          * can (by providing their fake-transparency or background color), this
202          * code was removed. See also http://bugs.i3wm.org/540 */
203
204         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
205     }
206
207     /* Check for fullscreen nodes */
208     Con *fullscreen = NULL;
209     if (con->type != CT_OUTPUT) {
210         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
211     }
212     if (fullscreen) {
213         fullscreen->rect = params.rect;
214         x_raise_con(fullscreen);
215         render_con(fullscreen, true);
216         /* Fullscreen containers are either global (underneath the CT_ROOT
217          * container) or per-output (underneath the CT_CONTENT container). For
218          * global fullscreen containers, we cannot abort rendering here yet,
219          * because the floating windows (with popup_during_fullscreen smart)
220          * have not yet been rendered (see the CT_ROOT code path below). See
221          * also http://bugs.i3wm.org/1393 */
222         if (con->type != CT_ROOT) {
223             return;
224         }
225     }
226
227     /* find the height for the decorations */
228     params.deco_height = render_deco_height();
229
230     /* precalculate the sizes to be able to correct rounding errors */
231     int sizes[params.children];
232     memset(sizes, 0, params.children * sizeof(int));
233     if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && params.children > 0) {
234         assert(!TAILQ_EMPTY(&con->nodes_head));
235
236         Con *child;
237         int i = 0, assigned = 0;
238         int total = con_orientation(con) == HORIZ ? params.rect.width : params.rect.height;
239         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
240             double percentage = child->percent > 0.0 ? child->percent : 1.0 / params.children;
241             assigned += sizes[i++] = percentage * total;
242         }
243         assert(assigned == total ||
244                (assigned > total && assigned - total <= params.children * 2) ||
245                (assigned < total && total - assigned <= params.children * 2));
246         int signal = assigned < total ? 1 : -1;
247         while (assigned != total) {
248             for (i = 0; i < params.children && assigned != total; ++i) {
249                 sizes[i] += signal;
250                 assigned += signal;
251             }
252         }
253     }
254     params.sizes = sizes;
255
256     if (con->layout == L_OUTPUT) {
257         /* Skip i3-internal outputs */
258         if (con_is_internal(con))
259             return;
260         render_l_output(con);
261     } else if (con->type == CT_ROOT) {
262         Con *output;
263         if (!fullscreen) {
264             TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
265                 render_con(output, false);
266             }
267         }
268
269         /* We need to render floating windows after rendering all outputs’
270          * tiling windows because they need to be on top of *every* output at
271          * all times. This is important when the user places floating
272          * windows/containers so that they overlap on another output. */
273         DLOG("Rendering floating windows:\n");
274         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
275             if (con_is_internal(output))
276                 continue;
277             /* Get the active workspace of that output */
278             Con *content = output_get_content(output);
279             if (!content || TAILQ_EMPTY(&(content->focus_head))) {
280                 DLOG("Skipping this output because it is currently being destroyed.\n");
281                 continue;
282             }
283             Con *workspace = TAILQ_FIRST(&(content->focus_head));
284             Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
285             Con *child;
286             TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
287                 /* Don’t render floating windows when there is a fullscreen window
288                  * on that workspace. Necessary to make floating fullscreen work
289                  * correctly (ticket #564). */
290                 /* If there is no fullscreen->window, this cannot be a
291                  * transient window, so we _know_ we need to skip it. This
292                  * happens during restarts where the container already exists,
293                  * but the window was not yet associated. */
294                 if (fullscreen != NULL && fullscreen->window == NULL)
295                     continue;
296                 if (fullscreen != NULL && fullscreen->window != NULL) {
297                     Con *floating_child = con_descend_focused(child);
298                     Con *transient_con = floating_child;
299                     bool is_transient_for = false;
300                     /* Exception to the above rule: smart
301                      * popup_during_fullscreen handling (popups belonging to
302                      * the fullscreen app will be rendered). */
303                     while (transient_con != NULL &&
304                            transient_con->window != NULL &&
305                            transient_con->window->transient_for != XCB_NONE) {
306                         DLOG("transient_con = 0x%08x, transient_con->window->transient_for = 0x%08x, fullscreen_id = 0x%08x\n",
307                              transient_con->window->id, transient_con->window->transient_for, fullscreen->window->id);
308                         if (transient_con->window->transient_for == fullscreen->window->id) {
309                             is_transient_for = true;
310                             break;
311                         }
312                         Con *next_transient = con_by_window_id(transient_con->window->transient_for);
313                         if (next_transient == NULL)
314                             break;
315                         /* Some clients (e.g. x11-ssh-askpass) actually set
316                          * WM_TRANSIENT_FOR to their own window id, so break instead of
317                          * looping endlessly. */
318                         if (transient_con == next_transient)
319                             break;
320                         transient_con = next_transient;
321                     }
322
323                     if (!is_transient_for)
324                         continue;
325                     else {
326                         DLOG("Rendering floating child even though in fullscreen mode: "
327                              "floating->transient_for (0x%08x) --> fullscreen->id (0x%08x)\n",
328                              floating_child->window->transient_for, fullscreen->window->id);
329                     }
330                 }
331                 DLOG("floating child at (%d,%d) with %d x %d\n",
332                      child->rect.x, child->rect.y, child->rect.width, child->rect.height);
333                 x_raise_con(child);
334                 render_con(child, false);
335             }
336         }
337
338     } else {
339         Con *child;
340         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
341             assert(params.children > 0);
342
343             if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
344                 render_con_split(con, child, &params, i);
345             } else if (con->layout == L_STACKED) {
346                 render_con_stacked(con, child, &params, i);
347             } else if (con->layout == L_TABBED) {
348                 render_con_tabbed(con, child, &params, i);
349             } else if (con->layout == L_DOCKAREA) {
350                 render_con_dockarea(con, child, &params);
351             }
352
353             DLOG("child at (%d, %d) with (%d x %d)\n",
354                  child->rect.x, child->rect.y, child->rect.width, child->rect.height);
355             x_raise_con(child);
356             render_con(child, false);
357             i++;
358         }
359
360         /* in a stacking or tabbed container, we ensure the focused client is raised */
361         if (con->layout == L_STACKED || con->layout == L_TABBED) {
362             TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
363             x_raise_con(child);
364             if ((child = TAILQ_FIRST(&(con->focus_head)))) {
365                 /* By rendering the stacked container again, we handle the case
366              * that we have a non-leaf-container inside the stack. In that
367              * case, the children of the non-leaf-container need to be raised
368              * aswell. */
369                 render_con(child, false);
370             }
371
372             if (params.children != 1)
373                 /* Raise the stack con itself. This will put the stack decoration on
374              * top of every stack window. That way, when a new window is opened in
375              * the stack, the old window will not obscure part of the decoration
376              * (it’s unmapped afterwards). */
377                 x_raise_con(con);
378         }
379     }
380 }
381
382 static void render_con_split(Con *con, Con *child, render_params *p, int i) {
383     assert(con->layout == L_SPLITH || con->layout == L_SPLITV);
384
385     if (con->layout == L_SPLITH) {
386         child->rect.x = p->x;
387         child->rect.y = p->y;
388         child->rect.width = p->sizes[i];
389         child->rect.height = p->rect.height;
390         p->x += child->rect.width;
391     } else {
392         child->rect.x = p->x;
393         child->rect.y = p->y;
394         child->rect.width = p->rect.width;
395         child->rect.height = p->sizes[i];
396         p->y += child->rect.height;
397     }
398
399     /* first we have the decoration, if this is a leaf node */
400     if (con_is_leaf(child)) {
401         if (child->border_style == BS_NORMAL) {
402             /* TODO: make a function for relative coords? */
403             child->deco_rect.x = child->rect.x - con->rect.x;
404             child->deco_rect.y = child->rect.y - con->rect.y;
405
406             child->rect.y += p->deco_height;
407             child->rect.height -= p->deco_height;
408
409             child->deco_rect.width = child->rect.width;
410             child->deco_rect.height = p->deco_height;
411         } else {
412             child->deco_rect.x = 0;
413             child->deco_rect.y = 0;
414             child->deco_rect.width = 0;
415             child->deco_rect.height = 0;
416         }
417     }
418 }
419
420 static void render_con_stacked(Con *con, Con *child, render_params *p, int i) {
421     assert(con->layout == L_STACKED);
422
423     child->rect.x = p->x;
424     child->rect.y = p->y;
425     child->rect.width = p->rect.width;
426     child->rect.height = p->rect.height;
427
428     child->deco_rect.x = p->x - con->rect.x;
429     child->deco_rect.y = p->y - con->rect.y + (i * p->deco_height);
430     child->deco_rect.width = child->rect.width;
431     child->deco_rect.height = p->deco_height;
432
433     if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
434         child->rect.y += (p->deco_height * p->children);
435         child->rect.height -= (p->deco_height * p->children);
436     }
437 }
438
439 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i) {
440     assert(con->layout == L_TABBED);
441
442     child->rect.x = p->x;
443     child->rect.y = p->y;
444     child->rect.width = p->rect.width;
445     child->rect.height = p->rect.height;
446
447     child->deco_rect.width = floor((float)child->rect.width / p->children);
448     child->deco_rect.x = p->x - con->rect.x + i * child->deco_rect.width;
449     child->deco_rect.y = p->y - con->rect.y;
450
451     /* Since the tab width may be something like 31,6 px per tab, we
452      * let the last tab have all the extra space (0,6 * children). */
453     if (i == (p->children - 1)) {
454         child->deco_rect.width += (child->rect.width - (child->deco_rect.x + child->deco_rect.width));
455     }
456
457     if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
458         child->rect.y += p->deco_height;
459         child->rect.height -= p->deco_height;
460         child->deco_rect.height = p->deco_height;
461     } else {
462         child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
463     }
464 }
465
466 static void render_con_dockarea(Con *con, Con *child, render_params *p) {
467     assert(con->layout == L_DOCKAREA);
468
469     child->rect.x = p->x;
470     child->rect.y = p->y;
471     child->rect.width = p->rect.width;
472     child->rect.height = child->geometry.height;
473
474     child->deco_rect.x = 0;
475     child->deco_rect.y = 0;
476     child->deco_rect.width = 0;
477     child->deco_rect.height = 0;
478     p->y += child->rect.height;
479 }