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, unless we are in fullscreen mode.
00147          *
00148          * The spec isn’t explicit on whether the aspect ratio hints should be
00149          * respected during fullscreen mode. Other WMs such as Openbox don’t do
00150          * that, and this post suggests that this is the correct way to do it:
00151          * http://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
00152          *
00153          * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
00154          * subtitle rendering, see http://bugs.i3wm.org/594 */
00155         if (!render_fullscreen &&
00156             con->proportional_height != 0 &&
00157             con->proportional_width != 0) {
00158             double new_height = inset->height + 1;
00159             int new_width = inset->width;
00160 
00161             while (new_height > inset->height) {
00162                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
00163 
00164                 if (new_height > inset->height)
00165                     new_width--;
00166             }
00167             /* Center the window */
00168             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
00169             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
00170 
00171             inset->height = new_height;
00172             inset->width = new_width;
00173         }
00174 
00175         if (con->height_increment > 1) {
00176             int old_height = inset->height;
00177             inset->height -= (inset->height - con->base_height) % con->height_increment;
00178             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
00179                 old_height - inset->height, con->height_increment, con->base_height);
00180         }
00181 
00182         if (con->width_increment > 1) {
00183             int old_width = inset->width;
00184             inset->width -= (inset->width - con->base_width) % con->width_increment;
00185             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
00186                 old_width - inset->width, con->width_increment, con->base_width);
00187         }
00188 
00189         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
00190     }
00191 
00192     /* Check for fullscreen nodes */
00193     Con *fullscreen = NULL;
00194     if (con->type != CT_OUTPUT) {
00195         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
00196     }
00197     if (fullscreen) {
00198         fullscreen->rect = rect;
00199         x_raise_con(fullscreen);
00200         render_con(fullscreen, true);
00201         return;
00202     }
00203 
00204     /* find the height for the decorations */
00205     int deco_height = config.font.height + 4;
00206     if (config.font.height & 0x01)
00207         ++deco_height;
00208 
00209     /* precalculate the sizes to be able to correct rounding errors */
00210     int sizes[children];
00211     if (con->layout == L_DEFAULT && children > 0) {
00212         assert(!TAILQ_EMPTY(&con->nodes_head));
00213         Con *child;
00214         int i = 0, assigned = 0;
00215         int total = con->orientation == HORIZ ? rect.width : rect.height;
00216         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00217             double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
00218             assigned += sizes[i++] = percentage * total;
00219         }
00220         assert(assigned == total ||
00221                 (assigned > total && assigned - total <= children * 2) ||
00222                 (assigned < total && total - assigned <= children * 2));
00223         int signal = assigned < total ? 1 : -1;
00224         while (assigned != total) {
00225             for (i = 0; i < children && assigned != total; ++i) {
00226                 sizes[i] += signal;
00227                 assigned += signal;
00228             }
00229         }
00230     }
00231 
00232     if (con->layout == L_OUTPUT) {
00233         /* Skip i3-internal outputs */
00234         if (con->name[0] == '_' && con->name[1] == '_')
00235             return;
00236         render_l_output(con);
00237     } else if (con->type == CT_ROOT) {
00238         Con *output;
00239         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
00240             render_con(output, false);
00241         }
00242 
00243         /* We need to render floating windows after rendering all outputs’
00244          * tiling windows because they need to be on top of *every* output at
00245          * all times. This is important when the user places floating
00246          * windows/containers so that they overlap on another output. */
00247         DLOG("Rendering floating windows:\n");
00248         TAILQ_FOREACH(output, &(con->nodes_head), nodes) {
00249             if (output->name[0] == '_' && output->name[1] == '_')
00250                 continue;
00251             /* Get the active workspace of that output */
00252             Con *content = output_get_content(output);
00253             Con *workspace = TAILQ_FIRST(&(content->focus_head));
00254 
00255             /* Check for (floating!) fullscreen nodes */
00256             /* XXX: This code duplication is unfortunate. Keep in mind to fix
00257              * this when we clean up the whole render.c */
00258             Con *fullscreen = NULL;
00259             fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
00260             if (fullscreen) {
00261                 /* Either the fullscreen window is inside the floating
00262                  * container, then we need to render and raise it now… */
00263                 if (con_inside_floating(fullscreen)) {
00264                     fullscreen->rect = output->rect;
00265                     x_raise_con(fullscreen);
00266                     render_con(fullscreen, true);
00267                     continue;
00268                 } else {
00269                     /* …or it’s a tiling window, in which case the floating
00270                      * windows should not overlap it, so we skip rendering this
00271                      * output. */
00272                     continue;
00273                 }
00274             }
00275 
00276             Con *child;
00277             TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
00278                 DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00279                 x_raise_con(child);
00280                 render_con(child, false);
00281             }
00282         }
00283 
00284     } else {
00285 
00286         /* FIXME: refactor this into separate functions: */
00287     Con *child;
00288     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00289         assert(children > 0);
00290 
00291         /* default layout */
00292         if (con->layout == L_DEFAULT) {
00293             if (con->orientation == HORIZ) {
00294                 child->rect.x = x;
00295                 child->rect.y = y;
00296                 child->rect.width = sizes[i];
00297                 child->rect.height = rect.height;
00298                 x += child->rect.width;
00299             } else {
00300                 child->rect.x = x;
00301                 child->rect.y = y;
00302                 child->rect.width = rect.width;
00303                 child->rect.height = sizes[i];
00304                 y += child->rect.height;
00305             }
00306 
00307             /* first we have the decoration, if this is a leaf node */
00308             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
00309                 /* TODO: make a function for relative coords? */
00310                 child->deco_rect.x = child->rect.x - con->rect.x;
00311                 child->deco_rect.y = child->rect.y - con->rect.y;
00312 
00313                 child->rect.y += deco_height;
00314                 child->rect.height -= deco_height;
00315 
00316                 child->deco_rect.width = child->rect.width;
00317                 child->deco_rect.height = deco_height;
00318             }
00319         }
00320 
00321         /* stacked layout */
00322         else if (con->layout == L_STACKED) {
00323             child->rect.x = x;
00324             child->rect.y = y;
00325             child->rect.width = rect.width;
00326             child->rect.height = rect.height;
00327 
00328             child->deco_rect.x = x - con->rect.x;
00329             child->deco_rect.y = y - con->rect.y + (i * deco_height);
00330             child->deco_rect.width = child->rect.width;
00331             child->deco_rect.height = deco_height;
00332 
00333             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
00334                 child->rect.y += (deco_height * children);
00335                 child->rect.height -= (deco_height * children);
00336             }
00337         }
00338 
00339         /* tabbed layout */
00340         else if (con->layout == L_TABBED) {
00341             child->rect.x = x;
00342             child->rect.y = y;
00343             child->rect.width = rect.width;
00344             child->rect.height = rect.height;
00345 
00346             child->deco_rect.width = child->rect.width / children;
00347             child->deco_rect.height = deco_height;
00348             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
00349             child->deco_rect.y = y - con->rect.y;
00350 
00351             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
00352                 child->rect.y += deco_height;
00353                 child->rect.height -= deco_height;
00354             }
00355         }
00356 
00357         /* dockarea layout */
00358         else if (con->layout == L_DOCKAREA) {
00359             child->rect.x = x;
00360             child->rect.y = y;
00361             child->rect.width = rect.width;
00362             child->rect.height = child->geometry.height;
00363 
00364             child->deco_rect.x = 0;
00365             child->deco_rect.y = 0;
00366             child->deco_rect.width = 0;
00367             child->deco_rect.height = 0;
00368             y += child->rect.height;
00369         }
00370 
00371         DLOG("child at (%d, %d) with (%d x %d)\n",
00372                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00373         x_raise_con(child);
00374         render_con(child, false);
00375         i++;
00376     }
00377 
00378     /* in a stacking or tabbed container, we ensure the focused client is raised */
00379     if (con->layout == L_STACKED || con->layout == L_TABBED) {
00380         TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
00381             x_raise_con(child);
00382         if ((child = TAILQ_FIRST(&(con->focus_head)))) {
00383             /* By rendering the stacked container again, we handle the case
00384              * that we have a non-leaf-container inside the stack. In that
00385              * case, the children of the non-leaf-container need to be raised
00386              * aswell. */
00387             render_con(child, false);
00388         }
00389 
00390         if (children != 1)
00391             /* Raise the stack con itself. This will put the stack decoration on
00392              * top of every stack window. That way, when a new window is opened in
00393              * the stack, the old window will not obscure part of the decoration
00394              * (it’s unmapped afterwards). */
00395             x_raise_con(con);
00396     }
00397     }
00398 }