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