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