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 int *precalculate_sizes(Con *con, render_params *p);
17 static void render_root(Con *con, Con *fullscreen);
18 static void render_output(Con *con);
19 static void render_con_split(Con *con, Con *child, render_params *p, int i);
20 static void render_con_stacked(Con *con, Con *child, render_params *p, int i);
21 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i);
22 static void render_con_dockarea(Con *con, Con *child, render_params *p);
24 /* change this to 'true' if you want to have additional borders around every
25 * container (for debugging purposes) */
26 static bool show_debug_borders = false;
29 * Returns the height for the decorations
31 int render_deco_height(void) {
32 int deco_height = config.font.height + 4;
33 if (config.font.height & 0x01)
39 * "Renders" the given container (and its children), meaning that all rects are
40 * updated correctly. Note that this function does not call any xcb_*
41 * functions, so the changes are completely done in memory only (and
42 * side-effect free). As soon as you call x_push_changes(), the changes will be
46 void render_con(Con *con, bool render_fullscreen) {
47 render_params params = {
51 .children = con_num_children(con)};
53 DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
54 (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
57 /* Display a border if this is a leaf node. For container nodes, we don’t
58 * draw borders (except when in debug mode) */
59 if (show_debug_borders) {
62 params.rect.width -= 2 * 2;
63 params.rect.height -= 2 * 2;
69 /* if this container contains a window, set the coordinates */
71 /* depending on the border style, the rect of the child window
72 * needs to be smaller */
73 Rect *inset = &(con->window_rect);
74 *inset = (Rect){0, 0, con->rect.width, con->rect.height};
75 if (!render_fullscreen)
76 *inset = rect_add(*inset, con_border_style_rect(con));
79 inset->width -= (2 * con->border_width);
80 inset->height -= (2 * con->border_width);
82 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
84 * The spec isn’t explicit on whether the aspect ratio hints should be
85 * respected during fullscreen mode. Other WMs such as Openbox don’t do
86 * that, and this post suggests that this is the correct way to do it:
87 * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
89 * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
90 * subtitle rendering, see http://bugs.i3wm.org/594 */
91 if (!render_fullscreen && con->window->aspect_ratio > 0.0) {
92 DLOG("aspect_ratio = %f, current width/height are %d/%d\n",
93 con->window->aspect_ratio, inset->width, inset->height);
94 double new_height = inset->height + 1;
95 int new_width = inset->width;
97 while (new_height > inset->height) {
98 new_height = (1.0 / con->window->aspect_ratio) * new_width;
100 if (new_height > inset->height)
103 /* Center the window */
104 inset->y += ceil(inset->height / 2) - floor((new_height + .5) / 2);
105 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
107 inset->height = new_height + .5;
108 inset->width = new_width;
111 /* NB: We used to respect resize increment size hints for tiling
112 * windows up until commit 0db93d9 here. However, since all terminal
113 * emulators cope with ignoring the size hints in a better way than we
114 * can (by providing their fake-transparency or background color), this
115 * code was removed. See also http://bugs.i3wm.org/540 */
117 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
120 /* Check for fullscreen nodes */
121 Con *fullscreen = NULL;
122 if (con->type != CT_OUTPUT) {
123 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
126 fullscreen->rect = params.rect;
127 x_raise_con(fullscreen);
128 render_con(fullscreen, true);
129 /* Fullscreen containers are either global (underneath the CT_ROOT
130 * container) or per-output (underneath the CT_CONTENT container). For
131 * global fullscreen containers, we cannot abort rendering here yet,
132 * because the floating windows (with popup_during_fullscreen smart)
133 * have not yet been rendered (see the CT_ROOT code path below). See
134 * also http://bugs.i3wm.org/1393 */
135 if (con->type != CT_ROOT) {
140 /* find the height for the decorations */
141 params.deco_height = render_deco_height();
143 /* precalculate the sizes to be able to correct rounding errors */
144 params.sizes = precalculate_sizes(con, ¶ms);
146 if (con->layout == L_OUTPUT) {
147 /* Skip i3-internal outputs */
148 if (con_is_internal(con))
151 } else if (con->type == CT_ROOT) {
152 render_root(con, fullscreen);
155 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
156 assert(params.children > 0);
158 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
159 render_con_split(con, child, ¶ms, i);
160 } else if (con->layout == L_STACKED) {
161 render_con_stacked(con, child, ¶ms, i);
162 } else if (con->layout == L_TABBED) {
163 render_con_tabbed(con, child, ¶ms, i);
164 } else if (con->layout == L_DOCKAREA) {
165 render_con_dockarea(con, child, ¶ms);
168 DLOG("child at (%d, %d) with (%d x %d)\n",
169 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
171 render_con(child, false);
175 /* in a stacking or tabbed container, we ensure the focused client is raised */
176 if (con->layout == L_STACKED || con->layout == L_TABBED) {
177 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
179 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
180 /* By rendering the stacked container again, we handle the case
181 * that we have a non-leaf-container inside the stack. In that
182 * case, the children of the non-leaf-container need to be raised
184 render_con(child, false);
187 if (params.children != 1)
188 /* Raise the stack con itself. This will put the stack decoration on
189 * top of every stack window. That way, when a new window is opened in
190 * the stack, the old window will not obscure part of the decoration
191 * (it’s unmapped afterwards). */
200 static int *precalculate_sizes(Con *con, render_params *p) {
201 int *sizes = smalloc(p->children * sizeof(int));
202 if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && p->children > 0) {
203 assert(!TAILQ_EMPTY(&con->nodes_head));
206 int i = 0, assigned = 0;
207 int total = con_orientation(con) == HORIZ ? p->rect.width : p->rect.height;
208 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
209 double percentage = child->percent > 0.0 ? child->percent : 1.0 / p->children;
210 assigned += sizes[i++] = percentage * total;
212 assert(assigned == total ||
213 (assigned > total && assigned - total <= p->children * 2) ||
214 (assigned < total && total - assigned <= p->children * 2));
215 int signal = assigned < total ? 1 : -1;
216 while (assigned != total) {
217 for (i = 0; i < p->children && assigned != total; ++i) {
227 static void render_root(Con *con, Con *fullscreen) {
230 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
231 render_con(output, false);
235 /* We need to render floating windows after rendering all outputs’
236 * tiling windows because they need to be on top of *every* output at
237 * all times. This is important when the user places floating
238 * windows/containers so that they overlap on another output. */
239 DLOG("Rendering floating windows:\n");
240 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
241 if (con_is_internal(output))
243 /* Get the active workspace of that output */
244 Con *content = output_get_content(output);
245 if (!content || TAILQ_EMPTY(&(content->focus_head))) {
246 DLOG("Skipping this output because it is currently being destroyed.\n");
249 Con *workspace = TAILQ_FIRST(&(content->focus_head));
250 Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
252 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
253 /* Don’t render floating windows when there is a fullscreen window
254 * on that workspace. Necessary to make floating fullscreen work
255 * correctly (ticket #564). */
256 /* If there is no fullscreen->window, this cannot be a
257 * transient window, so we _know_ we need to skip it. This
258 * happens during restarts where the container already exists,
259 * but the window was not yet associated. */
260 if (fullscreen != NULL && fullscreen->window == NULL)
262 if (fullscreen != NULL && fullscreen->window != NULL) {
263 Con *floating_child = con_descend_focused(child);
264 Con *transient_con = floating_child;
265 bool is_transient_for = false;
266 /* Exception to the above rule: smart
267 * popup_during_fullscreen handling (popups belonging to
268 * the fullscreen app will be rendered). */
269 while (transient_con != NULL &&
270 transient_con->window != NULL &&
271 transient_con->window->transient_for != XCB_NONE) {
272 DLOG("transient_con = 0x%08x, transient_con->window->transient_for = 0x%08x, fullscreen_id = 0x%08x\n",
273 transient_con->window->id, transient_con->window->transient_for, fullscreen->window->id);
274 if (transient_con->window->transient_for == fullscreen->window->id) {
275 is_transient_for = true;
278 Con *next_transient = con_by_window_id(transient_con->window->transient_for);
279 if (next_transient == NULL)
281 /* Some clients (e.g. x11-ssh-askpass) actually set
282 * WM_TRANSIENT_FOR to their own window id, so break instead of
283 * looping endlessly. */
284 if (transient_con == next_transient)
286 transient_con = next_transient;
289 if (!is_transient_for)
292 DLOG("Rendering floating child even though in fullscreen mode: "
293 "floating->transient_for (0x%08x) --> fullscreen->id (0x%08x)\n",
294 floating_child->window->transient_for, fullscreen->window->id);
297 DLOG("floating child at (%d,%d) with %d x %d\n",
298 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
300 render_con(child, false);
306 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
307 * get the height of their content and the remaining CT_CON gets the rest.
310 static void render_output(Con *con) {
311 Con *child, *dockchild;
315 int height = con->rect.height;
317 /* Find the content container and ensure that there is exactly one. Also
318 * check for any non-CT_DOCKAREA clients. */
320 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
321 if (child->type == CT_CON) {
322 if (content != NULL) {
323 DLOG("More than one CT_CON on output container\n");
327 } else if (child->type != CT_DOCKAREA) {
328 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
333 if (content == NULL) {
334 DLOG("Skipping this output because it is currently being destroyed.\n");
338 /* We need to find out if there is a fullscreen con on the current workspace
339 * and take the short-cut to render it directly (the user does not want to
340 * see the dockareas in that case) */
341 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
343 DLOG("Skipping this output because it is currently being destroyed.\n");
346 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
348 fullscreen->rect = con->rect;
349 x_raise_con(fullscreen);
350 render_con(fullscreen, true);
354 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
355 * children) and figure out how many pixels we have left for the rest */
356 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
357 if (child->type != CT_DOCKAREA)
360 child->rect.height = 0;
361 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
362 child->rect.height += dockchild->geometry.height;
364 height -= child->rect.height;
367 /* Second pass: Set the widths/heights */
368 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
369 if (child->type == CT_CON) {
372 child->rect.width = con->rect.width;
373 child->rect.height = height;
378 child->rect.width = con->rect.width;
380 child->deco_rect.x = 0;
381 child->deco_rect.y = 0;
382 child->deco_rect.width = 0;
383 child->deco_rect.height = 0;
385 y += child->rect.height;
387 DLOG("child at (%d, %d) with (%d x %d)\n",
388 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
390 render_con(child, false);
394 static void render_con_split(Con *con, Con *child, render_params *p, int i) {
395 assert(con->layout == L_SPLITH || con->layout == L_SPLITV);
397 if (con->layout == L_SPLITH) {
398 child->rect.x = p->x;
399 child->rect.y = p->y;
400 child->rect.width = p->sizes[i];
401 child->rect.height = p->rect.height;
402 p->x += child->rect.width;
404 child->rect.x = p->x;
405 child->rect.y = p->y;
406 child->rect.width = p->rect.width;
407 child->rect.height = p->sizes[i];
408 p->y += child->rect.height;
411 /* first we have the decoration, if this is a leaf node */
412 if (con_is_leaf(child)) {
413 if (child->border_style == BS_NORMAL) {
414 /* TODO: make a function for relative coords? */
415 child->deco_rect.x = child->rect.x - con->rect.x;
416 child->deco_rect.y = child->rect.y - con->rect.y;
418 child->rect.y += p->deco_height;
419 child->rect.height -= p->deco_height;
421 child->deco_rect.width = child->rect.width;
422 child->deco_rect.height = p->deco_height;
424 child->deco_rect.x = 0;
425 child->deco_rect.y = 0;
426 child->deco_rect.width = 0;
427 child->deco_rect.height = 0;
432 static void render_con_stacked(Con *con, Con *child, render_params *p, int i) {
433 assert(con->layout == L_STACKED);
435 child->rect.x = p->x;
436 child->rect.y = p->y;
437 child->rect.width = p->rect.width;
438 child->rect.height = p->rect.height;
440 child->deco_rect.x = p->x - con->rect.x;
441 child->deco_rect.y = p->y - con->rect.y + (i * p->deco_height);
442 child->deco_rect.width = child->rect.width;
443 child->deco_rect.height = p->deco_height;
445 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
446 child->rect.y += (p->deco_height * p->children);
447 child->rect.height -= (p->deco_height * p->children);
451 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i) {
452 assert(con->layout == L_TABBED);
454 child->rect.x = p->x;
455 child->rect.y = p->y;
456 child->rect.width = p->rect.width;
457 child->rect.height = p->rect.height;
459 child->deco_rect.width = floor((float)child->rect.width / p->children);
460 child->deco_rect.x = p->x - con->rect.x + i * child->deco_rect.width;
461 child->deco_rect.y = p->y - con->rect.y;
463 /* Since the tab width may be something like 31,6 px per tab, we
464 * let the last tab have all the extra space (0,6 * children). */
465 if (i == (p->children - 1)) {
466 child->deco_rect.width += (child->rect.width - (child->deco_rect.x + child->deco_rect.width));
469 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
470 child->rect.y += p->deco_height;
471 child->rect.height -= p->deco_height;
472 child->deco_rect.height = p->deco_height;
474 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
478 static void render_con_dockarea(Con *con, Con *child, render_params *p) {
479 assert(con->layout == L_DOCKAREA);
481 child->rect.x = p->x;
482 child->rect.y = p->y;
483 child->rect.width = p->rect.width;
484 child->rect.height = child->geometry.height;
486 child->deco_rect.x = 0;
487 child->deco_rect.y = 0;
488 child->deco_rect.width = 0;
489 child->deco_rect.height = 0;
490 p->y += child->rect.height;