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  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 */
101  params.deco_height = render_deco_height();
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 
159 free_params:
160  FREE(params.sizes);
161 }
162 
163 static 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 
192 static 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  if (con_find_transient_for_window(floating_child, fullscreen->window->id)) {
230  DLOG("Rendering floating child even though in fullscreen mode: "
231  "floating->transient_for (0x%08x) --> fullscreen->id (0x%08x)\n",
232  floating_child->window->transient_for, fullscreen->window->id);
233  } else {
234  continue;
235  }
236  }
237  DLOG("floating child at (%d,%d) with %d x %d\n",
238  child->rect.x, child->rect.y, child->rect.width, child->rect.height);
239  x_raise_con(child);
240  render_con(child);
241  }
242  }
243 }
244 
245 /*
246  * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
247  * get the height of their content and the remaining CT_CON gets the rest.
248  *
249  */
250 static void render_output(Con *con) {
251  Con *child, *dockchild;
252 
253  int x = con->rect.x;
254  int y = con->rect.y;
255  int height = con->rect.height;
256 
257  /* Find the content container and ensure that there is exactly one. Also
258  * check for any non-CT_DOCKAREA clients. */
259  Con *content = NULL;
260  TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
261  if (child->type == CT_CON) {
262  if (content != NULL) {
263  DLOG("More than one CT_CON on output container\n");
264  assert(false);
265  }
266  content = child;
267  } else if (child->type != CT_DOCKAREA) {
268  DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
269  assert(false);
270  }
271  }
272 
273  if (content == NULL) {
274  DLOG("Skipping this output because it is currently being destroyed.\n");
275  return;
276  }
277 
278  /* We need to find out if there is a fullscreen con on the current workspace
279  * and take the short-cut to render it directly (the user does not want to
280  * see the dockareas in that case) */
281  Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
282  if (!ws) {
283  DLOG("Skipping this output because it is currently being destroyed.\n");
284  return;
285  }
286  Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
287  if (fullscreen) {
288  fullscreen->rect = con->rect;
289  x_raise_con(fullscreen);
290  render_con(fullscreen);
291  return;
292  }
293 
294  /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
295  * children) and figure out how many pixels we have left for the rest */
296  TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
297  if (child->type != CT_DOCKAREA)
298  continue;
299 
300  child->rect.height = 0;
301  TAILQ_FOREACH (dockchild, &(child->nodes_head), nodes) {
302  child->rect.height += dockchild->geometry.height;
303  }
304 
305  height -= child->rect.height;
306  }
307 
308  /* Second pass: Set the widths/heights */
309  TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
310  if (child->type == CT_CON) {
311  child->rect.x = x;
312  child->rect.y = y;
313  child->rect.width = con->rect.width;
314  child->rect.height = height;
315  }
316 
317  child->rect.x = x;
318  child->rect.y = y;
319  child->rect.width = con->rect.width;
320 
321  child->deco_rect.x = 0;
322  child->deco_rect.y = 0;
323  child->deco_rect.width = 0;
324  child->deco_rect.height = 0;
325 
326  y += child->rect.height;
327 
328  DLOG("child at (%d, %d) with (%d x %d)\n",
329  child->rect.x, child->rect.y, child->rect.width, child->rect.height);
330  x_raise_con(child);
331  render_con(child);
332  }
333 }
334 
335 static void render_con_split(Con *con, Con *child, render_params *p, int i) {
336  assert(con->layout == L_SPLITH || con->layout == L_SPLITV);
337 
338  if (con->layout == L_SPLITH) {
339  child->rect.x = p->x;
340  child->rect.y = p->y;
341  child->rect.width = p->sizes[i];
342  child->rect.height = p->rect.height;
343  p->x += child->rect.width;
344  } else {
345  child->rect.x = p->x;
346  child->rect.y = p->y;
347  child->rect.width = p->rect.width;
348  child->rect.height = p->sizes[i];
349  p->y += child->rect.height;
350  }
351 
352  /* first we have the decoration, if this is a leaf node */
353  if (con_is_leaf(child)) {
354  if (child->border_style == BS_NORMAL) {
355  /* TODO: make a function for relative coords? */
356  child->deco_rect.x = child->rect.x - con->rect.x;
357  child->deco_rect.y = child->rect.y - con->rect.y;
358 
359  child->rect.y += p->deco_height;
360  child->rect.height -= p->deco_height;
361 
362  child->deco_rect.width = child->rect.width;
363  child->deco_rect.height = p->deco_height;
364  } else {
365  child->deco_rect.x = 0;
366  child->deco_rect.y = 0;
367  child->deco_rect.width = 0;
368  child->deco_rect.height = 0;
369  }
370  }
371 }
372 
373 static void render_con_stacked(Con *con, Con *child, render_params *p, int i) {
374  assert(con->layout == L_STACKED);
375 
376  child->rect.x = p->x;
377  child->rect.y = p->y;
378  child->rect.width = p->rect.width;
379  child->rect.height = p->rect.height;
380 
381  child->deco_rect.x = p->x - con->rect.x;
382  child->deco_rect.y = p->y - con->rect.y + (i * p->deco_height);
383  child->deco_rect.width = child->rect.width;
384  child->deco_rect.height = p->deco_height;
385 
386  if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
387  child->rect.y += (p->deco_height * p->children);
388  child->rect.height -= (p->deco_height * p->children);
389  }
390 }
391 
392 static void render_con_tabbed(Con *con, Con *child, render_params *p, int i) {
393  assert(con->layout == L_TABBED);
394 
395  child->rect.x = p->x;
396  child->rect.y = p->y;
397  child->rect.width = p->rect.width;
398  child->rect.height = p->rect.height;
399 
400  child->deco_rect.width = floor((float)child->rect.width / p->children);
401  child->deco_rect.x = p->x - con->rect.x + i * child->deco_rect.width;
402  child->deco_rect.y = p->y - con->rect.y;
403 
404  /* Since the tab width may be something like 31,6 px per tab, we
405  * let the last tab have all the extra space (0,6 * children). */
406  if (i == (p->children - 1)) {
407  child->deco_rect.width = child->rect.width - child->deco_rect.x;
408  }
409 
410  if (p->children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
411  child->rect.y += p->deco_height;
412  child->rect.height -= p->deco_height;
413  child->deco_rect.height = p->deco_height;
414  } else {
415  child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
416  }
417 }
418 
419 static void render_con_dockarea(Con *con, Con *child, render_params *p) {
420  assert(con->layout == L_DOCKAREA);
421 
422  child->rect.x = p->x;
423  child->rect.y = p->y;
424  child->rect.width = p->rect.width;
425  child->rect.height = child->geometry.height;
426 
427  child->deco_rect.x = 0;
428  child->deco_rect.y = 0;
429  child->deco_rect.width = 0;
430  child->deco_rect.height = 0;
431  p->y += child->rect.height;
432 }
#define y(x,...)
Definition: commands.c:18
Con * con_get_fullscreen_covering_ws(Con *ws)
Returns the fullscreen node that covers the given workspace if it exists.
Definition: con.c:573
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:739
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_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1587
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:1694
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition: con.c:2501
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:980
Config config
Definition: config.c:19
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: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:373
static void render_output(Con *con)
Definition: render.c:250
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:419
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:335
static void render_con_tabbed(Con *con, Con *child, render_params *p, int i)
Definition: render.c:392
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:1419
struct Rect Rect
Definition: data.h:44
@ L_STACKED
Definition: data.h:95
@ L_TABBED
Definition: data.h:96
@ L_DOCKAREA
Definition: data.h:97
@ L_OUTPUT
Definition: data.h:98
@ L_SPLITH
Definition: data.h:100
@ L_SPLITV
Definition: data.h:99
@ CF_OUTPUT
Definition: data.h:625
@ CF_GLOBAL
Definition: data.h:626
@ CF_NONE
Definition: data.h:624
@ BS_NONE
Definition: data.h:65
@ BS_PIXEL
Definition: data.h:66
@ BS_NORMAL
Definition: data.h:64
#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
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:176
uint32_t height
Definition: data.h:180
uint32_t x
Definition: data.h:177
uint32_t y
Definition: data.h:178
uint32_t width
Definition: data.h:179
xcb_window_t id
Definition: data.h:414
xcb_window_t transient_for
Definition: data.h:419
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:638
struct Rect deco_rect
Definition: data.h:680
enum Con::@18 type
int border_width
Definition: data.h:707
double percent
Definition: data.h:704
struct Rect rect
Definition: data.h:674
layout_t layout
Definition: data.h:747
bool mapped
Definition: data.h:639
struct Rect window_rect
Definition: data.h:677
struct Window * window
Definition: data.h:710
border_style_t border_style
Definition: data.h:748
char * name
Definition: data.h:684
struct Rect geometry
the geometry this window requested when getting mapped
Definition: data.h:682
fullscreen_mode_t fullscreen_mode
Definition: data.h:726
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