i3
src/render.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * render.c: Renders (determines position/sizes) the layout tree, updating the
00008  *           various rects. Needs to be pushed to X11 (see x.c) to be visible.
00009  *
00010  */
00011 #include "all.h"
00012 
00013 /* change this to 'true' if you want to have additional borders around every
00014  * container (for debugging purposes) */
00015 static bool show_debug_borders = false;
00016 
00017 /*
00018  * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
00019  * get the height of their content and the remaining CT_CON gets the rest.
00020  *
00021  */
00022 static void render_l_output(Con *con) {
00023     Con *child, *dockchild;
00024 
00025     int x = con->rect.x;
00026     int y = con->rect.y;
00027     int height = con->rect.height;
00028 
00029     /* Find the content container and ensure that there is exactly one. Also
00030      * check for any non-CT_DOCKAREA clients. */
00031     Con *content = NULL;
00032     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00033         if (child->type == CT_CON) {
00034             if (content != NULL) {
00035                 DLOG("More than one CT_CON on output container\n");
00036                 assert(false);
00037             }
00038             content = child;
00039         } else if (child->type != CT_DOCKAREA) {
00040             DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
00041             assert(false);
00042         }
00043     }
00044 
00045     assert(content != NULL);
00046 
00047     /* We need to find out if there is a fullscreen con on the current workspace
00048      * and take the short-cut to render it directly (the user does not want to
00049      * see the dockareas in that case) */
00050     Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
00051     Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
00052     if (fullscreen) {
00053         fullscreen->rect = con->rect;
00054         x_raise_con(fullscreen);
00055         render_con(fullscreen, true);
00056         return;
00057     }
00058 
00059     /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
00060      * children) and figure out how many pixels we have left for the rest */
00061     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00062         if (child->type != CT_DOCKAREA)
00063             continue;
00064 
00065         child->rect.height = 0;
00066         TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
00067             child->rect.height += dockchild->geometry.height;
00068 
00069         height -= child->rect.height;
00070     }
00071 
00072     /* Second pass: Set the widths/heights */
00073     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00074         if (child->type == CT_CON) {
00075             child->rect.x = x;
00076             child->rect.y = y;
00077             child->rect.width = con->rect.width;
00078             child->rect.height = height;
00079         }
00080 
00081         child->rect.x = x;
00082         child->rect.y = y;
00083         child->rect.width = con->rect.width;
00084 
00085         child->deco_rect.x = 0;
00086         child->deco_rect.y = 0;
00087         child->deco_rect.width = 0;
00088         child->deco_rect.height = 0;
00089 
00090         y += child->rect.height;
00091 
00092         DLOG("child at (%d, %d) with (%d x %d)\n",
00093                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00094         x_raise_con(child);
00095         render_con(child, false);
00096     }
00097 }
00098 
00099 /*
00100  * "Renders" the given container (and its children), meaning that all rects are
00101  * updated correctly. Note that this function does not call any xcb_*
00102  * functions, so the changes are completely done in memory only (and
00103  * side-effect free). As soon as you call x_push_changes(), the changes will be
00104  * updated in X11.
00105  *
00106  */
00107 void render_con(Con *con, bool render_fullscreen) {
00108     int children = con_num_children(con);
00109     DLOG("Rendering %snode %p / %s / layout %d / children %d / orient %d\n",
00110          (render_fullscreen ? "fullscreen " : ""), con, con->name, con->layout,
00111          children, con->orientation);
00112 
00113     /* Copy container rect, subtract container border */
00114     /* This is the actually usable space inside this container for clients */
00115     Rect rect = con->rect;
00116 
00117     /* Display a border if this is a leaf node. For container nodes, we don’t
00118      * draw borders (except when in debug mode) */
00119     if (show_debug_borders) {
00120         rect.x += 2;
00121         rect.y += 2;
00122         rect.width -= 2 * 2;
00123         rect.height -= 2 * 2;
00124     }
00125 
00126     int x = rect.x;
00127     int y = rect.y;
00128 
00129     int i = 0;
00130 
00131     con->mapped = true;
00132 
00133     /* if this container contains a window, set the coordinates */
00134     if (con->window) {
00135         /* depending on the border style, the rect of the child window
00136          * needs to be smaller */
00137         Rect *inset = &(con->window_rect);
00138         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
00139         if (!render_fullscreen)
00140             *inset = rect_add(*inset, con_border_style_rect(con));
00141 
00142         /* Obey x11 border */
00143         inset->width -= (2 * con->border_width);
00144         inset->height -= (2 * con->border_width);
00145 
00146         /* Obey the aspect ratio, if any */
00147         if (con->proportional_height != 0 &&
00148             con->proportional_width != 0) {
00149             double new_height = inset->height + 1;
00150             int new_width = inset->width;
00151 
00152             while (new_height > inset->height) {
00153                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
00154 
00155                 if (new_height > inset->height)
00156                     new_width--;
00157             }
00158             /* Center the window */
00159             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
00160             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
00161 
00162             inset->height = new_height;
00163             inset->width = new_width;
00164         }
00165 
00166         if (con->height_increment > 1) {
00167             int old_height = inset->height;
00168             inset->height -= (inset->height - con->base_height) % con->height_increment;
00169             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
00170                 old_height - inset->height, con->height_increment, con->base_height);
00171         }
00172 
00173         if (con->width_increment > 1) {
00174             int old_width = inset->width;
00175             inset->width -= (inset->width - con->base_width) % con->width_increment;
00176             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
00177                 old_width - inset->width, con->width_increment, con->base_width);
00178         }
00179 
00180         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
00181     }
00182 
00183     /* Check for fullscreen nodes */
00184     Con *fullscreen = NULL;
00185     if (con->type != CT_OUTPUT) {
00186         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
00187     }
00188     if (fullscreen) {
00189         fullscreen->rect = rect;
00190         x_raise_con(fullscreen);
00191         render_con(fullscreen, true);
00192         return;
00193     }
00194 
00195     /* find the height for the decorations */
00196     int deco_height = config.font.height + 5;
00197 
00198     /* precalculate the sizes to be able to correct rounding errors */
00199     int sizes[children];
00200     if (con->layout == L_DEFAULT && children > 0) {
00201         assert(!TAILQ_EMPTY(&con->nodes_head));
00202         Con *child;
00203         int i = 0, assigned = 0;
00204         int total = con->orientation == HORIZ ? rect.width : rect.height;
00205         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00206             double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
00207             assigned += sizes[i++] = percentage * total;
00208         }
00209         assert(assigned == total ||
00210                 (assigned > total && assigned - total <= children * 2) ||
00211                 (assigned < total && total - assigned <= children * 2));
00212         int signal = assigned < total ? 1 : -1;
00213         while (assigned != total) {
00214             for (i = 0; i < children && assigned != total; ++i) {
00215                 sizes[i] += signal;
00216                 assigned += signal;
00217             }
00218         }
00219     }
00220 
00221     if (con->layout == L_OUTPUT) {
00222         render_l_output(con);
00223     } else if (con->type == CT_ROOT) {
00224         Con *output;
00225         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
00226             render_con(output, false);
00227         }
00228 
00229         /* We need to render floating windows after rendering all outputs’
00230          * tiling windows because they need to be on top of *every* output at
00231          * all times. This is important when the user places floating
00232          * windows/containers so that they overlap on another output. */
00233         DLOG("Rendering floating windows:\n");
00234         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
00235             /* Get the active workspace of that output */
00236             Con *content = output_get_content(output);
00237             Con *workspace = TAILQ_FIRST(&(content->focus_head));
00238 
00239             /* Check for (floating!) fullscreen nodes */
00240             /* XXX: This code duplication is unfortunate. Keep in mind to fix
00241              * this when we clean up the whole render.c */
00242             Con *fullscreen = NULL;
00243             fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
00244             if (fullscreen) {
00245                 /* Either the fullscreen window is inside the floating
00246                  * container, then we need to render and raise it now… */
00247                 if (con_inside_floating(fullscreen)) {
00248                     fullscreen->rect = output->rect;
00249                     x_raise_con(fullscreen);
00250                     render_con(fullscreen, true);
00251                     continue;
00252                 } else {
00253                     /* …or it’s a tiling window, in which case the floating
00254                      * windows should not overlap it, so we skip rendering this
00255                      * output. */
00256                     continue;
00257                 }
00258             }
00259 
00260             Con *child;
00261             TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
00262                 DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00263                 x_raise_con(child);
00264                 render_con(child, false);
00265             }
00266         }
00267 
00268     } else {
00269 
00270         /* FIXME: refactor this into separate functions: */
00271     Con *child;
00272     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00273         assert(children > 0);
00274 
00275         /* default layout */
00276         if (con->layout == L_DEFAULT) {
00277             if (con->orientation == HORIZ) {
00278                 child->rect.x = x;
00279                 child->rect.y = y;
00280                 child->rect.width = sizes[i];
00281                 child->rect.height = rect.height;
00282                 x += child->rect.width;
00283             } else {
00284                 child->rect.x = x;
00285                 child->rect.y = y;
00286                 child->rect.width = rect.width;
00287                 child->rect.height = sizes[i];
00288                 y += child->rect.height;
00289             }
00290 
00291             /* first we have the decoration, if this is a leaf node */
00292             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
00293                 /* TODO: make a function for relative coords? */
00294                 child->deco_rect.x = child->rect.x - con->rect.x;
00295                 child->deco_rect.y = child->rect.y - con->rect.y;
00296 
00297                 child->rect.y += deco_height;
00298                 child->rect.height -= deco_height;
00299 
00300                 child->deco_rect.width = child->rect.width;
00301                 child->deco_rect.height = deco_height;
00302             }
00303         }
00304 
00305         /* stacked layout */
00306         else if (con->layout == L_STACKED) {
00307             child->rect.x = x;
00308             child->rect.y = y;
00309             child->rect.width = rect.width;
00310             child->rect.height = rect.height;
00311 
00312             child->deco_rect.x = x - con->rect.x;
00313             child->deco_rect.y = y - con->rect.y + (i * deco_height);
00314             child->deco_rect.width = child->rect.width;
00315             child->deco_rect.height = deco_height;
00316 
00317             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
00318                 child->rect.y += (deco_height * children);
00319                 child->rect.height -= (deco_height * children);
00320             }
00321         }
00322 
00323         /* tabbed layout */
00324         else if (con->layout == L_TABBED) {
00325             child->rect.x = x;
00326             child->rect.y = y;
00327             child->rect.width = rect.width;
00328             child->rect.height = rect.height;
00329 
00330             child->deco_rect.width = child->rect.width / children;
00331             child->deco_rect.height = deco_height;
00332             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
00333             child->deco_rect.y = y - con->rect.y;
00334 
00335             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
00336                 child->rect.y += deco_height;
00337                 child->rect.height -= deco_height;
00338             }
00339         }
00340 
00341         /* dockarea layout */
00342         else if (con->layout == L_DOCKAREA) {
00343             child->rect.x = x;
00344             child->rect.y = y;
00345             child->rect.width = rect.width;
00346             child->rect.height = child->geometry.height;
00347 
00348             child->deco_rect.x = 0;
00349             child->deco_rect.y = 0;
00350             child->deco_rect.width = 0;
00351             child->deco_rect.height = 0;
00352             y += child->rect.height;
00353         }
00354 
00355         DLOG("child at (%d, %d) with (%d x %d)\n",
00356                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00357         x_raise_con(child);
00358         render_con(child, false);
00359         i++;
00360     }
00361 
00362     /* in a stacking or tabbed container, we ensure the focused client is raised */
00363     if (con->layout == L_STACKED || con->layout == L_TABBED) {
00364         TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
00365             x_raise_con(child);
00366         if ((child = TAILQ_FIRST(&(con->focus_head)))) {
00367             /* By rendering the stacked container again, we handle the case
00368              * that we have a non-leaf-container inside the stack. In that
00369              * case, the children of the non-leaf-container need to be raised
00370              * aswell. */
00371             render_con(child, false);
00372         }
00373 
00374         if (children != 1)
00375             /* Raise the stack con itself. This will put the stack decoration on
00376              * top of every stack window. That way, when a new window is opened in
00377              * the stack, the old window will not obscure part of the decoration
00378              * (it’s unmapped afterwards). */
00379             x_raise_con(con);
00380     }
00381     }
00382 }