]> git.sur5r.net Git - i3/i3/blob - src/render.c
Merge i3bar into next
[i3/i3] / src / render.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 /* change this to 'true' if you want to have additional borders around every
8  * container (for debugging purposes) */
9 static bool show_debug_borders = false;
10
11 /*
12  * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
13  * get the height of their content and the remaining CT_CON gets the rest.
14  *
15  */
16 static void render_l_output(Con *con) {
17     Con *child, *dockchild;
18
19     int x = con->rect.x;
20     int y = con->rect.y;
21     int height = con->rect.height;
22     DLOG("Available height: %d\n", height);
23
24     /* Find the content container and ensure that there is exactly one. Also
25      * check for any non-CT_DOCKAREA clients. */
26     Con *content = NULL;
27     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
28         if (child->type == CT_CON) {
29             if (content != NULL) {
30                 DLOG("More than one CT_CON on output container\n");
31                 assert(false);
32             }
33             content = child;
34         } else if (child->type != CT_DOCKAREA) {
35             DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
36             assert(false);
37         }
38     }
39
40     assert(content != NULL);
41
42     /* We need to find out if there is a fullscreen con on the current workspace
43      * and take the short-cut to render it directly (the user does not want to
44      * see the dockareas in that case) */
45     Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
46     Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
47     if (fullscreen) {
48         DLOG("got fs node: %p\n", fullscreen);
49         fullscreen->rect = con->rect;
50         x_raise_con(fullscreen);
51         render_con(fullscreen, true);
52         return;
53     }
54
55     /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
56      * children) and figure out how many pixels we have left for the rest */
57     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
58         if (child->type != CT_DOCKAREA)
59             continue;
60
61         child->rect.height = 0;
62         TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
63             child->rect.height += dockchild->geometry.height;
64         DLOG("This dockarea's height: %d\n", child->rect.height);
65
66         height -= child->rect.height;
67     }
68
69     DLOG("Remaining: %d\n", height);
70
71     /* Second pass: Set the widths/heights */
72     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
73         if (child->type == CT_CON) {
74             child->rect.x = x;
75             child->rect.y = y;
76             child->rect.width = con->rect.width;
77             child->rect.height = height;
78         }
79
80         child->rect.x = x;
81         child->rect.y = y;
82         child->rect.width = con->rect.width;
83
84         child->deco_rect.x = 0;
85         child->deco_rect.y = 0;
86         child->deco_rect.width = 0;
87         child->deco_rect.height = 0;
88
89         y += child->rect.height;
90
91         DLOG("child at (%d, %d) with (%d x %d)\n",
92                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
93         DLOG("x now %d, y now %d\n", x, y);
94         x_raise_con(child);
95         render_con(child, false);
96     }
97 }
98
99 /*
100  * "Renders" the given container (and its children), meaning that all rects are
101  * updated correctly. Note that this function does not call any xcb_*
102  * functions, so the changes are completely done in memory only (and
103  * side-effect free). As soon as you call x_push_changes(), the changes will be
104  * updated in X11.
105  *
106  */
107 void render_con(Con *con, bool render_fullscreen) {
108     DLOG("currently rendering node %p / %s / layout %d\n",
109             con, con->name, con->layout);
110     int children = con_num_children(con);
111     DLOG("children: %d, orientation = %d\n", children, con->orientation);
112
113     /* Copy container rect, subtract container border */
114     /* This is the actually usable space inside this container for clients */
115     Rect rect = con->rect;
116
117     /* Display a border if this is a leaf node. For container nodes, we don’t
118      * draw borders (except when in debug mode) */
119     if (show_debug_borders) {
120         rect.x += 2;
121         rect.y += 2;
122         rect.width -= 2 * 2;
123         rect.height -= 2 * 2;
124     }
125
126     int x = rect.x;
127     int y = rect.y;
128
129     int i = 0;
130
131     con->mapped = true;
132
133     /* if this container contains a window, set the coordinates */
134     if (con->window) {
135         /* depending on the border style, the rect of the child window
136          * needs to be smaller */
137         Rect *inset = &(con->window_rect);
138         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
139         if (!render_fullscreen)
140             *inset = rect_add(*inset, con_border_style_rect(con));
141
142         DLOG("Starting with inset = (%d, %d) %d x %d\n", inset->x, inset->y, inset->width, inset->height);
143         /* Obey x11 border */
144         DLOG("X11 border: %d\n", con->border_width);
145         inset->width -= (2 * con->border_width);
146         inset->height -= (2 * con->border_width);
147
148         /* Obey the aspect ratio, if any */
149         if (con->proportional_height != 0 &&
150             con->proportional_width != 0) {
151             DLOG("proportional height = %d, width = %d\n", con->proportional_height, con->proportional_width);
152             double new_height = inset->height + 1;
153             int new_width = inset->width;
154
155             while (new_height > inset->height) {
156                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
157
158                 if (new_height > inset->height)
159                     new_width--;
160             }
161             /* Center the window */
162             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
163             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
164
165             inset->height = new_height;
166             inset->width = new_width;
167             DLOG("new_height = %f, new_width = %d\n", new_height, new_width);
168         }
169
170         if (con->height_increment > 1) {
171             int old_height = inset->height;
172             inset->height -= (inset->height - con->base_height) % con->height_increment;
173             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
174                 old_height - inset->height, con->height_increment, con->base_height);
175         }
176
177         if (con->width_increment > 1) {
178             int old_width = inset->width;
179             inset->width -= (inset->width - con->base_width) % con->width_increment;
180             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
181                 old_width - inset->width, con->width_increment, con->base_width);
182         }
183
184         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
185     }
186
187     /* Check for fullscreen nodes */
188     Con *fullscreen = NULL;
189     if (con->type != CT_OUTPUT) {
190         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
191     }
192     if (fullscreen) {
193         DLOG("got fs node: %p\n", fullscreen);
194         fullscreen->rect = rect;
195         x_raise_con(fullscreen);
196         render_con(fullscreen, true);
197         return;
198     }
199
200     /* find the height for the decorations */
201     int deco_height = config.font.height + 5;
202
203     /* precalculate the sizes to be able to correct rounding errors */
204     int sizes[children];
205     if (con->layout == L_DEFAULT && children > 0) {
206         assert(!TAILQ_EMPTY(&con->nodes_head));
207         Con *child;
208         int i = 0, assigned = 0;
209         int total = con->orientation == HORIZ ? rect.width : rect.height;
210         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
211             double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
212             assigned += sizes[i++] = percentage * total;
213         }
214         assert(assigned == total ||
215                 (assigned > total && assigned - total <= children * 2) ||
216                 (assigned < total && total - assigned <= children * 2));
217         int signal = assigned < total ? 1 : -1;
218         while (assigned != total) {
219             for (i = 0; i < children && assigned != total; ++i) {
220                 sizes[i] += signal;
221                 assigned += signal;
222             }
223         }
224     }
225
226     if (con->layout == L_OUTPUT) {
227         render_l_output(con);
228     } else if (con->type == CT_ROOT) {
229         DLOG("Root node, rendering outputs\n");
230         Con *child;
231         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
232             render_con(child, false);
233         }
234     } else {
235
236         /* FIXME: refactor this into separate functions: */
237     Con *child;
238     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
239         assert(children > 0);
240
241         /* default layout */
242         if (con->layout == L_DEFAULT) {
243             if (con->orientation == HORIZ) {
244                 child->rect.x = x;
245                 child->rect.y = y;
246                 child->rect.width = sizes[i];
247                 child->rect.height = rect.height;
248                 x += child->rect.width;
249             } else {
250                 child->rect.x = x;
251                 child->rect.y = y;
252                 child->rect.width = rect.width;
253                 child->rect.height = sizes[i];
254                 y += child->rect.height;
255             }
256
257             /* first we have the decoration, if this is a leaf node */
258             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
259                 DLOG("that child is a leaf node, subtracting deco\n");
260                 /* TODO: make a function for relative coords? */
261                 child->deco_rect.x = child->rect.x - con->rect.x;
262                 child->deco_rect.y = child->rect.y - con->rect.y;
263
264                 child->rect.y += deco_height;
265                 child->rect.height -= deco_height;
266
267                 child->deco_rect.width = child->rect.width;
268                 child->deco_rect.height = deco_height;
269             }
270         }
271
272         /* stacked layout */
273         else if (con->layout == L_STACKED) {
274             DLOG("stacked con\n");
275             child->rect.x = x;
276             child->rect.y = y;
277             child->rect.width = rect.width;
278             child->rect.height = rect.height;
279
280             child->deco_rect.x = x - con->rect.x;
281             child->deco_rect.y = y - con->rect.y + (i * deco_height);
282             child->deco_rect.width = child->rect.width;
283             child->deco_rect.height = deco_height;
284
285             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
286                 child->rect.y += (deco_height * children);
287                 child->rect.height -= (deco_height * children);
288             }
289         }
290
291         /* tabbed layout */
292         else if (con->layout == L_TABBED) {
293             DLOG("tabbed con\n");
294             child->rect.x = x;
295             child->rect.y = y;
296             child->rect.width = rect.width;
297             child->rect.height = rect.height;
298
299             child->deco_rect.width = child->rect.width / children;
300             child->deco_rect.height = deco_height;
301             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
302             child->deco_rect.y = y - con->rect.y;
303
304             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
305                 child->rect.y += deco_height;
306                 child->rect.height -= deco_height;
307             }
308         }
309
310         /* dockarea layout */
311         else if (con->layout == L_DOCKAREA) {
312             DLOG("dockarea con\n");
313             child->rect.x = x;
314             child->rect.y = y;
315             child->rect.width = rect.width;
316             child->rect.height = child->geometry.height;
317
318             child->deco_rect.x = 0;
319             child->deco_rect.y = 0;
320             child->deco_rect.width = 0;
321             child->deco_rect.height = 0;
322             y += child->rect.height;
323         }
324
325         DLOG("child at (%d, %d) with (%d x %d)\n",
326                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
327         DLOG("x now %d, y now %d\n", x, y);
328         x_raise_con(child);
329         render_con(child, false);
330         i++;
331     }
332
333     /* in a stacking or tabbed container, we ensure the focused client is raised */
334     if (con->layout == L_STACKED || con->layout == L_TABBED) {
335         DLOG("stacked/tabbed, raising focused reverse\n");
336         TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
337             x_raise_con(child);
338         DLOG("done\n");
339         if ((child = TAILQ_FIRST(&(con->focus_head)))) {
340             DLOG("con %p is stacking, raising %p\n", con, child);
341             /* By rendering the stacked container again, we handle the case
342              * that we have a non-leaf-container inside the stack. In that
343              * case, the children of the non-leaf-container need to be raised
344              * aswell. */
345             render_con(child, false);
346         }
347
348         if (children != 1)
349             /* Raise the stack con itself. This will put the stack decoration on
350              * top of every stack window. That way, when a new window is opened in
351              * the stack, the old window will not obscure part of the decoration
352              * (it’s unmapped afterwards). */
353             x_raise_con(con);
354     }
355     }
356
357     Con *child;
358     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
359         DLOG("render floating:\n");
360         DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
361         x_raise_con(child);
362         render_con(child, false);
363     }
364
365     DLOG("-- level up\n");
366 }