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