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) {
54 if (require_active && !output->active) {
57 struct output_name *output_name;
58 SLIST_FOREACH(output_name, &output->names_head, names) {
59 if (strcasecmp(output_name->name, name) == 0) {
69 * Returns the first output which is active.
72 Output *get_first_output(void) {
75 TAILQ_FOREACH(output, &outputs, outputs)
79 die("No usable outputs available.\n");
83 * Check whether there are any active outputs (excluding the root output).
86 static bool any_randr_output_active(void) {
89 TAILQ_FOREACH(output, &outputs, outputs) {
90 if (output != root_output && !output->to_be_disabled && output->active)
98 * Returns the active (!) output which contains the coordinates x, y or NULL
99 * if there is no output which contains these coordinates.
102 Output *get_output_containing(unsigned int x, unsigned int y) {
104 TAILQ_FOREACH(output, &outputs, outputs) {
107 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
108 x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
109 if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
110 y >= output->rect.y && y < (output->rect.y + output->rect.height))
118 * Returns the active output which spans exactly the area specified by
119 * rect or NULL if there is no output like this.
122 Output *get_output_with_dimensions(Rect rect) {
124 TAILQ_FOREACH(output, &outputs, outputs) {
127 DLOG("comparing x=%d y=%d %dx%d with x=%d and y=%d %dx%d\n",
128 rect.x, rect.y, rect.width, rect.height,
129 output->rect.x, output->rect.y, output->rect.width, output->rect.height);
130 if (rect.x == output->rect.x && rect.width == output->rect.width &&
131 rect.y == output->rect.y && rect.height == output->rect.height)
139 * In contained_by_output, we check if any active output contains part of the container.
140 * We do this by checking if the output rect is intersected by the Rect.
141 * This is the 2-dimensional counterpart of get_output_containing.
142 * Since we don't actually need the outputs intersected by the given Rect (There could
143 * be many), we just return true or false for convenience.
146 bool contained_by_output(Rect rect) {
148 int lx = rect.x, uy = rect.y;
149 int rx = rect.x + rect.width, by = rect.y + rect.height;
150 TAILQ_FOREACH(output, &outputs, outputs) {
153 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
154 rect.x, rect.y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
155 if (rx >= (int)output->rect.x && lx <= (int)(output->rect.x + output->rect.width) &&
156 by >= (int)output->rect.y && uy <= (int)(output->rect.y + output->rect.height))
163 * Like get_output_next with close_far == CLOSEST_OUTPUT, but wraps.
165 * For example if get_output_next(D_DOWN, x, FARTHEST_OUTPUT) = NULL, then
166 * get_output_next_wrap(D_DOWN, x) will return the topmost output.
168 * This function always returns a output: if no active outputs can be found,
169 * current itself is returned.
172 Output *get_output_next_wrap(direction_t direction, Output *current) {
173 Output *best = get_output_next(direction, current, CLOSEST_OUTPUT);
174 /* If no output can be found, wrap */
176 direction_t opposite;
177 if (direction == D_RIGHT)
179 else if (direction == D_LEFT)
181 else if (direction == D_DOWN)
185 best = get_output_next(opposite, current, FARTHEST_OUTPUT);
189 DLOG("current = %s, best = %s\n", output_primary_name(current), output_primary_name(best));
194 * Gets the output which is the next one in the given direction.
196 * If close_far == CLOSEST_OUTPUT, then the output next to the current one will
197 * selected. If close_far == FARTHEST_OUTPUT, the output which is the last one
198 * in the given direction will be selected.
200 * NULL will be returned when no active outputs are present in the direction
201 * specified (note that “current” counts as such an output).
204 Output *get_output_next(direction_t direction, Output *current, output_close_far_t close_far) {
205 Rect *cur = &(current->rect),
209 TAILQ_FOREACH(output, &outputs, outputs) {
213 other = &(output->rect);
215 if ((direction == D_RIGHT && other->x > cur->x) ||
216 (direction == D_LEFT && other->x < cur->x)) {
217 /* Skip the output when it doesn’t overlap the other one’s y
218 * coordinate at all. */
219 if ((other->y + other->height) <= cur->y ||
220 (cur->y + cur->height) <= other->y)
222 } else if ((direction == D_DOWN && other->y > cur->y) ||
223 (direction == D_UP && other->y < cur->y)) {
224 /* Skip the output when it doesn’t overlap the other one’s x
225 * coordinate at all. */
226 if ((other->x + other->width) <= cur->x ||
227 (cur->x + cur->width) <= other->x)
232 /* No candidate yet? Start with this one. */
238 if (close_far == CLOSEST_OUTPUT) {
239 /* Is this output better (closer to the current output) than our
240 * current best bet? */
241 if ((direction == D_RIGHT && other->x < best->rect.x) ||
242 (direction == D_LEFT && other->x > best->rect.x) ||
243 (direction == D_DOWN && other->y < best->rect.y) ||
244 (direction == D_UP && other->y > best->rect.y)) {
249 /* Is this output better (farther to the current output) than our
250 * current best bet? */
251 if ((direction == D_RIGHT && other->x > best->rect.x) ||
252 (direction == D_LEFT && other->x < best->rect.x) ||
253 (direction == D_DOWN && other->y > best->rect.y) ||
254 (direction == D_UP && other->y < best->rect.y)) {
261 DLOG("current = %s, best = %s\n", output_primary_name(current), (best ? output_primary_name(best) : "NULL"));
266 * Creates an output covering the root window.
269 Output *create_root_output(xcb_connection_t *conn) {
270 Output *s = scalloc(1, sizeof(Output));
275 s->rect.width = root_screen->width_in_pixels;
276 s->rect.height = root_screen->height_in_pixels;
278 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
279 output_name->name = "xroot-0";
280 SLIST_INIT(&s->names_head);
281 SLIST_INSERT_HEAD(&s->names_head, output_name, names);
287 * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
288 * before) to use for the given Output.
291 void output_init_con(Output *output) {
292 Con *con = NULL, *current;
295 DLOG("init_con for output %s\n", output_primary_name(output));
297 /* Search for a Con with that name directly below the root node. There
298 * might be one from a restored layout. */
299 TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
300 if (strcmp(current->name, output_primary_name(output)) != 0)
305 DLOG("Using existing con %p / %s\n", con, con->name);
310 con = con_new(croot, NULL);
312 con->name = sstrdup(output_primary_name(output));
313 con->type = CT_OUTPUT;
314 con->layout = L_OUTPUT;
315 con_fix_percent(croot);
317 con->rect = output->rect;
321 sasprintf(&name, "[i3 con] output %s", con->name);
322 x_set_name(con, name);
326 DLOG("Not adding workspace, this was a reused con\n");
330 DLOG("Changing layout, adding top/bottom dockarea\n");
331 Con *topdock = con_new(NULL, NULL);
332 topdock->type = CT_DOCKAREA;
333 topdock->layout = L_DOCKAREA;
334 /* this container swallows dock clients */
335 Match *match = scalloc(1, sizeof(Match));
337 match->dock = M_DOCK_TOP;
338 match->insert_where = M_BELOW;
339 TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
342 topdock->name = sstrdup("topdock");
344 sasprintf(&name, "[i3 con] top dockarea %s", con->name);
345 x_set_name(topdock, name);
348 con_attach(topdock, con, false);
350 /* content container */
352 DLOG("adding main content container\n");
353 Con *content = con_new(NULL, NULL);
354 content->type = CT_CON;
355 content->layout = L_SPLITH;
357 content->name = sstrdup("content");
359 sasprintf(&name, "[i3 con] content %s", con->name);
360 x_set_name(content, name);
362 con_attach(content, con, false);
364 /* bottom dock container */
365 Con *bottomdock = con_new(NULL, NULL);
366 bottomdock->type = CT_DOCKAREA;
367 bottomdock->layout = L_DOCKAREA;
368 /* this container swallows dock clients */
369 match = scalloc(1, sizeof(Match));
371 match->dock = M_DOCK_BOTTOM;
372 match->insert_where = M_BELOW;
373 TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
375 FREE(bottomdock->name);
376 bottomdock->name = sstrdup("bottomdock");
378 sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
379 x_set_name(bottomdock, name);
382 con_attach(bottomdock, con, false);
386 * Initializes at least one workspace for this output, trying the following
387 * steps until there is at least one workspace:
389 * • Move existing workspaces, which are assigned to be on the given output, to
391 * • Create the first assigned workspace for this output.
392 * • Create the first unused workspace.
395 void init_ws_for_output(Output *output, Con *content) {
396 /* go through all assignments and move the existing workspaces to this output */
397 struct Workspace_Assignment *assignment;
398 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
399 if (strcmp(assignment->output, output_primary_name(output)) != 0)
402 /* check if this workspace actually exists */
403 Con *workspace = NULL, *out;
404 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
405 GREP_FIRST(workspace, output_get_content(out),
406 !strcasecmp(child->name, assignment->name));
407 if (workspace == NULL)
410 /* check that this workspace is not already attached (that means the
411 * user configured this assignment twice) */
412 Con *workspace_out = con_get_output(workspace);
413 if (workspace_out == output->con) {
414 LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
415 "there. Do you have two assignment directives for the same "
416 "workspace in your configuration file?\n",
417 workspace->name, output_primary_name(output));
421 /* if so, move it over */
422 LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
423 workspace->name, workspace_out->name, output_primary_name(output));
425 /* if the workspace is currently visible on that output, we need to
426 * switch to a different workspace - otherwise the output would end up
427 * with no active workspace */
428 bool visible = workspace_is_visible(workspace);
429 Con *previous = NULL;
430 if (visible && (previous = TAILQ_NEXT(workspace, focused))) {
431 LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n",
432 previous->name, workspace_out->name);
433 workspace_show(previous);
436 /* Render the output on which the workspace was to get correct Rects.
437 * Then, we need to work with the "content" container, since we cannot
438 * be sure that the workspace itself was rendered at all (in case it’s
439 * invisible, it won’t be rendered). */
440 render_con(workspace_out, false);
441 Con *ws_out_content = output_get_content(workspace_out);
444 TAILQ_FOREACH(floating_con, &(workspace->floating_head), floating_windows)
445 /* NB: We use output->con here because content is not yet rendered,
446 * so it has a rect of {0, 0, 0, 0}. */
447 floating_fix_coordinates(floating_con, &(ws_out_content->rect), &(output->con->rect));
449 con_detach(workspace);
450 con_attach(workspace, content, false);
452 /* In case the workspace we just moved was visible but there was no
453 * other workspace to switch to, we need to initialize the source
455 if (visible && previous == NULL) {
456 LOG("There is no workspace left on \"%s\", re-initializing\n",
457 workspace_out->name);
458 init_ws_for_output(get_output_by_name(workspace_out->name, true),
459 output_get_content(workspace_out));
460 DLOG("Done re-initializing, continuing with \"%s\"\n", output_primary_name(output));
464 /* if a workspace exists, we are done now */
465 if (!TAILQ_EMPTY(&(content->nodes_head))) {
466 /* ensure that one of the workspaces is actually visible (in fullscreen
467 * mode), if they were invisible before, this might not be the case. */
469 GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
471 visible = TAILQ_FIRST(&(content->nodes_head));
473 workspace_show(visible);
478 /* otherwise, we create the first assigned ws for this output */
479 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
480 if (strcmp(assignment->output, output_primary_name(output)) != 0)
483 LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
484 assignment->name, assignment->output);
486 workspace_show_by_name(assignment->name);
490 /* if there is still no workspace, we create the first free workspace */
491 DLOG("Now adding a workspace\n");
492 Con *ws = create_workspace_on_output(output, content);
494 /* TODO: Set focus in main.c */
499 * This function needs to be called when changing the mode of an output when
500 * it already has some workspaces (or a bar window) assigned.
502 * It reconfigures the bar window for the new mode, copies the new rect into
503 * each workspace on this output and forces all windows on the affected
504 * workspaces to be reconfigured.
506 * It is necessary to call render_layout() afterwards.
509 static void output_change_mode(xcb_connection_t *conn, Output *output) {
510 DLOG("Output mode changed, updating rect\n");
511 assert(output->con != NULL);
512 output->con->rect = output->rect;
514 Con *content, *workspace, *child;
516 /* Point content to the container of the workspaces */
517 content = output_get_content(output->con);
519 /* Fix the position of all floating windows on this output.
520 * The 'rect' of each workspace will be updated in src/render.c. */
521 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
522 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
523 floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
527 /* If default_orientation is NO_ORIENTATION, we change the orientation of
528 * the workspaces and their childs depending on output resolution. This is
529 * only done for workspaces with maximum one child. */
530 if (config.default_orientation == NO_ORIENTATION) {
531 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
532 /* Workspaces with more than one child are left untouched because
533 * we do not want to change an existing layout. */
534 if (con_num_children(workspace) > 1)
537 workspace->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
538 DLOG("Setting workspace [%d,%s]'s layout to %d.\n", workspace->num, workspace->name, workspace->layout);
539 if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
540 if (child->layout == L_SPLITV || child->layout == L_SPLITH)
541 child->layout = workspace->layout;
542 DLOG("Setting child [%d,%s]'s layout to %d.\n", child->num, child->name, child->layout);
549 * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
552 static bool randr_query_outputs_15(void) {
553 #if XCB_RANDR_MINOR_VERSION < 5
556 /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
557 if (!has_randr_1_5) {
560 /* RandR 1.5 available at run-time (supported by the server and not
561 * disabled by the user) */
562 DLOG("Querying outputs using RandR 1.5\n");
563 xcb_generic_error_t *err;
564 xcb_randr_get_monitors_reply_t *monitors =
565 xcb_randr_get_monitors_reply(
566 conn, xcb_randr_get_monitors(conn, root, true), &err);
568 ELOG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
570 /* Fall back to RandR ≤ 1.4 */
574 /* Mark all outputs as to_be_disabled, since xcb_randr_get_monitors() will
575 * only return active outputs. */
577 TAILQ_FOREACH(output, &outputs, outputs) {
578 if (output != root_output) {
579 output->to_be_disabled = true;
583 DLOG("%d RandR monitors found (timestamp %d)\n",
584 xcb_randr_get_monitors_monitors_length(monitors),
585 monitors->timestamp);
587 xcb_randr_monitor_info_iterator_t iter;
588 for (iter = xcb_randr_get_monitors_monitors_iterator(monitors);
590 xcb_randr_monitor_info_next(&iter)) {
591 const xcb_randr_monitor_info_t *monitor_info = iter.data;
592 xcb_get_atom_name_reply_t *atom_reply =
593 xcb_get_atom_name_reply(
594 conn, xcb_get_atom_name(conn, monitor_info->name), &err);
596 ELOG("Could not get RandR monitor name: X11 error code %d\n", err->error_code);
601 sasprintf(&name, "%.*s",
602 xcb_get_atom_name_name_length(atom_reply),
603 xcb_get_atom_name_name(atom_reply));
606 Output *new = get_output_by_name(name, false);
608 new = scalloc(1, sizeof(Output));
610 SLIST_INIT(&new->names_head);
612 /* Register associated output names in addition to the monitor name */
613 xcb_randr_output_t *randr_outputs = xcb_randr_monitor_info_outputs(monitor_info);
614 int randr_output_len = xcb_randr_monitor_info_outputs_length(monitor_info);
615 for (int i = 0; i < randr_output_len; i++) {
616 xcb_randr_output_t randr_output = randr_outputs[i];
618 xcb_randr_get_output_info_reply_t *info =
619 xcb_randr_get_output_info_reply(conn,
620 xcb_randr_get_output_info(conn, randr_output, monitors->timestamp),
623 if (info != NULL && info->crtc != XCB_NONE) {
625 sasprintf(&oname, "%.*s",
626 xcb_randr_get_output_info_name_length(info),
627 xcb_randr_get_output_info_name(info));
629 if (strcmp(name, oname) != 0) {
630 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
631 output_name->name = sstrdup(oname);
632 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
640 /* Insert the monitor name last, so that it's used as the primary name */
641 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
642 output_name->name = sstrdup(name);
643 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
645 if (monitor_info->primary) {
646 TAILQ_INSERT_HEAD(&outputs, new, outputs);
648 TAILQ_INSERT_TAIL(&outputs, new, outputs);
651 /* We specified get_active == true in xcb_randr_get_monitors(), so we
652 * will only receive active outputs. */
654 new->to_be_disabled = false;
656 new->primary = monitor_info->primary;
659 update_if_necessary(&(new->rect.x), monitor_info->x) |
660 update_if_necessary(&(new->rect.y), monitor_info->y) |
661 update_if_necessary(&(new->rect.width), monitor_info->width) |
662 update_if_necessary(&(new->rect.height), monitor_info->height);
664 DLOG("name %s, x %d, y %d, width %d px, height %d px, width %d mm, height %d mm, primary %d, automatic %d\n",
666 monitor_info->x, monitor_info->y, monitor_info->width, monitor_info->height,
667 monitor_info->width_in_millimeters, monitor_info->height_in_millimeters,
668 monitor_info->primary, monitor_info->automatic);
677 * Gets called by randr_query_outputs_14() for each output. The function adds
678 * new outputs to the list of outputs, checks if the mode of existing outputs
679 * has been changed or if an existing output has been disabled. It will then
680 * change either the "changed" or the "to_be_deleted" flag of the output, if
684 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
685 xcb_randr_get_output_info_reply_t *output,
687 xcb_randr_get_screen_resources_current_reply_t *res) {
688 /* each CRT controller has a position in which we are interested in */
689 xcb_randr_get_crtc_info_reply_t *crtc;
691 Output *new = get_output_by_id(id);
692 bool existing = (new != NULL);
694 new = scalloc(1, sizeof(Output));
695 SLIST_INIT(&new->names_head);
698 new->primary = (primary && primary->output == id);
699 while (!SLIST_EMPTY(&new->names_head)) {
700 FREE(SLIST_FIRST(&new->names_head)->name);
701 struct output_name *old_head = SLIST_FIRST(&new->names_head);
702 SLIST_REMOVE_HEAD(&new->names_head, names);
705 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
706 sasprintf(&output_name->name, "%.*s",
707 xcb_randr_get_output_info_name_length(output),
708 xcb_randr_get_output_info_name(output));
709 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
711 DLOG("found output with name %s\n", output_primary_name(new));
713 /* Even if no CRTC is used at the moment, we store the output so that
714 * we do not need to change the list ever again (we only update the
716 if (output->crtc == XCB_NONE) {
719 TAILQ_INSERT_HEAD(&outputs, new, outputs);
721 TAILQ_INSERT_TAIL(&outputs, new, outputs);
722 } else if (new->active)
723 new->to_be_disabled = true;
727 xcb_randr_get_crtc_info_cookie_t icookie;
728 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
729 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
730 DLOG("Skipping output %s: could not get CRTC (%p)\n",
731 output_primary_name(new), crtc);
736 bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
737 update_if_necessary(&(new->rect.y), crtc->y) |
738 update_if_necessary(&(new->rect.width), crtc->width) |
739 update_if_necessary(&(new->rect.height), crtc->height);
741 new->active = (new->rect.width != 0 && new->rect.height != 0);
743 DLOG("width/height 0/0, disabling output\n");
747 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
748 new->rect.x, new->rect.y);
750 /* If we don’t need to change an existing output or if the output
751 * does not exist in the first place, the case is simple: we either
752 * need to insert the new output or we are done. */
753 if (!updated || !existing) {
756 TAILQ_INSERT_HEAD(&outputs, new, outputs);
758 TAILQ_INSERT_TAIL(&outputs, new, outputs);
767 * randr_query_outputs_14 uses RandR ≤ 1.4 to update outputs.
770 static void randr_query_outputs_14(void) {
771 DLOG("Querying outputs using RandR ≤ 1.4\n");
773 /* Get screen resources (primary output, crtcs, outputs, modes) */
774 xcb_randr_get_screen_resources_current_cookie_t rcookie;
775 rcookie = xcb_randr_get_screen_resources_current(conn, root);
776 xcb_randr_get_output_primary_cookie_t pcookie;
777 pcookie = xcb_randr_get_output_primary(conn, root);
779 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
780 ELOG("Could not get RandR primary output\n");
782 DLOG("primary output is %08x\n", primary->output);
784 xcb_randr_get_screen_resources_current_reply_t *res =
785 xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL);
787 ELOG("Could not query screen resources.\n");
791 /* timestamp of the configuration so that we get consistent replies to all
792 * requests (if the configuration changes between our different calls) */
793 const xcb_timestamp_t cts = res->config_timestamp;
795 const int len = xcb_randr_get_screen_resources_current_outputs_length(res);
797 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
798 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
800 /* Request information for each output */
801 xcb_randr_get_output_info_cookie_t ocookie[len];
802 for (int i = 0; i < len; i++)
803 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
805 /* Loop through all outputs available for this X11 screen */
806 for (int i = 0; i < len; i++) {
807 xcb_randr_get_output_info_reply_t *output;
809 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
812 handle_output(conn, randr_outputs[i], output, cts, res);
820 * (Re-)queries the outputs via RandR and stores them in the list of outputs.
822 * If no outputs are found use the root window.
825 void randr_query_outputs(void) {
826 Output *output, *other;
828 if (!randr_query_outputs_15()) {
829 randr_query_outputs_14();
832 /* If there's no randr output, enable the output covering the root window. */
833 if (any_randr_output_active()) {
834 DLOG("Active RandR output found. Disabling root output.\n");
835 if (root_output->active)
836 root_output->to_be_disabled = true;
838 DLOG("No active RandR output found. Enabling root output.\n");
839 root_output->active = true;
842 /* Check for clones, disable the clones and reduce the mode to the
843 * lowest common mode */
844 TAILQ_FOREACH(output, &outputs, outputs) {
845 if (!output->active || output->to_be_disabled)
847 DLOG("output %p / %s, position (%d, %d), checking for clones\n",
848 output, output_primary_name(output), output->rect.x, output->rect.y);
851 other != TAILQ_END(&outputs);
852 other = TAILQ_NEXT(other, outputs)) {
853 if (other == output || !other->active || other->to_be_disabled)
856 if (other->rect.x != output->rect.x ||
857 other->rect.y != output->rect.y)
860 DLOG("output %p has the same position, his mode = %d x %d\n",
861 other, other->rect.width, other->rect.height);
862 uint32_t width = min(other->rect.width, output->rect.width);
863 uint32_t height = min(other->rect.height, output->rect.height);
865 if (update_if_necessary(&(output->rect.width), width) |
866 update_if_necessary(&(output->rect.height), height))
867 output->changed = true;
869 update_if_necessary(&(other->rect.width), width);
870 update_if_necessary(&(other->rect.height), height);
872 DLOG("disabling output %p (%s)\n", other, output_primary_name(other));
873 other->to_be_disabled = true;
875 DLOG("new output mode %d x %d, other mode %d x %d\n",
876 output->rect.width, output->rect.height,
877 other->rect.width, other->rect.height);
881 /* Ensure that all outputs which are active also have a con. This is
882 * necessary because in the next step, a clone might get disabled. Example:
883 * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
884 * LVDS1 gets disabled. */
885 TAILQ_FOREACH(output, &outputs, outputs) {
886 if (output->active && output->con == NULL) {
887 DLOG("Need to initialize a Con for output %s\n", output_primary_name(output));
888 output_init_con(output);
889 output->changed = false;
893 /* Handle outputs which have a new mode or are disabled now (either
894 * because the user disabled them or because they are clones) */
895 TAILQ_FOREACH(output, &outputs, outputs) {
896 if (output->to_be_disabled) {
897 randr_disable_output(output);
900 if (output->changed) {
901 output_change_mode(conn, output);
902 output->changed = false;
906 /* Just go through each active output and assign one workspace */
907 TAILQ_FOREACH(output, &outputs, outputs) {
910 Con *content = output_get_content(output->con);
911 if (!TAILQ_EMPTY(&(content->nodes_head)))
913 DLOG("Should add ws for output %s\n", output_primary_name(output));
914 init_ws_for_output(output, content);
917 /* Focus the primary screen, if possible */
918 TAILQ_FOREACH(output, &outputs, outputs) {
919 if (!output->primary || !output->con)
922 DLOG("Focusing primary output %s\n", output_primary_name(output));
923 con_focus(con_descend_focused(output->con));
926 /* render_layout flushes */
933 * Disables the output and moves its content.
936 void randr_disable_output(Output *output) {
937 assert(output->to_be_disabled);
939 output->active = false;
940 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output_primary_name(output));
942 Output *first = get_first_output();
944 /* TODO: refactor the following code into a nice function. maybe
945 * use an on_destroy callback which is implement differently for
946 * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
947 Con *first_content = output_get_content(first->con);
949 if (output->con != NULL) {
950 /* We need to move the workspaces from the disappearing output to the first output */
951 /* 1: Get the con to focus next, if the disappearing ws is focused */
953 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
954 DLOG("This output (%p) was focused! Getting next\n", output->con);
956 DLOG("next = %p\n", next);
959 /* 2: iterate through workspaces and re-assign them, fixing the coordinates
960 * of floating containers as we go */
962 Con *old_content = output_get_content(output->con);
963 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
964 current = TAILQ_FIRST(&(old_content->nodes_head));
965 if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
966 /* the workspace is empty and not focused, get rid of it */
967 DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
968 tree_close_internal(current, DONT_KILL_WINDOW, false, false);
971 DLOG("Detaching current = %p / %s\n", current, current->name);
973 DLOG("Re-attaching current = %p / %s\n", current, current->name);
974 con_attach(current, first_content, false);
975 DLOG("Fixing the coordinates of floating containers\n");
977 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows) {
978 floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
980 DLOG("Done, next\n");
982 DLOG("re-attached all workspaces\n");
985 DLOG("now focusing next = %p\n", next);
987 workspace_show(con_get_workspace(next));
990 /* 3: move the dock clients to the first output */
992 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
993 if (child->type != CT_DOCKAREA)
995 DLOG("Handling dock con %p\n", child);
997 while (!TAILQ_EMPTY(&(child->nodes_head))) {
998 dock = TAILQ_FIRST(&(child->nodes_head));
1001 nc = con_for_window(first->con, dock->window, &match);
1002 DLOG("Moving dock client %p to nc %p\n", dock, nc);
1004 DLOG("Re-attaching\n");
1005 con_attach(dock, nc, false);
1010 DLOG("destroying disappearing con %p\n", output->con);
1011 Con *con = output->con;
1012 /* clear the pointer before calling tree_close_internal in which the memory is freed */
1014 tree_close_internal(con, DONT_KILL_WINDOW, true, false);
1015 DLOG("Done. Should be fine now\n");
1018 output->to_be_disabled = false;
1019 output->changed = false;
1022 static void fallback_to_root_output(void) {
1023 root_output->active = true;
1024 output_init_con(root_output);
1025 init_ws_for_output(root_output, output_get_content(root_output->con));
1029 * We have just established a connection to the X server and need the initial
1030 * XRandR information to setup workspaces for each screen.
1033 void randr_init(int *event_base, const bool disable_randr15) {
1034 const xcb_query_extension_reply_t *extreply;
1036 root_output = create_root_output(conn);
1037 TAILQ_INSERT_TAIL(&outputs, root_output, outputs);
1039 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
1040 if (!extreply->present) {
1041 DLOG("RandR is not present, activating root output.\n");
1042 fallback_to_root_output();
1046 xcb_generic_error_t *err;
1047 xcb_randr_query_version_reply_t *randr_version =
1048 xcb_randr_query_version_reply(
1049 conn, xcb_randr_query_version(conn, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION), &err);
1052 ELOG("Could not query RandR version: X11 error code %d\n", err->error_code);
1053 fallback_to_root_output();
1057 has_randr_1_5 = (randr_version->major_version >= 1) &&
1058 (randr_version->minor_version >= 5) &&
1061 free(randr_version);
1063 randr_query_outputs();
1065 if (event_base != NULL)
1066 *event_base = extreply->first_event;
1068 xcb_randr_select_input(conn, root,
1069 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
1070 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
1071 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
1072 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);