3 * vim:ts=4:sw=4:expandtab
5 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8 * render.c: Renders (determines position/sizes) the layout tree, updating the
9 * various rects. Needs to be pushed to X11 (see x.c) to be visible.
14 /* change this to 'true' if you want to have additional borders around every
15 * container (for debugging purposes) */
16 static bool show_debug_borders = false;
19 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
20 * get the height of their content and the remaining CT_CON gets the rest.
23 static void render_l_output(Con *con) {
24 Con *child, *dockchild;
28 int height = con->rect.height;
30 /* Find the content container and ensure that there is exactly one. Also
31 * check for any non-CT_DOCKAREA clients. */
33 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
34 if (child->type == CT_CON) {
35 if (content != NULL) {
36 DLOG("More than one CT_CON on output container\n");
40 } else if (child->type != CT_DOCKAREA) {
41 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
46 assert(content != NULL);
48 /* We need to find out if there is a fullscreen con on the current workspace
49 * and take the short-cut to render it directly (the user does not want to
50 * see the dockareas in that case) */
51 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
52 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
54 fullscreen->rect = con->rect;
55 x_raise_con(fullscreen);
56 render_con(fullscreen, true);
60 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
61 * children) and figure out how many pixels we have left for the rest */
62 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
63 if (child->type != CT_DOCKAREA)
66 child->rect.height = 0;
67 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
68 child->rect.height += dockchild->geometry.height;
70 height -= child->rect.height;
73 /* Second pass: Set the widths/heights */
74 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
75 if (child->type == CT_CON) {
78 child->rect.width = con->rect.width;
79 child->rect.height = height;
84 child->rect.width = con->rect.width;
86 child->deco_rect.x = 0;
87 child->deco_rect.y = 0;
88 child->deco_rect.width = 0;
89 child->deco_rect.height = 0;
91 y += child->rect.height;
93 DLOG("child at (%d, %d) with (%d x %d)\n",
94 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
96 render_con(child, false);
101 * "Renders" the given container (and its children), meaning that all rects are
102 * updated correctly. Note that this function does not call any xcb_*
103 * functions, so the changes are completely done in memory only (and
104 * side-effect free). As soon as you call x_push_changes(), the changes will be
108 void render_con(Con *con, bool render_fullscreen) {
109 int children = con_num_children(con);
110 DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
111 (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
114 /* Copy container rect, subtract container border */
115 /* This is the actually usable space inside this container for clients */
116 Rect rect = con->rect;
118 /* Display a border if this is a leaf node. For container nodes, we don’t
119 * draw borders (except when in debug mode) */
120 if (show_debug_borders) {
124 rect.height -= 2 * 2;
134 /* if this container contains a window, set the coordinates */
136 /* depending on the border style, the rect of the child window
137 * needs to be smaller */
138 Rect *inset = &(con->window_rect);
139 *inset = (Rect){0, 0, con->rect.width, con->rect.height};
140 if (!render_fullscreen)
141 *inset = rect_add(*inset, con_border_style_rect(con));
143 /* Obey x11 border */
144 inset->width -= (2 * con->border_width);
145 inset->height -= (2 * con->border_width);
147 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
149 * The spec isn’t explicit on whether the aspect ratio hints should be
150 * respected during fullscreen mode. Other WMs such as Openbox don’t do
151 * that, and this post suggests that this is the correct way to do it:
152 * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
154 * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
155 * subtitle rendering, see http://bugs.i3wm.org/594 */
156 if (!render_fullscreen &&
157 con->proportional_height != 0 &&
158 con->proportional_width != 0) {
159 double new_height = inset->height + 1;
160 int new_width = inset->width;
162 while (new_height > inset->height) {
163 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
165 if (new_height > inset->height)
168 /* Center the window */
169 inset->y += ceil(inset->height / 2) - floor(new_height / 2);
170 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
172 inset->height = new_height;
173 inset->width = new_width;
176 if (con->height_increment > 1) {
177 int old_height = inset->height;
178 inset->height -= (inset->height - con->base_height) % con->height_increment;
179 DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
180 old_height - inset->height, con->height_increment, con->base_height);
183 if (con->width_increment > 1) {
184 int old_width = inset->width;
185 inset->width -= (inset->width - con->base_width) % con->width_increment;
186 DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
187 old_width - inset->width, con->width_increment, con->base_width);
190 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
193 /* Check for fullscreen nodes */
194 Con *fullscreen = NULL;
195 if (con->type != CT_OUTPUT) {
196 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
199 fullscreen->rect = rect;
200 x_raise_con(fullscreen);
201 render_con(fullscreen, true);
205 /* find the height for the decorations */
206 int deco_height = config.font.height + 4;
207 if (config.font.height & 0x01)
210 /* precalculate the sizes to be able to correct rounding errors */
212 if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && children > 0) {
213 assert(!TAILQ_EMPTY(&con->nodes_head));
215 int i = 0, assigned = 0;
216 int total = con_orientation(con) == HORIZ ? rect.width : rect.height;
217 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
218 double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
219 assigned += sizes[i++] = percentage * total;
221 assert(assigned == total ||
222 (assigned > total && assigned - total <= children * 2) ||
223 (assigned < total && total - assigned <= children * 2));
224 int signal = assigned < total ? 1 : -1;
225 while (assigned != total) {
226 for (i = 0; i < children && assigned != total; ++i) {
233 if (con->layout == L_OUTPUT) {
234 /* Skip i3-internal outputs */
235 if (con->name[0] == '_' && con->name[1] == '_')
237 render_l_output(con);
238 } else if (con->type == CT_ROOT) {
240 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
241 render_con(output, false);
244 /* We need to render floating windows after rendering all outputs’
245 * tiling windows because they need to be on top of *every* output at
246 * all times. This is important when the user places floating
247 * windows/containers so that they overlap on another output. */
248 DLOG("Rendering floating windows:\n");
249 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
250 if (output->name[0] == '_' && output->name[1] == '_')
252 /* Get the active workspace of that output */
253 Con *content = output_get_content(output);
254 Con *workspace = TAILQ_FIRST(&(content->focus_head));
256 /* Check for (floating!) fullscreen nodes */
257 /* XXX: This code duplication is unfortunate. Keep in mind to fix
258 * this when we clean up the whole render.c */
259 Con *fullscreen = NULL;
260 fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
262 /* Either the fullscreen window is inside the floating
263 * container, then we need to render and raise it now… */
264 if (con_inside_floating(fullscreen)) {
265 fullscreen->rect = output->rect;
266 x_raise_con(fullscreen);
267 render_con(fullscreen, true);
270 /* …or it’s a tiling window, in which case the floating
271 * windows should not overlap it, so we skip rendering this
278 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
279 DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
281 render_con(child, false);
287 /* FIXME: refactor this into separate functions: */
289 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
290 assert(children > 0);
293 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
294 if (con->layout == L_SPLITH) {
297 child->rect.width = sizes[i];
298 child->rect.height = rect.height;
299 x += child->rect.width;
303 child->rect.width = rect.width;
304 child->rect.height = sizes[i];
305 y += child->rect.height;
308 /* first we have the decoration, if this is a leaf node */
309 if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
310 /* TODO: make a function for relative coords? */
311 child->deco_rect.x = child->rect.x - con->rect.x;
312 child->deco_rect.y = child->rect.y - con->rect.y;
314 child->rect.y += deco_height;
315 child->rect.height -= deco_height;
317 child->deco_rect.width = child->rect.width;
318 child->deco_rect.height = deco_height;
323 else if (con->layout == L_STACKED) {
326 child->rect.width = rect.width;
327 child->rect.height = rect.height;
329 child->deco_rect.x = x - con->rect.x;
330 child->deco_rect.y = y - con->rect.y + (i * deco_height);
331 child->deco_rect.width = child->rect.width;
332 child->deco_rect.height = deco_height;
334 if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
335 child->rect.y += (deco_height * children);
336 child->rect.height -= (deco_height * children);
341 else if (con->layout == L_TABBED) {
344 child->rect.width = rect.width;
345 child->rect.height = rect.height;
347 child->deco_rect.width = child->rect.width / children;
348 child->deco_rect.height = deco_height;
349 child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
350 child->deco_rect.y = y - con->rect.y;
352 if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
353 child->rect.y += deco_height;
354 child->rect.height -= deco_height;
358 /* dockarea layout */
359 else if (con->layout == L_DOCKAREA) {
362 child->rect.width = rect.width;
363 child->rect.height = child->geometry.height;
365 child->deco_rect.x = 0;
366 child->deco_rect.y = 0;
367 child->deco_rect.width = 0;
368 child->deco_rect.height = 0;
369 y += child->rect.height;
372 DLOG("child at (%d, %d) with (%d x %d)\n",
373 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
375 render_con(child, false);
379 /* in a stacking or tabbed container, we ensure the focused client is raised */
380 if (con->layout == L_STACKED || con->layout == L_TABBED) {
381 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
383 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
384 /* By rendering the stacked container again, we handle the case
385 * that we have a non-leaf-container inside the stack. In that
386 * case, the children of the non-leaf-container need to be raised
388 render_con(child, false);
392 /* Raise the stack con itself. This will put the stack decoration on
393 * top of every stack window. That way, when a new window is opened in
394 * the stack, the old window will not obscure part of the decoration
395 * (it’s unmapped afterwards). */