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