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