2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 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 /* Forward declarations */
14 static int *precalculate_sizes(Con *con, render_params *p);
15 static void render_root(Con *con, Con *fullscreen);
16 static void render_output(Con *con);
17 static void render_con_split(Con *con, Con *child, render_params *p, int i);
18 static void render_con_stacked(Con *con, Con *child, render_params *p, int i);
19 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i);
20 static void render_con_dockarea(Con *con, Con *child, render_params *p);
23 * Returns the height for the decorations
25 int render_deco_height(void) {
26 int deco_height = config.font.height + 4;
27 if (config.font.height & 0x01)
33 * "Renders" the given container (and its children), meaning that all rects are
34 * updated correctly. Note that this function does not call any xcb_*
35 * functions, so the changes are completely done in memory only (and
36 * side-effect free). As soon as you call x_push_changes(), the changes will be
40 void render_con(Con *con) {
41 render_params params = {
45 .children = con_num_children(con)};
47 DLOG("Rendering node %p / %s / layout %d / children %d\n", con, con->name,
48 con->layout, params.children);
53 /* if this container contains a window, set the coordinates */
55 /* depending on the border style, the rect of the child window
56 * needs to be smaller */
57 Rect *inset = &(con->window_rect);
58 *inset = (Rect){0, 0, con->rect.width, con->rect.height};
59 if (con->fullscreen_mode == CF_NONE) {
60 *inset = rect_add(*inset, con_border_style_rect(con));
64 inset->width -= (2 * con->border_width);
65 inset->height -= (2 * con->border_width);
67 /* NB: We used to respect resize increment size hints for tiling
68 * windows up until commit 0db93d9 here. However, since all terminal
69 * emulators cope with ignoring the size hints in a better way than we
70 * can (by providing their fake-transparency or background color), this
71 * code was removed. See also https://bugs.i3wm.org/540 */
73 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
76 /* Check for fullscreen nodes */
77 Con *fullscreen = NULL;
78 if (con->type != CT_OUTPUT) {
79 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
82 fullscreen->rect = params.rect;
83 x_raise_con(fullscreen);
84 render_con(fullscreen);
85 /* Fullscreen containers are either global (underneath the CT_ROOT
86 * container) or per-output (underneath the CT_CONTENT container). For
87 * global fullscreen containers, we cannot abort rendering here yet,
88 * because the floating windows (with popup_during_fullscreen smart)
89 * have not yet been rendered (see the CT_ROOT code path below). See
90 * also https://bugs.i3wm.org/1393 */
91 if (con->type != CT_ROOT) {
96 /* find the height for the decorations */
97 params.deco_height = render_deco_height();
99 /* precalculate the sizes to be able to correct rounding errors */
100 params.sizes = precalculate_sizes(con, ¶ms);
102 if (con->layout == L_OUTPUT) {
103 /* Skip i3-internal outputs */
104 if (con_is_internal(con))
107 } else if (con->type == CT_ROOT) {
108 render_root(con, fullscreen);
111 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
112 assert(params.children > 0);
114 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
115 render_con_split(con, child, ¶ms, i);
116 } else if (con->layout == L_STACKED) {
117 render_con_stacked(con, child, ¶ms, i);
118 } else if (con->layout == L_TABBED) {
119 render_con_tabbed(con, child, ¶ms, i);
120 } else if (con->layout == L_DOCKAREA) {
121 render_con_dockarea(con, child, ¶ms);
124 DLOG("child at (%d, %d) with (%d x %d)\n",
125 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
131 /* in a stacking or tabbed container, we ensure the focused client is raised */
132 if (con->layout == L_STACKED || con->layout == L_TABBED) {
133 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
135 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
136 /* By rendering the stacked container again, we handle the case
137 * that we have a non-leaf-container inside the stack. In that
138 * case, the children of the non-leaf-container need to be
143 if (params.children != 1)
144 /* Raise the stack con itself. This will put the stack
145 * decoration on top of every stack window. That way, when a
146 * new window is opened in the stack, the old window will not
147 * obscure part of the decoration (it’s unmapped afterwards). */
156 static int *precalculate_sizes(Con *con, render_params *p) {
157 if ((con->layout != L_SPLITH && con->layout != L_SPLITV) || p->children <= 0) {
161 int *sizes = smalloc(p->children * sizeof(int));
162 assert(!TAILQ_EMPTY(&con->nodes_head));
165 int i = 0, assigned = 0;
166 int total = con_rect_size_in_orientation(con);
167 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
168 double percentage = child->percent > 0.0 ? child->percent : 1.0 / p->children;
169 assigned += sizes[i++] = lround(percentage * total);
171 assert(assigned == total ||
172 (assigned > total && assigned - total <= p->children * 2) ||
173 (assigned < total && total - assigned <= p->children * 2));
174 int signal = assigned < total ? 1 : -1;
175 while (assigned != total) {
176 for (i = 0; i < p->children && assigned != total; ++i) {
185 static void render_root(Con *con, Con *fullscreen) {
188 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
193 /* We need to render floating windows after rendering all outputs’
194 * tiling windows because they need to be on top of *every* output at
195 * all times. This is important when the user places floating
196 * windows/containers so that they overlap on another output. */
197 DLOG("Rendering floating windows:\n");
198 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
199 if (con_is_internal(output))
201 /* Get the active workspace of that output */
202 Con *content = output_get_content(output);
203 if (!content || TAILQ_EMPTY(&(content->focus_head))) {
204 DLOG("Skipping this output because it is currently being destroyed.\n");
207 Con *workspace = TAILQ_FIRST(&(content->focus_head));
208 Con *fullscreen = con_get_fullscreen_covering_ws(workspace);
210 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
211 if (fullscreen != NULL) {
212 /* Don’t render floating windows when there is a fullscreen
213 * window on that workspace. Necessary to make floating
214 * fullscreen work correctly (ticket #564). Exception to the
215 * above rule: smart popup_during_fullscreen handling (popups
216 * belonging to the fullscreen app will be rendered). */
217 if (config.popup_during_fullscreen != PDF_SMART || fullscreen->window == NULL) {
221 Con *floating_child = con_descend_focused(child);
222 Con *transient_con = floating_child;
223 bool is_transient_for = false;
224 while (transient_con != NULL &&
225 transient_con->window != NULL &&
226 transient_con->window->transient_for != XCB_NONE) {
227 DLOG("transient_con = 0x%08x, transient_con->window->transient_for = 0x%08x, fullscreen_id = 0x%08x\n",
228 transient_con->window->id, transient_con->window->transient_for, fullscreen->window->id);
229 if (transient_con->window->transient_for == fullscreen->window->id) {
230 is_transient_for = true;
233 Con *next_transient = con_by_window_id(transient_con->window->transient_for);
234 if (next_transient == NULL)
236 /* Some clients (e.g. x11-ssh-askpass) actually set
237 * WM_TRANSIENT_FOR to their own window id, so break instead of
238 * looping endlessly. */
239 if (transient_con == next_transient)
241 transient_con = next_transient;
244 if (!is_transient_for)
247 DLOG("Rendering floating child even though in fullscreen mode: "
248 "floating->transient_for (0x%08x) --> fullscreen->id (0x%08x)\n",
249 floating_child->window->transient_for, fullscreen->window->id);
252 DLOG("floating child at (%d,%d) with %d x %d\n",
253 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
261 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
262 * get the height of their content and the remaining CT_CON gets the rest.
265 static void render_output(Con *con) {
266 Con *child, *dockchild;
270 int height = con->rect.height;
272 /* Find the content container and ensure that there is exactly one. Also
273 * check for any non-CT_DOCKAREA clients. */
275 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
276 if (child->type == CT_CON) {
277 if (content != NULL) {
278 DLOG("More than one CT_CON on output container\n");
282 } else if (child->type != CT_DOCKAREA) {
283 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
288 if (content == NULL) {
289 DLOG("Skipping this output because it is currently being destroyed.\n");
293 /* We need to find out if there is a fullscreen con on the current workspace
294 * and take the short-cut to render it directly (the user does not want to
295 * see the dockareas in that case) */
296 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
298 DLOG("Skipping this output because it is currently being destroyed.\n");
301 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
303 fullscreen->rect = con->rect;
304 x_raise_con(fullscreen);
305 render_con(fullscreen);
309 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
310 * children) and figure out how many pixels we have left for the rest */
311 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
312 if (child->type != CT_DOCKAREA)
315 child->rect.height = 0;
316 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
317 child->rect.height += dockchild->geometry.height;
319 height -= child->rect.height;
322 /* Second pass: Set the widths/heights */
323 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
324 if (child->type == CT_CON) {
327 child->rect.width = con->rect.width;
328 child->rect.height = height;
333 child->rect.width = con->rect.width;
335 child->deco_rect.x = 0;
336 child->deco_rect.y = 0;
337 child->deco_rect.width = 0;
338 child->deco_rect.height = 0;
340 y += child->rect.height;
342 DLOG("child at (%d, %d) with (%d x %d)\n",
343 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
349 static void render_con_split(Con *con, Con *child, render_params *p, int i) {
350 assert(con->layout == L_SPLITH || con->layout == L_SPLITV);
352 if (con->layout == L_SPLITH) {
353 child->rect.x = p->x;
354 child->rect.y = p->y;
355 child->rect.width = p->sizes[i];
356 child->rect.height = p->rect.height;
357 p->x += child->rect.width;
359 child->rect.x = p->x;
360 child->rect.y = p->y;
361 child->rect.width = p->rect.width;
362 child->rect.height = p->sizes[i];
363 p->y += child->rect.height;
366 /* first we have the decoration, if this is a leaf node */
367 if (con_is_leaf(child)) {
368 if (child->border_style == BS_NORMAL) {
369 /* TODO: make a function for relative coords? */
370 child->deco_rect.x = child->rect.x - con->rect.x;
371 child->deco_rect.y = child->rect.y - con->rect.y;
373 child->rect.y += p->deco_height;
374 child->rect.height -= p->deco_height;
376 child->deco_rect.width = child->rect.width;
377 child->deco_rect.height = p->deco_height;
379 child->deco_rect.x = 0;
380 child->deco_rect.y = 0;
381 child->deco_rect.width = 0;
382 child->deco_rect.height = 0;
387 static void render_con_stacked(Con *con, Con *child, render_params *p, int i) {
388 assert(con->layout == L_STACKED);
390 child->rect.x = p->x;
391 child->rect.y = p->y;
392 child->rect.width = p->rect.width;
393 child->rect.height = p->rect.height;
395 child->deco_rect.x = p->x - con->rect.x;
396 child->deco_rect.y = p->y - con->rect.y + (i * p->deco_height);
397 child->deco_rect.width = child->rect.width;
398 child->deco_rect.height = p->deco_height;
400 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
401 child->rect.y += (p->deco_height * p->children);
402 child->rect.height -= (p->deco_height * p->children);
406 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i) {
407 assert(con->layout == L_TABBED);
409 child->rect.x = p->x;
410 child->rect.y = p->y;
411 child->rect.width = p->rect.width;
412 child->rect.height = p->rect.height;
414 child->deco_rect.width = floor((float)child->rect.width / p->children);
415 child->deco_rect.x = p->x - con->rect.x + i * child->deco_rect.width;
416 child->deco_rect.y = p->y - con->rect.y;
418 /* Since the tab width may be something like 31,6 px per tab, we
419 * let the last tab have all the extra space (0,6 * children). */
420 if (i == (p->children - 1)) {
421 child->deco_rect.width += (child->rect.width - (child->deco_rect.x + child->deco_rect.width));
424 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
425 child->rect.y += p->deco_height;
426 child->rect.height -= p->deco_height;
427 child->deco_rect.height = p->deco_height;
429 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
433 static void render_con_dockarea(Con *con, Con *child, render_params *p) {
434 assert(con->layout == L_DOCKAREA);
436 child->rect.x = p->x;
437 child->rect.y = p->y;
438 child->rect.width = p->rect.width;
439 child->rect.height = child->geometry.height;
441 child->deco_rect.x = 0;
442 child->deco_rect.y = 0;
443 child->deco_rect.width = 0;
444 child->deco_rect.height = 0;
445 p->y += child->rect.height;