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