]> git.sur5r.net Git - i3/i3/blob - src/render.c
c6ebb5ccb0c4f97dd39731a9f6c6d4faab7925b8
[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" the given container (and its children), meaning that all rects are
13  * updated correctly. Note that this function does not call any xcb_*
14  * functions, so the changes are completely done in memory only (and
15  * side-effect free). As soon as you call x_push_changes(), the changes will be
16  * updated in X11.
17  *
18  */
19 void render_con(Con *con, bool render_fullscreen) {
20     DLOG("currently rendering node %p / %s / layout %d\n",
21             con, con->name, con->layout);
22     int children = con_num_children(con);
23     DLOG("children: %d, orientation = %d\n", children, con->orientation);
24
25     /* Copy container rect, subtract container border */
26     /* This is the actually usable space inside this container for clients */
27     Rect rect = con->rect;
28
29     /* Display a border if this is a leaf node. For container nodes, we don’t
30      * draw borders (except when in debug mode) */
31     if (show_debug_borders) {
32         rect.x += 2;
33         rect.y += 2;
34         rect.width -= 2 * 2;
35         rect.height -= 2 * 2;
36     }
37
38     int x = rect.x;
39     int y = rect.y;
40
41     int i = 0;
42
43     con->mapped = true;
44
45     /* if this container contains a window, set the coordinates */
46     if (con->window) {
47         /* depending on the border style, the rect of the child window
48          * needs to be smaller */
49         Rect *inset = &(con->window_rect);
50         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
51         if (!render_fullscreen)
52             *inset = rect_add(*inset, con_border_style_rect(con));
53
54         /* Obey x11 border */
55         inset->width -= (2 * con->border_width);
56         inset->height -= (2 * con->border_width);
57
58         /* Obey the aspect ratio, if any */
59         if (con->proportional_height != 0 &&
60             con->proportional_width != 0) {
61             DLOG("proportional height = %d, width = %d\n", con->proportional_height, con->proportional_width);
62             double new_height = inset->height + 1;
63             int new_width = inset->width;
64
65             while (new_height > inset->height) {
66                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
67
68                 if (new_height > inset->height)
69                     new_width--;
70             }
71             /* Center the window */
72             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
73             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
74
75             inset->height = new_height;
76             inset->width = new_width;
77             DLOG("new_height = %f, new_width = %d\n", new_height, new_width);
78         }
79
80         if (con->height_increment > 1) {
81             int old_height = inset->height;
82             inset->height -= (inset->height - con->base_height) % con->height_increment;
83             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
84                 old_height - inset->height, con->height_increment, con->base_height);
85         }
86
87         if (con->width_increment > 1) {
88             int old_width = inset->width;
89             inset->width -= (inset->width - con->base_width) % con->width_increment;
90             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
91                 old_width - inset->width, con->width_increment, con->base_width);
92         }
93
94         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
95     }
96
97     /* Check for fullscreen nodes */
98     Con *fullscreen = con_get_fullscreen_con(con);
99     if (fullscreen) {
100         DLOG("got fs node: %p\n", fullscreen);
101         fullscreen->rect = rect;
102         x_raise_con(fullscreen);
103         render_con(fullscreen, true);
104         return;
105     }
106
107     /* find the height for the decorations */
108     i3Font *font = load_font(conn, config.font);
109     int deco_height = font->height + 5;
110
111     /* precalculate the sizes to be able to correct rounding errors */
112     int sizes[children];
113     if (con->layout == L_DEFAULT && children > 0) {
114         Con *child;
115         int i = 0, assigned = 0;
116         int total = con->orientation == HORIZ ? rect.width : rect.height;
117         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
118             double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
119             assigned += sizes[i++] = percentage * total;
120         }
121         assert(assigned == total ||
122                 (assigned > total && assigned - total <= children * 2) ||
123                 (assigned < total && total - assigned <= children * 2));
124         int signal = assigned < total ? 1 : -1;
125         while (assigned != total) {
126             for (i = 0; i < children && assigned != total; ++i) {
127                 sizes[i] += signal;
128                 assigned += signal;
129             }
130         }
131     }
132
133     Con *child;
134     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
135
136         /* default layout */
137         if (con->layout == L_DEFAULT) {
138             if (con->orientation == HORIZ) {
139                 child->rect.x = x;
140                 child->rect.y = y;
141                 child->rect.width = sizes[i];
142                 child->rect.height = rect.height;
143                 x += child->rect.width;
144             } else {
145                 child->rect.x = x;
146                 child->rect.y = y;
147                 child->rect.width = rect.width;
148                 child->rect.height = sizes[i];
149                 y += child->rect.height;
150             }
151
152             /* first we have the decoration, if this is a leaf node */
153             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
154                 DLOG("that child is a leaf node, subtracting deco\n");
155                 /* TODO: make a function for relative coords? */
156                 child->deco_rect.x = child->rect.x - con->rect.x;
157                 child->deco_rect.y = child->rect.y - con->rect.y;
158
159                 child->rect.y += deco_height;
160                 child->rect.height -= deco_height;
161
162                 child->deco_rect.width = child->rect.width;
163                 child->deco_rect.height = deco_height;
164             }
165         }
166
167         /* stacked layout */
168         else if (con->layout == L_STACKED) {
169             DLOG("stacked con\n");
170             child->rect.x = x;
171             child->rect.y = y;
172             child->rect.width = rect.width;
173             child->rect.height = rect.height;
174
175             child->deco_rect.x = x - con->rect.x;
176             child->deco_rect.y = y - con->rect.y + (i * deco_height);
177             child->deco_rect.width = child->rect.width;
178             child->deco_rect.height = deco_height;
179
180             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
181                 child->rect.y += (deco_height * children);
182                 child->rect.height -= (deco_height * children);
183             }
184         }
185
186         /* tabbed layout */
187         else if (con->layout == L_TABBED) {
188             DLOG("tabbed con\n");
189             child->rect.x = x;
190             child->rect.y = y;
191             child->rect.width = rect.width;
192             child->rect.height = rect.height;
193
194             child->deco_rect.width = child->rect.width / children;
195             child->deco_rect.height = deco_height;
196             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
197             child->deco_rect.y = y - con->rect.y;
198
199             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
200                 child->rect.y += deco_height;
201                 child->rect.height -= deco_height;
202             }
203         }
204
205         DLOG("child at (%d, %d) with (%d x %d)\n",
206                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
207         DLOG("x now %d, y now %d\n", x, y);
208         x_raise_con(child);
209         render_con(child, false);
210         i++;
211     }
212
213     /* in a stacking or tabbed container, we ensure the focused client is raised */
214     if (con->layout == L_STACKED || con->layout == L_TABBED) {
215         Con *foc = TAILQ_FIRST(&(con->focus_head));
216         if (foc != TAILQ_END(&(con->focus_head))) {
217             DLOG("con %p is stacking, raising %p\n", con, foc);
218             x_raise_con(foc);
219             /* by rendering the stacked container again, we handle the case
220              * that we have a non-leaf-container inside the stack. */
221             render_con(foc, false);
222         }
223     }
224
225     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
226         DLOG("render floating:\n");
227         DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
228         x_raise_con(child);
229         render_con(child, false);
230     }
231
232     DLOG("-- level up\n");
233 }