i3
src/render.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  */
00004 
00005 #include "all.h"
00006 
00007 /* change this to 'true' if you want to have additional borders around every
00008  * container (for debugging purposes) */
00009 static bool show_debug_borders = false;
00010 
00011 /*
00012  * Renders a container with layout L_OUTPUT. In this layout, all CT_DOCKAREAs
00013  * get the height of their content and the remaining CT_CON gets the rest.
00014  *
00015  */
00016 static void render_l_output(Con *con) {
00017     Con *child, *dockchild;
00018 
00019     int x = con->rect.x;
00020     int y = con->rect.y;
00021     int height = con->rect.height;
00022     DLOG("Available height: %d\n", height);
00023 
00024     /* Find the content container and ensure that there is exactly one. Also
00025      * check for any non-CT_DOCKAREA clients. */
00026     Con *content = NULL;
00027     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00028         if (child->type == CT_CON) {
00029             if (content != NULL) {
00030                 DLOG("More than one CT_CON on output container\n");
00031                 assert(false);
00032             }
00033             content = child;
00034         } else if (child->type != CT_DOCKAREA) {
00035             DLOG("Child %p of type %d is inside the OUTPUT con\n", child, child->type);
00036             assert(false);
00037         }
00038     }
00039 
00040     assert(content != NULL);
00041 
00042     /* We need to find out if there is a fullscreen con on the current workspace
00043      * and take the short-cut to render it directly (the user does not want to
00044      * see the dockareas in that case) */
00045     Con *ws = con_get_fullscreen_con(content, CF_OUTPUT);
00046     Con *fullscreen = con_get_fullscreen_con(ws, CF_OUTPUT);
00047     if (fullscreen) {
00048         DLOG("got fs node: %p\n", fullscreen);
00049         fullscreen->rect = con->rect;
00050         x_raise_con(fullscreen);
00051         render_con(fullscreen, true);
00052         return;
00053     }
00054 
00055     /* First pass: determine the height of all CT_DOCKAREAs (the sum of their
00056      * children) and figure out how many pixels we have left for the rest */
00057     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00058         if (child->type != CT_DOCKAREA)
00059             continue;
00060 
00061         child->rect.height = 0;
00062         TAILQ_FOREACH(dockchild, &(child->nodes_head), nodes)
00063             child->rect.height += dockchild->geometry.height;
00064         DLOG("This dockarea's height: %d\n", child->rect.height);
00065 
00066         height -= child->rect.height;
00067     }
00068 
00069     DLOG("Remaining: %d\n", height);
00070 
00071     /* Second pass: Set the widths/heights */
00072     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00073         if (child->type == CT_CON) {
00074             child->rect.x = x;
00075             child->rect.y = y;
00076             child->rect.width = con->rect.width;
00077             child->rect.height = height;
00078         }
00079 
00080         child->rect.x = x;
00081         child->rect.y = y;
00082         child->rect.width = con->rect.width;
00083 
00084         child->deco_rect.x = 0;
00085         child->deco_rect.y = 0;
00086         child->deco_rect.width = 0;
00087         child->deco_rect.height = 0;
00088 
00089         y += child->rect.height;
00090 
00091         DLOG("child at (%d, %d) with (%d x %d)\n",
00092                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00093         DLOG("x now %d, y now %d\n", x, y);
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     DLOG("currently rendering node %p / %s / layout %d\n",
00109             con, con->name, con->layout);
00110     int children = con_num_children(con);
00111     DLOG("children: %d, orientation = %d\n", 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         DLOG("Starting with inset = (%d, %d) %d x %d\n", inset->x, inset->y, inset->width, inset->height);
00143         /* Obey x11 border */
00144         DLOG("X11 border: %d\n", con->border_width);
00145         inset->width -= (2 * con->border_width);
00146         inset->height -= (2 * con->border_width);
00147 
00148         /* Obey the aspect ratio, if any */
00149         if (con->proportional_height != 0 &&
00150             con->proportional_width != 0) {
00151             DLOG("proportional height = %d, width = %d\n", con->proportional_height, con->proportional_width);
00152             double new_height = inset->height + 1;
00153             int new_width = inset->width;
00154 
00155             while (new_height > inset->height) {
00156                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
00157 
00158                 if (new_height > inset->height)
00159                     new_width--;
00160             }
00161             /* Center the window */
00162             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
00163             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
00164 
00165             inset->height = new_height;
00166             inset->width = new_width;
00167             DLOG("new_height = %f, new_width = %d\n", new_height, new_width);
00168         }
00169 
00170         if (con->height_increment > 1) {
00171             int old_height = inset->height;
00172             inset->height -= (inset->height - con->base_height) % con->height_increment;
00173             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
00174                 old_height - inset->height, con->height_increment, con->base_height);
00175         }
00176 
00177         if (con->width_increment > 1) {
00178             int old_width = inset->width;
00179             inset->width -= (inset->width - con->base_width) % con->width_increment;
00180             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
00181                 old_width - inset->width, con->width_increment, con->base_width);
00182         }
00183 
00184         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
00185     }
00186 
00187     /* Check for fullscreen nodes */
00188     Con *fullscreen = NULL;
00189     if (con->type != CT_OUTPUT) {
00190         fullscreen = con_get_fullscreen_con(con, (con->type == CT_ROOT ? CF_GLOBAL : CF_OUTPUT));
00191     }
00192     if (fullscreen) {
00193         DLOG("got fs node: %p\n", fullscreen);
00194         fullscreen->rect = rect;
00195         x_raise_con(fullscreen);
00196         render_con(fullscreen, true);
00197         return;
00198     }
00199 
00200     /* find the height for the decorations */
00201     int deco_height = config.font.height + 5;
00202 
00203     /* precalculate the sizes to be able to correct rounding errors */
00204     int sizes[children];
00205     if (con->layout == L_DEFAULT && children > 0) {
00206         assert(!TAILQ_EMPTY(&con->nodes_head));
00207         Con *child;
00208         int i = 0, assigned = 0;
00209         int total = con->orientation == HORIZ ? rect.width : rect.height;
00210         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00211             double percentage = child->percent > 0.0 ? child->percent : 1.0 / children;
00212             assigned += sizes[i++] = percentage * total;
00213         }
00214         assert(assigned == total ||
00215                 (assigned > total && assigned - total <= children * 2) ||
00216                 (assigned < total && total - assigned <= children * 2));
00217         int signal = assigned < total ? 1 : -1;
00218         while (assigned != total) {
00219             for (i = 0; i < children && assigned != total; ++i) {
00220                 sizes[i] += signal;
00221                 assigned += signal;
00222             }
00223         }
00224     }
00225 
00226     if (con->layout == L_OUTPUT) {
00227         render_l_output(con);
00228     } else if (con->type == CT_ROOT) {
00229         DLOG("Root node, rendering outputs\n");
00230         Con *child;
00231         TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00232             render_con(child, false);
00233         }
00234     } else {
00235 
00236         /* FIXME: refactor this into separate functions: */
00237     Con *child;
00238     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00239         assert(children > 0);
00240 
00241         /* default layout */
00242         if (con->layout == L_DEFAULT) {
00243             if (con->orientation == HORIZ) {
00244                 child->rect.x = x;
00245                 child->rect.y = y;
00246                 child->rect.width = sizes[i];
00247                 child->rect.height = rect.height;
00248                 x += child->rect.width;
00249             } else {
00250                 child->rect.x = x;
00251                 child->rect.y = y;
00252                 child->rect.width = rect.width;
00253                 child->rect.height = sizes[i];
00254                 y += child->rect.height;
00255             }
00256 
00257             /* first we have the decoration, if this is a leaf node */
00258             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
00259                 DLOG("that child is a leaf node, subtracting deco\n");
00260                 /* TODO: make a function for relative coords? */
00261                 child->deco_rect.x = child->rect.x - con->rect.x;
00262                 child->deco_rect.y = child->rect.y - con->rect.y;
00263 
00264                 child->rect.y += deco_height;
00265                 child->rect.height -= deco_height;
00266 
00267                 child->deco_rect.width = child->rect.width;
00268                 child->deco_rect.height = deco_height;
00269             }
00270         }
00271 
00272         /* stacked layout */
00273         else if (con->layout == L_STACKED) {
00274             DLOG("stacked con\n");
00275             child->rect.x = x;
00276             child->rect.y = y;
00277             child->rect.width = rect.width;
00278             child->rect.height = rect.height;
00279 
00280             child->deco_rect.x = x - con->rect.x;
00281             child->deco_rect.y = y - con->rect.y + (i * deco_height);
00282             child->deco_rect.width = child->rect.width;
00283             child->deco_rect.height = deco_height;
00284 
00285             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
00286                 child->rect.y += (deco_height * children);
00287                 child->rect.height -= (deco_height * children);
00288             }
00289         }
00290 
00291         /* tabbed layout */
00292         else if (con->layout == L_TABBED) {
00293             DLOG("tabbed con\n");
00294             child->rect.x = x;
00295             child->rect.y = y;
00296             child->rect.width = rect.width;
00297             child->rect.height = rect.height;
00298 
00299             child->deco_rect.width = child->rect.width / children;
00300             child->deco_rect.height = deco_height;
00301             child->deco_rect.x = x - con->rect.x + i * child->deco_rect.width;
00302             child->deco_rect.y = y - con->rect.y;
00303 
00304             if (children > 1 || (child->border_style != BS_1PIXEL && child->border_style != BS_NONE)) {
00305                 child->rect.y += deco_height;
00306                 child->rect.height -= deco_height;
00307             }
00308         }
00309 
00310         /* dockarea layout */
00311         else if (con->layout == L_DOCKAREA) {
00312             DLOG("dockarea con\n");
00313             child->rect.x = x;
00314             child->rect.y = y;
00315             child->rect.width = rect.width;
00316             child->rect.height = child->geometry.height;
00317 
00318             child->deco_rect.x = 0;
00319             child->deco_rect.y = 0;
00320             child->deco_rect.width = 0;
00321             child->deco_rect.height = 0;
00322             y += child->rect.height;
00323         }
00324 
00325         DLOG("child at (%d, %d) with (%d x %d)\n",
00326                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00327         DLOG("x now %d, y now %d\n", x, y);
00328         x_raise_con(child);
00329         render_con(child, false);
00330         i++;
00331     }
00332 
00333     /* in a stacking or tabbed container, we ensure the focused client is raised */
00334     if (con->layout == L_STACKED || con->layout == L_TABBED) {
00335         DLOG("stacked/tabbed, raising focused reverse\n");
00336         TAILQ_FOREACH_REVERSE(child, &(con->focus_head), focus_head, focused)
00337             x_raise_con(child);
00338         DLOG("done\n");
00339         if ((child = TAILQ_FIRST(&(con->focus_head)))) {
00340             DLOG("con %p is stacking, raising %p\n", con, child);
00341             /* By rendering the stacked container again, we handle the case
00342              * that we have a non-leaf-container inside the stack. In that
00343              * case, the children of the non-leaf-container need to be raised
00344              * aswell. */
00345             render_con(child, false);
00346         }
00347 
00348         if (children != 1)
00349             /* Raise the stack con itself. This will put the stack decoration on
00350              * top of every stack window. That way, when a new window is opened in
00351              * the stack, the old window will not obscure part of the decoration
00352              * (it’s unmapped afterwards). */
00353             x_raise_con(con);
00354     }
00355     }
00356 
00357     Con *child;
00358     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
00359         DLOG("render floating:\n");
00360         DLOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
00361         x_raise_con(child);
00362         render_con(child, false);
00363     }
00364 
00365     DLOG("-- level up\n");
00366 }