i3
src/randr.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-2012 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * For more information on RandR, please see the X.org RandR specification at
00008  * http://cgit.freedesktop.org/xorg/proto/randrproto/tree/randrproto.txt
00009  * (take your time to read it completely, it answers all questions).
00010  *
00011  */
00012 #include "all.h"
00013 
00014 #include <time.h>
00015 #include <xcb/randr.h>
00016 
00017 /* While a clean namespace is usually a pretty good thing, we really need
00018  * to use shorter names than the whole xcb_randr_* default names. */
00019 typedef xcb_randr_get_crtc_info_reply_t crtc_info;
00020 typedef xcb_randr_get_screen_resources_current_reply_t resources_reply;
00021 
00022 /* Pointer to the result of the query for primary output */
00023 xcb_randr_get_output_primary_reply_t *primary;
00024 
00025 /* Stores all outputs available in your current session. */
00026 struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs);
00027 
00028 static bool randr_disabled = false;
00029 
00030 /*
00031  * Get a specific output by its internal X11 id. Used by randr_query_outputs
00032  * to check if the output is new (only in the first scan) or if we are
00033  * re-scanning.
00034  *
00035  */
00036 static Output *get_output_by_id(xcb_randr_output_t id) {
00037     Output *output;
00038     TAILQ_FOREACH(output, &outputs, outputs)
00039         if (output->id == id)
00040             return output;
00041 
00042     return NULL;
00043 }
00044 
00045 /*
00046  * Returns the output with the given name if it is active (!) or NULL.
00047  *
00048  */
00049 Output *get_output_by_name(const char *name) {
00050     Output *output;
00051     TAILQ_FOREACH(output, &outputs, outputs)
00052         if (output->active &&
00053             strcasecmp(output->name, name) == 0)
00054             return output;
00055 
00056     return NULL;
00057 }
00058 
00059 /*
00060  * Returns the first output which is active.
00061  *
00062  */
00063 Output *get_first_output() {
00064     Output *output;
00065 
00066     TAILQ_FOREACH(output, &outputs, outputs)
00067         if (output->active)
00068             return output;
00069 
00070     return NULL;
00071 }
00072 
00073 /*
00074  * Returns the active (!) output which contains the coordinates x, y or NULL
00075  * if there is no output which contains these coordinates.
00076  *
00077  */
00078 Output *get_output_containing(int x, int y) {
00079     Output *output;
00080     TAILQ_FOREACH(output, &outputs, outputs) {
00081         if (!output->active)
00082             continue;
00083         DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
00084                         x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
00085         if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
00086             y >= output->rect.y && y < (output->rect.y + output->rect.height))
00087             return output;
00088     }
00089 
00090     return NULL;
00091 }
00092 
00093 /*
00094  * Gets the output which is the last one in the given direction, for example
00095  * the output on the most bottom when direction == D_DOWN, the output most
00096  * right when direction == D_RIGHT and so on.
00097  *
00098  * This function always returns a output.
00099  *
00100  */
00101 Output *get_output_most(direction_t direction, Output *current) {
00102     Output *output, *candidate = NULL;
00103     int position = 0;
00104     TAILQ_FOREACH(output, &outputs, outputs) {
00105         if (!output->active)
00106             continue;
00107 
00108         /* Repeated calls of WIN determine the winner of the comparison */
00109         #define WIN(variable, condition) \
00110             if (variable condition) { \
00111                 candidate = output; \
00112                 position = variable; \
00113             } \
00114             break;
00115 
00116         if (((direction == D_UP) || (direction == D_DOWN)) &&
00117             (current->rect.x != output->rect.x))
00118             continue;
00119 
00120         if (((direction == D_LEFT) || (direction == D_RIGHT)) &&
00121             (current->rect.y != output->rect.y))
00122             continue;
00123 
00124         switch (direction) {
00125             case D_UP:
00126                 WIN(output->rect.y, <= position);
00127             case D_DOWN:
00128                 WIN(output->rect.y, >= position);
00129             case D_LEFT:
00130                 WIN(output->rect.x, <= position);
00131             case D_RIGHT:
00132                 WIN(output->rect.x, >= position);
00133         }
00134     }
00135 
00136     assert(candidate != NULL);
00137 
00138     return candidate;
00139 }
00140 
00141 /*
00142  * Gets the output which is the next one in the given direction.
00143  *
00144  */
00145 Output *get_output_next(direction_t direction, Output *current) {
00146     Output *output, *candidate = NULL;
00147 
00148     TAILQ_FOREACH(output, &outputs, outputs) {
00149         if (!output->active)
00150             continue;
00151 
00152         if (((direction == D_UP) || (direction == D_DOWN)) &&
00153             (current->rect.x != output->rect.x))
00154             continue;
00155 
00156         if (((direction == D_LEFT) || (direction == D_RIGHT)) &&
00157             (current->rect.y != output->rect.y))
00158             continue;
00159 
00160         switch (direction) {
00161             case D_UP:
00162                 if (output->rect.y < current->rect.y &&
00163                     (!candidate || output->rect.y < candidate->rect.y))
00164                     candidate = output;
00165                 break;
00166             case D_DOWN:
00167                 if (output->rect.y > current->rect.y &&
00168                     (!candidate || output->rect.y > candidate->rect.y))
00169                     candidate = output;
00170                 break;
00171             case D_LEFT:
00172                 if (output->rect.x < current->rect.x &&
00173                     (!candidate || output->rect.x > candidate->rect.x))
00174                     candidate = output;
00175                 break;
00176             case D_RIGHT:
00177                 if (output->rect.x > current->rect.x &&
00178                     (!candidate || output->rect.x < candidate->rect.x))
00179                     candidate = output;
00180                 break;
00181         }
00182     }
00183 
00184     return candidate;
00185 }
00186 
00187 /*
00188  * Disables RandR support by creating exactly one output with the size of the
00189  * X11 screen.
00190  *
00191  */
00192 void disable_randr(xcb_connection_t *conn) {
00193     DLOG("RandR extension unusable, disabling.\n");
00194 
00195     Output *s = scalloc(sizeof(Output));
00196 
00197     s->active = true;
00198     s->rect.x = 0;
00199     s->rect.y = 0;
00200     s->rect.width = root_screen->width_in_pixels;
00201     s->rect.height = root_screen->height_in_pixels;
00202     s->name = "xroot-0";
00203     output_init_con(s);
00204     init_ws_for_output(s, output_get_content(s->con));
00205 
00206     TAILQ_INSERT_TAIL(&outputs, s, outputs);
00207 
00208     randr_disabled = true;
00209 }
00210 
00211 /*
00212  * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
00213  * before) to use for the given Output.
00214  *
00215  */
00216 void output_init_con(Output *output) {
00217     Con *con = NULL, *current;
00218     bool reused = false;
00219 
00220     DLOG("init_con for output %s\n", output->name);
00221 
00222     /* Search for a Con with that name directly below the root node. There
00223      * might be one from a restored layout. */
00224     TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
00225         if (strcmp(current->name, output->name) != 0)
00226             continue;
00227 
00228         con = current;
00229         reused = true;
00230         DLOG("Using existing con %p / %s\n", con, con->name);
00231         break;
00232     }
00233 
00234     if (con == NULL) {
00235         con = con_new(croot, NULL);
00236         FREE(con->name);
00237         con->name = sstrdup(output->name);
00238         con->type = CT_OUTPUT;
00239         con->layout = L_OUTPUT;
00240         con_fix_percent(croot);
00241     }
00242     con->rect = output->rect;
00243     output->con = con;
00244 
00245     char *name;
00246     sasprintf(&name, "[i3 con] output %s", con->name);
00247     x_set_name(con, name);
00248     FREE(name);
00249 
00250     if (reused) {
00251         DLOG("Not adding workspace, this was a reused con\n");
00252         return;
00253     }
00254 
00255     DLOG("Changing layout, adding top/bottom dockarea\n");
00256     Con *topdock = con_new(NULL, NULL);
00257     topdock->type = CT_DOCKAREA;
00258     topdock->layout = L_DOCKAREA;
00259     topdock->orientation = VERT;
00260     /* this container swallows dock clients */
00261     Match *match = scalloc(sizeof(Match));
00262     match_init(match);
00263     match->dock = M_DOCK_TOP;
00264     match->insert_where = M_BELOW;
00265     TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
00266 
00267     FREE(topdock->name);
00268     topdock->name = sstrdup("topdock");
00269 
00270     sasprintf(&name, "[i3 con] top dockarea %s", con->name);
00271     x_set_name(topdock, name);
00272     FREE(name);
00273     DLOG("attaching\n");
00274     con_attach(topdock, con, false);
00275 
00276     /* content container */
00277 
00278     DLOG("adding main content container\n");
00279     Con *content = con_new(NULL, NULL);
00280     content->type = CT_CON;
00281     FREE(content->name);
00282     content->name = sstrdup("content");
00283 
00284     sasprintf(&name, "[i3 con] content %s", con->name);
00285     x_set_name(content, name);
00286     FREE(name);
00287     con_attach(content, con, false);
00288 
00289     /* bottom dock container */
00290     Con *bottomdock = con_new(NULL, NULL);
00291     bottomdock->type = CT_DOCKAREA;
00292     bottomdock->layout = L_DOCKAREA;
00293     bottomdock->orientation = VERT;
00294     /* this container swallows dock clients */
00295     match = scalloc(sizeof(Match));
00296     match_init(match);
00297     match->dock = M_DOCK_BOTTOM;
00298     match->insert_where = M_BELOW;
00299     TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
00300 
00301     FREE(bottomdock->name);
00302     bottomdock->name = sstrdup("bottomdock");
00303 
00304     sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
00305     x_set_name(bottomdock, name);
00306     FREE(name);
00307     DLOG("attaching\n");
00308     con_attach(bottomdock, con, false);
00309 }
00310 
00311 /*
00312  * Initializes at least one workspace for this output, trying the following
00313  * steps until there is at least one workspace:
00314  *
00315  * • Move existing workspaces, which are assigned to be on the given output, to
00316  *   the output.
00317  * • Create the first assigned workspace for this output.
00318  * • Create the first unused workspace.
00319  *
00320  */
00321 void init_ws_for_output(Output *output, Con *content) {
00322     char *name;
00323 
00324     /* go through all assignments and move the existing workspaces to this output */
00325     struct Workspace_Assignment *assignment;
00326     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
00327         if (strcmp(assignment->output, output->name) != 0)
00328             continue;
00329 
00330         /* check if this workspace actually exists */
00331         Con *workspace = NULL, *out;
00332         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
00333             GREP_FIRST(workspace, output_get_content(out),
00334                        !strcasecmp(child->name, assignment->name));
00335         if (workspace == NULL)
00336             continue;
00337 
00338         /* check that this workspace is not already attached (that means the
00339          * user configured this assignment twice) */
00340         Con *workspace_out = con_get_output(workspace);
00341         if (workspace_out == output->con) {
00342             LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
00343                 "there. Do you have two assignment directives for the same "
00344                 "workspace in your configuration file?\n",
00345                 workspace->name, output->name);
00346             continue;
00347         }
00348 
00349         /* if so, move it over */
00350         LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
00351             workspace->name, workspace_out->name, output->name);
00352 
00353         /* if the workspace is currently visible on that output, we need to
00354          * switch to a different workspace - otherwise the output would end up
00355          * with no active workspace */
00356         bool visible = workspace_is_visible(workspace);
00357         Con *previous = NULL;
00358         if (visible && (previous = TAILQ_NEXT(workspace, focused))) {
00359             LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n",
00360                 previous->name, workspace_out->name);
00361             workspace_show(previous);
00362         }
00363 
00364         con_detach(workspace);
00365         con_attach(workspace, content, false);
00366 
00367         /* In case the workspace we just moved was visible but there was no
00368          * other workspace to switch to, we need to initialize the source
00369          * output aswell */
00370         if (visible && previous == NULL) {
00371             LOG("There is no workspace left on \"%s\", re-initializing\n",
00372                 workspace_out->name);
00373             init_ws_for_output(get_output_by_name(workspace_out->name),
00374                                output_get_content(workspace_out));
00375             DLOG("Done re-initializing, continuing with \"%s\"\n", output->name);
00376         }
00377     }
00378 
00379     /* if a workspace exists, we are done now */
00380     if (!TAILQ_EMPTY(&(content->nodes_head))) {
00381         /* ensure that one of the workspaces is actually visible (in fullscreen
00382          * mode), if they were invisible before, this might not be the case. */
00383         Con *visible = NULL;
00384         GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
00385         if (!visible) {
00386             visible = TAILQ_FIRST(&(content->nodes_head));
00387             focused = content;
00388             workspace_show(visible);
00389         }
00390         return;
00391     }
00392 
00393     /* otherwise, we create the first assigned ws for this output */
00394     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
00395         if (strcmp(assignment->output, output->name) != 0)
00396             continue;
00397 
00398         LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
00399             assignment->name, assignment->output);
00400         focused = content;
00401         workspace_show_by_name(assignment->name);
00402         return;
00403     }
00404 
00405     /* if there is still no workspace, we create the first free workspace */
00406     DLOG("Now adding a workspace\n");
00407 
00408     /* add a workspace to this output */
00409     Con *out, *current;
00410     bool exists = true;
00411     Con *ws = con_new(NULL, NULL);
00412     ws->type = CT_WORKSPACE;
00413 
00414     /* try the configured workspace bindings first to find a free name */
00415     Binding *bind;
00416     TAILQ_FOREACH(bind, bindings, bindings) {
00417         DLOG("binding with command %s\n", bind->command);
00418         if (strlen(bind->command) < strlen("workspace ") ||
00419             strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
00420             continue;
00421         DLOG("relevant command = %s\n", bind->command);
00422         char *target = bind->command + strlen("workspace ");
00423         /* We check if this is the workspace next/prev/back_and_forth command.
00424          * Beware: The workspace names "next", "prev" and "back_and_forth" are
00425          * OK, so we check before stripping the double quotes */
00426         if (strncasecmp(target, "next", strlen("next")) == 0 ||
00427             strncasecmp(target, "prev", strlen("prev")) == 0 ||
00428             strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0)
00429             continue;
00430         if (*target == '"')
00431             target++;
00432         FREE(ws->name);
00433         ws->name = strdup(target);
00434         if (ws->name[strlen(ws->name)-1] == '"')
00435             ws->name[strlen(ws->name)-1] = '\0';
00436         DLOG("trying name *%s*\n", ws->name);
00437 
00438         current = NULL;
00439         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
00440             GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
00441 
00442         exists = (current != NULL);
00443         if (!exists) {
00444             /* Set ->num to the number of the workspace, if the name actually
00445              * is a number or starts with a number */
00446             char *endptr = NULL;
00447             long parsed_num = strtol(ws->name, &endptr, 10);
00448             if (parsed_num == LONG_MIN ||
00449                 parsed_num == LONG_MAX ||
00450                 parsed_num < 0 ||
00451                 endptr == ws->name)
00452                 ws->num = -1;
00453             else ws->num = parsed_num;
00454             LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
00455 
00456             break;
00457         }
00458     }
00459 
00460     if (exists) {
00461         /* get the next unused workspace number */
00462         DLOG("Getting next unused workspace by number\n");
00463         int c = 0;
00464         while (exists) {
00465             c++;
00466 
00467             FREE(ws->name);
00468             sasprintf(&(ws->name), "%d", c);
00469 
00470             current = NULL;
00471             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
00472                 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
00473             exists = (current != NULL);
00474 
00475             DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
00476         }
00477         ws->num = c;
00478     }
00479     con_attach(ws, content, false);
00480 
00481     sasprintf(&name, "[i3 con] workspace %s", ws->name);
00482     x_set_name(ws, name);
00483     free(name);
00484 
00485     ws->fullscreen_mode = CF_OUTPUT;
00486 
00487     /* If default_orientation is set to NO_ORIENTATION we determine
00488      * orientation depending on output resolution. */
00489     if (config.default_orientation == NO_ORIENTATION) {
00490         ws->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
00491         DLOG("Auto orientation. Workspace size set to (%d,%d), setting orientation to %d.\n",
00492              output->rect.width, output->rect.height, ws->orientation);
00493     } else {
00494         ws->orientation = config.default_orientation;
00495     }
00496 
00497 
00498     /* TODO: Set focus in main.c */
00499     con_focus(ws);
00500 }
00501 
00502 /*
00503  * This function needs to be called when changing the mode of an output when
00504  * it already has some workspaces (or a bar window) assigned.
00505  *
00506  * It reconfigures the bar window for the new mode, copies the new rect into
00507  * each workspace on this output and forces all windows on the affected
00508  * workspaces to be reconfigured.
00509  *
00510  * It is necessary to call render_layout() afterwards.
00511  *
00512  */
00513 static void output_change_mode(xcb_connection_t *conn, Output *output) {
00514     DLOG("Output mode changed, updating rect\n");
00515     assert(output->con != NULL);
00516     output->con->rect = output->rect;
00517 
00518     Con *content, *workspace, *child;
00519 
00520     /* Point content to the container of the workspaces */
00521     content = output_get_content(output->con);
00522 
00523     /* Fix the position of all floating windows on this output.
00524      * The 'rect' of each workspace will be updated in src/render.c. */
00525     TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
00526         TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
00527             floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
00528         }
00529     }
00530 
00531     /* If default_orientation is NO_ORIENTATION, we change the orientation of
00532      * the workspaces and their childs depending on output resolution. This is
00533      * only done for workspaces with maximum one child. */
00534     if (config.default_orientation == NO_ORIENTATION) {
00535         TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
00536             /* Workspaces with more than one child are left untouched because
00537              * we do not want to change an existing layout. */
00538             if (con_num_children(workspace) > 1)
00539                 continue;
00540 
00541             workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
00542             DLOG("Setting workspace [%d,%s]'s orientation to %d.\n", workspace->num, workspace->name, workspace->orientation);
00543             if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
00544                 child->orientation = workspace->orientation;
00545                 DLOG("Setting child [%d,%s]'s orientation to %d.\n", child->num, child->name, child->orientation);
00546             }
00547         }
00548     }
00549 }
00550 
00551 /*
00552  * Gets called by randr_query_outputs() for each output. The function adds new
00553  * outputs to the list of outputs, checks if the mode of existing outputs has
00554  * been changed or if an existing output has been disabled. It will then change
00555  * either the "changed" or the "to_be_deleted" flag of the output, if
00556  * appropriate.
00557  *
00558  */
00559 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
00560                           xcb_randr_get_output_info_reply_t *output,
00561                           xcb_timestamp_t cts, resources_reply *res) {
00562     /* each CRT controller has a position in which we are interested in */
00563     crtc_info *crtc;
00564 
00565     Output *new = get_output_by_id(id);
00566     bool existing = (new != NULL);
00567     if (!existing)
00568         new = scalloc(sizeof(Output));
00569     new->id = id;
00570     new->primary = (primary && primary->output == id);
00571     FREE(new->name);
00572     sasprintf(&new->name, "%.*s",
00573             xcb_randr_get_output_info_name_length(output),
00574             xcb_randr_get_output_info_name(output));
00575 
00576     DLOG("found output with name %s\n", new->name);
00577 
00578     /* Even if no CRTC is used at the moment, we store the output so that
00579      * we do not need to change the list ever again (we only update the
00580      * position/size) */
00581     if (output->crtc == XCB_NONE) {
00582         if (!existing) {
00583             if (new->primary)
00584                 TAILQ_INSERT_HEAD(&outputs, new, outputs);
00585             else TAILQ_INSERT_TAIL(&outputs, new, outputs);
00586         } else if (new->active)
00587             new->to_be_disabled = true;
00588         return;
00589     }
00590 
00591     xcb_randr_get_crtc_info_cookie_t icookie;
00592     icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
00593     if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
00594         DLOG("Skipping output %s: could not get CRTC (%p)\n",
00595              new->name, crtc);
00596         free(new);
00597         return;
00598     }
00599 
00600     bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
00601                    update_if_necessary(&(new->rect.y), crtc->y) |
00602                    update_if_necessary(&(new->rect.width), crtc->width) |
00603                    update_if_necessary(&(new->rect.height), crtc->height);
00604     free(crtc);
00605     new->active = (new->rect.width != 0 && new->rect.height != 0);
00606     if (!new->active) {
00607         DLOG("width/height 0/0, disabling output\n");
00608         return;
00609     }
00610 
00611     DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
00612                                 new->rect.x, new->rect.y);
00613 
00614     /* If we don’t need to change an existing output or if the output
00615      * does not exist in the first place, the case is simple: we either
00616      * need to insert the new output or we are done. */
00617     if (!updated || !existing) {
00618         if (!existing) {
00619             if (new->primary)
00620                 TAILQ_INSERT_HEAD(&outputs, new, outputs);
00621             else TAILQ_INSERT_TAIL(&outputs, new, outputs);
00622         }
00623         return;
00624     }
00625 
00626     new->changed = true;
00627 }
00628 
00629 /*
00630  * (Re-)queries the outputs via RandR and stores them in the list of outputs.
00631  *
00632  */
00633 void randr_query_outputs() {
00634     Output *output, *other, *first;
00635     xcb_randr_get_output_primary_cookie_t pcookie;
00636     xcb_randr_get_screen_resources_current_cookie_t rcookie;
00637     resources_reply *res;
00638 
00639     /* timestamp of the configuration so that we get consistent replies to all
00640      * requests (if the configuration changes between our different calls) */
00641     xcb_timestamp_t cts;
00642 
00643     /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
00644     xcb_randr_output_t *randr_outputs;
00645 
00646     if (randr_disabled)
00647         return;
00648 
00649     /* Get screen resources (primary output, crtcs, outputs, modes) */
00650     rcookie = xcb_randr_get_screen_resources_current(conn, root);
00651     pcookie = xcb_randr_get_output_primary(conn, root);
00652 
00653     if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
00654         ELOG("Could not get RandR primary output\n");
00655     else DLOG("primary output is %08x\n", primary->output);
00656     if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
00657         disable_randr(conn);
00658         return;
00659     }
00660     cts = res->config_timestamp;
00661 
00662     int len = xcb_randr_get_screen_resources_current_outputs_length(res);
00663     randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
00664 
00665     /* Request information for each output */
00666     xcb_randr_get_output_info_cookie_t ocookie[len];
00667     for (int i = 0; i < len; i++)
00668         ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
00669 
00670     /* Loop through all outputs available for this X11 screen */
00671     for (int i = 0; i < len; i++) {
00672         xcb_randr_get_output_info_reply_t *output;
00673 
00674         if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
00675             continue;
00676 
00677         handle_output(conn, randr_outputs[i], output, cts, res);
00678         free(output);
00679     }
00680 
00681     /* Check for clones, disable the clones and reduce the mode to the
00682      * lowest common mode */
00683     TAILQ_FOREACH(output, &outputs, outputs) {
00684         if (!output->active || output->to_be_disabled)
00685             continue;
00686         DLOG("output %p / %s, position (%d, %d), checking for clones\n",
00687                 output, output->name, output->rect.x, output->rect.y);
00688 
00689         for (other = output;
00690              other != TAILQ_END(&outputs);
00691              other = TAILQ_NEXT(other, outputs)) {
00692             if (other == output || !other->active || other->to_be_disabled)
00693                 continue;
00694 
00695             if (other->rect.x != output->rect.x ||
00696                 other->rect.y != output->rect.y)
00697                 continue;
00698 
00699             DLOG("output %p has the same position, his mode = %d x %d\n",
00700                             other, other->rect.width, other->rect.height);
00701             uint32_t width = min(other->rect.width, output->rect.width);
00702             uint32_t height = min(other->rect.height, output->rect.height);
00703 
00704             if (update_if_necessary(&(output->rect.width), width) |
00705                 update_if_necessary(&(output->rect.height), height))
00706                 output->changed = true;
00707 
00708             update_if_necessary(&(other->rect.width), width);
00709             update_if_necessary(&(other->rect.height), height);
00710 
00711             DLOG("disabling output %p (%s)\n", other, other->name);
00712             other->to_be_disabled = true;
00713 
00714             DLOG("new output mode %d x %d, other mode %d x %d\n",
00715                             output->rect.width, output->rect.height,
00716                             other->rect.width, other->rect.height);
00717         }
00718     }
00719 
00720     /* Ensure that all outputs which are active also have a con. This is
00721      * necessary because in the next step, a clone might get disabled. Example:
00722      * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
00723      * LVDS1 gets disabled. */
00724     TAILQ_FOREACH(output, &outputs, outputs) {
00725         if (output->active && output->con == NULL) {
00726             DLOG("Need to initialize a Con for output %s\n", output->name);
00727             output_init_con(output);
00728             output->changed = false;
00729         }
00730     }
00731 
00732     /* Handle outputs which have a new mode or are disabled now (either
00733      * because the user disabled them or because they are clones) */
00734     TAILQ_FOREACH(output, &outputs, outputs) {
00735         if (output->to_be_disabled) {
00736             output->active = false;
00737             DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name);
00738 
00739             if ((first = get_first_output()) == NULL)
00740                 die("No usable outputs available\n");
00741 
00742             /* TODO: refactor the following code into a nice function. maybe
00743              * use an on_destroy callback which is implement differently for
00744              * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
00745             Con *first_content = output_get_content(first->con);
00746 
00747             if (output->con != NULL) {
00748                 /* We need to move the workspaces from the disappearing output to the first output */
00749                 /* 1: Get the con to focus next, if the disappearing ws is focused */
00750                 Con *next = NULL;
00751                 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
00752                     DLOG("This output (%p) was focused! Getting next\n", output->con);
00753                     next = focused;
00754                     DLOG("next = %p\n", next);
00755                 }
00756 
00757                 /* 2: iterate through workspaces and re-assign them */
00758                 Con *current;
00759                 Con *old_content = output_get_content(output->con);
00760                 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
00761                     current = TAILQ_FIRST(&(old_content->nodes_head));
00762                     DLOG("Detaching current = %p / %s\n", current, current->name);
00763                     con_detach(current);
00764                     DLOG("Re-attaching current = %p / %s\n", current, current->name);
00765                     con_attach(current, first_content, false);
00766                     DLOG("Done, next\n");
00767                 }
00768                 DLOG("re-attached all workspaces\n");
00769 
00770                 if (next) {
00771                     DLOG("now focusing next = %p\n", next);
00772                     con_focus(next);
00773                     workspace_show(con_get_workspace(next));
00774                 }
00775 
00776                 /* 3: move the dock clients to the first output */
00777                 Con *child;
00778                 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
00779                     if (child->type != CT_DOCKAREA)
00780                         continue;
00781                     DLOG("Handling dock con %p\n", child);
00782                     Con *dock;
00783                     while (!TAILQ_EMPTY(&(child->nodes_head))) {
00784                         dock = TAILQ_FIRST(&(child->nodes_head));
00785                         Con *nc;
00786                         Match *match;
00787                         nc = con_for_window(first->con, dock->window, &match);
00788                         DLOG("Moving dock client %p to nc %p\n", dock, nc);
00789                         con_detach(dock);
00790                         DLOG("Re-attaching\n");
00791                         con_attach(dock, nc, false);
00792                         DLOG("Done\n");
00793                     }
00794                 }
00795 
00796                 DLOG("destroying disappearing con %p\n", output->con);
00797                 tree_close(output->con, DONT_KILL_WINDOW, true, false);
00798                 DLOG("Done. Should be fine now\n");
00799                 output->con = NULL;
00800             }
00801 
00802             output->to_be_disabled = false;
00803             output->changed = false;
00804         }
00805 
00806         if (output->changed) {
00807             output_change_mode(conn, output);
00808             output->changed = false;
00809         }
00810     }
00811 
00812     if (TAILQ_EMPTY(&outputs)) {
00813         ELOG("No outputs found via RandR, disabling\n");
00814         disable_randr(conn);
00815     }
00816 
00817     /* Just go through each active output and assign one workspace */
00818     TAILQ_FOREACH(output, &outputs, outputs) {
00819         if (!output->active)
00820             continue;
00821         Con *content = output_get_content(output->con);
00822         if (!TAILQ_EMPTY(&(content->nodes_head)))
00823             continue;
00824         DLOG("Should add ws for output %s\n", output->name);
00825         init_ws_for_output(output, content);
00826     }
00827 
00828     /* Focus the primary screen, if possible */
00829     TAILQ_FOREACH(output, &outputs, outputs) {
00830         if (!output->primary || !output->con)
00831             continue;
00832 
00833         DLOG("Focusing primary output %s\n", output->name);
00834         con_focus(con_descend_focused(output->con));
00835     }
00836 
00837     /* render_layout flushes */
00838     tree_render();
00839 
00840     FREE(res);
00841     FREE(primary);
00842 }
00843 
00844 /*
00845  * We have just established a connection to the X server and need the initial
00846  * XRandR information to setup workspaces for each screen.
00847  *
00848  */
00849 void randr_init(int *event_base) {
00850     const xcb_query_extension_reply_t *extreply;
00851 
00852     extreply = xcb_get_extension_data(conn, &xcb_randr_id);
00853     if (!extreply->present) {
00854         disable_randr(conn);
00855         return;
00856     } else randr_query_outputs();
00857 
00858     if (event_base != NULL)
00859         *event_base = extreply->first_event;
00860 
00861     xcb_randr_select_input(conn, root,
00862             XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
00863             XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
00864             XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
00865             XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
00866 
00867     xcb_flush(conn);
00868 }