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 * https://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 contains the midpoint of the given rect. If
119 * such an output doesn't exist, returns the output which contains most of the
120 * rectangle or NULL if there is no output which intersects with it.
123 Output *get_output_from_rect(Rect rect) {
124 unsigned int mid_x = rect.x + rect.width / 2;
125 unsigned int mid_y = rect.y + rect.height / 2;
126 Output *output = get_output_containing(mid_x, mid_y);
128 return output ? output : output_containing_rect(rect);
132 * Returns the active output which spans exactly the area specified by
133 * rect or NULL if there is no output like this.
136 Output *get_output_with_dimensions(Rect rect) {
138 TAILQ_FOREACH(output, &outputs, outputs) {
141 DLOG("comparing x=%d y=%d %dx%d with x=%d and y=%d %dx%d\n",
142 rect.x, rect.y, rect.width, rect.height,
143 output->rect.x, output->rect.y, output->rect.width, output->rect.height);
144 if (rect.x == output->rect.x && rect.width == output->rect.width &&
145 rect.y == output->rect.y && rect.height == output->rect.height)
153 * In output_containing_rect, we check if any active output contains part of the container.
154 * We do this by checking if the output rect is intersected by the Rect.
155 * This is the 2-dimensional counterpart of get_output_containing.
156 * Returns the output with the maximum intersecting area.
159 Output *output_containing_rect(Rect rect) {
161 int lx = rect.x, uy = rect.y;
162 int rx = rect.x + rect.width, by = rect.y + rect.height;
164 Output *result = NULL;
165 TAILQ_FOREACH(output, &outputs, outputs) {
168 int lx_o = (int)output->rect.x, uy_o = (int)output->rect.y;
169 int rx_o = (int)(output->rect.x + output->rect.width), by_o = (int)(output->rect.y + output->rect.height);
170 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
171 rect.x, rect.y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
172 int left = max(lx, lx_o);
173 int right = min(rx, rx_o);
174 int bottom = min(by, by_o);
175 int top = max(uy, uy_o);
176 if (left < right && bottom > top) {
177 long area = (right - left) * (bottom - top);
178 if (area > max_area) {
187 * Like get_output_next with close_far == CLOSEST_OUTPUT, but wraps.
189 * For example if get_output_next(D_DOWN, x, FARTHEST_OUTPUT) = NULL, then
190 * get_output_next_wrap(D_DOWN, x) will return the topmost output.
192 * This function always returns a output: if no active outputs can be found,
193 * current itself is returned.
196 Output *get_output_next_wrap(direction_t direction, Output *current) {
197 Output *best = get_output_next(direction, current, CLOSEST_OUTPUT);
198 /* If no output can be found, wrap */
200 direction_t opposite;
201 if (direction == D_RIGHT)
203 else if (direction == D_LEFT)
205 else if (direction == D_DOWN)
209 best = get_output_next(opposite, current, FARTHEST_OUTPUT);
213 DLOG("current = %s, best = %s\n", output_primary_name(current), output_primary_name(best));
218 * Gets the output which is the next one in the given direction.
220 * If close_far == CLOSEST_OUTPUT, then the output next to the current one will
221 * selected. If close_far == FARTHEST_OUTPUT, the output which is the last one
222 * in the given direction will be selected.
224 * NULL will be returned when no active outputs are present in the direction
225 * specified (note that “current” counts as such an output).
228 Output *get_output_next(direction_t direction, Output *current, output_close_far_t close_far) {
229 Rect *cur = &(current->rect),
233 TAILQ_FOREACH(output, &outputs, outputs) {
237 other = &(output->rect);
239 if ((direction == D_RIGHT && other->x > cur->x) ||
240 (direction == D_LEFT && other->x < cur->x)) {
241 /* Skip the output when it doesn’t overlap the other one’s y
242 * coordinate at all. */
243 if ((other->y + other->height) <= cur->y ||
244 (cur->y + cur->height) <= other->y)
246 } else if ((direction == D_DOWN && other->y > cur->y) ||
247 (direction == D_UP && other->y < cur->y)) {
248 /* Skip the output when it doesn’t overlap the other one’s x
249 * coordinate at all. */
250 if ((other->x + other->width) <= cur->x ||
251 (cur->x + cur->width) <= other->x)
256 /* No candidate yet? Start with this one. */
262 if (close_far == CLOSEST_OUTPUT) {
263 /* Is this output better (closer to the current output) than our
264 * current best bet? */
265 if ((direction == D_RIGHT && other->x < best->rect.x) ||
266 (direction == D_LEFT && other->x > best->rect.x) ||
267 (direction == D_DOWN && other->y < best->rect.y) ||
268 (direction == D_UP && other->y > best->rect.y)) {
273 /* Is this output better (farther to the current output) than our
274 * current best bet? */
275 if ((direction == D_RIGHT && other->x > best->rect.x) ||
276 (direction == D_LEFT && other->x < best->rect.x) ||
277 (direction == D_DOWN && other->y > best->rect.y) ||
278 (direction == D_UP && other->y < best->rect.y)) {
285 DLOG("current = %s, best = %s\n", output_primary_name(current), (best ? output_primary_name(best) : "NULL"));
290 * Creates an output covering the root window.
293 Output *create_root_output(xcb_connection_t *conn) {
294 Output *s = scalloc(1, sizeof(Output));
299 s->rect.width = root_screen->width_in_pixels;
300 s->rect.height = root_screen->height_in_pixels;
302 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
303 output_name->name = "xroot-0";
304 SLIST_INIT(&s->names_head);
305 SLIST_INSERT_HEAD(&s->names_head, output_name, names);
311 * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
312 * before) to use for the given Output.
315 void output_init_con(Output *output) {
316 Con *con = NULL, *current;
319 DLOG("init_con for output %s\n", output_primary_name(output));
321 /* Search for a Con with that name directly below the root node. There
322 * might be one from a restored layout. */
323 TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
324 if (strcmp(current->name, output_primary_name(output)) != 0)
329 DLOG("Using existing con %p / %s\n", con, con->name);
334 con = con_new(croot, NULL);
336 con->name = sstrdup(output_primary_name(output));
337 con->type = CT_OUTPUT;
338 con->layout = L_OUTPUT;
339 con_fix_percent(croot);
341 con->rect = output->rect;
345 sasprintf(&name, "[i3 con] output %s", con->name);
346 x_set_name(con, name);
350 DLOG("Not adding workspace, this was a reused con\n");
354 DLOG("Changing layout, adding top/bottom dockarea\n");
355 Con *topdock = con_new(NULL, NULL);
356 topdock->type = CT_DOCKAREA;
357 topdock->layout = L_DOCKAREA;
358 /* this container swallows dock clients */
359 Match *match = scalloc(1, sizeof(Match));
361 match->dock = M_DOCK_TOP;
362 match->insert_where = M_BELOW;
363 TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
366 topdock->name = sstrdup("topdock");
368 sasprintf(&name, "[i3 con] top dockarea %s", con->name);
369 x_set_name(topdock, name);
372 con_attach(topdock, con, false);
374 /* content container */
376 DLOG("adding main content container\n");
377 Con *content = con_new(NULL, NULL);
378 content->type = CT_CON;
379 content->layout = L_SPLITH;
381 content->name = sstrdup("content");
383 sasprintf(&name, "[i3 con] content %s", con->name);
384 x_set_name(content, name);
386 con_attach(content, con, false);
388 /* bottom dock container */
389 Con *bottomdock = con_new(NULL, NULL);
390 bottomdock->type = CT_DOCKAREA;
391 bottomdock->layout = L_DOCKAREA;
392 /* this container swallows dock clients */
393 match = scalloc(1, sizeof(Match));
395 match->dock = M_DOCK_BOTTOM;
396 match->insert_where = M_BELOW;
397 TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
399 FREE(bottomdock->name);
400 bottomdock->name = sstrdup("bottomdock");
402 sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
403 x_set_name(bottomdock, name);
406 con_attach(bottomdock, con, false);
408 /* Change focus to the content container */
409 TAILQ_REMOVE(&(con->focus_head), content, focused);
410 TAILQ_INSERT_HEAD(&(con->focus_head), content, focused);
414 * Initializes at least one workspace for this output, trying the following
415 * steps until there is at least one workspace:
417 * • Move existing workspaces, which are assigned to be on the given output, to
419 * • Create the first assigned workspace for this output.
420 * • Create the first unused workspace.
423 void init_ws_for_output(Output *output, Con *content) {
424 /* go through all assignments and move the existing workspaces to this output */
425 struct Workspace_Assignment *assignment;
426 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
427 if (!output_triggers_assignment(output, assignment)) {
430 Con *workspace = get_existing_workspace_by_name(assignment->name);
431 if (workspace == NULL)
434 /* check that this workspace is not already attached (that means the
435 * user configured this assignment twice) */
436 Con *workspace_out = con_get_output(workspace);
437 if (workspace_out == output->con) {
438 LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
439 "there. Do you have two assignment directives for the same "
440 "workspace in your configuration file?\n",
441 workspace->name, output_primary_name(output));
445 DLOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
446 workspace->name, workspace_out->name, output_primary_name(output));
447 /* Need to copy output's rect since content is not yet rendered. We
448 * can't call render_con here because render_output only proceeds if a
449 * workspace exists. */
450 content->rect = output->con->rect;
451 workspace_move_to_output(workspace, output);
454 /* if a workspace exists, we are done now */
455 if (!TAILQ_EMPTY(&(content->nodes_head))) {
456 /* ensure that one of the workspaces is actually visible (in fullscreen
457 * mode), if they were invisible before, this might not be the case. */
459 GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
461 visible = TAILQ_FIRST(&(content->nodes_head));
463 workspace_show(visible);
468 /* otherwise, we create the first assigned ws for this output */
469 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
470 if (!output_triggers_assignment(output, assignment)) {
474 LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
475 assignment->name, assignment->output);
477 workspace_show_by_name(assignment->name);
481 /* if there is still no workspace, we create the first free workspace */
482 DLOG("Now adding a workspace\n");
483 Con *ws = create_workspace_on_output(output, content);
485 /* TODO: Set focus in main.c */
490 * This function needs to be called when changing the mode of an output when
491 * it already has some workspaces (or a bar window) assigned.
493 * It reconfigures the bar window for the new mode, copies the new rect into
494 * each workspace on this output and forces all windows on the affected
495 * workspaces to be reconfigured.
497 * It is necessary to call render_layout() afterwards.
500 static void output_change_mode(xcb_connection_t *conn, Output *output) {
501 DLOG("Output mode changed, updating rect\n");
502 assert(output->con != NULL);
503 output->con->rect = output->rect;
505 Con *content, *workspace, *child;
507 /* Point content to the container of the workspaces */
508 content = output_get_content(output->con);
510 /* Fix the position of all floating windows on this output.
511 * The 'rect' of each workspace will be updated in src/render.c. */
512 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
513 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
514 floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
518 /* If default_orientation is NO_ORIENTATION, we change the orientation of
519 * the workspaces and their childs depending on output resolution. This is
520 * only done for workspaces with maximum one child. */
521 if (config.default_orientation == NO_ORIENTATION) {
522 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
523 /* Workspaces with more than one child are left untouched because
524 * we do not want to change an existing layout. */
525 if (con_num_children(workspace) > 1)
528 workspace->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
529 DLOG("Setting workspace [%d,%s]'s layout to %d.\n", workspace->num, workspace->name, workspace->layout);
530 if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
531 if (child->layout == L_SPLITV || child->layout == L_SPLITH)
532 child->layout = workspace->layout;
533 DLOG("Setting child [%d,%s]'s layout to %d.\n", child->num, child->name, child->layout);
540 * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
543 static bool randr_query_outputs_15(void) {
544 #if XCB_RANDR_MINOR_VERSION < 5
547 /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
548 if (!has_randr_1_5) {
551 /* RandR 1.5 available at run-time (supported by the server and not
552 * disabled by the user) */
553 DLOG("Querying outputs using RandR 1.5\n");
554 xcb_generic_error_t *err;
555 xcb_randr_get_monitors_reply_t *monitors =
556 xcb_randr_get_monitors_reply(
557 conn, xcb_randr_get_monitors(conn, root, true), &err);
559 ELOG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
561 /* Fall back to RandR ≤ 1.4 */
565 /* Mark all outputs as to_be_disabled, since xcb_randr_get_monitors() will
566 * only return active outputs. */
568 TAILQ_FOREACH(output, &outputs, outputs) {
569 if (output != root_output) {
570 output->to_be_disabled = true;
574 DLOG("%d RandR monitors found (timestamp %d)\n",
575 xcb_randr_get_monitors_monitors_length(monitors),
576 monitors->timestamp);
578 xcb_randr_monitor_info_iterator_t iter;
579 for (iter = xcb_randr_get_monitors_monitors_iterator(monitors);
581 xcb_randr_monitor_info_next(&iter)) {
582 const xcb_randr_monitor_info_t *monitor_info = iter.data;
583 xcb_get_atom_name_reply_t *atom_reply =
584 xcb_get_atom_name_reply(
585 conn, xcb_get_atom_name(conn, monitor_info->name), &err);
587 ELOG("Could not get RandR monitor name: X11 error code %d\n", err->error_code);
592 sasprintf(&name, "%.*s",
593 xcb_get_atom_name_name_length(atom_reply),
594 xcb_get_atom_name_name(atom_reply));
597 Output *new = get_output_by_name(name, false);
599 new = scalloc(1, sizeof(Output));
601 SLIST_INIT(&new->names_head);
603 /* Register associated output names in addition to the monitor name */
604 xcb_randr_output_t *randr_outputs = xcb_randr_monitor_info_outputs(monitor_info);
605 int randr_output_len = xcb_randr_monitor_info_outputs_length(monitor_info);
606 for (int i = 0; i < randr_output_len; i++) {
607 xcb_randr_output_t randr_output = randr_outputs[i];
609 xcb_randr_get_output_info_reply_t *info =
610 xcb_randr_get_output_info_reply(conn,
611 xcb_randr_get_output_info(conn, randr_output, monitors->timestamp),
614 if (info != NULL && info->crtc != XCB_NONE) {
616 sasprintf(&oname, "%.*s",
617 xcb_randr_get_output_info_name_length(info),
618 xcb_randr_get_output_info_name(info));
620 if (strcmp(name, oname) != 0) {
621 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
622 output_name->name = sstrdup(oname);
623 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
631 /* Insert the monitor name last, so that it's used as the primary name */
632 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
633 output_name->name = sstrdup(name);
634 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
636 if (monitor_info->primary) {
637 TAILQ_INSERT_HEAD(&outputs, new, outputs);
639 TAILQ_INSERT_TAIL(&outputs, new, outputs);
642 /* We specified get_active == true in xcb_randr_get_monitors(), so we
643 * will only receive active outputs. */
645 new->to_be_disabled = false;
647 new->primary = monitor_info->primary;
650 update_if_necessary(&(new->rect.x), monitor_info->x) |
651 update_if_necessary(&(new->rect.y), monitor_info->y) |
652 update_if_necessary(&(new->rect.width), monitor_info->width) |
653 update_if_necessary(&(new->rect.height), monitor_info->height);
655 DLOG("name %s, x %d, y %d, width %d px, height %d px, width %d mm, height %d mm, primary %d, automatic %d\n",
657 monitor_info->x, monitor_info->y, monitor_info->width, monitor_info->height,
658 monitor_info->width_in_millimeters, monitor_info->height_in_millimeters,
659 monitor_info->primary, monitor_info->automatic);
668 * Gets called by randr_query_outputs_14() for each output. The function adds
669 * new outputs to the list of outputs, checks if the mode of existing outputs
670 * has been changed or if an existing output has been disabled. It will then
671 * change either the "changed" or the "to_be_deleted" flag of the output, if
675 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
676 xcb_randr_get_output_info_reply_t *output,
678 xcb_randr_get_screen_resources_current_reply_t *res) {
679 /* each CRT controller has a position in which we are interested in */
680 xcb_randr_get_crtc_info_reply_t *crtc;
682 Output *new = get_output_by_id(id);
683 bool existing = (new != NULL);
685 new = scalloc(1, sizeof(Output));
686 SLIST_INIT(&new->names_head);
689 new->primary = (primary && primary->output == id);
690 while (!SLIST_EMPTY(&new->names_head)) {
691 FREE(SLIST_FIRST(&new->names_head)->name);
692 struct output_name *old_head = SLIST_FIRST(&new->names_head);
693 SLIST_REMOVE_HEAD(&new->names_head, names);
696 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
697 sasprintf(&output_name->name, "%.*s",
698 xcb_randr_get_output_info_name_length(output),
699 xcb_randr_get_output_info_name(output));
700 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
702 DLOG("found output with name %s\n", output_primary_name(new));
704 /* Even if no CRTC is used at the moment, we store the output so that
705 * we do not need to change the list ever again (we only update the
707 if (output->crtc == XCB_NONE) {
710 TAILQ_INSERT_HEAD(&outputs, new, outputs);
712 TAILQ_INSERT_TAIL(&outputs, new, outputs);
713 } else if (new->active)
714 new->to_be_disabled = true;
718 xcb_randr_get_crtc_info_cookie_t icookie;
719 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
720 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
721 DLOG("Skipping output %s: could not get CRTC (%p)\n",
722 output_primary_name(new), crtc);
727 bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
728 update_if_necessary(&(new->rect.y), crtc->y) |
729 update_if_necessary(&(new->rect.width), crtc->width) |
730 update_if_necessary(&(new->rect.height), crtc->height);
732 new->active = (new->rect.width != 0 && new->rect.height != 0);
734 DLOG("width/height 0/0, disabling output\n");
738 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
739 new->rect.x, new->rect.y);
741 /* If we don’t need to change an existing output or if the output
742 * does not exist in the first place, the case is simple: we either
743 * need to insert the new output or we are done. */
744 if (!updated || !existing) {
747 TAILQ_INSERT_HEAD(&outputs, new, outputs);
749 TAILQ_INSERT_TAIL(&outputs, new, outputs);
758 * randr_query_outputs_14 uses RandR ≤ 1.4 to update outputs.
761 static void randr_query_outputs_14(void) {
762 DLOG("Querying outputs using RandR ≤ 1.4\n");
764 /* Get screen resources (primary output, crtcs, outputs, modes) */
765 xcb_randr_get_screen_resources_current_cookie_t rcookie;
766 rcookie = xcb_randr_get_screen_resources_current(conn, root);
767 xcb_randr_get_output_primary_cookie_t pcookie;
768 pcookie = xcb_randr_get_output_primary(conn, root);
770 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
771 ELOG("Could not get RandR primary output\n");
773 DLOG("primary output is %08x\n", primary->output);
775 xcb_randr_get_screen_resources_current_reply_t *res =
776 xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL);
778 ELOG("Could not query screen resources.\n");
782 /* timestamp of the configuration so that we get consistent replies to all
783 * requests (if the configuration changes between our different calls) */
784 const xcb_timestamp_t cts = res->config_timestamp;
786 const int len = xcb_randr_get_screen_resources_current_outputs_length(res);
788 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
789 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
791 /* Request information for each output */
792 xcb_randr_get_output_info_cookie_t ocookie[len];
793 for (int i = 0; i < len; i++)
794 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
796 /* Loop through all outputs available for this X11 screen */
797 for (int i = 0; i < len; i++) {
798 xcb_randr_get_output_info_reply_t *output;
800 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
803 handle_output(conn, randr_outputs[i], output, cts, res);
811 * (Re-)queries the outputs via RandR and stores them in the list of outputs.
813 * If no outputs are found use the root window.
816 void randr_query_outputs(void) {
817 Output *output, *other;
819 if (!randr_query_outputs_15()) {
820 randr_query_outputs_14();
823 /* If there's no randr output, enable the output covering the root window. */
824 if (any_randr_output_active()) {
825 DLOG("Active RandR output found. Disabling root output.\n");
826 if (root_output && root_output->active) {
827 root_output->to_be_disabled = true;
830 DLOG("No active RandR output found. Enabling root output.\n");
831 root_output->active = true;
834 /* Check for clones, disable the clones and reduce the mode to the
835 * lowest common mode */
836 TAILQ_FOREACH(output, &outputs, outputs) {
837 if (!output->active || output->to_be_disabled)
839 DLOG("output %p / %s, position (%d, %d), checking for clones\n",
840 output, output_primary_name(output), output->rect.x, output->rect.y);
843 other != TAILQ_END(&outputs);
844 other = TAILQ_NEXT(other, outputs)) {
845 if (other == output || !other->active || other->to_be_disabled)
848 if (other->rect.x != output->rect.x ||
849 other->rect.y != output->rect.y)
852 DLOG("output %p has the same position, his mode = %d x %d\n",
853 other, other->rect.width, other->rect.height);
854 uint32_t width = min(other->rect.width, output->rect.width);
855 uint32_t height = min(other->rect.height, output->rect.height);
857 if (update_if_necessary(&(output->rect.width), width) |
858 update_if_necessary(&(output->rect.height), height))
859 output->changed = true;
861 update_if_necessary(&(other->rect.width), width);
862 update_if_necessary(&(other->rect.height), height);
864 DLOG("disabling output %p (%s)\n", other, output_primary_name(other));
865 other->to_be_disabled = true;
867 DLOG("new output mode %d x %d, other mode %d x %d\n",
868 output->rect.width, output->rect.height,
869 other->rect.width, other->rect.height);
873 /* Ensure that all outputs which are active also have a con. This is
874 * necessary because in the next step, a clone might get disabled. Example:
875 * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
876 * LVDS1 gets disabled. */
877 TAILQ_FOREACH(output, &outputs, outputs) {
878 if (output->active && output->con == NULL) {
879 DLOG("Need to initialize a Con for output %s\n", output_primary_name(output));
880 output_init_con(output);
881 output->changed = false;
885 /* Handle outputs which have a new mode or are disabled now (either
886 * because the user disabled them or because they are clones) */
887 TAILQ_FOREACH(output, &outputs, outputs) {
888 if (output->to_be_disabled) {
889 randr_disable_output(output);
892 if (output->changed) {
893 output_change_mode(conn, output);
894 output->changed = false;
898 /* Just go through each active output and assign one workspace */
899 TAILQ_FOREACH(output, &outputs, outputs) {
902 Con *content = output_get_content(output->con);
903 if (!TAILQ_EMPTY(&(content->nodes_head)))
905 DLOG("Should add ws for output %s\n", output_primary_name(output));
906 init_ws_for_output(output, content);
909 /* Focus the primary screen, if possible */
910 TAILQ_FOREACH(output, &outputs, outputs) {
911 if (!output->primary || !output->con)
914 DLOG("Focusing primary output %s\n", output_primary_name(output));
915 Con *content = output_get_content(output->con);
916 Con *ws = TAILQ_FIRST(&(content)->focus_head);
920 /* render_layout flushes */
927 * Disables the output and moves its content.
930 void randr_disable_output(Output *output) {
931 assert(output->to_be_disabled);
933 output->active = false;
934 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output_primary_name(output));
936 Output *first = get_first_output();
938 /* TODO: refactor the following code into a nice function. maybe
939 * use an on_destroy callback which is implement differently for
940 * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
941 Con *first_content = output_get_content(first->con);
943 if (output->con != NULL) {
944 /* We need to move the workspaces from the disappearing output to the first output */
945 /* 1: Get the con to focus next, if the disappearing ws is focused */
947 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
948 DLOG("This output (%p) was focused! Getting next\n", output->con);
950 DLOG("next = %p\n", next);
953 /* 2: iterate through workspaces and re-assign them, fixing the coordinates
954 * of floating containers as we go */
956 Con *old_content = output_get_content(output->con);
957 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
958 current = TAILQ_FIRST(&(old_content->nodes_head));
959 if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
960 /* the workspace is empty and not focused, get rid of it */
961 DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
962 tree_close_internal(current, DONT_KILL_WINDOW, false, false);
965 DLOG("Detaching current = %p / %s\n", current, current->name);
967 DLOG("Re-attaching current = %p / %s\n", current, current->name);
968 con_attach(current, first_content, false);
969 DLOG("Fixing the coordinates of floating containers\n");
971 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows) {
972 floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
974 DLOG("Done, next\n");
976 DLOG("re-attached all workspaces\n");
979 DLOG("now focusing next = %p\n", next);
981 workspace_show(con_get_workspace(next));
984 /* 3: move the dock clients to the first output */
986 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
987 if (child->type != CT_DOCKAREA)
989 DLOG("Handling dock con %p\n", child);
991 while (!TAILQ_EMPTY(&(child->nodes_head))) {
992 dock = TAILQ_FIRST(&(child->nodes_head));
995 nc = con_for_window(first->con, dock->window, &match);
996 DLOG("Moving dock client %p to nc %p\n", dock, nc);
998 DLOG("Re-attaching\n");
999 con_attach(dock, nc, false);
1004 DLOG("destroying disappearing con %p\n", output->con);
1005 Con *con = output->con;
1006 /* clear the pointer before calling tree_close_internal in which the memory is freed */
1008 tree_close_internal(con, DONT_KILL_WINDOW, true, false);
1009 DLOG("Done. Should be fine now\n");
1012 output->to_be_disabled = false;
1013 output->changed = false;
1016 static void fallback_to_root_output(void) {
1017 root_output->active = true;
1018 output_init_con(root_output);
1019 init_ws_for_output(root_output, output_get_content(root_output->con));
1023 * We have just established a connection to the X server and need the initial
1024 * XRandR information to setup workspaces for each screen.
1027 void randr_init(int *event_base, const bool disable_randr15) {
1028 const xcb_query_extension_reply_t *extreply;
1030 root_output = create_root_output(conn);
1031 TAILQ_INSERT_TAIL(&outputs, root_output, outputs);
1033 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
1034 if (!extreply->present) {
1035 DLOG("RandR is not present, activating root output.\n");
1036 fallback_to_root_output();
1040 xcb_generic_error_t *err;
1041 xcb_randr_query_version_reply_t *randr_version =
1042 xcb_randr_query_version_reply(
1043 conn, xcb_randr_query_version(conn, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION), &err);
1045 ELOG("Could not query RandR version: X11 error code %d\n", err->error_code);
1047 fallback_to_root_output();
1051 has_randr_1_5 = (randr_version->major_version >= 1) &&
1052 (randr_version->minor_version >= 5) &&
1055 free(randr_version);
1057 randr_query_outputs();
1059 if (event_base != NULL)
1060 *event_base = extreply->first_event;
1062 xcb_randr_select_input(conn, root,
1063 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
1064 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
1065 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
1066 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);