i3
randr.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
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).
10 *
11 */
12#include "all.h"
13
14#include <time.h>
15#include <xcb/randr.h>
16
17/* Pointer to the result of the query for primary output */
18xcb_randr_get_output_primary_reply_t *primary;
19
20/* Stores all outputs available in your current session. */
21struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs);
22
23/* This is the output covering the root window */
25static bool has_randr_1_5 = false;
26
27/*
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
30 * re-scanning.
31 *
32 */
33static Output *get_output_by_id(xcb_randr_output_t id) {
34 Output *output;
36 if (output->id == id)
37 return output;
38
39 return NULL;
40}
41
42/*
43 * Returns the output with the given name or NULL.
44 * If require_active is true, only active outputs are considered.
45 *
46 */
47Output *get_output_by_name(const char *name, const bool require_active) {
48 Output *output;
49 bool get_primary = (strcasecmp("primary", name) == 0);
50 TAILQ_FOREACH(output, &outputs, outputs) {
51 if (require_active && !output->active) {
52 continue;
53 }
54 if (output->primary && get_primary) {
55 return output;
56 }
59 if (strcasecmp(output_name->name, name) == 0) {
60 return output;
61 }
62 }
63 }
64
65 return NULL;
66}
67
68/*
69 * Returns the first output which is active.
70 *
71 */
73 Output *output, *result = NULL;
74
75 TAILQ_FOREACH(output, &outputs, outputs) {
76 if (output->active) {
77 if (output->primary) {
78 return output;
79 }
80 if (!result) {
81 result = output;
82 }
83 }
84 }
85
86 if (result) {
87 return result;
88 }
89
90 die("No usable outputs available.\n");
91}
92
93/*
94 * Check whether there are any active outputs (excluding the root output).
95 *
96 */
97static bool any_randr_output_active(void) {
98 Output *output;
99
100 TAILQ_FOREACH(output, &outputs, outputs) {
101 if (output != root_output && !output->to_be_disabled && output->active)
102 return true;
103 }
104
105 return false;
106}
107
108/*
109 * Returns the active (!) output which contains the coordinates x, y or NULL
110 * if there is no output which contains these coordinates.
111 *
112 */
113Output *get_output_containing(unsigned int x, unsigned int y) {
114 Output *output;
115 TAILQ_FOREACH(output, &outputs, outputs) {
116 if (!output->active)
117 continue;
118 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
119 x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
120 if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
121 y >= output->rect.y && y < (output->rect.y + output->rect.height))
122 return output;
123 }
124
125 return NULL;
126}
127
128/*
129 * Returns the active output which contains the midpoint of the given rect. If
130 * such an output doesn't exist, returns the output which contains most of the
131 * rectangle or NULL if there is no output which intersects with it.
132 *
133 */
135 unsigned int mid_x = rect.x + rect.width / 2;
136 unsigned int mid_y = rect.y + rect.height / 2;
137 Output *output = get_output_containing(mid_x, mid_y);
138
139 return output ? output : output_containing_rect(rect);
140}
141
142/*
143 * Returns the active output which spans exactly the area specified by
144 * rect or NULL if there is no output like this.
145 *
146 */
148 Output *output;
149 TAILQ_FOREACH(output, &outputs, outputs) {
150 if (!output->active)
151 continue;
152 DLOG("comparing x=%d y=%d %dx%d with x=%d and y=%d %dx%d\n",
153 rect.x, rect.y, rect.width, rect.height,
154 output->rect.x, output->rect.y, output->rect.width, output->rect.height);
155 if (rect.x == output->rect.x && rect.width == output->rect.width &&
156 rect.y == output->rect.y && rect.height == output->rect.height)
157 return output;
158 }
159
160 return NULL;
161}
162
163/*
164 * In output_containing_rect, we check if any active output contains part of the container.
165 * We do this by checking if the output rect is intersected by the Rect.
166 * This is the 2-dimensional counterpart of get_output_containing.
167 * Returns the output with the maximum intersecting area.
168 *
169 */
171 Output *output;
172 int lx = rect.x, uy = rect.y;
173 int rx = rect.x + rect.width, by = rect.y + rect.height;
174 long max_area = 0;
175 Output *result = NULL;
176 TAILQ_FOREACH(output, &outputs, outputs) {
177 if (!output->active)
178 continue;
179 int lx_o = (int)output->rect.x, uy_o = (int)output->rect.y;
180 int rx_o = (int)(output->rect.x + output->rect.width), by_o = (int)(output->rect.y + output->rect.height);
181 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
182 rect.x, rect.y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
183 int left = max(lx, lx_o);
184 int right = min(rx, rx_o);
185 int bottom = min(by, by_o);
186 int top = max(uy, uy_o);
187 if (left < right && bottom > top) {
188 long area = (right - left) * (bottom - top);
189 if (area > max_area) {
190 result = output;
191 }
192 }
193 }
194 return result;
195}
196
197/*
198 * Like get_output_next with close_far == CLOSEST_OUTPUT, but wraps.
199 *
200 * For example if get_output_next(D_DOWN, x, FARTHEST_OUTPUT) = NULL, then
201 * get_output_next_wrap(D_DOWN, x) will return the topmost output.
202 *
203 * This function always returns a output: if no active outputs can be found,
204 * current itself is returned.
205 *
206 */
208 Output *best = get_output_next(direction, current, CLOSEST_OUTPUT);
209 /* If no output can be found, wrap */
210 if (!best) {
211 direction_t opposite;
212 if (direction == D_RIGHT)
213 opposite = D_LEFT;
214 else if (direction == D_LEFT)
215 opposite = D_RIGHT;
216 else if (direction == D_DOWN)
217 opposite = D_UP;
218 else
219 opposite = D_DOWN;
220 best = get_output_next(opposite, current, FARTHEST_OUTPUT);
221 }
222 if (!best)
223 best = current;
224 DLOG("current = %s, best = %s\n", output_primary_name(current), output_primary_name(best));
225 return best;
226}
227
228/*
229 * Gets the output which is the next one in the given direction.
230 *
231 * If close_far == CLOSEST_OUTPUT, then the output next to the current one will
232 * selected. If close_far == FARTHEST_OUTPUT, the output which is the last one
233 * in the given direction will be selected.
234 *
235 * NULL will be returned when no active outputs are present in the direction
236 * specified (note that “current” counts as such an output).
237 *
238 */
240 Rect *cur = &(current->rect),
241 *other;
242 Output *output,
243 *best = NULL;
244 TAILQ_FOREACH(output, &outputs, outputs) {
245 if (!output->active)
246 continue;
247
248 other = &(output->rect);
249
250 if ((direction == D_RIGHT && other->x > cur->x) ||
251 (direction == D_LEFT && other->x < cur->x)) {
252 /* Skip the output when it doesn’t overlap the other one’s y
253 * coordinate at all. */
254 if ((other->y + other->height) <= cur->y ||
255 (cur->y + cur->height) <= other->y)
256 continue;
257 } else if ((direction == D_DOWN && other->y > cur->y) ||
258 (direction == D_UP && other->y < cur->y)) {
259 /* Skip the output when it doesn’t overlap the other one’s x
260 * coordinate at all. */
261 if ((other->x + other->width) <= cur->x ||
262 (cur->x + cur->width) <= other->x)
263 continue;
264 } else
265 continue;
266
267 /* No candidate yet? Start with this one. */
268 if (!best) {
269 best = output;
270 continue;
271 }
272
273 if (close_far == CLOSEST_OUTPUT) {
274 /* Is this output better (closer to the current output) than our
275 * current best bet? */
276 if ((direction == D_RIGHT && other->x < best->rect.x) ||
277 (direction == D_LEFT && other->x > best->rect.x) ||
278 (direction == D_DOWN && other->y < best->rect.y) ||
279 (direction == D_UP && other->y > best->rect.y)) {
280 best = output;
281 continue;
282 }
283 } else {
284 /* Is this output better (farther to the current output) than our
285 * current best bet? */
286 if ((direction == D_RIGHT && other->x > best->rect.x) ||
287 (direction == D_LEFT && other->x < best->rect.x) ||
288 (direction == D_DOWN && other->y > best->rect.y) ||
289 (direction == D_UP && other->y < best->rect.y)) {
290 best = output;
291 continue;
292 }
293 }
294 }
295
296 DLOG("current = %s, best = %s\n", output_primary_name(current), (best ? output_primary_name(best) : "NULL"));
297 return best;
298}
299
300/*
301 * Creates an output covering the root window.
302 *
303 */
304Output *create_root_output(xcb_connection_t *conn) {
305 Output *s = scalloc(1, sizeof(Output));
306
307 s->active = false;
308 s->rect.x = 0;
309 s->rect.y = 0;
310 s->rect.width = root_screen->width_in_pixels;
311 s->rect.height = root_screen->height_in_pixels;
312
313 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
314 output_name->name = "xroot-0";
317
318 return s;
319}
320
321/*
322 * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
323 * before) to use for the given Output.
324 *
325 */
326void output_init_con(Output *output) {
327 Con *con = NULL, *current;
328 bool reused = false;
329
330 DLOG("init_con for output %s\n", output_primary_name(output));
331
332 /* Search for a Con with that name directly below the root node. There
333 * might be one from a restored layout. */
334 TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
335 if (strcmp(current->name, output_primary_name(output)) != 0)
336 continue;
337
338 con = current;
339 reused = true;
340 DLOG("Using existing con %p / %s\n", con, con->name);
341 break;
342 }
343
344 if (con == NULL) {
345 con = con_new(croot, NULL);
346 FREE(con->name);
347 con->name = sstrdup(output_primary_name(output));
348 con->type = CT_OUTPUT;
349 con->layout = L_OUTPUT;
351 }
352 con->rect = output->rect;
353 output->con = con;
354
355 char *name;
356 sasprintf(&name, "[i3 con] output %s", con->name);
357 x_set_name(con, name);
358 FREE(name);
359
360 if (reused) {
361 DLOG("Not adding workspace, this was a reused con\n");
362 return;
363 }
364
365 DLOG("Changing layout, adding top/bottom dockarea\n");
366 Con *topdock = con_new(NULL, NULL);
367 topdock->type = CT_DOCKAREA;
368 topdock->layout = L_DOCKAREA;
369 /* this container swallows dock clients */
370 Match *match = scalloc(1, sizeof(Match));
371 match_init(match);
372 match->dock = M_DOCK_TOP;
373 match->insert_where = M_BELOW;
374 TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
375
376 FREE(topdock->name);
377 topdock->name = sstrdup("topdock");
378
379 sasprintf(&name, "[i3 con] top dockarea %s", con->name);
380 x_set_name(topdock, name);
381 FREE(name);
382 DLOG("attaching\n");
383 con_attach(topdock, con, false);
384
385 /* content container */
386
387 DLOG("adding main content container\n");
388 Con *content = con_new(NULL, NULL);
389 content->type = CT_CON;
390 content->layout = L_SPLITH;
391 FREE(content->name);
392 content->name = sstrdup("content");
393
394 sasprintf(&name, "[i3 con] content %s", con->name);
395 x_set_name(content, name);
396 FREE(name);
397 con_attach(content, con, false);
398
399 /* bottom dock container */
400 Con *bottomdock = con_new(NULL, NULL);
401 bottomdock->type = CT_DOCKAREA;
402 bottomdock->layout = L_DOCKAREA;
403 /* this container swallows dock clients */
404 match = scalloc(1, sizeof(Match));
405 match_init(match);
406 match->dock = M_DOCK_BOTTOM;
407 match->insert_where = M_BELOW;
408 TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
409
410 FREE(bottomdock->name);
411 bottomdock->name = sstrdup("bottomdock");
412
413 sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
414 x_set_name(bottomdock, name);
415 FREE(name);
416 DLOG("attaching\n");
417 con_attach(bottomdock, con, false);
418
419 /* Change focus to the content container */
420 TAILQ_REMOVE(&(con->focus_head), content, focused);
421 TAILQ_INSERT_HEAD(&(con->focus_head), content, focused);
422}
423
424/*
425 * Initializes at least one workspace for this output, trying the following
426 * steps until there is at least one workspace:
427 *
428 * • Move existing workspaces, which are assigned to be on the given output, to
429 * the output.
430 * • Create the first assigned workspace for this output.
431 * • Create the first unused workspace.
432 *
433 */
435 Con *content = output_get_content(output->con);
436 Con *previous_focus = con_get_workspace(focused);
437
438 /* go through all assignments and move the existing workspaces to this output */
439 struct Workspace_Assignment *assignment;
441 if (!output_triggers_assignment(output, assignment)) {
442 continue;
443 }
444 Con *workspace = get_existing_workspace_by_name(assignment->name);
445 if (workspace == NULL)
446 continue;
447
448 /* check that this workspace is not already attached (that means the
449 * user configured this assignment twice) */
450 Con *workspace_out = con_get_output(workspace);
451 if (workspace_out == output->con) {
452 LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
453 "there. Do you have two assignment directives for the same "
454 "workspace in your configuration file?\n",
455 workspace->name, output_primary_name(output));
456 continue;
457 }
458
459 DLOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
460 workspace->name, workspace_out->name, output_primary_name(output));
461 /* Need to copy output's rect since content is not yet rendered. We
462 * can't call render_con here because render_output only proceeds if a
463 * workspace exists. */
464 content->rect = output->con->rect;
466 }
467
468 /* Temporarily set the focused container, might not be initialized yet. */
469 focused = content;
470
471 /* if a workspace exists, we are done now */
472 if (!TAILQ_EMPTY(&(content->nodes_head))) {
473 /* ensure that one of the workspaces is actually visible (in fullscreen
474 * mode), if they were invisible before, this might not be the case. */
475 Con *visible = NULL;
476 GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
477 if (!visible) {
478 visible = TAILQ_FIRST(&(content->nodes_head));
479 workspace_show(visible);
480 }
481 goto restore_focus;
482 }
483
484 /* otherwise, we create the first assigned ws for this output */
486 if (!output_triggers_assignment(output, assignment)) {
487 continue;
488 }
489
490 LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
491 assignment->name, assignment->output);
492 workspace_show_by_name(assignment->name);
493 goto restore_focus;
494 }
495
496 /* if there is still no workspace, we create the first free workspace */
497 DLOG("Now adding a workspace\n");
499
500restore_focus:
501 if (previous_focus) {
502 workspace_show(previous_focus);
503 }
504}
505
506/*
507 * This function needs to be called when changing the mode of an output when
508 * it already has some workspaces (or a bar window) assigned.
509 *
510 * It reconfigures the bar window for the new mode, copies the new rect into
511 * each workspace on this output and forces all windows on the affected
512 * workspaces to be reconfigured.
513 *
514 * It is necessary to call render_layout() afterwards.
515 *
516 */
517static void output_change_mode(xcb_connection_t *conn, Output *output) {
518 DLOG("Output mode changed, updating rect\n");
519 assert(output->con != NULL);
520 output->con->rect = output->rect;
521
522 Con *content, *workspace, *child;
523
524 /* Point content to the container of the workspaces */
525 content = output_get_content(output->con);
526
527 /* Fix the position of all floating windows on this output.
528 * The 'rect' of each workspace will be updated in src/render.c. */
529 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
530 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
531 floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
532 }
533 }
534
535 /* If default_orientation is NO_ORIENTATION, we change the orientation of
536 * the workspaces and their children depending on output resolution. This is
537 * only done for workspaces with maximum one child. */
539 TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
540 /* Workspaces with more than one child are left untouched because
541 * we do not want to change an existing layout. */
542 if (con_num_children(workspace) > 1)
543 continue;
544
545 workspace->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
546 DLOG("Setting workspace [%d,%s]'s layout to %d.\n", workspace->num, workspace->name, workspace->layout);
547 if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
548 if (child->layout == L_SPLITV || child->layout == L_SPLITH)
549 child->layout = workspace->layout;
550 DLOG("Setting child [%d,%s]'s layout to %d.\n", child->num, child->name, child->layout);
551 }
552 }
553 }
554}
555
556/*
557 * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
558 *
559 */
560static bool randr_query_outputs_15(void) {
561#if XCB_RANDR_MINOR_VERSION < 5
562 return false;
563#else
564 /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
565 if (!has_randr_1_5) {
566 return false;
567 }
568 /* RandR 1.5 available at run-time (supported by the server and not
569 * disabled by the user) */
570 DLOG("Querying outputs using RandR 1.5\n");
571 xcb_generic_error_t *err;
572 xcb_randr_get_monitors_reply_t *monitors =
573 xcb_randr_get_monitors_reply(
574 conn, xcb_randr_get_monitors(conn, root, true), &err);
575 if (err != NULL) {
576 ELOG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
577 free(err);
578 /* Fall back to RandR ≤ 1.4 */
579 return false;
580 }
581
582 /* Mark all outputs as to_be_disabled, since xcb_randr_get_monitors() will
583 * only return active outputs. */
584 Output *output;
586 if (output != root_output) {
587 output->to_be_disabled = true;
588 }
589 }
590
591 DLOG("%d RandR monitors found (timestamp %d)\n",
592 xcb_randr_get_monitors_monitors_length(monitors),
593 monitors->timestamp);
594
595 xcb_randr_monitor_info_iterator_t iter;
596 for (iter = xcb_randr_get_monitors_monitors_iterator(monitors);
597 iter.rem;
598 xcb_randr_monitor_info_next(&iter)) {
599 const xcb_randr_monitor_info_t *monitor_info = iter.data;
600 xcb_get_atom_name_reply_t *atom_reply =
601 xcb_get_atom_name_reply(
602 conn, xcb_get_atom_name(conn, monitor_info->name), &err);
603 if (err != NULL) {
604 ELOG("Could not get RandR monitor name: X11 error code %d\n", err->error_code);
605 free(err);
606 continue;
607 }
608 char *name;
609 sasprintf(&name, "%.*s",
610 xcb_get_atom_name_name_length(atom_reply),
611 xcb_get_atom_name_name(atom_reply));
612 free(atom_reply);
613
614 Output *new = get_output_by_name(name, false);
615 if (new == NULL) {
616 new = scalloc(1, sizeof(Output));
617
618 SLIST_INIT(&new->names_head);
619
620 /* Register associated output names in addition to the monitor name */
621 xcb_randr_output_t *randr_outputs = xcb_randr_monitor_info_outputs(monitor_info);
622 int randr_output_len = xcb_randr_monitor_info_outputs_length(monitor_info);
623 for (int i = 0; i < randr_output_len; i++) {
624 xcb_randr_output_t randr_output = randr_outputs[i];
625
626 xcb_randr_get_output_info_reply_t *info =
627 xcb_randr_get_output_info_reply(conn,
628 xcb_randr_get_output_info(conn, randr_output, monitors->timestamp),
629 NULL);
630
631 if (info != NULL && info->crtc != XCB_NONE) {
632 char *oname;
633 sasprintf(&oname, "%.*s",
634 xcb_randr_get_output_info_name_length(info),
635 xcb_randr_get_output_info_name(info));
636
637 if (strcmp(name, oname) != 0) {
638 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
639 output_name->name = sstrdup(oname);
640 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
641 } else {
642 free(oname);
643 }
644 }
645 FREE(info);
646 }
647
648 /* Insert the monitor name last, so that it's used as the primary name */
649 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
651 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
652
653 if (monitor_info->primary) {
655 } else {
657 }
658 }
659 /* We specified get_active == true in xcb_randr_get_monitors(), so we
660 * will only receive active outputs. */
661 new->active = true;
662 new->to_be_disabled = false;
663
664 new->primary = monitor_info->primary;
665
666 new->changed =
667 update_if_necessary(&(new->rect.x), monitor_info->x) |
668 update_if_necessary(&(new->rect.y), monitor_info->y) |
669 update_if_necessary(&(new->rect.width), monitor_info->width) |
670 update_if_necessary(&(new->rect.height), monitor_info->height);
671
672 DLOG("name %s, x %d, y %d, width %d px, height %d px, width %d mm, height %d mm, primary %d, automatic %d\n",
673 name,
674 monitor_info->x, monitor_info->y, monitor_info->width, monitor_info->height,
675 monitor_info->width_in_millimeters, monitor_info->height_in_millimeters,
676 monitor_info->primary, monitor_info->automatic);
677 free(name);
678 }
679 free(monitors);
680 return true;
681#endif
682}
683
684/*
685 * Gets called by randr_query_outputs_14() for each output. The function adds
686 * new outputs to the list of outputs, checks if the mode of existing outputs
687 * has been changed or if an existing output has been disabled. It will then
688 * change either the "changed" or the "to_be_deleted" flag of the output, if
689 * appropriate.
690 *
691 */
692static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
693 xcb_randr_get_output_info_reply_t *output,
694 xcb_timestamp_t cts,
695 xcb_randr_get_screen_resources_current_reply_t *res) {
696 /* each CRT controller has a position in which we are interested in */
697 xcb_randr_get_crtc_info_reply_t *crtc;
698
699 Output *new = get_output_by_id(id);
700 bool existing = (new != NULL);
701 if (!existing) {
702 new = scalloc(1, sizeof(Output));
703 SLIST_INIT(&new->names_head);
704 }
705 new->id = id;
706 new->primary = (primary && primary->output == id);
707 while (!SLIST_EMPTY(&new->names_head)) {
708 FREE(SLIST_FIRST(&new->names_head)->name);
709 struct output_name *old_head = SLIST_FIRST(&new->names_head);
710 SLIST_REMOVE_HEAD(&new->names_head, names);
711 FREE(old_head);
712 }
713 struct output_name *output_name = scalloc(1, sizeof(struct output_name));
714 sasprintf(&output_name->name, "%.*s",
715 xcb_randr_get_output_info_name_length(output),
716 xcb_randr_get_output_info_name(output));
717 SLIST_INSERT_HEAD(&new->names_head, output_name, names);
718
719 DLOG("found output with name %s\n", output_primary_name(new));
720
721 /* Even if no CRTC is used at the moment, we store the output so that
722 * we do not need to change the list ever again (we only update the
723 * position/size) */
724 if (output->crtc == XCB_NONE) {
725 if (!existing) {
726 if (new->primary)
728 else
730 } else if (new->active)
731 new->to_be_disabled = true;
732 return;
733 }
734
735 xcb_randr_get_crtc_info_cookie_t icookie;
736 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
737 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
738 DLOG("Skipping output %s: could not get CRTC (%p)\n",
739 output_primary_name(new), crtc);
740 free(new);
741 return;
742 }
743
744 bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
745 update_if_necessary(&(new->rect.y), crtc->y) |
746 update_if_necessary(&(new->rect.width), crtc->width) |
747 update_if_necessary(&(new->rect.height), crtc->height);
748 free(crtc);
749 new->active = (new->rect.width != 0 && new->rect.height != 0);
750 if (!new->active) {
751 DLOG("width/height 0/0, disabling output\n");
752 return;
753 }
754
755 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
756 new->rect.x, new->rect.y);
757
758 /* If we don’t need to change an existing output or if the output
759 * does not exist in the first place, the case is simple: we either
760 * need to insert the new output or we are done. */
761 if (!updated || !existing) {
762 if (!existing) {
763 if (new->primary)
765 else
767 }
768 return;
769 }
770
771 new->changed = true;
772}
773
774/*
775 * randr_query_outputs_14 uses RandR ≤ 1.4 to update outputs.
776 *
777 */
778static void randr_query_outputs_14(void) {
779 DLOG("Querying outputs using RandR ≤ 1.4\n");
780
781 /* Get screen resources (primary output, crtcs, outputs, modes) */
782 xcb_randr_get_screen_resources_current_cookie_t rcookie;
783 rcookie = xcb_randr_get_screen_resources_current(conn, root);
784 xcb_randr_get_output_primary_cookie_t pcookie;
785 pcookie = xcb_randr_get_output_primary(conn, root);
786
787 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
788 ELOG("Could not get RandR primary output\n");
789 else
790 DLOG("primary output is %08x\n", primary->output);
791
792 xcb_randr_get_screen_resources_current_reply_t *res =
793 xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL);
794 if (res == NULL) {
795 ELOG("Could not query screen resources.\n");
796 return;
797 }
798
799 /* timestamp of the configuration so that we get consistent replies to all
800 * requests (if the configuration changes between our different calls) */
801 const xcb_timestamp_t cts = res->config_timestamp;
802
803 const int len = xcb_randr_get_screen_resources_current_outputs_length(res);
804
805 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
806 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
807
808 /* Request information for each output */
809 xcb_randr_get_output_info_cookie_t ocookie[len];
810 for (int i = 0; i < len; i++)
811 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
812
813 /* Loop through all outputs available for this X11 screen */
814 for (int i = 0; i < len; i++) {
815 xcb_randr_get_output_info_reply_t *output;
816
817 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
818 continue;
819
820 handle_output(conn, randr_outputs[i], output, cts, res);
821 free(output);
822 }
823
824 FREE(res);
825}
826
827/*
828 * (Re-)queries the outputs via RandR and stores them in the list of outputs.
829 *
830 * If no outputs are found use the root window.
831 *
832 */
834 Output *output, *other;
835
836 if (!randr_query_outputs_15()) {
838 }
839
840 /* If there's no randr output, enable the output covering the root window. */
842 DLOG("Active RandR output found. Disabling root output.\n");
845 }
846 } else {
847 DLOG("No active RandR output found. Enabling root output.\n");
848 root_output->active = true;
849 }
850
851 /* Check for clones, disable the clones and reduce the mode to the
852 * lowest common mode */
853 TAILQ_FOREACH(output, &outputs, outputs) {
854 if (!output->active || output->to_be_disabled)
855 continue;
856 DLOG("output %p / %s, position (%d, %d), checking for clones\n",
857 output, output_primary_name(output), output->rect.x, output->rect.y);
858
859 for (other = output;
860 other != TAILQ_END(&outputs);
861 other = TAILQ_NEXT(other, outputs)) {
862 if (other == output || !other->active || other->to_be_disabled)
863 continue;
864
865 if (other->rect.x != output->rect.x ||
866 other->rect.y != output->rect.y)
867 continue;
868
869 DLOG("output %p has the same position, his mode = %d x %d\n",
870 other, other->rect.width, other->rect.height);
871 uint32_t width = min(other->rect.width, output->rect.width);
872 uint32_t height = min(other->rect.height, output->rect.height);
873
874 if (update_if_necessary(&(output->rect.width), width) |
875 update_if_necessary(&(output->rect.height), height))
876 output->changed = true;
877
878 update_if_necessary(&(other->rect.width), width);
879 update_if_necessary(&(other->rect.height), height);
880
881 DLOG("disabling output %p (%s)\n", other, output_primary_name(other));
882 other->to_be_disabled = true;
883
884 DLOG("new output mode %d x %d, other mode %d x %d\n",
885 output->rect.width, output->rect.height,
886 other->rect.width, other->rect.height);
887 }
888 }
889
890 /* Ensure that all outputs which are active also have a con. This is
891 * necessary because in the next step, a clone might get disabled. Example:
892 * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
893 * LVDS1 gets disabled. */
894 TAILQ_FOREACH(output, &outputs, outputs) {
895 if (output->active && output->con == NULL) {
896 DLOG("Need to initialize a Con for output %s\n", output_primary_name(output));
897 output_init_con(output);
898 output->changed = false;
899 }
900 }
901
902 /* Handle outputs which have a new mode or are disabled now (either
903 * because the user disabled them or because they are clones) */
904 TAILQ_FOREACH(output, &outputs, outputs) {
905 if (output->to_be_disabled) {
906 randr_disable_output(output);
907 }
908
909 if (output->changed) {
910 output_change_mode(conn, output);
911 output->changed = false;
912 }
913 }
914
915 /* Just go through each active output and assign one workspace */
916 TAILQ_FOREACH(output, &outputs, outputs) {
917 if (!output->active)
918 continue;
919 Con *content = output_get_content(output->con);
920 if (!TAILQ_EMPTY(&(content->nodes_head)))
921 continue;
922 DLOG("Should add ws for output %s\n", output_primary_name(output));
923 init_ws_for_output(output);
924 }
925
926 /* Focus the primary screen, if possible */
927 TAILQ_FOREACH(output, &outputs, outputs) {
928 if (!output->primary || !output->con)
929 continue;
930
931 DLOG("Focusing primary output %s\n", output_primary_name(output));
932 Con *content = output_get_content(output->con);
933 Con *ws = TAILQ_FIRST(&(content)->focus_head);
934 workspace_show(ws);
935 }
936
937 /* render_layout flushes */
938 tree_render();
939
940 FREE(primary);
941}
942
943/*
944 * Disables the output and moves its content.
945 *
946 */
948 assert(output->to_be_disabled);
949
950 output->active = false;
951 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output_primary_name(output));
952
953 Output *first = get_first_output();
954
955 /* TODO: refactor the following code into a nice function. maybe
956 * use an on_destroy callback which is implement differently for
957 * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
958 Con *first_content = output_get_content(first->con);
959
960 if (output->con != NULL) {
961 /* We need to move the workspaces from the disappearing output to the first output */
962 /* 1: Get the con to focus next */
963 Con *next = focused;
964
965 /* 2: iterate through workspaces and re-assign them, fixing the coordinates
966 * of floating containers as we go */
967 Con *current;
968 Con *old_content = output_get_content(output->con);
969 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
970 current = TAILQ_FIRST(&(old_content->nodes_head));
971 if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
972 /* the workspace is empty and not focused, get rid of it */
973 DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
974 tree_close_internal(current, DONT_KILL_WINDOW, false);
975 continue;
976 }
977 DLOG("Detaching current = %p / %s\n", current, current->name);
978 con_detach(current);
979 DLOG("Re-attaching current = %p / %s\n", current, current->name);
980 con_attach(current, first_content, false);
981 DLOG("Fixing the coordinates of floating containers\n");
982 Con *floating_con;
983 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows) {
984 floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
985 }
986 }
987
988 /* Restore focus after con_detach / con_attach. next can be NULL, see #3523. */
989 if (next) {
990 DLOG("now focusing next = %p\n", next);
991 con_focus(next);
993 }
994
995 /* 3: move the dock clients to the first output */
996 Con *child;
997 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
998 if (child->type != CT_DOCKAREA)
999 continue;
1000 DLOG("Handling dock con %p\n", child);
1001 Con *dock;
1002 while (!TAILQ_EMPTY(&(child->nodes_head))) {
1003 dock = TAILQ_FIRST(&(child->nodes_head));
1004 Con *nc;
1005 Match *match;
1006 nc = con_for_window(first->con, dock->window, &match);
1007 DLOG("Moving dock client %p to nc %p\n", dock, nc);
1008 con_detach(dock);
1009 DLOG("Re-attaching\n");
1010 con_attach(dock, nc, false);
1011 DLOG("Done\n");
1012 }
1013 }
1014
1015 DLOG("destroying disappearing con %p\n", output->con);
1016 Con *con = output->con;
1017 /* clear the pointer before calling tree_close_internal in which the memory is freed */
1018 output->con = NULL;
1020 DLOG("Done. Should be fine now\n");
1021 }
1022
1023 output->to_be_disabled = false;
1024 output->changed = false;
1025}
1026
1027static void fallback_to_root_output(void) {
1028 root_output->active = true;
1031}
1032
1033/*
1034 * We have just established a connection to the X server and need the initial
1035 * XRandR information to setup workspaces for each screen.
1036 *
1037 */
1038void randr_init(int *event_base, const bool disable_randr15) {
1039 const xcb_query_extension_reply_t *extreply;
1040
1043
1044 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
1045 if (!extreply->present) {
1046 DLOG("RandR is not present, activating root output.\n");
1048 return;
1049 }
1050
1051 xcb_generic_error_t *err;
1052 xcb_randr_query_version_reply_t *randr_version =
1053 xcb_randr_query_version_reply(
1054 conn, xcb_randr_query_version(conn, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION), &err);
1055 if (err != NULL) {
1056 ELOG("Could not query RandR version: X11 error code %d\n", err->error_code);
1057 free(err);
1059 return;
1060 }
1061
1062 has_randr_1_5 = (randr_version->major_version >= 1) &&
1063 (randr_version->minor_version >= 5) &&
1064 !disable_randr15;
1065
1066 free(randr_version);
1067
1069
1070 if (event_base != NULL)
1071 *event_base = extreply->first_event;
1072
1073 xcb_randr_select_input(conn, root,
1074 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
1075 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
1076 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
1077 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
1078
1079 xcb_flush(conn);
1080}
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Definition: output.c:51
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
bool update_if_necessary(uint32_t *destination, const uint32_t new_value)
Updates *destination with new_value and returns true if it was changed or false if it was the same.
Definition: util.c:125
int min(int a, int b)
Definition: util.c:27
int max(int a, int b)
Definition: util.c:31
struct Con * focused
Definition: tree.c:13
struct Con * croot
Definition: tree.c:12
bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent)
Closes the given container including all children.
Definition: tree.c:191
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition: tree.c:449
#define y(x,...)
Definition: commands.c:21
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name.
Definition: x.c:1399
void match_init(Match *match)
Initializes the Match data structure.
Definition: match.c:26
Config config
Definition: config.c:17
Con * con_for_window(Con *con, i3Window *window, Match **store_match)
Returns the first container below 'con' which wants to swallow this window TODO: priority.
Definition: con.c:827
Con * con_new(Con *parent, i3Window *window)
A wrapper for con_new_skeleton, to retain the old con_new behaviour.
Definition: con.c:69
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:453
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition: con.c:206
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:985
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:198
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:922
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:222
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition: con.c:439
void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect)
Fixes the coordinates of the floating window whenever the window gets reassigned to a different outpu...
Definition: floating.c:801
Con * get_existing_workspace_by_name(const char *name)
Returns the workspace with the given name or NULL if such a workspace does not exist.
Definition: workspace.c:30
bool output_triggers_assignment(Output *output, struct Workspace_Assignment *assignment)
Returns true if the first output assigned to a workspace with the given workspace assignment is the s...
Definition: workspace.c:114
Con * create_workspace_on_output(Output *output, Con *content)
Returns a pointer to a new workspace in the given output.
Definition: workspace.c:244
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:421
void workspace_move_to_output(Con *ws, Output *output)
Move the given workspace to the specified output.
Definition: workspace.c:968
void workspace_show_by_name(const char *num)
Looks up the workspace by name and switches to it.
Definition: workspace.c:553
Output * get_output_by_name(const char *name, const bool require_active)
Returns the output with the given name or NULL.
Definition: randr.c:47
Output * get_output_from_rect(Rect rect)
Returns the active output which contains the midpoint of the given rect.
Definition: randr.c:134
static void randr_query_outputs_14(void)
Definition: randr.c:778
Output * output_containing_rect(Rect rect)
In output_containing_rect, we check if any active output contains part of the container.
Definition: randr.c:170
Output * get_output_with_dimensions(Rect rect)
Returns the active output which spans exactly the area specified by rect or NULL if there is no outpu...
Definition: randr.c:147
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
Definition: randr.c:113
void init_ws_for_output(Output *output)
Initializes at least one workspace for this output, trying the following steps until there is at leas...
Definition: randr.c:434
void randr_query_outputs(void)
Initializes the specified output, assigning the specified workspace to it.
Definition: randr.c:833
static void output_change_mode(xcb_connection_t *conn, Output *output)
Definition: randr.c:517
static Output * get_output_by_id(xcb_randr_output_t id)
Definition: randr.c:33
Output * get_output_next_wrap(direction_t direction, Output *current)
Like get_output_next with close_far == CLOSEST_OUTPUT, but wraps.
Definition: randr.c:207
void output_init_con(Output *output)
Initializes a CT_OUTPUT Con (searches existing ones from inplace restart before) to use for the given...
Definition: randr.c:326
Output * get_output_next(direction_t direction, Output *current, output_close_far_t close_far)
Gets the output which is the next one in the given direction.
Definition: randr.c:239
struct outputs_head outputs
Definition: randr.c:21
static Output * root_output
Definition: randr.c:24
void randr_init(int *event_base, const bool disable_randr15)
We have just established a connection to the X server and need the initial XRandR information to setu...
Definition: randr.c:1038
Output * create_root_output(xcb_connection_t *conn)
Creates an output covering the root window.
Definition: randr.c:304
Output * get_first_output(void)
Returns the first output which is active.
Definition: randr.c:72
xcb_randr_get_output_primary_reply_t * primary
Definition: randr.c:18
static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id, xcb_randr_get_output_info_reply_t *output, xcb_timestamp_t cts, xcb_randr_get_screen_resources_current_reply_t *res)
Definition: randr.c:692
static void fallback_to_root_output(void)
Definition: randr.c:1027
static bool has_randr_1_5
Definition: randr.c:25
static bool randr_query_outputs_15(void)
Definition: randr.c:560
void randr_disable_output(Output *output)
Disables the output and moves its content.
Definition: randr.c:947
static bool any_randr_output_active(void)
Definition: randr.c:97
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:44
xcb_window_t root
Definition: main.c:57
xcb_screen_t * root_screen
Definition: main.c:56
struct ws_assignments_head ws_assignments
Definition: main.c:87
#define SLIST_FOREACH(var, head, field)
Definition: queue.h:114
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define SLIST_INIT(head)
Definition: queue.h:127
#define TAILQ_END(head)
Definition: queue.h:337
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
#define SLIST_EMPTY(head)
Definition: queue.h:111
#define TAILQ_FIRST(head)
Definition: queue.h:336
#define SLIST_FIRST(head)
Definition: queue.h:109
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_EMPTY(head)
Definition: queue.h:344
#define SLIST_REMOVE_HEAD(head, field)
Definition: queue.h:149
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
#define DLOG(fmt,...)
Definition: libi3.h:104
#define LOG(fmt,...)
Definition: libi3.h:94
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define ELOG(fmt,...)
Definition: libi3.h:99
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
@ L_DOCKAREA
Definition: data.h:97
@ L_OUTPUT
Definition: data.h:98
@ L_SPLITH
Definition: data.h:100
@ L_SPLITV
Definition: data.h:99
@ NO_ORIENTATION
Definition: data.h:59
@ CF_OUTPUT
Definition: data.h:627
@ DONT_KILL_WINDOW
Definition: data.h:70
direction_t
Definition: data.h:55
@ D_RIGHT
Definition: data.h:56
@ D_LEFT
Definition: data.h:55
@ D_UP
Definition: data.h:57
@ D_DOWN
Definition: data.h:58
#define die(...)
Definition: util.h:19
#define GREP_FIRST(dest, head, condition)
Definition: util.h:38
#define FREE(pointer)
Definition: util.h:47
output_close_far_t
Definition: randr.h:22
@ FARTHEST_OUTPUT
Definition: randr.h:24
@ CLOSEST_OUTPUT
Definition: randr.h:23
int default_orientation
Default orientation for new containers.
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:175
uint32_t height
Definition: data.h:179
uint32_t x
Definition: data.h:176
uint32_t y
Definition: data.h:177
uint32_t width
Definition: data.h:178
Stores which workspace (by name or number) goes to which output.
Definition: data.h:225
char * name
Definition: data.h:380
An Output is a physical output on your graphics driver.
Definition: data.h:393
Con * con
Pointer to the Con which represents this output.
Definition: data.h:414
names_head
List of names for the output.
Definition: data.h:411
bool changed
Internal flags, necessary for querying RandR screens (happens in two stages)
Definition: data.h:403
bool to_be_disabled
Definition: data.h:404
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
Definition: data.h:399
xcb_randr_output_t id
Output id, so that we can requery the output directly later.
Definition: data.h:395
bool primary
Definition: data.h:405
Rect rect
x, y, width, height
Definition: data.h:417
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
Definition: data.h:530
enum Match::@15 dock
enum Match::@17 insert_where
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:641
struct Rect rect
Definition: data.h:677
swallow_head
Definition: data.h:728
enum Con::@20 type
focus_head
Definition: data.h:725
layout_t layout
Definition: data.h:751
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
Definition: data.h:671
struct Window * window
Definition: data.h:709
nodes_head
Definition: data.h:722
char * name
Definition: data.h:687
floating_head
Definition: data.h:719
fullscreen_mode_t fullscreen_mode
Definition: data.h:730