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