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