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