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