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