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