2 #define I3__FILE__ "randr.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * For more information on RandR, please see the X.org RandR specification at
10 * http://cgit.freedesktop.org/xorg/proto/randrproto/tree/randrproto.txt
11 * (take your time to read it completely, it answers all questions).
17 #include <xcb/randr.h>
19 /* While a clean namespace is usually a pretty good thing, we really need
20 * to use shorter names than the whole xcb_randr_* default names. */
21 typedef xcb_randr_get_crtc_info_reply_t crtc_info;
22 typedef xcb_randr_get_screen_resources_current_reply_t resources_reply;
24 /* Pointer to the result of the query for primary output */
25 xcb_randr_get_output_primary_reply_t *primary;
27 /* Stores all outputs available in your current session. */
28 struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs);
30 static bool randr_disabled = false;
33 * Get a specific output by its internal X11 id. Used by randr_query_outputs
34 * to check if the output is new (only in the first scan) or if we are
38 static Output *get_output_by_id(xcb_randr_output_t id) {
40 TAILQ_FOREACH(output, &outputs, outputs)
48 * Returns the output with the given name if it is active (!) or NULL.
51 Output *get_output_by_name(const char *name) {
53 TAILQ_FOREACH(output, &outputs, outputs)
55 strcasecmp(output->name, name) == 0)
62 * Returns the first output which is active.
65 Output *get_first_output(void) {
68 TAILQ_FOREACH(output, &outputs, outputs)
72 die("No usable outputs available.\n");
76 * Returns the active (!) output which contains the coordinates x, y or NULL
77 * if there is no output which contains these coordinates.
80 Output *get_output_containing(int x, int y) {
82 TAILQ_FOREACH(output, &outputs, outputs) {
85 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
86 x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
87 if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
88 y >= output->rect.y && y < (output->rect.y + output->rect.height))
96 * Gets the output which is the last one in the given direction, for example
97 * the output on the most bottom when direction == D_DOWN, the output most
98 * right when direction == D_RIGHT and so on.
100 * This function always returns a output.
103 Output *get_output_most(direction_t direction, Output *current) {
104 Output *best = get_output_next(direction, current, FARTHEST_OUTPUT);
107 DLOG("current = %s, best = %s\n", current->name, best->name);
112 * Gets the output which is the next one in the given direction.
115 Output *get_output_next(direction_t direction, Output *current, output_close_far_t close_far) {
116 Rect *cur = &(current->rect),
120 TAILQ_FOREACH(output, &outputs, outputs) {
124 other = &(output->rect);
126 if ((direction == D_RIGHT && other->x > cur->x) ||
127 (direction == D_LEFT && other->x < cur->x)) {
128 /* Skip the output when it doesn’t overlap the other one’s y
129 * coordinate at all. */
130 if ((other->y + other->height) <= cur->y ||
131 (cur->y + cur->height) <= other->y)
133 } else if ((direction == D_DOWN && other->y > cur->y) ||
134 (direction == D_UP && other->y < cur->y)) {
135 /* Skip the output when it doesn’t overlap the other one’s x
136 * coordinate at all. */
137 if ((other->x + other->width) <= cur->x ||
138 (cur->x + cur->width) <= other->x)
143 /* No candidate yet? Start with this one. */
149 if (close_far == CLOSEST_OUTPUT) {
150 /* Is this output better (closer to the current output) than our
151 * current best bet? */
152 if ((direction == D_RIGHT && other->x < best->rect.x) ||
153 (direction == D_LEFT && other->x > best->rect.x) ||
154 (direction == D_DOWN && other->y < best->rect.y) ||
155 (direction == D_UP && other->y > best->rect.y)) {
160 /* Is this output better (farther to the current output) than our
161 * current best bet? */
162 if ((direction == D_RIGHT && other->x > best->rect.x) ||
163 (direction == D_LEFT && other->x < best->rect.x) ||
164 (direction == D_DOWN && other->y > best->rect.y) ||
165 (direction == D_UP && other->y < best->rect.y)) {
172 DLOG("current = %s, best = %s\n", current->name, (best ? best->name : "NULL"));
177 * Disables RandR support by creating exactly one output with the size of the
181 void disable_randr(xcb_connection_t *conn) {
182 DLOG("RandR extension unusable, disabling.\n");
184 Output *s = scalloc(sizeof(Output));
189 s->rect.width = root_screen->width_in_pixels;
190 s->rect.height = root_screen->height_in_pixels;
193 init_ws_for_output(s, output_get_content(s->con));
195 TAILQ_INSERT_TAIL(&outputs, s, outputs);
197 randr_disabled = true;
201 * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
202 * before) to use for the given Output.
205 void output_init_con(Output *output) {
206 Con *con = NULL, *current;
209 DLOG("init_con for output %s\n", output->name);
211 /* Search for a Con with that name directly below the root node. There
212 * might be one from a restored layout. */
213 TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
214 if (strcmp(current->name, output->name) != 0)
219 DLOG("Using existing con %p / %s\n", con, con->name);
224 con = con_new(croot, NULL);
226 con->name = sstrdup(output->name);
227 con->type = CT_OUTPUT;
228 con->layout = L_OUTPUT;
229 con_fix_percent(croot);
231 con->rect = output->rect;
235 sasprintf(&name, "[i3 con] output %s", con->name);
236 x_set_name(con, name);
240 DLOG("Not adding workspace, this was a reused con\n");
244 DLOG("Changing layout, adding top/bottom dockarea\n");
245 Con *topdock = con_new(NULL, NULL);
246 topdock->type = CT_DOCKAREA;
247 topdock->layout = L_DOCKAREA;
248 /* this container swallows dock clients */
249 Match *match = scalloc(sizeof(Match));
251 match->dock = M_DOCK_TOP;
252 match->insert_where = M_BELOW;
253 TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
256 topdock->name = sstrdup("topdock");
258 sasprintf(&name, "[i3 con] top dockarea %s", con->name);
259 x_set_name(topdock, name);
262 con_attach(topdock, con, false);
264 /* content container */
266 DLOG("adding main content container\n");
267 Con *content = con_new(NULL, NULL);
268 content->type = CT_CON;
269 content->layout = L_SPLITH;
271 content->name = sstrdup("content");
273 sasprintf(&name, "[i3 con] content %s", con->name);
274 x_set_name(content, name);
276 con_attach(content, con, false);
278 /* bottom dock container */
279 Con *bottomdock = con_new(NULL, NULL);
280 bottomdock->type = CT_DOCKAREA;
281 bottomdock->layout = L_DOCKAREA;
282 /* this container swallows dock clients */
283 match = scalloc(sizeof(Match));
285 match->dock = M_DOCK_BOTTOM;
286 match->insert_where = M_BELOW;
287 TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
289 FREE(bottomdock->name);
290 bottomdock->name = sstrdup("bottomdock");
292 sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
293 x_set_name(bottomdock, name);
296 con_attach(bottomdock, con, false);
300 * Initializes at least one workspace for this output, trying the following
301 * steps until there is at least one workspace:
303 * • Move existing workspaces, which are assigned to be on the given output, to
305 * • Create the first assigned workspace for this output.
306 * • Create the first unused workspace.
309 void init_ws_for_output(Output *output, Con *content) {
310 /* go through all assignments and move the existing workspaces to this output */
311 struct Workspace_Assignment *assignment;
312 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
313 if (strcmp(assignment->output, output->name) != 0)
316 /* check if this workspace actually exists */
317 Con *workspace = NULL, *out;
318 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
319 GREP_FIRST(workspace, output_get_content(out),
320 !strcasecmp(child->name, assignment->name));
321 if (workspace == NULL)
324 /* check that this workspace is not already attached (that means the
325 * user configured this assignment twice) */
326 Con *workspace_out = con_get_output(workspace);
327 if (workspace_out == output->con) {
328 LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
329 "there. Do you have two assignment directives for the same "
330 "workspace in your configuration file?\n",
331 workspace->name, output->name);
335 /* if so, move it over */
336 LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
337 workspace->name, workspace_out->name, output->name);
339 /* if the workspace is currently visible on that output, we need to
340 * switch to a different workspace - otherwise the output would end up
341 * with no active workspace */
342 bool visible = workspace_is_visible(workspace);
343 Con *previous = NULL;
344 if (visible && (previous = TAILQ_NEXT(workspace, focused))) {
345 LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n",
346 previous->name, workspace_out->name);
347 workspace_show(previous);
350 /* Render the output on which the workspace was to get correct Rects.
351 * Then, we need to work with the "content" container, since we cannot
352 * be sure that the workspace itself was rendered at all (in case it’s
353 * invisible, it won’t be rendered). */
354 render_con(workspace_out, false);
355 Con *ws_out_content = output_get_content(workspace_out);
358 TAILQ_FOREACH(floating_con, &(workspace->floating_head), floating_windows)
359 /* NB: We use output->con here because content is not yet rendered,
360 * so it has a rect of {0, 0, 0, 0}. */
361 floating_fix_coordinates(floating_con, &(ws_out_content->rect), &(output->con->rect));
363 con_detach(workspace);
364 con_attach(workspace, content, false);
366 /* In case the workspace we just moved was visible but there was no
367 * other workspace to switch to, we need to initialize the source
369 if (visible && previous == NULL) {
370 LOG("There is no workspace left on \"%s\", re-initializing\n",
371 workspace_out->name);
372 init_ws_for_output(get_output_by_name(workspace_out->name),
373 output_get_content(workspace_out));
374 DLOG("Done re-initializing, continuing with \"%s\"\n", output->name);
378 /* if a workspace exists, we are done now */
379 if (!TAILQ_EMPTY(&(content->nodes_head))) {
380 /* ensure that one of the workspaces is actually visible (in fullscreen
381 * mode), if they were invisible before, this might not be the case. */
383 GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
385 visible = TAILQ_FIRST(&(content->nodes_head));
387 workspace_show(visible);
392 /* otherwise, we create the first assigned ws for this output */
393 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
394 if (strcmp(assignment->output, output->name) != 0)
397 LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
398 assignment->name, assignment->output);
400 workspace_show_by_name(assignment->name);
404 /* if there is still no workspace, we create the first free workspace */
405 DLOG("Now adding a workspace\n");
406 Con *ws = create_workspace_on_output(output, content);
408 /* TODO: Set focus in main.c */
413 * This function needs to be called when changing the mode of an output when
414 * it already has some workspaces (or a bar window) assigned.
416 * It reconfigures the bar window for the new mode, copies the new rect into
417 * each workspace on this output and forces all windows on the affected
418 * workspaces to be reconfigured.
420 * It is necessary to call render_layout() afterwards.
423 static void output_change_mode(xcb_connection_t *conn, Output *output) {
424 DLOG("Output mode changed, updating rect\n");
425 assert(output->con != NULL);
426 output->con->rect = output->rect;
428 Con *content, *workspace, *child;
430 /* Point content to the container of the workspaces */
431 content = output_get_content(output->con);
433 /* Fix the position of all floating windows on this output.
434 * The 'rect' of each workspace will be updated in src/render.c. */
435 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
436 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
437 floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
441 /* If default_orientation is NO_ORIENTATION, we change the orientation of
442 * the workspaces and their childs depending on output resolution. This is
443 * only done for workspaces with maximum one child. */
444 if (config.default_orientation == NO_ORIENTATION) {
445 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
446 /* Workspaces with more than one child are left untouched because
447 * we do not want to change an existing layout. */
448 if (con_num_children(workspace) > 1)
451 workspace->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
452 DLOG("Setting workspace [%d,%s]'s layout to %d.\n", workspace->num, workspace->name, workspace->layout);
453 if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
454 if (child->layout == L_SPLITV || child->layout == L_SPLITH)
455 child->layout = workspace->layout;
456 DLOG("Setting child [%d,%s]'s layout to %d.\n", child->num, child->name, child->layout);
463 * Gets called by randr_query_outputs() for each output. The function adds new
464 * outputs to the list of outputs, checks if the mode of existing outputs has
465 * been changed or if an existing output has been disabled. It will then change
466 * either the "changed" or the "to_be_deleted" flag of the output, if
470 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
471 xcb_randr_get_output_info_reply_t *output,
472 xcb_timestamp_t cts, resources_reply *res) {
473 /* each CRT controller has a position in which we are interested in */
476 Output *new = get_output_by_id(id);
477 bool existing = (new != NULL);
479 new = scalloc(sizeof(Output));
481 new->primary = (primary && primary->output == id);
483 sasprintf(&new->name, "%.*s",
484 xcb_randr_get_output_info_name_length(output),
485 xcb_randr_get_output_info_name(output));
487 DLOG("found output with name %s\n", new->name);
489 /* Even if no CRTC is used at the moment, we store the output so that
490 * we do not need to change the list ever again (we only update the
492 if (output->crtc == XCB_NONE) {
495 TAILQ_INSERT_HEAD(&outputs, new, outputs);
496 else TAILQ_INSERT_TAIL(&outputs, new, outputs);
497 } else if (new->active)
498 new->to_be_disabled = true;
502 xcb_randr_get_crtc_info_cookie_t icookie;
503 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
504 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
505 DLOG("Skipping output %s: could not get CRTC (%p)\n",
511 bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
512 update_if_necessary(&(new->rect.y), crtc->y) |
513 update_if_necessary(&(new->rect.width), crtc->width) |
514 update_if_necessary(&(new->rect.height), crtc->height);
516 new->active = (new->rect.width != 0 && new->rect.height != 0);
518 DLOG("width/height 0/0, disabling output\n");
522 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
523 new->rect.x, new->rect.y);
525 /* If we don’t need to change an existing output or if the output
526 * does not exist in the first place, the case is simple: we either
527 * need to insert the new output or we are done. */
528 if (!updated || !existing) {
531 TAILQ_INSERT_HEAD(&outputs, new, outputs);
532 else TAILQ_INSERT_TAIL(&outputs, new, outputs);
541 * (Re-)queries the outputs via RandR and stores them in the list of outputs.
544 void randr_query_outputs(void) {
545 Output *output, *other, *first;
546 xcb_randr_get_output_primary_cookie_t pcookie;
547 xcb_randr_get_screen_resources_current_cookie_t rcookie;
548 resources_reply *res;
550 /* timestamp of the configuration so that we get consistent replies to all
551 * requests (if the configuration changes between our different calls) */
554 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
555 xcb_randr_output_t *randr_outputs;
560 /* Get screen resources (primary output, crtcs, outputs, modes) */
561 rcookie = xcb_randr_get_screen_resources_current(conn, root);
562 pcookie = xcb_randr_get_output_primary(conn, root);
564 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
565 ELOG("Could not get RandR primary output\n");
566 else DLOG("primary output is %08x\n", primary->output);
567 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
571 cts = res->config_timestamp;
573 int len = xcb_randr_get_screen_resources_current_outputs_length(res);
574 randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
576 /* Request information for each output */
577 xcb_randr_get_output_info_cookie_t ocookie[len];
578 for (int i = 0; i < len; i++)
579 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
581 /* Loop through all outputs available for this X11 screen */
582 for (int i = 0; i < len; i++) {
583 xcb_randr_get_output_info_reply_t *output;
585 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
588 handle_output(conn, randr_outputs[i], output, cts, res);
592 /* Check for clones, disable the clones and reduce the mode to the
593 * lowest common mode */
594 TAILQ_FOREACH(output, &outputs, outputs) {
595 if (!output->active || output->to_be_disabled)
597 DLOG("output %p / %s, position (%d, %d), checking for clones\n",
598 output, output->name, output->rect.x, output->rect.y);
601 other != TAILQ_END(&outputs);
602 other = TAILQ_NEXT(other, outputs)) {
603 if (other == output || !other->active || other->to_be_disabled)
606 if (other->rect.x != output->rect.x ||
607 other->rect.y != output->rect.y)
610 DLOG("output %p has the same position, his mode = %d x %d\n",
611 other, other->rect.width, other->rect.height);
612 uint32_t width = min(other->rect.width, output->rect.width);
613 uint32_t height = min(other->rect.height, output->rect.height);
615 if (update_if_necessary(&(output->rect.width), width) |
616 update_if_necessary(&(output->rect.height), height))
617 output->changed = true;
619 update_if_necessary(&(other->rect.width), width);
620 update_if_necessary(&(other->rect.height), height);
622 DLOG("disabling output %p (%s)\n", other, other->name);
623 other->to_be_disabled = true;
625 DLOG("new output mode %d x %d, other mode %d x %d\n",
626 output->rect.width, output->rect.height,
627 other->rect.width, other->rect.height);
631 /* Ensure that all outputs which are active also have a con. This is
632 * necessary because in the next step, a clone might get disabled. Example:
633 * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
634 * LVDS1 gets disabled. */
635 TAILQ_FOREACH(output, &outputs, outputs) {
636 if (output->active && output->con == NULL) {
637 DLOG("Need to initialize a Con for output %s\n", output->name);
638 output_init_con(output);
639 output->changed = false;
643 /* Handle outputs which have a new mode or are disabled now (either
644 * because the user disabled them or because they are clones) */
645 TAILQ_FOREACH(output, &outputs, outputs) {
646 if (output->to_be_disabled) {
647 output->active = false;
648 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name);
650 first = get_first_output();
652 /* TODO: refactor the following code into a nice function. maybe
653 * use an on_destroy callback which is implement differently for
654 * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
655 Con *first_content = output_get_content(first->con);
657 if (output->con != NULL) {
658 /* We need to move the workspaces from the disappearing output to the first output */
659 /* 1: Get the con to focus next, if the disappearing ws is focused */
661 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
662 DLOG("This output (%p) was focused! Getting next\n", output->con);
664 DLOG("next = %p\n", next);
667 /* 2: iterate through workspaces and re-assign them, fixing the coordinates
668 * of floating containers as we go */
670 Con *old_content = output_get_content(output->con);
671 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
672 current = TAILQ_FIRST(&(old_content->nodes_head));
673 if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
674 /* the workspace is empty and not focused, get rid of it */
675 DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
676 tree_close(current, DONT_KILL_WINDOW, false, false);
679 DLOG("Detaching current = %p / %s\n", current, current->name);
681 DLOG("Re-attaching current = %p / %s\n", current, current->name);
682 con_attach(current, first_content, false);
683 DLOG("Fixing the coordinates of floating containers\n");
685 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows)
686 floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
687 DLOG("Done, next\n");
689 DLOG("re-attached all workspaces\n");
692 DLOG("now focusing next = %p\n", next);
694 workspace_show(con_get_workspace(next));
697 /* 3: move the dock clients to the first output */
699 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
700 if (child->type != CT_DOCKAREA)
702 DLOG("Handling dock con %p\n", child);
704 while (!TAILQ_EMPTY(&(child->nodes_head))) {
705 dock = TAILQ_FIRST(&(child->nodes_head));
708 nc = con_for_window(first->con, dock->window, &match);
709 DLOG("Moving dock client %p to nc %p\n", dock, nc);
711 DLOG("Re-attaching\n");
712 con_attach(dock, nc, false);
717 DLOG("destroying disappearing con %p\n", output->con);
718 tree_close(output->con, DONT_KILL_WINDOW, true, false);
719 DLOG("Done. Should be fine now\n");
723 output->to_be_disabled = false;
724 output->changed = false;
727 if (output->changed) {
728 output_change_mode(conn, output);
729 output->changed = false;
733 if (TAILQ_EMPTY(&outputs)) {
734 ELOG("No outputs found via RandR, disabling\n");
738 /* Verifies that there is at least one active output as a side-effect. */
741 /* Just go through each active output and assign one workspace */
742 TAILQ_FOREACH(output, &outputs, outputs) {
745 Con *content = output_get_content(output->con);
746 if (!TAILQ_EMPTY(&(content->nodes_head)))
748 DLOG("Should add ws for output %s\n", output->name);
749 init_ws_for_output(output, content);
752 /* Focus the primary screen, if possible */
753 TAILQ_FOREACH(output, &outputs, outputs) {
754 if (!output->primary || !output->con)
757 DLOG("Focusing primary output %s\n", output->name);
758 con_focus(con_descend_focused(output->con));
761 /* render_layout flushes */
769 * We have just established a connection to the X server and need the initial
770 * XRandR information to setup workspaces for each screen.
773 void randr_init(int *event_base) {
774 const xcb_query_extension_reply_t *extreply;
776 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
777 if (!extreply->present) {
780 } else randr_query_outputs();
782 if (event_base != NULL)
783 *event_base = extreply->first_event;
785 xcb_randr_select_input(conn, root,
786 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
787 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
788 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
789 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);