2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * render.c: Renders (determines position/sizes) the layout tree, updating the
8 * various rects. Needs to be pushed to X11 (see x.c) to be visible.
13 /* change this to 'true' if you want to have additional borders around every
14 * container (for debugging purposes) */
15 static bool show_debug_borders = false;
18 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
19 * get the height of their content and the remaining CT_CON gets the rest.
22 static void render_l_output(Con *con) {
23 Con *child, *dockchild;
27 int height = con->rect.height;
29 /* Find the content container and ensure that there is exactly one. Also
30 * check for any non-CT_DOCKAREA clients. */
32 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
33 if (child->type == CT_CON) {
34 if (content != NULL) {
35 DLOG("More than one CT_CON on output container\n");
39 } else if (child->type != CT_DOCKAREA) {
40 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
45 assert(content != NULL);
47 /* We need to find out if there is a fullscreen con on the current workspace
48 * and take the short-cut to render it directly (the user does not want to
49 * see the dockareas in that case) */
50 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
51 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
53 fullscreen->rect = con->rect;
54 x_raise_con(fullscreen);
55 render_con(fullscreen, true);
59 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
60 * children) and figure out how many pixels we have left for the rest */
61 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
62 if (child->type != CT_DOCKAREA)
65 child->rect.height = 0;
66 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
67 child->rect.height += dockchild->geometry.height;
69 height -= child->rect.height;
72 /* Second pass: Set the widths/heights */
73 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
74 if (child->type == CT_CON) {
77 child->rect.width = con->rect.width;
78 child->rect.height = height;
83 child->rect.width = con->rect.width;
85 child->deco_rect.x = 0;
86 child->deco_rect.y = 0;
87 child->deco_rect.width = 0;
88 child->deco_rect.height = 0;
90 y += child->rect.height;
92 DLOG("child at (%d, %d) with (%d x %d)\n",
93 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
95 render_con(child, false);
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
107 void render_con(Con *con, bool render_fullscreen) {
108 int children = con_num_children(con);
109 DLOG("Rendering %snode %p / %s / layout %d / children %d / orient %d\n",
110 (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
111 children, con->orientation);
113 /* Copy container rect, subtract container border */
114 /* This is the actually usable space inside this container for clients */
115 Rect rect = con->rect;
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) {
123 rect.height -= 2 * 2;
133 /* if this container contains a window, set the coordinates */
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));
142 /* Obey x11 border */
143 inset->width -= (2 * con->border_width);
144 inset->height -= (2 * con->border_width);
146 /* Obey the aspect ratio, if any */
147 if (con->proportional_height != 0 &&
148 con->proportional_width != 0) {
149 double new_height = inset->height + 1;
150 int new_width = inset->width;
152 while (new_height > inset->height) {
153 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
155 if (new_height > inset->height)
158 /* Center the window */
159 inset->y += ceil(inset->height / 2) - floor(new_height / 2);
160 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
162 inset->height = new_height;
163 inset->width = new_width;
166 if (con->height_increment > 1) {
167 int old_height = inset->height;
168 inset->height -= (inset->height - con->base_height) % con->height_increment;
169 DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
170 old_height - inset->height, con->height_increment, con->base_height);
173 if (con->width_increment > 1) {
174 int old_width = inset->width;
175 inset->width -= (inset->width - con->base_width) % con->width_increment;
176 DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
177 old_width - inset->width, con->width_increment, con->base_width);
180 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
183 /* Check for fullscreen nodes */
184 Con *fullscreen = NULL;
185 if (con->type != CT_OUTPUT) {
186 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
189 fullscreen->rect = rect;
190 x_raise_con(fullscreen);
191 render_con(fullscreen, true);
195 /* find the height for the decorations */
196 int deco_height = config.font.height + 4;
197 if (config.font.height & 0x01)
200 /* precalculate the sizes to be able to correct rounding errors */
202 if (con->layout == L_DEFAULT && children > 0) {
203 assert(!TAILQ_EMPTY(&con->nodes_head));
205 int i = 0, assigned = 0;
206 int total = con->orientation == HORIZ ? rect.width : rect.height;
207 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
208 double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
209 assigned += sizes[i++] = percentage * total;
211 assert(assigned == total ||
212 (assigned > total && assigned - total <= children * 2) ||
213 (assigned < total && total - assigned <= children * 2));
214 int signal = assigned < total ? 1 : -1;
215 while (assigned != total) {
216 for (i = 0; i < children && assigned != total; ++i) {
223 if (con->layout == L_OUTPUT) {
224 render_l_output(con);
225 } else if (con->type == CT_ROOT) {
227 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
228 render_con(output, false);
231 /* We need to render floating windows after rendering all outputs’
232 * tiling windows because they need to be on top of *every* output at
233 * all times. This is important when the user places floating
234 * windows/containers so that they overlap on another output. */
235 DLOG("Rendering floating windows:\n");
236 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
237 /* Get the active workspace of that output */
238 Con *content = output_get_content(output);
239 Con *workspace = TAILQ_FIRST(&(content->focus_head));
241 /* Check for (floating!) fullscreen nodes */
242 /* XXX: This code duplication is unfortunate. Keep in mind to fix
243 * this when we clean up the whole render.c */
244 Con *fullscreen = NULL;
245 fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
247 /* Either the fullscreen window is inside the floating
248 * container, then we need to render and raise it now… */
249 if (con_inside_floating(fullscreen)) {
250 fullscreen->rect = output->rect;
251 x_raise_con(fullscreen);
252 render_con(fullscreen, true);
255 /* …or it’s a tiling window, in which case the floating
256 * windows should not overlap it, so we skip rendering this
263 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
264 DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
266 render_con(child, false);
272 /* FIXME: refactor this into separate functions: */
274 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
275 assert(children > 0);
278 if (con->layout == L_DEFAULT) {
279 if (con->orientation == HORIZ) {
282 child->rect.width = sizes[i];
283 child->rect.height = rect.height;
284 x += child->rect.width;
288 child->rect.width = rect.width;
289 child->rect.height = sizes[i];
290 y += child->rect.height;
293 /* first we have the decoration, if this is a leaf node */
294 if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
295 /* TODO: make a function for relative coords? */
296 child->deco_rect.x = child->rect.x - con->rect.x;
297 child->deco_rect.y = child->rect.y - con->rect.y;
299 child->rect.y += deco_height;
300 child->rect.height -= deco_height;
302 child->deco_rect.width = child->rect.width;
303 child->deco_rect.height = deco_height;
308 else if (con->layout == L_STACKED) {
311 child->rect.width = rect.width;
312 child->rect.height = rect.height;
314 child->deco_rect.x = x - con->rect.x;
315 child->deco_rect.y = y - con->rect.y + (i * deco_height);
316 child->deco_rect.width = child->rect.width;
317 child->deco_rect.height = deco_height;
319 if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
320 child->rect.y += (deco_height * children);
321 child->rect.height -= (deco_height * children);
326 else if (con->layout == L_TABBED) {
329 child->rect.width = rect.width;
330 child->rect.height = rect.height;
332 child->deco_rect.width = child->rect.width / children;
333 child->deco_rect.height = deco_height;
334 child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
335 child->deco_rect.y = y - con->rect.y;
337 if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
338 child->rect.y += deco_height;
339 child->rect.height -= deco_height;
343 /* dockarea layout */
344 else if (con->layout == L_DOCKAREA) {
347 child->rect.width = rect.width;
348 child->rect.height = child->geometry.height;
350 child->deco_rect.x = 0;
351 child->deco_rect.y = 0;
352 child->deco_rect.width = 0;
353 child->deco_rect.height = 0;
354 y += child->rect.height;
357 DLOG("child at (%d, %d) with (%d x %d)\n",
358 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
360 render_con(child, false);
364 /* in a stacking or tabbed container, we ensure the focused client is raised */
365 if (con->layout == L_STACKED || con->layout == L_TABBED) {
366 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
368 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
369 /* By rendering the stacked container again, we handle the case
370 * that we have a non-leaf-container inside the stack. In that
371 * case, the children of the non-leaf-container need to be raised
373 render_con(child, false);
377 /* Raise the stack con itself. This will put the stack decoration on
378 * top of every stack window. That way, when a new window is opened in
379 * the stack, the old window will not obscure part of the decoration
380 * (it’s unmapped afterwards). */