i3
render.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
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.
9 *
10 */
11#include "all.h"
12
13#include <math.h>
14
15/* Forward declarations */
16static int *precalculate_sizes(Con *con, render_params *p);
17static void render_root(Con *con, Con *fullscreen);
18static void render_output(Con *con);
19static void render_con_split(Con *con, Con *child, render_params *p, int i);
20static void render_con_stacked(Con *con, Con *child, render_params *p, int i);
21static void render_con_tabbed(Con *con, Con *child, render_params *p, int i);
22static void render_con_dockarea(Con *con, Con *child, render_params *p);
23
24/*
25 * Returns the height for the decorations
26 */
28 int deco_height = config.font.height + 4;
29 if (config.font.height & 0x01) {
30 ++deco_height;
31 }
32 return deco_height;
33}
34
35/*
36 * "Renders" the given container (and its children), meaning that all rects are
37 * updated correctly. Note that this function does not call any xcb_*
38 * functions, so the changes are completely done in memory only (and
39 * side-effect free). As soon as you call x_push_changes(), the changes will be
40 * updated in X11.
41 *
42 */
43void render_con(Con *con) {
44 render_params params = {
45 .rect = con->rect,
46 .x = con->rect.x,
47 .y = con->rect.y,
48 .children = con_num_children(con)};
49
50 DLOG("Rendering node %p / %s / layout %d / children %d\n", con, con->name,
51 con->layout, params.children);
52
53 if (con->type == CT_WORKSPACE) {
55 Rect inset = (Rect){
56 gaps.left,
57 gaps.top,
58 -(gaps.left + gaps.right),
59 -(gaps.top + gaps.bottom),
60 };
61 con->rect = rect_add(con->rect, inset);
62 params.rect = rect_add(params.rect, inset);
63 params.x += gaps.left;
64 params.y += gaps.top;
65 }
66
67 if (gaps_should_inset_con(con, params.children)) {
69 Rect inset = (Rect){
70 gaps_has_adjacent_container(con, D_LEFT) ? gaps.inner / 2 : gaps.inner,
71 gaps_has_adjacent_container(con, D_UP) ? gaps.inner / 2 : gaps.inner,
72 gaps_has_adjacent_container(con, D_RIGHT) ? -(gaps.inner / 2) : -gaps.inner,
73 gaps_has_adjacent_container(con, D_DOWN) ? -(gaps.inner / 2) : -gaps.inner,
74 };
75 inset.width -= inset.x;
76 inset.height -= inset.y;
77
78 if (con->fullscreen_mode == CF_NONE) {
79 params.rect = rect_add(params.rect, inset);
80 con->rect = rect_add(con->rect, inset);
81 }
82 inset.height = 0;
83
84 params.x = con->rect.x;
85 params.y = con->rect.y;
86 }
87
88 int i = 0;
89 con->mapped = true;
90
91 /* if this container contains a window, set the coordinates */
92 if (con->window) {
93 /* depending on the border style, the rect of the child window
94 * needs to be smaller */
95 Rect inset = (Rect){
96 .x = 0,
97 .y = 0,
98 .width = con->rect.width,
99 .height = con->rect.height,
100 };
101 if (con->fullscreen_mode == CF_NONE) {
102 DLOG("deco_rect.height = %d\n", con->deco_rect.height);
103 Rect bsr = con_border_style_rect(con);
104 DLOG("bsr at %dx%d with size %dx%d\n",
105 bsr.x, bsr.y, bsr.width, bsr.height);
106
107 inset = rect_add(inset, bsr);
108 }
109
110 /* Obey x11 border */
111 inset.width -= (2 * con->border_width);
112 inset.height -= (2 * con->border_width);
113
114 inset = rect_sanitize_dimensions(inset);
115 con->window_rect = inset;
116
117 /* NB: We used to respect resize increment size hints for tiling
118 * windows up until commit 0db93d9 here. However, since all terminal
119 * emulators cope with ignoring the size hints in a better way than we
120 * can (by providing their fake-transparency or background color), this
121 * code was removed. See also https://bugs.i3wm.org/540 */
122
123 DLOG("child will be at %dx%d with size %dx%d\n",
124 inset.x, inset.y, inset.width, inset.height);
125 }
126
127 /* Check for fullscreen nodes */
128 Con *fullscreen = NULL;
129 if (con->type != CT_OUTPUT) {
130 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
131 }
132 if (fullscreen) {
133 fullscreen->rect = params.rect;
134 x_raise_con(fullscreen);
135 render_con(fullscreen);
136 /* Fullscreen containers are either global (underneath the CT_ROOT
137 * container) or per-output (underneath the CT_CONTENT container). For
138 * global fullscreen containers, we cannot abort rendering here yet,
139 * because the floating windows (with popup_during_fullscreen smart)
140 * have not yet been rendered (see the CT_ROOT code path below). See
141 * also https://bugs.i3wm.org/1393 */
142 if (con->type != CT_ROOT) {
143 return;
144 }
145 }
146
147 /* find the height for the decorations */
149
150 /* precalculate the sizes to be able to correct rounding errors */
151 params.sizes = precalculate_sizes(con, &params);
152
153 if (con->layout == L_OUTPUT) {
154 /* Skip i3-internal outputs */
155 if (con_is_internal(con)) {
156 goto free_params;
157 }
158 render_output(con);
159 } else if (con->type == CT_ROOT) {
160 render_root(con, fullscreen);
161 } else {
162 Con *child;
163 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
164 assert(params.children > 0);
165
166 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
167 render_con_split(con, child, &params, i);
168 } else if (con->layout == L_STACKED) {
169 render_con_stacked(con, child, &params, i);
170 } else if (con->layout == L_TABBED) {
171 render_con_tabbed(con, child, &params, i);
172 } else if (con->layout == L_DOCKAREA) {
173 render_con_dockarea(con, child, &params);
174 }
175
176 child->rect = rect_sanitize_dimensions(child->rect);
177
178 DLOG("child at (%d, %d) with (%d x %d)\n",
179 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
180 x_raise_con(child);
181 render_con(child);
182
183 /* render_con_split() sets the deco_rect width based on the rect
184 * width, but the render_con() call updates the rect width by
185 * applying gaps, so we need to update deco_rect. */
186 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
187 if (con_is_leaf(child)) {
188 if (child->border_style == BS_NORMAL) {
189 child->deco_rect.width = child->rect.width;
190 }
191 }
192 }
193
194 i++;
195 }
196
197 /* in a stacking or tabbed container, we ensure the focused client is raised */
198 if (con->layout == L_STACKED || con->layout == L_TABBED) {
199 TAILQ_FOREACH_REVERSE (child, &(con->focus_head), focus_head, focused) {
200 x_raise_con(child);
201 }
202 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
203 /* By rendering the stacked container again, we handle the case
204 * that we have a non-leaf-container inside the stack. In that
205 * case, the children of the non-leaf-container need to be
206 * raised as well. */
207 render_con(child);
208 }
209
210 if (params.children != 1) {
211 /* Raise the stack con itself. This will put the stack
212 * decoration on top of every stack window. That way, when a
213 * new window is opened in the stack, the old window will not
214 * obscure part of the decoration (it’s unmapped afterwards). */
215 x_raise_con(con);
216 }
217 }
218 }
219
220free_params:
221 FREE(params.sizes);
222}
223
224static int *precalculate_sizes(Con *con, render_params *p) {
225 if ((con->layout != L_SPLITH && con->layout != L_SPLITV) || p->children <= 0) {
226 return NULL;
227 }
228
229 int *sizes = smalloc(p->children * sizeof(int));
230 assert(!TAILQ_EMPTY(&con->nodes_head));
231
232 Con *child;
233 int i = 0, assigned = 0;
234 int total = con_rect_size_in_orientation(con);
235 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
236 double percentage = child->percent > 0.0 ? child->percent : 1.0 / p->children;
237 assigned += sizes[i++] = lround(percentage * total);
238 }
239 assert(assigned == total ||
240 (assigned > total && assigned - total <= p->children * 2) ||
241 (assigned < total && total - assigned <= p->children * 2));
242 int signal = assigned < total ? 1 : -1;
243 while (assigned != total) {
244 for (i = 0; i < p->children && assigned != total; ++i) {
245 sizes[i] += signal;
246 assigned += signal;
247 }
248 }
249
250 return sizes;
251}
252
253static bool fullscreen_blocks_floating_render(Con *fullscreen, Con *floating) {
254 if (fullscreen == NULL) {
255 return false;
256 }
257 /* Don’t render floating windows when there is a fullscreen window on that
258 * workspace. Necessary to make floating fullscreen work correctly (ticket
259 * #564). Exception to the above rule: popup_during_fullscreen smart|all. */
261 case PDF_LEAVE_FULLSCREEN:
262 case PDF_IGNORE:
263 return true;
264 case PDF_SMART:
265 return fullscreen->window == NULL ||
267 case PDF_ALL:
268 return con_has_parent(fullscreen, floating);
269 }
270 return false; /* not reachable */
271}
272
273static void render_root(Con *con, Con *fullscreen) {
274 Con *output;
275 if (!fullscreen) {
276 TAILQ_FOREACH (output, &(con->nodes_head), nodes) {
277 render_con(output);
278 }
279 }
280
281 /* We need to render floating windows after rendering all outputs’
282 * tiling windows because they need to be on top of *every* output at
283 * all times. This is important when the user places floating
284 * windows/containers so that they overlap on another output. */
285 DLOG("Rendering floating windows:\n");
286 TAILQ_FOREACH (output, &(con->nodes_head), nodes) {
287 if (con_is_internal(output)) {
288 continue;
289 }
290 /* Get the active workspace of that output */
291 Con *content = output_get_content(output);
292 if (!content || TAILQ_EMPTY(&(content->focus_head))) {
293 DLOG("Skipping this output because it is currently being destroyed.\n");
294 continue;
295 }
296 Con *workspace = TAILQ_FIRST(&(content->focus_head));
297 Con *fullscreen = con_get_fullscreen_covering_ws(workspace);
298 Con *child;
299 TAILQ_FOREACH (child, &(workspace->floating_head), floating_windows) {
300 if (fullscreen_blocks_floating_render(fullscreen, child)) {
301 continue;
302 }
303 DLOG("floating child at (%d,%d) with %d x %d\n",
304 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
305 x_raise_con(child);
306 render_con(child);
307 }
308 }
309}
310
311/*
312 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
313 * get the height of their content and the remaining CT_CON gets the rest.
314 *
315 */
316static void render_output(Con *con) {
317 Con *child, *dockchild;
318
319 int x = con->rect.x;
320 int y = con->rect.y;
321 int height = con->rect.height;
322
323 /* Find the content container and ensure that there is exactly one. Also
324 * check for any non-CT_DOCKAREA clients. */
325 Con *content = NULL;
326 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
327 if (child->type == CT_CON) {
328 if (content != NULL) {
329 DLOG("More than one CT_CON on output container\n");
330 assert(false);
331 }
332 content = child;
333 } else if (child->type != CT_DOCKAREA) {
334 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
335 assert(false);
336 }
337 }
338
339 if (content == NULL) {
340 DLOG("Skipping this output because it is currently being destroyed.\n");
341 return;
342 }
343
344 /* We need to find out if there is a fullscreen con on the current workspace
345 * and take the short-cut to render it directly (the user does not want to
346 * see the dockareas in that case) */
347 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
348 if (!ws) {
349 DLOG("Skipping this output because it is currently being destroyed.\n");
350 return;
351 }
352 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
353 if (fullscreen) {
354 fullscreen->rect = con->rect;
355 x_raise_con(fullscreen);
356 render_con(fullscreen);
357 return;
358 }
359
360 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
361 * children) and figure out how many pixels we have left for the rest */
362 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
363 if (child->type != CT_DOCKAREA) {
364 continue;
365 }
366
367 child->rect.height = 0;
368 TAILQ_FOREACH (dockchild, &(child->nodes_head), nodes) {
369 child->rect.height += dockchild->geometry.height;
370 }
371
372 height -= child->rect.height;
373 }
374
375 /* Second pass: Set the widths/heights */
376 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
377 if (child->type == CT_CON) {
378 child->rect.x = x;
379 child->rect.y = y;
380 child->rect.width = con->rect.width;
381 child->rect.height = height;
382 }
383
384 child->rect.x = x;
385 child->rect.y = y;
386 child->rect.width = con->rect.width;
387
388 child->deco_rect.x = 0;
389 child->deco_rect.y = 0;
390 child->deco_rect.width = 0;
391 child->deco_rect.height = 0;
392
393 y += child->rect.height;
394
395 DLOG("child at (%d, %d) with (%d x %d)\n",
396 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
397 x_raise_con(child);
398 render_con(child);
399 }
400}
401
402static void render_con_split(Con *con, Con *child, render_params *p, int i) {
403 assert(con->layout == L_SPLITH || con->layout == L_SPLITV);
404
405 if (con->layout == L_SPLITH) {
406 child->rect.x = p->x;
407 child->rect.y = p->y;
408 child->rect.width = p->sizes[i];
409 child->rect.height = p->rect.height;
410 p->x += child->rect.width;
411 } else {
412 child->rect.x = p->x;
413 child->rect.y = p->y;
414 child->rect.width = p->rect.width;
415 child->rect.height = p->sizes[i];
416 p->y += child->rect.height;
417 }
418
419 /* first we have the decoration, if this is a leaf node */
420 if (con_is_leaf(child)) {
421 if (child->border_style == BS_NORMAL) {
422 /* TODO: make a function for relative coords? */
423 child->deco_rect.x = 0;
424 child->deco_rect.y = 0;
425
426 child->deco_rect.width = child->rect.width;
427 child->deco_rect.height = p->deco_height;
428 } else {
429 child->deco_rect.x = 0;
430 child->deco_rect.y = 0;
431 child->deco_rect.width = 0;
432 child->deco_rect.height = 0;
433 }
434 }
435}
436
437static void render_con_stacked(Con *con, Con *child, render_params *p, int i) {
438 assert(con->layout == L_STACKED);
439
440 child->rect.x = p->x;
441 child->rect.y = p->y;
442 child->rect.width = p->rect.width;
443 child->rect.height = p->rect.height;
444
445 child->deco_rect.x = p->x - con->rect.x;
446 child->deco_rect.y = p->y - con->rect.y + (i * p->deco_height);
447 child->deco_rect.width = child->rect.width;
448 child->deco_rect.height = p->deco_height;
449
450 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
451 child->rect.y += (p->deco_height * p->children);
452 child->rect.height -= (p->deco_height * p->children);
453 }
454}
455
456static void render_con_tabbed(Con *con, Con *child, render_params *p, int i) {
457 assert(con->layout == L_TABBED);
458
459 child->rect.x = p->x;
460 child->rect.y = p->y;
461 child->rect.width = p->rect.width;
462 child->rect.height = p->rect.height;
463
464 child->deco_rect.width = floor((float)child->rect.width / p->children);
465 child->deco_rect.x = p->x - con->rect.x + i * child->deco_rect.width;
466 child->deco_rect.y = p->y - con->rect.y;
467
468 /* Since the tab width may be something like 31,6 px per tab, we
469 * let the last tab have all the extra space (0,6 * children). */
470 if (i == (p->children - 1)) {
471 child->deco_rect.width = child->rect.width - child->deco_rect.x;
472 }
473
474 if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
475 child->rect.y += p->deco_height;
476 child->rect.height -= p->deco_height;
477 child->deco_rect.height = p->deco_height;
478 } else {
479 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
480 }
481}
482
483static void render_con_dockarea(Con *con, Con *child, render_params *p) {
484 assert(con->layout == L_DOCKAREA);
485
486 child->rect.x = p->x;
487 child->rect.y = p->y;
488 child->rect.width = p->rect.width;
489 child->rect.height = child->geometry.height;
490
491 child->deco_rect.x = 0;
492 child->deco_rect.y = 0;
493 child->deco_rect.width = 0;
494 child->deco_rect.height = 0;
495 p->y += child->rect.height;
496}
#define y(x,...)
Definition commands.c:18
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
Definition con.c:599
bool con_find_transient_for_window(Con *start, xcb_window_t target)
Start from a container and traverse the transient_for linked list.
Definition con.c:824
Rect con_border_style_rect(Con *con)
Returns a "relative" Rect which contains the amount of pixels that need to be added to the original R...
Definition con.c:1887
Con * con_get_fullscreen_covering_ws(Con *ws)
Returns the fullscreen node that covers the given workspace if it exists.
Definition con.c:647
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition con.c:2707
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:662
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition con.c:366
int con_num_children(Con *con)
Returns the number of children of this container.
Definition con.c:1075
bool con_has_parent(Con *con, Con *parent)
Checks if the container has the given parent as an actual parent.
Definition con.c:734
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1698
Config config
Definition config.c:19
gaps_t calculate_effective_gaps(Con *con)
Calculates the effective gap sizes for a container.
Definition gaps.c:16
bool gaps_has_adjacent_container(Con *con, direction_t direction)
Definition gaps.c:92
bool gaps_should_inset_con(Con *con, int children)
Definition gaps.c:51
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
void render_con(Con *con)
"Renders" the given container (and its children), meaning that all rects are updated correctly.
Definition render.c:43
int render_deco_height(void)
Returns the height for the decorations.
Definition render.c:27
static bool fullscreen_blocks_floating_render(Con *fullscreen, Con *floating)
Definition render.c:253
static void render_con_stacked(Con *con, Con *child, render_params *p, int i)
Definition render.c:437
static void render_output(Con *con)
Definition render.c:316
static void render_root(Con *con, Con *fullscreen)
Definition render.c:273
static void render_con_dockarea(Con *con, Con *child, render_params *p)
Definition render.c:483
static int * precalculate_sizes(Con *con, render_params *p)
Definition render.c:224
static void render_con_split(Con *con, Con *child, render_params *p, int i)
Definition render.c:402
static void render_con_tabbed(Con *con, Con *child, render_params *p, int i)
Definition render.c:456
struct Con * focused
Definition tree.c:13
Rect rect_add(Rect a, Rect b)
Definition util.c:39
Rect rect_sanitize_dimensions(Rect rect)
Definition util.c:53
void x_raise_con(Con *con)
Raises the specified container in the internal stack of X windows.
Definition x.c:1483
struct Rect Rect
Definition data.h:44
@ L_STACKED
Definition data.h:103
@ L_TABBED
Definition data.h:104
@ L_DOCKAREA
Definition data.h:105
@ L_OUTPUT
Definition data.h:106
@ L_SPLITH
Definition data.h:108
@ L_SPLITV
Definition data.h:107
@ CF_OUTPUT
Definition data.h:630
@ CF_GLOBAL
Definition data.h:631
@ CF_NONE
Definition data.h:629
@ BS_NONE
Definition data.h:66
@ BS_PIXEL
Definition data.h:67
@ BS_NORMAL
Definition data.h:68
@ D_RIGHT
Definition data.h:57
@ D_LEFT
Definition data.h:56
@ D_UP
Definition data.h:58
@ D_DOWN
Definition data.h:59
#define DLOG(fmt,...)
Definition libi3.h:105
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
Definition queue.h:352
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define FREE(pointer)
Definition util.h:47
enum Config::@6 popup_during_fullscreen
What should happen when a new popup is opened during fullscreen mode.
i3Font font
Definition data.h:146
int inner
Definition data.h:147
int left
Definition data.h:151
int right
Definition data.h:149
int top
Definition data.h:148
int bottom
Definition data.h:150
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:185
uint32_t height
Definition data.h:189
uint32_t x
Definition data.h:186
uint32_t y
Definition data.h:187
uint32_t width
Definition data.h:188
xcb_window_t id
Definition data.h:425
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Rect deco_rect
Definition data.h:688
enum Con::@18 type
int border_width
Definition data.h:715
double percent
Definition data.h:712
struct Rect rect
Definition data.h:682
layout_t layout
Definition data.h:755
bool mapped
Definition data.h:644
struct Rect window_rect
Definition data.h:685
struct Window * window
Definition data.h:718
border_style_t border_style
Definition data.h:757
char * name
Definition data.h:692
struct Rect geometry
the geometry this window requested when getting mapped
Definition data.h:690
fullscreen_mode_t fullscreen_mode
Definition data.h:734
int height
The height of the font, built from font_ascent + font_descent.
Definition libi3.h:68
This is used to keep a state to pass around when rendering a con in render_con().
Definition render.h:19
Rect rect
Definition render.h:28
int children
Definition render.h:30
int * sizes
Definition render.h:32
int deco_height
Definition render.h:25