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