i3
|
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(void) { 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 /* go through all assignments and move the existing workspaces to this output */ 00323 struct Workspace_Assignment *assignment; 00324 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) { 00325 if (strcmp(assignment->output, output->name) != 0) 00326 continue; 00327 00328 /* check if this workspace actually exists */ 00329 Con *workspace = NULL, *out; 00330 TAILQ_FOREACH(out, &(croot->nodes_head), nodes) 00331 GREP_FIRST(workspace, output_get_content(out), 00332 !strcasecmp(child->name, assignment->name)); 00333 if (workspace == NULL) 00334 continue; 00335 00336 /* check that this workspace is not already attached (that means the 00337 * user configured this assignment twice) */ 00338 Con *workspace_out = con_get_output(workspace); 00339 if (workspace_out == output->con) { 00340 LOG("Workspace \"%s\" assigned to output \"%s\", but it is already " 00341 "there. Do you have two assignment directives for the same " 00342 "workspace in your configuration file?\n", 00343 workspace->name, output->name); 00344 continue; 00345 } 00346 00347 /* if so, move it over */ 00348 LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n", 00349 workspace->name, workspace_out->name, output->name); 00350 00351 /* if the workspace is currently visible on that output, we need to 00352 * switch to a different workspace - otherwise the output would end up 00353 * with no active workspace */ 00354 bool visible = workspace_is_visible(workspace); 00355 Con *previous = NULL; 00356 if (visible && (previous = TAILQ_NEXT(workspace, focused))) { 00357 LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n", 00358 previous->name, workspace_out->name); 00359 workspace_show(previous); 00360 } 00361 00362 con_detach(workspace); 00363 con_attach(workspace, content, false); 00364 00365 /* In case the workspace we just moved was visible but there was no 00366 * other workspace to switch to, we need to initialize the source 00367 * output aswell */ 00368 if (visible && previous == NULL) { 00369 LOG("There is no workspace left on \"%s\", re-initializing\n", 00370 workspace_out->name); 00371 init_ws_for_output(get_output_by_name(workspace_out->name), 00372 output_get_content(workspace_out)); 00373 DLOG("Done re-initializing, continuing with \"%s\"\n", output->name); 00374 } 00375 } 00376 00377 /* if a workspace exists, we are done now */ 00378 if (!TAILQ_EMPTY(&(content->nodes_head))) { 00379 /* ensure that one of the workspaces is actually visible (in fullscreen 00380 * mode), if they were invisible before, this might not be the case. */ 00381 Con *visible = NULL; 00382 GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT); 00383 if (!visible) { 00384 visible = TAILQ_FIRST(&(content->nodes_head)); 00385 focused = content; 00386 workspace_show(visible); 00387 } 00388 return; 00389 } 00390 00391 /* otherwise, we create the first assigned ws for this output */ 00392 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) { 00393 if (strcmp(assignment->output, output->name) != 0) 00394 continue; 00395 00396 LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n", 00397 assignment->name, assignment->output); 00398 focused = content; 00399 workspace_show_by_name(assignment->name); 00400 return; 00401 } 00402 00403 /* if there is still no workspace, we create the first free workspace */ 00404 DLOG("Now adding a workspace\n"); 00405 Con *ws = create_workspace_on_output(output, content); 00406 00407 /* TODO: Set focus in main.c */ 00408 con_focus(ws); 00409 } 00410 00411 /* 00412 * This function needs to be called when changing the mode of an output when 00413 * it already has some workspaces (or a bar window) assigned. 00414 * 00415 * It reconfigures the bar window for the new mode, copies the new rect into 00416 * each workspace on this output and forces all windows on the affected 00417 * workspaces to be reconfigured. 00418 * 00419 * It is necessary to call render_layout() afterwards. 00420 * 00421 */ 00422 static void output_change_mode(xcb_connection_t *conn, Output *output) { 00423 DLOG("Output mode changed, updating rect\n"); 00424 assert(output->con != NULL); 00425 output->con->rect = output->rect; 00426 00427 Con *content, *workspace, *child; 00428 00429 /* Point content to the container of the workspaces */ 00430 content = output_get_content(output->con); 00431 00432 /* Fix the position of all floating windows on this output. 00433 * The 'rect' of each workspace will be updated in src/render.c. */ 00434 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) { 00435 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) { 00436 floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect)); 00437 } 00438 } 00439 00440 /* If default_orientation is NO_ORIENTATION, we change the orientation of 00441 * the workspaces and their childs depending on output resolution. This is 00442 * only done for workspaces with maximum one child. */ 00443 if (config.default_orientation == NO_ORIENTATION) { 00444 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) { 00445 /* Workspaces with more than one child are left untouched because 00446 * we do not want to change an existing layout. */ 00447 if (con_num_children(workspace) > 1) 00448 continue; 00449 00450 workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ; 00451 DLOG("Setting workspace [%d,%s]'s orientation to %d.\n", workspace->num, workspace->name, workspace->orientation); 00452 if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) { 00453 child->orientation = workspace->orientation; 00454 DLOG("Setting child [%d,%s]'s orientation to %d.\n", child->num, child->name, child->orientation); 00455 } 00456 } 00457 } 00458 } 00459 00460 /* 00461 * Gets called by randr_query_outputs() for each output. The function adds new 00462 * outputs to the list of outputs, checks if the mode of existing outputs has 00463 * been changed or if an existing output has been disabled. It will then change 00464 * either the "changed" or the "to_be_deleted" flag of the output, if 00465 * appropriate. 00466 * 00467 */ 00468 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id, 00469 xcb_randr_get_output_info_reply_t *output, 00470 xcb_timestamp_t cts, resources_reply *res) { 00471 /* each CRT controller has a position in which we are interested in */ 00472 crtc_info *crtc; 00473 00474 Output *new = get_output_by_id(id); 00475 bool existing = (new != NULL); 00476 if (!existing) 00477 new = scalloc(sizeof(Output)); 00478 new->id = id; 00479 new->primary = (primary && primary->output == id); 00480 FREE(new->name); 00481 sasprintf(&new->name, "%.*s", 00482 xcb_randr_get_output_info_name_length(output), 00483 xcb_randr_get_output_info_name(output)); 00484 00485 DLOG("found output with name %s\n", new->name); 00486 00487 /* Even if no CRTC is used at the moment, we store the output so that 00488 * we do not need to change the list ever again (we only update the 00489 * position/size) */ 00490 if (output->crtc == XCB_NONE) { 00491 if (!existing) { 00492 if (new->primary) 00493 TAILQ_INSERT_HEAD(&outputs, new, outputs); 00494 else TAILQ_INSERT_TAIL(&outputs, new, outputs); 00495 } else if (new->active) 00496 new->to_be_disabled = true; 00497 return; 00498 } 00499 00500 xcb_randr_get_crtc_info_cookie_t icookie; 00501 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts); 00502 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) { 00503 DLOG("Skipping output %s: could not get CRTC (%p)\n", 00504 new->name, crtc); 00505 free(new); 00506 return; 00507 } 00508 00509 bool updated = update_if_necessary(&(new->rect.x), crtc->x) | 00510 update_if_necessary(&(new->rect.y), crtc->y) | 00511 update_if_necessary(&(new->rect.width), crtc->width) | 00512 update_if_necessary(&(new->rect.height), crtc->height); 00513 free(crtc); 00514 new->active = (new->rect.width != 0 && new->rect.height != 0); 00515 if (!new->active) { 00516 DLOG("width/height 0/0, disabling output\n"); 00517 return; 00518 } 00519 00520 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height, 00521 new->rect.x, new->rect.y); 00522 00523 /* If we don’t need to change an existing output or if the output 00524 * does not exist in the first place, the case is simple: we either 00525 * need to insert the new output or we are done. */ 00526 if (!updated || !existing) { 00527 if (!existing) { 00528 if (new->primary) 00529 TAILQ_INSERT_HEAD(&outputs, new, outputs); 00530 else TAILQ_INSERT_TAIL(&outputs, new, outputs); 00531 } 00532 return; 00533 } 00534 00535 new->changed = true; 00536 } 00537 00538 /* 00539 * (Re-)queries the outputs via RandR and stores them in the list of outputs. 00540 * 00541 */ 00542 void randr_query_outputs(void) { 00543 Output *output, *other, *first; 00544 xcb_randr_get_output_primary_cookie_t pcookie; 00545 xcb_randr_get_screen_resources_current_cookie_t rcookie; 00546 resources_reply *res; 00547 00548 /* timestamp of the configuration so that we get consistent replies to all 00549 * requests (if the configuration changes between our different calls) */ 00550 xcb_timestamp_t cts; 00551 00552 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */ 00553 xcb_randr_output_t *randr_outputs; 00554 00555 if (randr_disabled) 00556 return; 00557 00558 /* Get screen resources (primary output, crtcs, outputs, modes) */ 00559 rcookie = xcb_randr_get_screen_resources_current(conn, root); 00560 pcookie = xcb_randr_get_output_primary(conn, root); 00561 00562 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) 00563 ELOG("Could not get RandR primary output\n"); 00564 else DLOG("primary output is %08x\n", primary->output); 00565 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) { 00566 disable_randr(conn); 00567 return; 00568 } 00569 cts = res->config_timestamp; 00570 00571 int len = xcb_randr_get_screen_resources_current_outputs_length(res); 00572 randr_outputs = xcb_randr_get_screen_resources_current_outputs(res); 00573 00574 /* Request information for each output */ 00575 xcb_randr_get_output_info_cookie_t ocookie[len]; 00576 for (int i = 0; i < len; i++) 00577 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts); 00578 00579 /* Loop through all outputs available for this X11 screen */ 00580 for (int i = 0; i < len; i++) { 00581 xcb_randr_get_output_info_reply_t *output; 00582 00583 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL) 00584 continue; 00585 00586 handle_output(conn, randr_outputs[i], output, cts, res); 00587 free(output); 00588 } 00589 00590 /* Check for clones, disable the clones and reduce the mode to the 00591 * lowest common mode */ 00592 TAILQ_FOREACH(output, &outputs, outputs) { 00593 if (!output->active || output->to_be_disabled) 00594 continue; 00595 DLOG("output %p / %s, position (%d, %d), checking for clones\n", 00596 output, output->name, output->rect.x, output->rect.y); 00597 00598 for (other = output; 00599 other != TAILQ_END(&outputs); 00600 other = TAILQ_NEXT(other, outputs)) { 00601 if (other == output || !other->active || other->to_be_disabled) 00602 continue; 00603 00604 if (other->rect.x != output->rect.x || 00605 other->rect.y != output->rect.y) 00606 continue; 00607 00608 DLOG("output %p has the same position, his mode = %d x %d\n", 00609 other, other->rect.width, other->rect.height); 00610 uint32_t width = min(other->rect.width, output->rect.width); 00611 uint32_t height = min(other->rect.height, output->rect.height); 00612 00613 if (update_if_necessary(&(output->rect.width), width) | 00614 update_if_necessary(&(output->rect.height), height)) 00615 output->changed = true; 00616 00617 update_if_necessary(&(other->rect.width), width); 00618 update_if_necessary(&(other->rect.height), height); 00619 00620 DLOG("disabling output %p (%s)\n", other, other->name); 00621 other->to_be_disabled = true; 00622 00623 DLOG("new output mode %d x %d, other mode %d x %d\n", 00624 output->rect.width, output->rect.height, 00625 other->rect.width, other->rect.height); 00626 } 00627 } 00628 00629 /* Ensure that all outputs which are active also have a con. This is 00630 * necessary because in the next step, a clone might get disabled. Example: 00631 * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con). 00632 * LVDS1 gets disabled. */ 00633 TAILQ_FOREACH(output, &outputs, outputs) { 00634 if (output->active && output->con == NULL) { 00635 DLOG("Need to initialize a Con for output %s\n", output->name); 00636 output_init_con(output); 00637 output->changed = false; 00638 } 00639 } 00640 00641 /* Handle outputs which have a new mode or are disabled now (either 00642 * because the user disabled them or because they are clones) */ 00643 TAILQ_FOREACH(output, &outputs, outputs) { 00644 if (output->to_be_disabled) { 00645 output->active = false; 00646 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name); 00647 00648 if ((first = get_first_output()) == NULL) 00649 die("No usable outputs available\n"); 00650 00651 /* TODO: refactor the following code into a nice function. maybe 00652 * use an on_destroy callback which is implement differently for 00653 * different container types (CT_CONTENT vs. CT_DOCKAREA)? */ 00654 Con *first_content = output_get_content(first->con); 00655 00656 if (output->con != NULL) { 00657 /* We need to move the workspaces from the disappearing output to the first output */ 00658 /* 1: Get the con to focus next, if the disappearing ws is focused */ 00659 Con *next = NULL; 00660 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) { 00661 DLOG("This output (%p) was focused! Getting next\n", output->con); 00662 next = focused; 00663 DLOG("next = %p\n", next); 00664 } 00665 00666 /* 2: iterate through workspaces and re-assign them, fixing the coordinates 00667 * of floating containers as we go */ 00668 Con *current; 00669 Con *old_content = output_get_content(output->con); 00670 while (!TAILQ_EMPTY(&(old_content->nodes_head))) { 00671 current = TAILQ_FIRST(&(old_content->nodes_head)); 00672 if (current != next && TAILQ_EMPTY(&(current->focus_head))) { 00673 /* the workspace is empty and not focused, get rid of it */ 00674 DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name); 00675 tree_close(current, DONT_KILL_WINDOW, false, false); 00676 continue; 00677 } 00678 DLOG("Detaching current = %p / %s\n", current, current->name); 00679 con_detach(current); 00680 DLOG("Re-attaching current = %p / %s\n", current, current->name); 00681 con_attach(current, first_content, false); 00682 DLOG("Fixing the coordinates of floating containers\n"); 00683 Con *floating_con; 00684 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows) 00685 floating_fix_coordinates(floating_con, &(old_content->rect), &(first_content->rect)); 00686 DLOG("Done, next\n"); 00687 } 00688 DLOG("re-attached all workspaces\n"); 00689 00690 if (next) { 00691 DLOG("now focusing next = %p\n", next); 00692 con_focus(next); 00693 workspace_show(con_get_workspace(next)); 00694 } 00695 00696 /* 3: move the dock clients to the first output */ 00697 Con *child; 00698 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) { 00699 if (child->type != CT_DOCKAREA) 00700 continue; 00701 DLOG("Handling dock con %p\n", child); 00702 Con *dock; 00703 while (!TAILQ_EMPTY(&(child->nodes_head))) { 00704 dock = TAILQ_FIRST(&(child->nodes_head)); 00705 Con *nc; 00706 Match *match; 00707 nc = con_for_window(first->con, dock->window, &match); 00708 DLOG("Moving dock client %p to nc %p\n", dock, nc); 00709 con_detach(dock); 00710 DLOG("Re-attaching\n"); 00711 con_attach(dock, nc, false); 00712 DLOG("Done\n"); 00713 } 00714 } 00715 00716 DLOG("destroying disappearing con %p\n", output->con); 00717 tree_close(output->con, DONT_KILL_WINDOW, true, false); 00718 DLOG("Done. Should be fine now\n"); 00719 output->con = NULL; 00720 } 00721 00722 output->to_be_disabled = false; 00723 output->changed = false; 00724 } 00725 00726 if (output->changed) { 00727 output_change_mode(conn, output); 00728 output->changed = false; 00729 } 00730 } 00731 00732 if (TAILQ_EMPTY(&outputs)) { 00733 ELOG("No outputs found via RandR, disabling\n"); 00734 disable_randr(conn); 00735 } 00736 00737 /* Just go through each active output and assign one workspace */ 00738 TAILQ_FOREACH(output, &outputs, outputs) { 00739 if (!output->active) 00740 continue; 00741 Con *content = output_get_content(output->con); 00742 if (!TAILQ_EMPTY(&(content->nodes_head))) 00743 continue; 00744 DLOG("Should add ws for output %s\n", output->name); 00745 init_ws_for_output(output, content); 00746 } 00747 00748 /* Focus the primary screen, if possible */ 00749 TAILQ_FOREACH(output, &outputs, outputs) { 00750 if (!output->primary || !output->con) 00751 continue; 00752 00753 DLOG("Focusing primary output %s\n", output->name); 00754 con_focus(con_descend_focused(output->con)); 00755 } 00756 00757 /* render_layout flushes */ 00758 tree_render(); 00759 00760 FREE(res); 00761 FREE(primary); 00762 } 00763 00764 /* 00765 * We have just established a connection to the X server and need the initial 00766 * XRandR information to setup workspaces for each screen. 00767 * 00768 */ 00769 void randr_init(int *event_base) { 00770 const xcb_query_extension_reply_t *extreply; 00771 00772 extreply = xcb_get_extension_data(conn, &xcb_randr_id); 00773 if (!extreply->present) { 00774 disable_randr(conn); 00775 return; 00776 } else randr_query_outputs(); 00777 00778 if (event_base != NULL) 00779 *event_base = extreply->first_event; 00780 00781 xcb_randr_select_input(conn, root, 00782 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE | 00783 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE | 00784 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE | 00785 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY); 00786 00787 xcb_flush(conn); 00788 }