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