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