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