2 #define I3__FILE__ "render.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * render.c: Renders (determines position/sizes) the layout tree, updating the
10 * various rects. Needs to be pushed to X11 (see x.c) to be visible.
15 /* Forward declarations */
16 static void render_con_split(Con *con, Con *child, render_params *p, int i);
17 static void render_con_stacked(Con *con, Con *child, render_params *p, int i);
18 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i);
19 static void render_con_dockarea(Con *con, Con *child, render_params *p);
21 /* change this to 'true' if you want to have additional borders around every
22 * container (for debugging purposes) */
23 static bool show_debug_borders = false;
26 * Returns the height for the decorations
28 int render_deco_height(void) {
29 int deco_height = config.font.height + 4;
30 if (config.font.height & 0x01)
36 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
37 * get the height of their content and the remaining CT_CON gets the rest.
40 static void render_l_output(Con *con) {
41 Con *child, *dockchild;
45 int height = con->rect.height;
47 /* Find the content container and ensure that there is exactly one. Also
48 * check for any non-CT_DOCKAREA clients. */
50 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
51 if (child->type == CT_CON) {
52 if (content != NULL) {
53 DLOG("More than one CT_CON on output container\n");
57 } else if (child->type != CT_DOCKAREA) {
58 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
63 if (content == NULL) {
64 DLOG("Skipping this output because it is currently being destroyed.\n");
68 /* We need to find out if there is a fullscreen con on the current workspace
69 * and take the short-cut to render it directly (the user does not want to
70 * see the dockareas in that case) */
71 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
73 DLOG("Skipping this output because it is currently being destroyed.\n");
76 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
78 fullscreen->rect = con->rect;
79 x_raise_con(fullscreen);
80 render_con(fullscreen, true);
84 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
85 * children) and figure out how many pixels we have left for the rest */
86 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
87 if (child->type != CT_DOCKAREA)
90 child->rect.height = 0;
91 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
92 child->rect.height += dockchild->geometry.height;
94 height -= child->rect.height;
97 /* Second pass: Set the widths/heights */
98 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
99 if (child->type == CT_CON) {
102 child->rect.width = con->rect.width;
103 child->rect.height = height;
108 child->rect.width = con->rect.width;
110 child->deco_rect.x = 0;
111 child->deco_rect.y = 0;
112 child->deco_rect.width = 0;
113 child->deco_rect.height = 0;
115 y += child->rect.height;
117 DLOG("child at (%d, %d) with (%d x %d)\n",
118 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
120 render_con(child, false);
125 * "Renders" the given container (and its children), meaning that all rects are
126 * updated correctly. Note that this function does not call any xcb_*
127 * functions, so the changes are completely done in memory only (and
128 * side-effect free). As soon as you call x_push_changes(), the changes will be
132 void render_con(Con *con, bool render_fullscreen) {
133 render_params params = {
137 .children = con_num_children(con)};
139 DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
140 (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
143 /* Display a border if this is a leaf node. For container nodes, we don’t
144 * draw borders (except when in debug mode) */
145 if (show_debug_borders) {
148 params.rect.width -= 2 * 2;
149 params.rect.height -= 2 * 2;
155 /* if this container contains a window, set the coordinates */
157 /* depending on the border style, the rect of the child window
158 * needs to be smaller */
159 Rect *inset = &(con->window_rect);
160 *inset = (Rect){0, 0, con->rect.width, con->rect.height};
161 if (!render_fullscreen)
162 *inset = rect_add(*inset, con_border_style_rect(con));
164 /* Obey x11 border */
165 inset->width -= (2 * con->border_width);
166 inset->height -= (2 * con->border_width);
168 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
170 * The spec isn’t explicit on whether the aspect ratio hints should be
171 * respected during fullscreen mode. Other WMs such as Openbox don’t do
172 * that, and this post suggests that this is the correct way to do it:
173 * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
175 * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
176 * subtitle rendering, see http://bugs.i3wm.org/594 */
177 if (!render_fullscreen &&
178 con->window->aspect_ratio > 0.0) {
179 DLOG("aspect_ratio = %f, current width/height are %d/%d\n",
180 con->window->aspect_ratio, inset->width, inset->height);
181 double new_height = inset->height + 1;
182 int new_width = inset->width;
184 while (new_height > inset->height) {
185 new_height = (1.0 / con->window->aspect_ratio) * new_width;
187 if (new_height > inset->height)
190 /* Center the window */
191 inset->y += ceil(inset->height / 2) - floor((new_height + .5) / 2);
192 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
194 inset->height = new_height + .5;
195 inset->width = new_width;
198 /* NB: We used to respect resize increment size hints for tiling
199 * windows up until commit 0db93d9 here. However, since all terminal
200 * emulators cope with ignoring the size hints in a better way than we
201 * can (by providing their fake-transparency or background color), this
202 * code was removed. See also http://bugs.i3wm.org/540 */
204 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
207 /* Check for fullscreen nodes */
208 Con *fullscreen = NULL;
209 if (con->type != CT_OUTPUT) {
210 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
213 fullscreen->rect = params.rect;
214 x_raise_con(fullscreen);
215 render_con(fullscreen, true);
216 /* Fullscreen containers are either global (underneath the CT_ROOT
217 * container) or per-output (underneath the CT_CONTENT container). For
218 * global fullscreen containers, we cannot abort rendering here yet,
219 * because the floating windows (with popup_during_fullscreen smart)
220 * have not yet been rendered (see the CT_ROOT code path below). See
221 * also http://bugs.i3wm.org/1393 */
222 if (con->type != CT_ROOT) {
227 /* find the height for the decorations */
228 params.deco_height = render_deco_height();
230 /* precalculate the sizes to be able to correct rounding errors */
231 int sizes[params.children];
232 memset(sizes, 0, params.children * sizeof(int));
233 if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && params.children > 0) {
234 assert(!TAILQ_EMPTY(&con->nodes_head));
237 int i = 0, assigned = 0;
238 int total = con_orientation(con) == HORIZ ? params.rect.width : params.rect.height;
239 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
240 double percentage = child->percent > 0.0 ? child->percent : 1.0 / params.children;
241 assigned += sizes[i++] = percentage * total;
243 assert(assigned == total ||
244 (assigned > total && assigned - total <= params.children * 2) ||
245 (assigned < total && total - assigned <= params.children * 2));
246 int signal = assigned < total ? 1 : -1;
247 while (assigned != total) {
248 for (i = 0; i < params.children && assigned != total; ++i) {
254 params.sizes = sizes;
256 if (con->layout == L_OUTPUT) {
257 /* Skip i3-internal outputs */
258 if (con_is_internal(con))
260 render_l_output(con);
261 } else if (con->type == CT_ROOT) {
264 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
265 render_con(output, false);
269 /* We need to render floating windows after rendering all outputs’
270 * tiling windows because they need to be on top of *every* output at
271 * all times. This is important when the user places floating
272 * windows/containers so that they overlap on another output. */
273 DLOG("Rendering floating windows:\n");
274 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
275 if (con_is_internal(output))
277 /* Get the active workspace of that output */
278 Con *content = output_get_content(output);
279 if (!content || TAILQ_EMPTY(&(content->focus_head))) {
280 DLOG("Skipping this output because it is currently being destroyed.\n");
283 Con *workspace = TAILQ_FIRST(&(content->focus_head));
284 Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
286 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
287 /* Don’t render floating windows when there is a fullscreen window
288 * on that workspace. Necessary to make floating fullscreen work
289 * correctly (ticket #564). */
290 /* If there is no fullscreen->window, this cannot be a
291 * transient window, so we _know_ we need to skip it. This
292 * happens during restarts where the container already exists,
293 * but the window was not yet associated. */
294 if (fullscreen != NULL && fullscreen->window == NULL)
296 if (fullscreen != NULL && fullscreen->window != NULL) {
297 Con *floating_child = con_descend_focused(child);
298 Con *transient_con = floating_child;
299 bool is_transient_for = false;
300 /* Exception to the above rule: smart
301 * popup_during_fullscreen handling (popups belonging to
302 * the fullscreen app will be rendered). */
303 while (transient_con != NULL &&
304 transient_con->window != NULL &&
305 transient_con->window->transient_for != XCB_NONE) {
306 DLOG("transient_con = 0x%08x, transient_con->window->transient_for = 0x%08x, fullscreen_id = 0x%08x\n",
307 transient_con->window->id, transient_con->window->transient_for, fullscreen->window->id);
308 if (transient_con->window->transient_for == fullscreen->window->id) {
309 is_transient_for = true;
312 Con *next_transient = con_by_window_id(transient_con->window->transient_for);
313 if (next_transient == NULL)
315 /* Some clients (e.g. x11-ssh-askpass) actually set
316 * WM_TRANSIENT_FOR to their own window id, so break instead of
317 * looping endlessly. */
318 if (transient_con == next_transient)
320 transient_con = next_transient;
323 if (!is_transient_for)
326 DLOG("Rendering floating child even though in fullscreen mode: "
327 "floating->transient_for (0x%08x) --> fullscreen->id (0x%08x)\n",
328 floating_child->window->transient_for, fullscreen->window->id);
331 DLOG("floating child at (%d,%d) with %d x %d\n",
332 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
334 render_con(child, false);
340 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
341 assert(params.children > 0);
343 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
344 render_con_split(con, child, ¶ms, i);
345 } else if (con->layout == L_STACKED) {
346 render_con_stacked(con, child, ¶ms, i);
347 } else if (con->layout == L_TABBED) {
348 render_con_tabbed(con, child, ¶ms, i);
349 } else if (con->layout == L_DOCKAREA) {
350 render_con_dockarea(con, child, ¶ms);
353 DLOG("child at (%d, %d) with (%d x %d)\n",
354 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
356 render_con(child, false);
360 /* in a stacking or tabbed container, we ensure the focused client is raised */
361 if (con->layout == L_STACKED || con->layout == L_TABBED) {
362 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
364 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
365 /* By rendering the stacked container again, we handle the case
366 * that we have a non-leaf-container inside the stack. In that
367 * case, the children of the non-leaf-container need to be raised
369 render_con(child, false);
372 if (params.children != 1)
373 /* Raise the stack con itself. This will put the stack decoration on
374 * top of every stack window. That way, when a new window is opened in
375 * the stack, the old window will not obscure part of the decoration
376 * (it’s unmapped afterwards). */
382 static void render_con_split(Con *con, Con *child, render_params *p, int i) {
383 assert(con->layout == L_SPLITH || con->layout == L_SPLITV);
385 if (con->layout == L_SPLITH) {
386 child->rect.x = p->x;
387 child->rect.y = p->y;
388 child->rect.width = p->sizes[i];
389 child->rect.height = p->rect.height;
390 p->x += child->rect.width;
392 child->rect.x = p->x;
393 child->rect.y = p->y;
394 child->rect.width = p->rect.width;
395 child->rect.height = p->sizes[i];
396 p->y += child->rect.height;
399 /* first we have the decoration, if this is a leaf node */
400 if (con_is_leaf(child)) {
401 if (child->border_style == BS_NORMAL) {
402 /* TODO: make a function for relative coords? */
403 child->deco_rect.x = child->rect.x - con->rect.x;
404 child->deco_rect.y = child->rect.y - con->rect.y;
406 child->rect.y += p->deco_height;
407 child->rect.height -= p->deco_height;
409 child->deco_rect.width = child->rect.width;
410 child->deco_rect.height = p->deco_height;
412 child->deco_rect.x = 0;
413 child->deco_rect.y = 0;
414 child->deco_rect.width = 0;
415 child->deco_rect.height = 0;
420 static void render_con_stacked(Con *con, Con *child, render_params *p, int i) {
421 assert(con->layout == L_STACKED);
423 child->rect.x = p->x;
424 child->rect.y = p->y;
425 child->rect.width = p->rect.width;
426 child->rect.height = p->rect.height;
428 child->deco_rect.x = p->x - con->rect.x;
429 child->deco_rect.y = p->y - con->rect.y + (i * p->deco_height);
430 child->deco_rect.width = child->rect.width;
431 child->deco_rect.height = p->deco_height;
433 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
434 child->rect.y += (p->deco_height * p->children);
435 child->rect.height -= (p->deco_height * p->children);
439 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i) {
440 assert(con->layout == L_TABBED);
442 child->rect.x = p->x;
443 child->rect.y = p->y;
444 child->rect.width = p->rect.width;
445 child->rect.height = p->rect.height;
447 child->deco_rect.width = floor((float)child->rect.width / p->children);
448 child->deco_rect.x = p->x - con->rect.x + i * child->deco_rect.width;
449 child->deco_rect.y = p->y - con->rect.y;
451 /* Since the tab width may be something like 31,6 px per tab, we
452 * let the last tab have all the extra space (0,6 * children). */
453 if (i == (p->children - 1)) {
454 child->deco_rect.width += (child->rect.width - (child->deco_rect.x + child->deco_rect.width));
457 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
458 child->rect.y += p->deco_height;
459 child->rect.height -= p->deco_height;
460 child->deco_rect.height = p->deco_height;
462 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
466 static void render_con_dockarea(Con *con, Con *child, render_params *p) {
467 assert(con->layout == L_DOCKAREA);
469 child->rect.x = p->x;
470 child->rect.y = p->y;
471 child->rect.width = p->rect.width;
472 child->rect.height = child->geometry.height;
474 child->deco_rect.x = 0;
475 child->deco_rect.y = 0;
476 child->deco_rect.width = 0;
477 child->deco_rect.height = 0;
478 p->y += child->rect.height;