2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * For more information on RandR, please see the X.org RandR specification at
8 * http://cgit.freedesktop.org/xorg/proto/randrproto/tree/randrproto.txt
9 * (take your time to read it completely, it answers all questions).
15 #include <xcb/randr.h>
17 /* Pointer to the result of the query for primary output */
18 xcb_randr_get_output_primary_reply_t *primary;
20 /* Stores all outputs available in your current session. */
21 struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs);
23 /* This is the output covering the root window */
24 static Output *root_output;
25 static bool has_randr_1_5 = false;
28 * Get a specific output by its internal X11 id. Used by randr_query_outputs
29 * to check if the output is new (only in the first scan) or if we are
33 static Output *get_output_by_id(xcb_randr_output_t id) {
35 TAILQ_FOREACH(output, &outputs, outputs)
43 * Returns the output with the given name or NULL.
44 * If require_active is true, only active outputs are considered.
47 Output *get_output_by_name(const char *name, const bool require_active) {
49 bool get_primary = (strcasecmp("primary", name) == 0);
50 TAILQ_FOREACH(output, &outputs, outputs) {
51 if ((output->primary && get_primary) ||
52 ((!require_active || output->active) && strcasecmp(output_primary_name(output), name) == 0)) {
61 * Returns the first output which is active.
64 Output *get_first_output(void) {
67 TAILQ_FOREACH(output, &outputs, outputs)
71 die("No usable outputs available.\n");
75 * Check whether there are any active outputs (excluding the root output).
78 static bool any_randr_output_active(void) {
81 TAILQ_FOREACH(output, &outputs, outputs) {
82 if (output != root_output && !output->to_be_disabled && output->active)
90 * Returns the active (!) output which contains the coordinates x, y or NULL
91 * if there is no output which contains these coordinates.
94 Output *get_output_containing(unsigned int x, unsigned int y) {
96 TAILQ_FOREACH(output, &outputs, outputs) {
99 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
100 x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
101 if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
102 y >= output->rect.y && y < (output->rect.y + output->rect.height))
110 * Returns the active output which spans exactly the area specified by
111 * rect or NULL if there is no output like this.
114 Output *get_output_with_dimensions(Rect rect) {
116 TAILQ_FOREACH(output, &outputs, outputs) {
119 DLOG("comparing x=%d y=%d %dx%d with x=%d and y=%d %dx%d\n",
120 rect.x, rect.y, rect.width, rect.height,
121 output->rect.x, output->rect.y, output->rect.width, output->rect.height);
122 if (rect.x == output->rect.x && rect.width == output->rect.width &&
123 rect.y == output->rect.y && rect.height == output->rect.height)
131 * In contained_by_output, we check if any active output contains part of the container.
132 * We do this by checking if the output rect is intersected by the Rect.
133 * This is the 2-dimensional counterpart of get_output_containing.
134 * Since we don't actually need the outputs intersected by the given Rect (There could
135 * be many), we just return true or false for convenience.
138 bool contained_by_output(Rect rect) {
140 int lx = rect.x, uy = rect.y;
141 int rx = rect.x + rect.width, by = rect.y + rect.height;
142 TAILQ_FOREACH(output, &outputs, outputs) {
145 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
146 rect.x, rect.y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
147 if (rx >= (int)output->rect.x && lx <= (int)(output->rect.x + output->rect.width) &&
148 by >= (int)output->rect.y && uy <= (int)(output->rect.y + output->rect.height))
155 * Like get_output_next with close_far == CLOSEST_OUTPUT, but wraps.
157 * For example if get_output_next(D_DOWN, x, FARTHEST_OUTPUT) = NULL, then
158 * get_output_next_wrap(D_DOWN, x) will return the topmost output.
160 * This function always returns a output: if no active outputs can be found,
161 * current itself is returned.
164 Output *get_output_next_wrap(direction_t direction, Output *current) {
165 Output *best = get_output_next(direction, current, CLOSEST_OUTPUT);
166 /* If no output can be found, wrap */
168 direction_t opposite;
169 if (direction == D_RIGHT)
171 else if (direction == D_LEFT)
173 else if (direction == D_DOWN)
177 best = get_output_next(opposite, current, FARTHEST_OUTPUT);
181 DLOG("current = %s, best = %s\n", output_primary_name(current), output_primary_name(best));
186 * Gets the output which is the next one in the given direction.
188 * If close_far == CLOSEST_OUTPUT, then the output next to the current one will
189 * selected. If close_far == FARTHEST_OUTPUT, the output which is the last one
190 * in the given direction will be selected.
192 * NULL will be returned when no active outputs are present in the direction
193 * specified (note that “current” counts as such an output).
196 Output *get_output_next(direction_t direction, Output *current, output_close_far_t close_far) {
197 Rect *cur = &(current->rect),
201 TAILQ_FOREACH(output, &outputs, outputs) {
205 other = &(output->rect);
207 if ((direction == D_RIGHT && other->x > cur->x) ||
208 (direction == D_LEFT && other->x < cur->x)) {
209 /* Skip the output when it doesn’t overlap the other one’s y
210 * coordinate at all. */
211 if ((other->y + other->height) <= cur->y ||
212 (cur->y + cur->height) <= other->y)
214 } else if ((direction == D_DOWN && other->y > cur->y) ||
215 (direction == D_UP && other->y < cur->y)) {
216 /* Skip the output when it doesn’t overlap the other one’s x
217 * coordinate at all. */
218 if ((other->x + other->width) <= cur->x ||
219 (cur->x + cur->width) <= other->x)
224 /* No candidate yet? Start with this one. */
230 if (close_far == CLOSEST_OUTPUT) {
231 /* Is this output better (closer to the current output) than our
232 * current best bet? */
233 if ((direction == D_RIGHT && other->x < best->rect.x) ||
234 (direction == D_LEFT && other->x > best->rect.x) ||
235 (direction == D_DOWN && other->y < best->rect.y) ||
236 (direction == D_UP && other->y > best->rect.y)) {
241 /* Is this output better (farther to the current output) than our
242 * current best bet? */
243 if ((direction == D_RIGHT && other->x > best->rect.x) ||
244 (direction == D_LEFT && other->x < best->rect.x) ||
245 (direction == D_DOWN && other->y > best->rect.y) ||
246 (direction == D_UP && other->y < best->rect.y)) {
253 DLOG("current = %s, best = %s\n", output_primary_name(current), (best ? output_primary_name(best) : "NULL"));
258 * Creates an output covering the root window.
261 Output *create_root_output(xcb_connection_t *conn) {
262 Output *s = scalloc(1, sizeof(Output));
267 s->rect.width = root_screen->width_in_pixels;
268 s->rect.height = root_screen->height_in_pixels;
275 * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
276 * before) to use for the given Output.
279 void output_init_con(Output *output) {
280 Con *con = NULL, *current;
283 DLOG("init_con for output %s\n", output_primary_name(output));
285 /* Search for a Con with that name directly below the root node. There
286 * might be one from a restored layout. */
287 TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
288 if (strcmp(current->name, output_primary_name(output)) != 0)
293 DLOG("Using existing con %p / %s\n", con, con->name);
298 con = con_new(croot, NULL);
300 con->name = sstrdup(output_primary_name(output));
301 con->type = CT_OUTPUT;
302 con->layout = L_OUTPUT;
303 con_fix_percent(croot);
305 con->rect = output->rect;
309 sasprintf(&name, "[i3 con] output %s", con->name);
310 x_set_name(con, name);
314 DLOG("Not adding workspace, this was a reused con\n");
318 DLOG("Changing layout, adding top/bottom dockarea\n");
319 Con *topdock = con_new(NULL, NULL);
320 topdock->type = CT_DOCKAREA;
321 topdock->layout = L_DOCKAREA;
322 /* this container swallows dock clients */
323 Match *match = scalloc(1, sizeof(Match));
325 match->dock = M_DOCK_TOP;
326 match->insert_where = M_BELOW;
327 TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
330 topdock->name = sstrdup("topdock");
332 sasprintf(&name, "[i3 con] top dockarea %s", con->name);
333 x_set_name(topdock, name);
336 con_attach(topdock, con, false);
338 /* content container */
340 DLOG("adding main content container\n");
341 Con *content = con_new(NULL, NULL);
342 content->type = CT_CON;
343 content->layout = L_SPLITH;
345 content->name = sstrdup("content");
347 sasprintf(&name, "[i3 con] content %s", con->name);
348 x_set_name(content, name);
350 con_attach(content, con, false);
352 /* bottom dock container */
353 Con *bottomdock = con_new(NULL, NULL);
354 bottomdock->type = CT_DOCKAREA;
355 bottomdock->layout = L_DOCKAREA;
356 /* this container swallows dock clients */
357 match = scalloc(1, sizeof(Match));
359 match->dock = M_DOCK_BOTTOM;
360 match->insert_where = M_BELOW;
361 TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
363 FREE(bottomdock->name);
364 bottomdock->name = sstrdup("bottomdock");
366 sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
367 x_set_name(bottomdock, name);
370 con_attach(bottomdock, con, false);
374 * Initializes at least one workspace for this output, trying the following
375 * steps until there is at least one workspace:
377 * • Move existing workspaces, which are assigned to be on the given output, to
379 * • Create the first assigned workspace for this output.
380 * • Create the first unused workspace.
383 void init_ws_for_output(Output *output, Con *content) {
384 /* go through all assignments and move the existing workspaces to this output */
385 struct Workspace_Assignment *assignment;
386 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
387 if (strcmp(assignment->output, output_primary_name(output)) != 0)
390 /* check if this workspace actually exists */
391 Con *workspace = NULL, *out;
392 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
393 GREP_FIRST(workspace, output_get_content(out),
394 !strcasecmp(child->name, assignment->name));
395 if (workspace == NULL)
398 /* check that this workspace is not already attached (that means the
399 * user configured this assignment twice) */
400 Con *workspace_out = con_get_output(workspace);
401 if (workspace_out == output->con) {
402 LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
403 "there. Do you have two assignment directives for the same "
404 "workspace in your configuration file?\n",
405 workspace->name, output_primary_name(output));
409 /* if so, move it over */
410 LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
411 workspace->name, workspace_out->name, output_primary_name(output));
413 /* if the workspace is currently visible on that output, we need to
414 * switch to a different workspace - otherwise the output would end up
415 * with no active workspace */
416 bool visible = workspace_is_visible(workspace);
417 Con *previous = NULL;
418 if (visible && (previous = TAILQ_NEXT(workspace, focused))) {
419 LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n",
420 previous->name, workspace_out->name);
421 workspace_show(previous);
424 /* Render the output on which the workspace was to get correct Rects.
425 * Then, we need to work with the "content" container, since we cannot
426 * be sure that the workspace itself was rendered at all (in case it’s
427 * invisible, it won’t be rendered). */
428 render_con(workspace_out, false);
429 Con *ws_out_content = output_get_content(workspace_out);
432 TAILQ_FOREACH(floating_con, &(workspace->floating_head), floating_windows)
433 /* NB: We use output->con here because content is not yet rendered,
434 * so it has a rect of {0, 0, 0, 0}. */
435 floating_fix_coordinates(floating_con, &(ws_out_content->rect), &(output->con->rect));
437 con_detach(workspace);
438 con_attach(workspace, content, false);
440 /* In case the workspace we just moved was visible but there was no
441 * other workspace to switch to, we need to initialize the source
443 if (visible && previous == NULL) {
444 LOG("There is no workspace left on \"%s\", re-initializing\n",
445 workspace_out->name);
446 init_ws_for_output(get_output_by_name(workspace_out->name, true),
447 output_get_content(workspace_out));
448 DLOG("Done re-initializing, continuing with \"%s\"\n", output_primary_name(output));
452 /* if a workspace exists, we are done now */
453 if (!TAILQ_EMPTY(&(content->nodes_head))) {
454 /* ensure that one of the workspaces is actually visible (in fullscreen
455 * mode), if they were invisible before, this might not be the case. */
457 GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
459 visible = TAILQ_FIRST(&(content->nodes_head));
461 workspace_show(visible);
466 /* otherwise, we create the first assigned ws for this output */
467 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
468 if (strcmp(assignment->output, output_primary_name(output)) != 0)
471 LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
472 assignment->name, assignment->output);
474 workspace_show_by_name(assignment->name);
478 /* if there is still no workspace, we create the first free workspace */
479 DLOG("Now adding a workspace\n");
480 Con *ws = create_workspace_on_output(output, content);
482 /* TODO: Set focus in main.c */
487 * This function needs to be called when changing the mode of an output when
488 * it already has some workspaces (or a bar window) assigned.
490 * It reconfigures the bar window for the new mode, copies the new rect into
491 * each workspace on this output and forces all windows on the affected
492 * workspaces to be reconfigured.
494 * It is necessary to call render_layout() afterwards.
497 static void output_change_mode(xcb_connection_t *conn, Output *output) {
498 DLOG("Output mode changed, updating rect\n");
499 assert(output->con != NULL);
500 output->con->rect = output->rect;
502 Con *content, *workspace, *child;
504 /* Point content to the container of the workspaces */
505 content = output_get_content(output->con);
507 /* Fix the position of all floating windows on this output.
508 * The 'rect' of each workspace will be updated in src/render.c. */
509 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
510 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
511 floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
515 /* If default_orientation is NO_ORIENTATION, we change the orientation of
516 * the workspaces and their childs depending on output resolution. This is
517 * only done for workspaces with maximum one child. */
518 if (config.default_orientation == NO_ORIENTATION) {
519 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
520 /* Workspaces with more than one child are left untouched because
521 * we do not want to change an existing layout. */
522 if (con_num_children(workspace) > 1)
525 workspace->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
526 DLOG("Setting workspace [%d,%s]'s layout to %d.\n", workspace->num, workspace->name, workspace->layout);
527 if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
528 if (child->layout == L_SPLITV || child->layout == L_SPLITH)
529 child->layout = workspace->layout;
530 DLOG("Setting child [%d,%s]'s layout to %d.\n", child->num, child->name, child->layout);
537 * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
540 static bool randr_query_outputs_15(void) {
541 #if XCB_RANDR_MINOR_VERSION < 5
544 /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
545 if (!has_randr_1_5) {
548 /* RandR 1.5 available at run-time (supported by the server and not
549 * disabled by the user) */
550 DLOG("Querying outputs using RandR 1.5\n");
551 xcb_generic_error_t *err;
552 xcb_randr_get_monitors_reply_t *monitors =
553 xcb_randr_get_monitors_reply(
554 conn, xcb_randr_get_monitors(conn, root, true), &err);
556 ELOG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
558 /* Fall back to RandR ≤ 1.4 */
562 /* Mark all outputs as to_be_disabled, since xcb_randr_get_monitors() will
563 * only return active outputs. */
565 TAILQ_FOREACH(output, &outputs, outputs) {
566 if (output != root_output) {
567 output->to_be_disabled = true;
571 DLOG("%d RandR monitors found (timestamp %d)\n",
572 xcb_randr_get_monitors_monitors_length(monitors),
573 monitors->timestamp);
575 xcb_randr_monitor_info_iterator_t iter;
576 for (iter = xcb_randr_get_monitors_monitors_iterator(monitors);
578 xcb_randr_monitor_info_next(&iter)) {
579 const xcb_randr_monitor_info_t *monitor_info = iter.data;
580 xcb_get_atom_name_reply_t *atom_reply =
581 xcb_get_atom_name_reply(
582 conn, xcb_get_atom_name(conn, monitor_info->name), &err);
584 ELOG("Could not get RandR monitor name: X11 error code %d\n", err->error_code);
589 sasprintf(&name, "%.*s",
590 xcb_get_atom_name_name_length(atom_reply),
591 xcb_get_atom_name_name(atom_reply));
594 Output *new = get_output_by_name(name, false);
596 new = scalloc(1, sizeof(Output));
597 new->name = sstrdup(name);
598 if (monitor_info->primary) {
599 TAILQ_INSERT_HEAD(&outputs, new, outputs);
601 TAILQ_INSERT_TAIL(&outputs, new, outputs);
604 /* We specified get_active == true in xcb_randr_get_monitors(), so we
605 * will only receive active outputs. */
607 new->to_be_disabled = false;
609 new->primary = monitor_info->primary;
612 update_if_necessary(&(new->rect.x), monitor_info->x) |
613 update_if_necessary(&(new->rect.y), monitor_info->y) |
614 update_if_necessary(&(new->rect.width), monitor_info->width) |
615 update_if_necessary(&(new->rect.height), monitor_info->height);
617 DLOG("name %s, x %d, y %d, width %d px, height %d px, width %d mm, height %d mm, primary %d, automatic %d\n",
619 monitor_info->x, monitor_info->y, monitor_info->width, monitor_info->height,
620 monitor_info->width_in_millimeters, monitor_info->height_in_millimeters,
621 monitor_info->primary, monitor_info->automatic);
630 * Gets called by randr_query_outputs_14() for each output. The function adds
631 * new outputs to the list of outputs, checks if the mode of existing outputs
632 * has been changed or if an existing output has been disabled. It will then
633 * change either the "changed" or the "to_be_deleted" flag of the output, if
637 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
638 xcb_randr_get_output_info_reply_t *output,
640 xcb_randr_get_screen_resources_current_reply_t *res) {
641 /* each CRT controller has a position in which we are interested in */
642 xcb_randr_get_crtc_info_reply_t *crtc;
644 Output *new = get_output_by_id(id);
645 bool existing = (new != NULL);
647 new = scalloc(1, sizeof(Output));
649 new->primary = (primary && primary->output == id);
651 sasprintf(&new->name, "%.*s",
652 xcb_randr_get_output_info_name_length(output),
653 xcb_randr_get_output_info_name(output));
655 DLOG("found output with name %s\n", output_primary_name(new));
657 /* Even if no CRTC is used at the moment, we store the output so that
658 * we do not need to change the list ever again (we only update the
660 if (output->crtc == XCB_NONE) {
663 TAILQ_INSERT_HEAD(&outputs, new, outputs);
665 TAILQ_INSERT_TAIL(&outputs, new, outputs);
666 } else if (new->active)
667 new->to_be_disabled = true;
671 xcb_randr_get_crtc_info_cookie_t icookie;
672 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
673 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
674 DLOG("Skipping output %s: could not get CRTC (%p)\n",
675 output_primary_name(new), crtc);
680 bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
681 update_if_necessary(&(new->rect.y), crtc->y) |
682 update_if_necessary(&(new->rect.width), crtc->width) |
683 update_if_necessary(&(new->rect.height), crtc->height);
685 new->active = (new->rect.width != 0 && new->rect.height != 0);
687 DLOG("width/height 0/0, disabling output\n");
691 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
692 new->rect.x, new->rect.y);
694 /* If we don’t need to change an existing output or if the output
695 * does not exist in the first place, the case is simple: we either
696 * need to insert the new output or we are done. */
697 if (!updated || !existing) {
700 TAILQ_INSERT_HEAD(&outputs, new, outputs);
702 TAILQ_INSERT_TAIL(&outputs, new, outputs);
711 * randr_query_outputs_14 uses RandR ≤ 1.4 to update outputs.
714 static void randr_query_outputs_14(void) {
715 DLOG("Querying outputs using RandR ≤ 1.4\n");
717 /* Get screen resources (primary output, crtcs, outputs, modes) */
718 xcb_randr_get_screen_resources_current_cookie_t rcookie;
719 rcookie = xcb_randr_get_screen_resources_current(conn, root);
720 xcb_randr_get_output_primary_cookie_t pcookie;
721 pcookie = xcb_randr_get_output_primary(conn, root);
723 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
724 ELOG("Could not get RandR primary output\n");
726 DLOG("primary output is %08x\n", primary->output);
728 xcb_randr_get_screen_resources_current_reply_t *res =
729 xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL);
731 ELOG("Could not query screen resources.\n");
735 /* timestamp of the configuration so that we get consistent replies to all
736 * requests (if the configuration changes between our different calls) */
737 const xcb_timestamp_t cts = res->config_timestamp;
739 const int len = xcb_randr_get_screen_resources_current_outputs_length(res);
741 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
742 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
744 /* Request information for each output */
745 xcb_randr_get_output_info_cookie_t ocookie[len];
746 for (int i = 0; i < len; i++)
747 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
749 /* Loop through all outputs available for this X11 screen */
750 for (int i = 0; i < len; i++) {
751 xcb_randr_get_output_info_reply_t *output;
753 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
756 handle_output(conn, randr_outputs[i], output, cts, res);
764 * (Re-)queries the outputs via RandR and stores them in the list of outputs.
766 * If no outputs are found use the root window.
769 void randr_query_outputs(void) {
770 Output *output, *other;
772 if (!randr_query_outputs_15()) {
773 randr_query_outputs_14();
776 /* If there's no randr output, enable the output covering the root window. */
777 if (any_randr_output_active()) {
778 DLOG("Active RandR output found. Disabling root output.\n");
779 if (root_output->active)
780 root_output->to_be_disabled = true;
782 DLOG("No active RandR output found. Enabling root output.\n");
783 root_output->active = true;
786 /* Check for clones, disable the clones and reduce the mode to the
787 * lowest common mode */
788 TAILQ_FOREACH(output, &outputs, outputs) {
789 if (!output->active || output->to_be_disabled)
791 DLOG("output %p / %s, position (%d, %d), checking for clones\n",
792 output, output_primary_name(output), output->rect.x, output->rect.y);
795 other != TAILQ_END(&outputs);
796 other = TAILQ_NEXT(other, outputs)) {
797 if (other == output || !other->active || other->to_be_disabled)
800 if (other->rect.x != output->rect.x ||
801 other->rect.y != output->rect.y)
804 DLOG("output %p has the same position, his mode = %d x %d\n",
805 other, other->rect.width, other->rect.height);
806 uint32_t width = min(other->rect.width, output->rect.width);
807 uint32_t height = min(other->rect.height, output->rect.height);
809 if (update_if_necessary(&(output->rect.width), width) |
810 update_if_necessary(&(output->rect.height), height))
811 output->changed = true;
813 update_if_necessary(&(other->rect.width), width);
814 update_if_necessary(&(other->rect.height), height);
816 DLOG("disabling output %p (%s)\n", other, output_primary_name(other));
817 other->to_be_disabled = true;
819 DLOG("new output mode %d x %d, other mode %d x %d\n",
820 output->rect.width, output->rect.height,
821 other->rect.width, other->rect.height);
825 /* Ensure that all outputs which are active also have a con. This is
826 * necessary because in the next step, a clone might get disabled. Example:
827 * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
828 * LVDS1 gets disabled. */
829 TAILQ_FOREACH(output, &outputs, outputs) {
830 if (output->active && output->con == NULL) {
831 DLOG("Need to initialize a Con for output %s\n", output_primary_name(output));
832 output_init_con(output);
833 output->changed = false;
837 /* Handle outputs which have a new mode or are disabled now (either
838 * because the user disabled them or because they are clones) */
839 TAILQ_FOREACH(output, &outputs, outputs) {
840 if (output->to_be_disabled) {
841 randr_disable_output(output);
844 if (output->changed) {
845 output_change_mode(conn, output);
846 output->changed = false;
850 /* Just go through each active output and assign one workspace */
851 TAILQ_FOREACH(output, &outputs, outputs) {
854 Con *content = output_get_content(output->con);
855 if (!TAILQ_EMPTY(&(content->nodes_head)))
857 DLOG("Should add ws for output %s\n", output_primary_name(output));
858 init_ws_for_output(output, content);
861 /* Focus the primary screen, if possible */
862 TAILQ_FOREACH(output, &outputs, outputs) {
863 if (!output->primary || !output->con)
866 DLOG("Focusing primary output %s\n", output_primary_name(output));
867 con_focus(con_descend_focused(output->con));
870 /* render_layout flushes */
877 * Disables the output and moves its content.
880 void randr_disable_output(Output *output) {
881 assert(output->to_be_disabled);
883 output->active = false;
884 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output_primary_name(output));
886 Output *first = get_first_output();
888 /* TODO: refactor the following code into a nice function. maybe
889 * use an on_destroy callback which is implement differently for
890 * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
891 Con *first_content = output_get_content(first->con);
893 if (output->con != NULL) {
894 /* We need to move the workspaces from the disappearing output to the first output */
895 /* 1: Get the con to focus next, if the disappearing ws is focused */
897 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
898 DLOG("This output (%p) was focused! Getting next\n", output->con);
900 DLOG("next = %p\n", next);
903 /* 2: iterate through workspaces and re-assign them, fixing the coordinates
904 * of floating containers as we go */
906 Con *old_content = output_get_content(output->con);
907 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
908 current = TAILQ_FIRST(&(old_content->nodes_head));
909 if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
910 /* the workspace is empty and not focused, get rid of it */
911 DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
912 tree_close_internal(current, DONT_KILL_WINDOW, false, false);
915 DLOG("Detaching current = %p / %s\n", current, current->name);
917 DLOG("Re-attaching current = %p / %s\n", current, current->name);
918 con_attach(current, first_content, false);
919 DLOG("Fixing the coordinates of floating containers\n");
921 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows) {
922 floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
924 DLOG("Done, next\n");
926 DLOG("re-attached all workspaces\n");
929 DLOG("now focusing next = %p\n", next);
931 workspace_show(con_get_workspace(next));
934 /* 3: move the dock clients to the first output */
936 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
937 if (child->type != CT_DOCKAREA)
939 DLOG("Handling dock con %p\n", child);
941 while (!TAILQ_EMPTY(&(child->nodes_head))) {
942 dock = TAILQ_FIRST(&(child->nodes_head));
945 nc = con_for_window(first->con, dock->window, &match);
946 DLOG("Moving dock client %p to nc %p\n", dock, nc);
948 DLOG("Re-attaching\n");
949 con_attach(dock, nc, false);
954 DLOG("destroying disappearing con %p\n", output->con);
955 Con *con = output->con;
956 /* clear the pointer before calling tree_close_internal in which the memory is freed */
958 tree_close_internal(con, DONT_KILL_WINDOW, true, false);
959 DLOG("Done. Should be fine now\n");
962 output->to_be_disabled = false;
963 output->changed = false;
966 static void fallback_to_root_output(void) {
967 root_output->active = true;
968 output_init_con(root_output);
969 init_ws_for_output(root_output, output_get_content(root_output->con));
973 * We have just established a connection to the X server and need the initial
974 * XRandR information to setup workspaces for each screen.
977 void randr_init(int *event_base, const bool disable_randr15) {
978 const xcb_query_extension_reply_t *extreply;
980 root_output = create_root_output(conn);
981 TAILQ_INSERT_TAIL(&outputs, root_output, outputs);
983 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
984 if (!extreply->present) {
985 DLOG("RandR is not present, activating root output.\n");
986 fallback_to_root_output();
990 xcb_generic_error_t *err;
991 xcb_randr_query_version_reply_t *randr_version =
992 xcb_randr_query_version_reply(
993 conn, xcb_randr_query_version(conn, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION), &err);
996 ELOG("Could not query RandR version: X11 error code %d\n", err->error_code);
997 fallback_to_root_output();
1001 has_randr_1_5 = (randr_version->major_version >= 1) &&
1002 (randr_version->minor_version >= 5) &&
1005 free(randr_version);
1007 randr_query_outputs();
1009 if (event_base != NULL)
1010 *event_base = extreply->first_event;
1012 xcb_randr_select_input(conn, root,
1013 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
1014 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
1015 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
1016 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);