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 assert(content != NULL);
49 /* We need to find out if there is a fullscreen con on the current workspace
50 * and take the short-cut to render it directly (the user does not want to
51 * see the dockareas in that case) */
52 Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
53 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
55 fullscreen->rect = con->rect;
56 x_raise_con(fullscreen);
57 render_con(fullscreen, true);
61 /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
62 * children) and figure out how many pixels we have left for the rest */
63 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
64 if (child->type != CT_DOCKAREA)
67 child->rect.height = 0;
68 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
69 child->rect.height += dockchild->geometry.height;
71 height -= child->rect.height;
74 /* Second pass: Set the widths/heights */
75 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
76 if (child->type == CT_CON) {
79 child->rect.width = con->rect.width;
80 child->rect.height = height;
85 child->rect.width = con->rect.width;
87 child->deco_rect.x = 0;
88 child->deco_rect.y = 0;
89 child->deco_rect.width = 0;
90 child->deco_rect.height = 0;
92 y += child->rect.height;
94 DLOG("child at (%d, %d) with (%d x %d)\n",
95 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
97 render_con(child, false);
102 * "Renders" the given container (and its children), meaning that all rects are
103 * updated correctly. Note that this function does not call any xcb_*
104 * functions, so the changes are completely done in memory only (and
105 * side-effect free). As soon as you call x_push_changes(), the changes will be
109 void render_con(Con *con, bool render_fullscreen) {
110 int children = con_num_children(con);
111 DLOG("Rendering %snode %p / %s / layout %d / children %d\n",
112 (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
115 /* Copy container rect, subtract container border */
116 /* This is the actually usable space inside this container for clients */
117 Rect rect = con->rect;
119 /* Display a border if this is a leaf node. For container nodes, we don’t
120 * draw borders (except when in debug mode) */
121 if (show_debug_borders) {
125 rect.height -= 2 * 2;
135 /* if this container contains a window, set the coordinates */
137 /* depending on the border style, the rect of the child window
138 * needs to be smaller */
139 Rect *inset = &(con->window_rect);
140 *inset = (Rect){0, 0, con->rect.width, con->rect.height};
141 if (!render_fullscreen)
142 *inset = rect_add(*inset, con_border_style_rect(con));
144 /* Obey x11 border */
145 inset->width -= (2 * con->border_width);
146 inset->height -= (2 * con->border_width);
148 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
150 * The spec isn’t explicit on whether the aspect ratio hints should be
151 * respected during fullscreen mode. Other WMs such as Openbox don’t do
152 * that, and this post suggests that this is the correct way to do it:
153 * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
155 * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
156 * subtitle rendering, see http://bugs.i3wm.org/594 */
157 if (!render_fullscreen &&
158 con->proportional_height != 0 &&
159 con->proportional_width != 0) {
160 double new_height = inset->height + 1;
161 int new_width = inset->width;
163 while (new_height > inset->height) {
164 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
166 if (new_height > inset->height)
169 /* Center the window */
170 inset->y += ceil(inset->height / 2) - floor(new_height / 2);
171 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
173 inset->height = new_height;
174 inset->width = new_width;
177 /* NB: We used to respect resize increment size hints for tiling
178 * windows up until commit 0db93d9 here. However, since all terminal
179 * emulators cope with ignoring the size hints in a better way than we
180 * can (by providing their fake-transparency or background color), this
181 * code was removed. See also http://bugs.i3wm.org/540 */
183 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
186 /* Check for fullscreen nodes */
187 Con *fullscreen = NULL;
188 if (con->type != CT_OUTPUT) {
189 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
192 fullscreen->rect = rect;
193 x_raise_con(fullscreen);
194 render_con(fullscreen, true);
198 /* find the height for the decorations */
199 int deco_height = config.font.height + 4;
200 if (config.font.height & 0x01)
203 /* precalculate the sizes to be able to correct rounding errors */
205 memset(sizes, 0, children*sizeof(int));
206 if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && children > 0) {
207 assert(!TAILQ_EMPTY(&con->nodes_head));
209 int i = 0, assigned = 0;
210 int total = con_orientation(con) == HORIZ ? rect.width : rect.height;
211 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
212 double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
213 assigned += sizes[i++] = percentage * total;
215 assert(assigned == total ||
216 (assigned > total && assigned - total <= children * 2) ||
217 (assigned < total && total - assigned <= children * 2));
218 int signal = assigned < total ? 1 : -1;
219 while (assigned != total) {
220 for (i = 0; i < children && assigned != total; ++i) {
227 if (con->layout == L_OUTPUT) {
228 /* Skip i3-internal outputs */
229 if (con_is_internal(con))
231 render_l_output(con);
232 } else if (con->type == CT_ROOT) {
234 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
235 render_con(output, false);
238 /* We need to render floating windows after rendering all outputs’
239 * tiling windows because they need to be on top of *every* output at
240 * all times. This is important when the user places floating
241 * windows/containers so that they overlap on another output. */
242 DLOG("Rendering floating windows:\n");
243 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
244 if (con_is_internal(output))
246 /* Get the active workspace of that output */
247 Con *content = output_get_content(output);
248 Con *workspace = TAILQ_FIRST(&(content->focus_head));
249 Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
251 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
252 /* Don’t render floating windows when there is a fullscreen window
253 * on that workspace. Necessary to make floating fullscreen work
254 * correctly (ticket #564). */
255 if (fullscreen != NULL) {
256 Con *floating_child = con_descend_focused(child);
257 /* Exception to the above rule: smart
258 * popup_during_fullscreen handling (popups belonging to
259 * the fullscreen app will be rendered). */
260 if (floating_child->window == NULL ||
261 fullscreen->window == NULL ||
262 floating_child->window->transient_for != fullscreen->window->id)
265 DLOG("Rendering floating child even though in fullscreen mode: "
266 "floating->transient_for (0x%08x) == fullscreen->id (0x%08x)\n",
267 floating_child->window->transient_for, fullscreen->window->id);
270 DLOG("floating child at (%d,%d) with %d x %d\n",
271 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
273 render_con(child, false);
279 /* FIXME: refactor this into separate functions: */
281 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
282 assert(children > 0);
285 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
286 if (con->layout == L_SPLITH) {
289 child->rect.width = sizes[i];
290 child->rect.height = rect.height;
291 x += child->rect.width;
295 child->rect.width = rect.width;
296 child->rect.height = sizes[i];
297 y += child->rect.height;
300 /* first we have the decoration, if this is a leaf node */
301 if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
302 /* TODO: make a function for relative coords? */
303 child->deco_rect.x = child->rect.x - con->rect.x;
304 child->deco_rect.y = child->rect.y - con->rect.y;
306 child->rect.y += deco_height;
307 child->rect.height -= deco_height;
309 child->deco_rect.width = child->rect.width;
310 child->deco_rect.height = deco_height;
315 else if (con->layout == L_STACKED) {
318 child->rect.width = rect.width;
319 child->rect.height = rect.height;
321 child->deco_rect.x = x - con->rect.x;
322 child->deco_rect.y = y - con->rect.y + (i * deco_height);
323 child->deco_rect.width = child->rect.width;
324 child->deco_rect.height = deco_height;
326 if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
327 child->rect.y += (deco_height * children);
328 child->rect.height -= (deco_height * children);
333 else if (con->layout == L_TABBED) {
336 child->rect.width = rect.width;
337 child->rect.height = rect.height;
339 child->deco_rect.width = ceil((float)child->rect.width / children);
340 child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
341 child->deco_rect.y = y - con->rect.y;
343 if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
344 child->rect.y += deco_height;
345 child->rect.height -= deco_height;
346 child->deco_rect.height = deco_height;
348 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
352 /* dockarea layout */
353 else if (con->layout == L_DOCKAREA) {
356 child->rect.width = rect.width;
357 child->rect.height = child->geometry.height;
359 child->deco_rect.x = 0;
360 child->deco_rect.y = 0;
361 child->deco_rect.width = 0;
362 child->deco_rect.height = 0;
363 y += child->rect.height;
366 DLOG("child at (%d, %d) with (%d x %d)\n",
367 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
369 render_con(child, false);
373 /* in a stacking or tabbed container, we ensure the focused client is raised */
374 if (con->layout == L_STACKED || con->layout == L_TABBED) {
375 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
377 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
378 /* By rendering the stacked container again, we handle the case
379 * that we have a non-leaf-container inside the stack. In that
380 * case, the children of the non-leaf-container need to be raised
382 render_con(child, false);
386 /* Raise the stack con itself. This will put the stack decoration on
387 * top of every stack window. That way, when a new window is opened in
388 * the stack, the old window will not obscure part of the decoration
389 * (it’s unmapped afterwards). */