2 #define I3__FILE__ "render.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
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.
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;
20 * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
21 * get the height of their content and the remaining CT_CON gets the rest.
24 static void render_l_output(Con *con) {
25 Con *child, *dockchild;
29 int height = con->rect.height;
31 /* Find the content container and ensure that there is exactly one. Also
32 * check for any non-CT_DOCKAREA clients. */
34 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
35 if (child->type == CT_CON) {
36 if (content != NULL) {
37 DLOG("More than one CT_CON on output container\n");
41 } else if (child->type != CT_DOCKAREA) {
42 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
47 if (content == NULL) {
48 DLOG("Skipping this output because it is currently being destroyed.\n");
52 /* We need to find out if there is a fullscreen con on the current workspace
53 * and take the short-cut to render it directly (the user does not want to
54 * see the dockareas in that case) */
55 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
57 DLOG("Skipping this output because it is currently being destroyed.\n");
60 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
62 fullscreen->rect = con->rect;
63 x_raise_con(fullscreen);
64 render_con(fullscreen, true);
68 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
69 * children) and figure out how many pixels we have left for the rest */
70 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
71 if (child->type != CT_DOCKAREA)
74 child->rect.height = 0;
75 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
76 child->rect.height += dockchild->geometry.height;
78 height -= child->rect.height;
81 /* Second pass: Set the widths/heights */
82 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
83 if (child->type == CT_CON) {
86 child->rect.width = con->rect.width;
87 child->rect.height = height;
92 child->rect.width = con->rect.width;
94 child->deco_rect.x = 0;
95 child->deco_rect.y = 0;
96 child->deco_rect.width = 0;
97 child->deco_rect.height = 0;
99 y += child->rect.height;
101 DLOG("child at (%d, %d) with (%d x %d)\n",
102 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
104 render_con(child, false);
109 * "Renders" the given container (and its children), meaning that all rects are
110 * updated correctly. Note that this function does not call any xcb_*
111 * functions, so the changes are completely done in memory only (and
112 * side-effect free). As soon as you call x_push_changes(), the changes will be
116 void render_con(Con *con, bool render_fullscreen) {
117 int children = con_num_children(con);
118 DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
119 (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
122 /* Copy container rect, subtract container border */
123 /* This is the actually usable space inside this container for clients */
124 Rect rect = con->rect;
126 /* Display a border if this is a leaf node. For container nodes, we don’t
127 * draw borders (except when in debug mode) */
128 if (show_debug_borders) {
132 rect.height -= 2 * 2;
142 /* if this container contains a window, set the coordinates */
144 /* depending on the border style, the rect of the child window
145 * needs to be smaller */
146 Rect *inset = &(con->window_rect);
147 *inset = (Rect){0, 0, con->rect.width, con->rect.height};
148 if (!render_fullscreen)
149 *inset = rect_add(*inset, con_border_style_rect(con));
151 /* Obey x11 border */
152 inset->width -= (2 * con->border_width);
153 inset->height -= (2 * con->border_width);
155 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
157 * The spec isn’t explicit on whether the aspect ratio hints should be
158 * respected during fullscreen mode. Other WMs such as Openbox don’t do
159 * that, and this post suggests that this is the correct way to do it:
160 * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
162 * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
163 * subtitle rendering, see http://bugs.i3wm.org/594 */
164 if (!render_fullscreen &&
165 con->proportional_height != 0 &&
166 con->proportional_width != 0) {
167 double new_height = inset->height + 1;
168 int new_width = inset->width;
170 while (new_height > inset->height) {
171 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
173 if (new_height > inset->height)
176 /* Center the window */
177 inset->y += ceil(inset->height / 2) - floor(new_height / 2);
178 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
180 inset->height = new_height;
181 inset->width = new_width;
184 /* NB: We used to respect resize increment size hints for tiling
185 * windows up until commit 0db93d9 here. However, since all terminal
186 * emulators cope with ignoring the size hints in a better way than we
187 * can (by providing their fake-transparency or background color), this
188 * code was removed. See also http://bugs.i3wm.org/540 */
190 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
193 /* Check for fullscreen nodes */
194 Con *fullscreen = NULL;
195 if (con->type != CT_OUTPUT) {
196 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
199 fullscreen->rect = rect;
200 x_raise_con(fullscreen);
201 render_con(fullscreen, true);
205 /* find the height for the decorations */
206 int deco_height = config.font.height + 4;
207 if (config.font.height & 0x01)
210 /* precalculate the sizes to be able to correct rounding errors */
212 memset(sizes, 0, children*sizeof(int));
213 if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && children > 0) {
214 assert(!TAILQ_EMPTY(&con->nodes_head));
216 int i = 0, assigned = 0;
217 int total = con_orientation(con) == HORIZ ? rect.width : rect.height;
218 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
219 double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
220 assigned += sizes[i++] = percentage * total;
222 assert(assigned == total ||
223 (assigned > total && assigned - total <= children * 2) ||
224 (assigned < total && total - assigned <= children * 2));
225 int signal = assigned < total ? 1 : -1;
226 while (assigned != total) {
227 for (i = 0; i < children && assigned != total; ++i) {
234 if (con->layout == L_OUTPUT) {
235 /* Skip i3-internal outputs */
236 if (con_is_internal(con))
238 render_l_output(con);
239 } else if (con->type == CT_ROOT) {
241 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
242 render_con(output, false);
245 /* We need to render floating windows after rendering all outputs’
246 * tiling windows because they need to be on top of *every* output at
247 * all times. This is important when the user places floating
248 * windows/containers so that they overlap on another output. */
249 DLOG("Rendering floating windows:\n");
250 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
251 if (con_is_internal(output))
253 /* Get the active workspace of that output */
254 Con *content = output_get_content(output);
255 if (!content || TAILQ_EMPTY(&(content->focus_head))) {
256 DLOG("Skipping this output because it is currently being destroyed.\n");
259 Con *workspace = TAILQ_FIRST(&(content->focus_head));
260 Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
262 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
263 /* Don’t render floating windows when there is a fullscreen window
264 * on that workspace. Necessary to make floating fullscreen work
265 * correctly (ticket #564). */
266 if (fullscreen != NULL) {
267 Con *floating_child = con_descend_focused(child);
268 /* Exception to the above rule: smart
269 * popup_during_fullscreen handling (popups belonging to
270 * the fullscreen app will be rendered). */
271 if (floating_child->window == NULL ||
272 fullscreen->window == NULL ||
273 floating_child->window->transient_for != fullscreen->window->id)
276 DLOG("Rendering floating child even though in fullscreen mode: "
277 "floating->transient_for (0x%08x) == fullscreen->id (0x%08x)\n",
278 floating_child->window->transient_for, fullscreen->window->id);
281 DLOG("floating child at (%d,%d) with %d x %d\n",
282 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
284 render_con(child, false);
290 /* FIXME: refactor this into separate functions: */
292 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
293 assert(children > 0);
296 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
297 if (con->layout == L_SPLITH) {
300 child->rect.width = sizes[i];
301 child->rect.height = rect.height;
302 x += child->rect.width;
306 child->rect.width = rect.width;
307 child->rect.height = sizes[i];
308 y += child->rect.height;
311 /* first we have the decoration, if this is a leaf node */
312 if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
313 /* TODO: make a function for relative coords? */
314 child->deco_rect.x = child->rect.x - con->rect.x;
315 child->deco_rect.y = child->rect.y - con->rect.y;
317 child->rect.y += deco_height;
318 child->rect.height -= deco_height;
320 child->deco_rect.width = child->rect.width;
321 child->deco_rect.height = deco_height;
326 else if (con->layout == L_STACKED) {
329 child->rect.width = rect.width;
330 child->rect.height = rect.height;
332 child->deco_rect.x = x - con->rect.x;
333 child->deco_rect.y = y - con->rect.y + (i * deco_height);
334 child->deco_rect.width = child->rect.width;
335 child->deco_rect.height = deco_height;
337 if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
338 child->rect.y += (deco_height * children);
339 child->rect.height -= (deco_height * children);
344 else if (con->layout == L_TABBED) {
347 child->rect.width = rect.width;
348 child->rect.height = rect.height;
350 child->deco_rect.width = ceil((float)child->rect.width / children);
351 child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
352 child->deco_rect.y = y - con->rect.y;
354 if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
355 child->rect.y += deco_height;
356 child->rect.height -= deco_height;
357 child->deco_rect.height = deco_height;
359 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
363 /* dockarea layout */
364 else if (con->layout == L_DOCKAREA) {
367 child->rect.width = rect.width;
368 child->rect.height = child->geometry.height;
370 child->deco_rect.x = 0;
371 child->deco_rect.y = 0;
372 child->deco_rect.width = 0;
373 child->deco_rect.height = 0;
374 y += child->rect.height;
377 DLOG("child at (%d, %d) with (%d x %d)\n",
378 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
380 render_con(child, false);
384 /* in a stacking or tabbed container, we ensure the focused client is raised */
385 if (con->layout == L_STACKED || con->layout == L_TABBED) {
386 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
388 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
389 /* By rendering the stacked container again, we handle the case
390 * that we have a non-leaf-container inside the stack. In that
391 * case, the children of the non-leaf-container need to be raised
393 render_con(child, false);
397 /* Raise the stack con itself. This will put the stack decoration on
398 * top of every stack window. That way, when a new window is opened in
399 * the stack, the old window will not obscure part of the decoration
400 * (it’s unmapped afterwards). */