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