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 * Returns the height for the decorations
22 int render_deco_height(void) {
23 int deco_height = config.font.height + 4;
24 if (config.font.height & 0x01)
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.
34 static void render_l_output(Con *con) {
35 Con *child, *dockchild;
39 int height = con->rect.height;
41 /* Find the content container and ensure that there is exactly one. Also
42 * check for any non-CT_DOCKAREA clients. */
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");
51 } else if (child->type != CT_DOCKAREA) {
52 DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
57 if (content == NULL) {
58 DLOG("Skipping this output because it is currently being destroyed.\n");
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);
67 DLOG("Skipping this output because it is currently being destroyed.\n");
70 Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
72 fullscreen->rect = con->rect;
73 x_raise_con(fullscreen);
74 render_con(fullscreen, true);
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)
84 child->rect.height = 0;
85 TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
86 child->rect.height += dockchild->geometry.height;
88 height -= child->rect.height;
91 /* Second pass: Set the widths/heights */
92 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
93 if (child->type == CT_CON) {
96 child->rect.width = con->rect.width;
97 child->rect.height = height;
102 child->rect.width = con->rect.width;
104 child->deco_rect.x = 0;
105 child->deco_rect.y = 0;
106 child->deco_rect.width = 0;
107 child->deco_rect.height = 0;
109 y += child->rect.height;
111 DLOG("child at (%d, %d) with (%d x %d)\n",
112 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
114 render_con(child, false);
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
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,
132 /* Copy container rect, subtract container border */
133 /* This is the actually usable space inside this container for clients */
134 Rect rect = con->rect;
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) {
142 rect.height -= 2 * 2;
152 /* if this container contains a window, set the coordinates */
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));
161 /* Obey x11 border */
162 inset->width -= (2 * con->border_width);
163 inset->height -= (2 * con->border_width);
165 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
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
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->proportional_height != 0 &&
176 con->proportional_width != 0) {
177 double new_height = inset->height + 1;
178 int new_width = inset->width;
180 while (new_height > inset->height) {
181 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
183 if (new_height > inset->height)
186 /* Center the window */
187 inset->y += ceil(inset->height / 2) - floor((new_height + .5) / 2);
188 inset->x += ceil(inset->width / 2) - floor(new_width / 2);
190 inset->height = new_height + .5;
191 inset->width = new_width;
194 /* NB: We used to respect resize increment size hints for tiling
195 * windows up until commit 0db93d9 here. However, since all terminal
196 * emulators cope with ignoring the size hints in a better way than we
197 * can (by providing their fake-transparency or background color), this
198 * code was removed. See also http://bugs.i3wm.org/540 */
200 DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
203 /* Check for fullscreen nodes */
204 Con *fullscreen = NULL;
205 if (con->type != CT_OUTPUT) {
206 fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
209 fullscreen->rect = rect;
210 x_raise_con(fullscreen);
211 render_con(fullscreen, true);
215 /* find the height for the decorations */
216 int deco_height = render_deco_height();
218 /* precalculate the sizes to be able to correct rounding errors */
220 memset(sizes, 0, children*sizeof(int));
221 if ((con->layout == L_SPLITH || con->layout == L_SPLITV) && children > 0) {
222 assert(!TAILQ_EMPTY(&con->nodes_head));
224 int i = 0, assigned = 0;
225 int total = con_orientation(con) == HORIZ ? rect.width : rect.height;
226 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
227 double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
228 assigned += sizes[i++] = percentage * total;
230 assert(assigned == total ||
231 (assigned > total && assigned - total <= children * 2) ||
232 (assigned < total && total - assigned <= children * 2));
233 int signal = assigned < total ? 1 : -1;
234 while (assigned != total) {
235 for (i = 0; i < children && assigned != total; ++i) {
242 if (con->layout == L_OUTPUT) {
243 /* Skip i3-internal outputs */
244 if (con_is_internal(con))
246 render_l_output(con);
247 } else if (con->type == CT_ROOT) {
249 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
250 render_con(output, false);
253 /* We need to render floating windows after rendering all outputs’
254 * tiling windows because they need to be on top of *every* output at
255 * all times. This is important when the user places floating
256 * windows/containers so that they overlap on another output. */
257 DLOG("Rendering floating windows:\n");
258 TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
259 if (con_is_internal(output))
261 /* Get the active workspace of that output */
262 Con *content = output_get_content(output);
263 if (!content || TAILQ_EMPTY(&(content->focus_head))) {
264 DLOG("Skipping this output because it is currently being destroyed.\n");
267 Con *workspace = TAILQ_FIRST(&(content->focus_head));
268 Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
270 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
271 /* Don’t render floating windows when there is a fullscreen window
272 * on that workspace. Necessary to make floating fullscreen work
273 * correctly (ticket #564). */
274 if (fullscreen != NULL && fullscreen->window != NULL) {
275 Con *floating_child = con_descend_focused(child);
276 Con *transient_con = floating_child;
277 bool is_transient_for = false;
278 /* Exception to the above rule: smart
279 * popup_during_fullscreen handling (popups belonging to
280 * the fullscreen app will be rendered). */
281 while (transient_con != NULL &&
282 transient_con->window != NULL &&
283 transient_con->window->transient_for != XCB_NONE) {
284 if (transient_con->window->transient_for == fullscreen->window->id) {
285 is_transient_for = true;
288 transient_con = con_by_window_id(transient_con->window->transient_for);
291 if (!is_transient_for)
294 DLOG("Rendering floating child even though in fullscreen mode: "
295 "floating->transient_for (0x%08x) --> fullscreen->id (0x%08x)\n",
296 floating_child->window->transient_for, fullscreen->window->id);
299 DLOG("floating child at (%d,%d) with %d x %d\n",
300 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
302 render_con(child, false);
308 /* FIXME: refactor this into separate functions: */
310 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
311 assert(children > 0);
314 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
315 if (con->layout == L_SPLITH) {
318 child->rect.width = sizes[i];
319 child->rect.height = rect.height;
320 x += child->rect.width;
324 child->rect.width = rect.width;
325 child->rect.height = sizes[i];
326 y += child->rect.height;
329 /* first we have the decoration, if this is a leaf node */
330 if (con_is_leaf(child)) {
331 if (child->border_style == BS_NORMAL) {
332 /* TODO: make a function for relative coords? */
333 child->deco_rect.x = child->rect.x - con->rect.x;
334 child->deco_rect.y = child->rect.y - con->rect.y;
336 child->rect.y += deco_height;
337 child->rect.height -= deco_height;
339 child->deco_rect.width = child->rect.width;
340 child->deco_rect.height = deco_height;
342 child->deco_rect.x = 0;
343 child->deco_rect.y = 0;
344 child->deco_rect.width = 0;
345 child->deco_rect.height = 0;
351 else if (con->layout == L_STACKED) {
354 child->rect.width = rect.width;
355 child->rect.height = rect.height;
357 child->deco_rect.x = x - con->rect.x;
358 child->deco_rect.y = y - con->rect.y + (i * deco_height);
359 child->deco_rect.width = child->rect.width;
360 child->deco_rect.height = deco_height;
362 if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
363 child->rect.y += (deco_height * children);
364 child->rect.height -= (deco_height * children);
369 else if (con->layout == L_TABBED) {
372 child->rect.width = rect.width;
373 child->rect.height = rect.height;
375 child->deco_rect.width = floor((float)child->rect.width / children);
376 child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
377 child->deco_rect.y = y - con->rect.y;
379 /* Since the tab width may be something like 31,6 px per tab, we
380 * let the last tab have all the extra space (0,6 * children). */
381 if (i == (children-1)) {
382 child->deco_rect.width += (child->rect.width - (child->deco_rect.x + child->deco_rect.width));
385 if (children > 1 || (child->border_style != BS_PIXEL && child->border_style != BS_NONE)) {
386 child->rect.y += deco_height;
387 child->rect.height -= deco_height;
388 child->deco_rect.height = deco_height;
390 child->deco_rect.height = (child->border_style == BS_PIXEL ? 1 : 0);
394 /* dockarea layout */
395 else if (con->layout == L_DOCKAREA) {
398 child->rect.width = rect.width;
399 child->rect.height = child->geometry.height;
401 child->deco_rect.x = 0;
402 child->deco_rect.y = 0;
403 child->deco_rect.width = 0;
404 child->deco_rect.height = 0;
405 y += child->rect.height;
408 DLOG("child at (%d, %d) with (%d x %d)\n",
409 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
411 render_con(child, false);
415 /* in a stacking or tabbed container, we ensure the focused client is raised */
416 if (con->layout == L_STACKED || con->layout == L_TABBED) {
417 TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
419 if ((child = TAILQ_FIRST(&(con->focus_head)))) {
420 /* By rendering the stacked container again, we handle the case
421 * that we have a non-leaf-container inside the stack. In that
422 * case, the children of the non-leaf-container need to be raised
424 render_con(child, false);
428 /* Raise the stack con itself. This will put the stack decoration on
429 * top of every stack window. That way, when a new window is opened in
430 * the stack, the old window will not obscure part of the decoration
431 * (it’s unmapped afterwards). */