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-2012 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 static bool randr_disabled = false;
31 
32 /*
33  * Get a specific output by its internal X11 id. Used by randr_query_outputs
34  * to check if the output is new (only in the first scan) or if we are
35  * re-scanning.
36  *
37  */
38 static Output *get_output_by_id(xcb_randr_output_t id) {
39  Output *output;
40  TAILQ_FOREACH(output, &outputs, outputs)
41  if (output->id == id)
42  return output;
43 
44  return NULL;
45 }
46 
47 /*
48  * Returns the output with the given name if it is active (!) or NULL.
49  *
50  */
51 Output *get_output_by_name(const char *name) {
52  Output *output;
53  TAILQ_FOREACH(output, &outputs, outputs)
54  if (output->active &&
55  strcasecmp(output->name, name) == 0)
56  return output;
57 
58  return NULL;
59 }
60 
61 /*
62  * Returns the first output which is active.
63  *
64  */
66  Output *output;
67 
68  TAILQ_FOREACH(output, &outputs, outputs)
69  if (output->active)
70  return output;
71 
72  die("No usable outputs available.\n");
73 }
74 
75 /*
76  * Returns the active (!) output which contains the coordinates x, y or NULL
77  * if there is no output which contains these coordinates.
78  *
79  */
81  Output *output;
82  TAILQ_FOREACH(output, &outputs, outputs) {
83  if (!output->active)
84  continue;
85  DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
86  x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
87  if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
88  y >= output->rect.y && y < (output->rect.y + output->rect.height))
89  return output;
90  }
91 
92  return NULL;
93 }
94 
95 /*
96  * Gets the output which is the last one in the given direction, for example
97  * the output on the most bottom when direction == D_DOWN, the output most
98  * right when direction == D_RIGHT and so on.
99  *
100  * This function always returns a output.
101  *
102  */
103 Output *get_output_most(direction_t direction, Output *current) {
104  Output *best = get_output_next(direction, current, FARTHEST_OUTPUT);
105  if (!best)
106  best = current;
107  DLOG("current = %s, best = %s\n", current->name, best->name);
108  return best;
109 }
110 
111 /*
112  * Gets the output which is the next one in the given direction.
113  *
114  */
115 Output *get_output_next(direction_t direction, Output *current, output_close_far_t close_far) {
116  Rect *cur = &(current->rect),
117  *other;
118  Output *output,
119  *best = NULL;
120  TAILQ_FOREACH(output, &outputs, outputs) {
121  if (!output->active)
122  continue;
123 
124  other = &(output->rect);
125 
126  if ((direction == D_RIGHT && other->x > cur->x) ||
127  (direction == D_LEFT && other->x < cur->x)) {
128  /* Skip the output when it doesn’t overlap the other one’s y
129  * coordinate at all. */
130  if ((other->y + other->height) <= cur->y ||
131  (cur->y + cur->height) <= other->y)
132  continue;
133  } else if ((direction == D_DOWN && other->y > cur->y) ||
134  (direction == D_UP && other->y < cur->y)) {
135  /* Skip the output when it doesn’t overlap the other one’s x
136  * coordinate at all. */
137  if ((other->x + other->width) <= cur->x ||
138  (cur->x + cur->width) <= other->x)
139  continue;
140  } else
141  continue;
142 
143  /* No candidate yet? Start with this one. */
144  if (!best) {
145  best = output;
146  continue;
147  }
148 
149  if (close_far == CLOSEST_OUTPUT) {
150  /* Is this output better (closer to the current output) than our
151  * current best bet? */
152  if ((direction == D_RIGHT && other->x < best->rect.x) ||
153  (direction == D_LEFT && other->x > best->rect.x) ||
154  (direction == D_DOWN && other->y < best->rect.y) ||
155  (direction == D_UP && other->y > best->rect.y)) {
156  best = output;
157  continue;
158  }
159  } else {
160  /* Is this output better (farther to the current output) than our
161  * current best bet? */
162  if ((direction == D_RIGHT && other->x > best->rect.x) ||
163  (direction == D_LEFT && other->x < best->rect.x) ||
164  (direction == D_DOWN && other->y > best->rect.y) ||
165  (direction == D_UP && other->y < best->rect.y)) {
166  best = output;
167  continue;
168  }
169  }
170  }
171 
172  DLOG("current = %s, best = %s\n", current->name, (best ? best->name : "NULL"));
173  return best;
174 }
175 
176 /*
177  * Disables RandR support by creating exactly one output with the size of the
178  * X11 screen.
179  *
180  */
181 void disable_randr(xcb_connection_t *conn) {
182  DLOG("RandR extension unusable, disabling.\n");
183 
184  Output *s = scalloc(sizeof(Output));
185 
186  s->active = true;
187  s->rect.x = 0;
188  s->rect.y = 0;
189  s->rect.width = root_screen->width_in_pixels;
190  s->rect.height = root_screen->height_in_pixels;
191  s->name = "xroot-0";
192  output_init_con(s);
194 
196 
197  randr_disabled = true;
198 }
199 
200 /*
201  * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
202  * before) to use for the given Output.
203  *
204  */
205 void output_init_con(Output *output) {
206  Con *con = NULL, *current;
207  bool reused = false;
208 
209  DLOG("init_con for output %s\n", output->name);
210 
211  /* Search for a Con with that name directly below the root node. There
212  * might be one from a restored layout. */
213  TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
214  if (strcmp(current->name, output->name) != 0)
215  continue;
216 
217  con = current;
218  reused = true;
219  DLOG("Using existing con %p / %s\n", con, con->name);
220  break;
221  }
222 
223  if (con == NULL) {
224  con = con_new(croot, NULL);
225  FREE(con->name);
226  con->name = sstrdup(output->name);
227  con->type = CT_OUTPUT;
228  con->layout = L_OUTPUT;
230  }
231  con->rect = output->rect;
232  output->con = con;
233 
234  char *name;
235  sasprintf(&name, "[i3 con] output %s", con->name);
236  x_set_name(con, name);
237  FREE(name);
238 
239  if (reused) {
240  DLOG("Not adding workspace, this was a reused con\n");
241  return;
242  }
243 
244  DLOG("Changing layout, adding top/bottom dockarea\n");
245  Con *topdock = con_new(NULL, NULL);
246  topdock->type = CT_DOCKAREA;
247  topdock->layout = L_DOCKAREA;
248  /* this container swallows dock clients */
249  Match *match = scalloc(sizeof(Match));
250  match_init(match);
251  match->dock = M_DOCK_TOP;
252  match->insert_where = M_BELOW;
253  TAILQ_INSERT_TAIL(&(topdock->swallow_head), match, matches);
254 
255  FREE(topdock->name);
256  topdock->name = sstrdup("topdock");
257 
258  sasprintf(&name, "[i3 con] top dockarea %s", con->name);
259  x_set_name(topdock, name);
260  FREE(name);
261  DLOG("attaching\n");
262  con_attach(topdock, con, false);
263 
264  /* content container */
265 
266  DLOG("adding main content container\n");
267  Con *content = con_new(NULL, NULL);
268  content->type = CT_CON;
269  content->layout = L_SPLITH;
270  FREE(content->name);
271  content->name = sstrdup("content");
272 
273  sasprintf(&name, "[i3 con] content %s", con->name);
274  x_set_name(content, name);
275  FREE(name);
276  con_attach(content, con, false);
277 
278  /* bottom dock container */
279  Con *bottomdock = con_new(NULL, NULL);
280  bottomdock->type = CT_DOCKAREA;
281  bottomdock->layout = L_DOCKAREA;
282  /* this container swallows dock clients */
283  match = scalloc(sizeof(Match));
284  match_init(match);
285  match->dock = M_DOCK_BOTTOM;
286  match->insert_where = M_BELOW;
287  TAILQ_INSERT_TAIL(&(bottomdock->swallow_head), match, matches);
288 
289  FREE(bottomdock->name);
290  bottomdock->name = sstrdup("bottomdock");
291 
292  sasprintf(&name, "[i3 con] bottom dockarea %s", con->name);
293  x_set_name(bottomdock, name);
294  FREE(name);
295  DLOG("attaching\n");
296  con_attach(bottomdock, con, false);
297 }
298 
299 /*
300  * Initializes at least one workspace for this output, trying the following
301  * steps until there is at least one workspace:
302  *
303  * • Move existing workspaces, which are assigned to be on the given output, to
304  * the output.
305  * • Create the first assigned workspace for this output.
306  * • Create the first unused workspace.
307  *
308  */
309 void init_ws_for_output(Output *output, Con *content) {
310  /* go through all assignments and move the existing workspaces to this output */
311  struct Workspace_Assignment *assignment;
313  if (strcmp(assignment->output, output->name) != 0)
314  continue;
315 
316  /* check if this workspace actually exists */
317  Con *workspace = NULL, *out;
318  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
319  GREP_FIRST(workspace, output_get_content(out),
320  !strcasecmp(child->name, assignment->name));
321  if (workspace == NULL)
322  continue;
323 
324  /* check that this workspace is not already attached (that means the
325  * user configured this assignment twice) */
326  Con *workspace_out = con_get_output(workspace);
327  if (workspace_out == output->con) {
328  LOG("Workspace \"%s\" assigned to output \"%s\", but it is already "
329  "there. Do you have two assignment directives for the same "
330  "workspace in your configuration file?\n",
331  workspace->name, output->name);
332  continue;
333  }
334 
335  /* if so, move it over */
336  LOG("Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
337  workspace->name, workspace_out->name, output->name);
338 
339  /* if the workspace is currently visible on that output, we need to
340  * switch to a different workspace - otherwise the output would end up
341  * with no active workspace */
342  bool visible = workspace_is_visible(workspace);
343  Con *previous = NULL;
344  if (visible && (previous = TAILQ_NEXT(workspace, focused))) {
345  LOG("Switching to previously used workspace \"%s\" on output \"%s\"\n",
346  previous->name, workspace_out->name);
347  workspace_show(previous);
348  }
349 
350  /* Render the output on which the workspace was to get correct Rects.
351  * Then, we need to work with the "content" container, since we cannot
352  * be sure that the workspace itself was rendered at all (in case it’s
353  * invisible, it won’t be rendered). */
354  render_con(workspace_out, false);
355  Con *ws_out_content = output_get_content(workspace_out);
356 
357  Con *floating_con;
358  TAILQ_FOREACH(floating_con, &(workspace->floating_head), floating_windows)
359  /* NB: We use output->con here because content is not yet rendered,
360  * so it has a rect of {0, 0, 0, 0}. */
361  floating_fix_coordinates(floating_con, &(ws_out_content->rect), &(output->con->rect));
362 
363  con_detach(workspace);
364  con_attach(workspace, content, false);
365 
366  /* In case the workspace we just moved was visible but there was no
367  * other workspace to switch to, we need to initialize the source
368  * output aswell */
369  if (visible && previous == NULL) {
370  LOG("There is no workspace left on \"%s\", re-initializing\n",
371  workspace_out->name);
373  output_get_content(workspace_out));
374  DLOG("Done re-initializing, continuing with \"%s\"\n", output->name);
375  }
376  }
377 
378  /* if a workspace exists, we are done now */
379  if (!TAILQ_EMPTY(&(content->nodes_head))) {
380  /* ensure that one of the workspaces is actually visible (in fullscreen
381  * mode), if they were invisible before, this might not be the case. */
382  Con *visible = NULL;
383  GREP_FIRST(visible, content, child->fullscreen_mode == CF_OUTPUT);
384  if (!visible) {
385  visible = TAILQ_FIRST(&(content->nodes_head));
386  focused = content;
387  workspace_show(visible);
388  }
389  return;
390  }
391 
392  /* otherwise, we create the first assigned ws for this output */
394  if (strcmp(assignment->output, output->name) != 0)
395  continue;
396 
397  LOG("Initializing first assigned workspace \"%s\" for output \"%s\"\n",
398  assignment->name, assignment->output);
399  focused = content;
400  workspace_show_by_name(assignment->name);
401  return;
402  }
403 
404  /* if there is still no workspace, we create the first free workspace */
405  DLOG("Now adding a workspace\n");
406  Con *ws = create_workspace_on_output(output, content);
407 
408  /* TODO: Set focus in main.c */
409  con_focus(ws);
410 }
411 
412 /*
413  * This function needs to be called when changing the mode of an output when
414  * it already has some workspaces (or a bar window) assigned.
415  *
416  * It reconfigures the bar window for the new mode, copies the new rect into
417  * each workspace on this output and forces all windows on the affected
418  * workspaces to be reconfigured.
419  *
420  * It is necessary to call render_layout() afterwards.
421  *
422  */
423 static void output_change_mode(xcb_connection_t *conn, Output *output) {
424  DLOG("Output mode changed, updating rect\n");
425  assert(output->con != NULL);
426  output->con->rect = output->rect;
427 
428  Con *content, *workspace, *child;
429 
430  /* Point content to the container of the workspaces */
431  content = output_get_content(output->con);
432 
433  /* Fix the position of all floating windows on this output.
434  * The 'rect' of each workspace will be updated in src/render.c. */
435  TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
436  TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
437  floating_fix_coordinates(child, &(workspace->rect), &(output->con->rect));
438  }
439  }
440 
441  /* If default_orientation is NO_ORIENTATION, we change the orientation of
442  * the workspaces and their childs depending on output resolution. This is
443  * only done for workspaces with maximum one child. */
445  TAILQ_FOREACH(workspace, &(content->nodes_head), nodes) {
446  /* Workspaces with more than one child are left untouched because
447  * we do not want to change an existing layout. */
448  if (con_num_children(workspace) > 1)
449  continue;
450 
451  workspace->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
452  DLOG("Setting workspace [%d,%s]'s layout to %d.\n", workspace->num, workspace->name, workspace->layout);
453  if ((child = TAILQ_FIRST(&(workspace->nodes_head)))) {
454  if (child->layout == L_SPLITV || child->layout == L_SPLITH)
455  child->layout = workspace->layout;
456  DLOG("Setting child [%d,%s]'s layout to %d.\n", child->num, child->name, child->layout);
457  }
458  }
459  }
460 }
461 
462 /*
463  * Gets called by randr_query_outputs() for each output. The function adds new
464  * outputs to the list of outputs, checks if the mode of existing outputs has
465  * been changed or if an existing output has been disabled. It will then change
466  * either the "changed" or the "to_be_deleted" flag of the output, if
467  * appropriate.
468  *
469  */
470 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
471  xcb_randr_get_output_info_reply_t *output,
472  xcb_timestamp_t cts, resources_reply *res) {
473  /* each CRT controller has a position in which we are interested in */
474  crtc_info *crtc;
475 
476  Output *new = get_output_by_id(id);
477  bool existing = (new != NULL);
478  if (!existing)
479  new = scalloc(sizeof(Output));
480  new->id = id;
481  new->primary = (primary && primary->output == id);
482  FREE(new->name);
483  sasprintf(&new->name, "%.*s",
484  xcb_randr_get_output_info_name_length(output),
485  xcb_randr_get_output_info_name(output));
486 
487  DLOG("found output with name %s\n", new->name);
488 
489  /* Even if no CRTC is used at the moment, we store the output so that
490  * we do not need to change the list ever again (we only update the
491  * position/size) */
492  if (output->crtc == XCB_NONE) {
493  if (!existing) {
494  if (new->primary)
496  else TAILQ_INSERT_TAIL(&outputs, new, outputs);
497  } else if (new->active)
498  new->to_be_disabled = true;
499  return;
500  }
501 
502  xcb_randr_get_crtc_info_cookie_t icookie;
503  icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
504  if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
505  DLOG("Skipping output %s: could not get CRTC (%p)\n",
506  new->name, crtc);
507  free(new);
508  return;
509  }
510 
511  bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
512  update_if_necessary(&(new->rect.y), crtc->y) |
513  update_if_necessary(&(new->rect.width), crtc->width) |
514  update_if_necessary(&(new->rect.height), crtc->height);
515  free(crtc);
516  new->active = (new->rect.width != 0 && new->rect.height != 0);
517  if (!new->active) {
518  DLOG("width/height 0/0, disabling output\n");
519  return;
520  }
521 
522  DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
523  new->rect.x, new->rect.y);
524 
525  /* If we don’t need to change an existing output or if the output
526  * does not exist in the first place, the case is simple: we either
527  * need to insert the new output or we are done. */
528  if (!updated || !existing) {
529  if (!existing) {
530  if (new->primary)
532  else TAILQ_INSERT_TAIL(&outputs, new, outputs);
533  }
534  return;
535  }
536 
537  new->changed = true;
538 }
539 
540 /*
541  * (Re-)queries the outputs via RandR and stores them in the list of outputs.
542  *
543  */
545  Output *output, *other, *first;
546  xcb_randr_get_output_primary_cookie_t pcookie;
547  xcb_randr_get_screen_resources_current_cookie_t rcookie;
548  resources_reply *res;
549 
550  /* timestamp of the configuration so that we get consistent replies to all
551  * requests (if the configuration changes between our different calls) */
552  xcb_timestamp_t cts;
553 
554  /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
555  xcb_randr_output_t *randr_outputs;
556 
557  if (randr_disabled)
558  return;
559 
560  /* Get screen resources (primary output, crtcs, outputs, modes) */
561  rcookie = xcb_randr_get_screen_resources_current(conn, root);
562  pcookie = xcb_randr_get_output_primary(conn, root);
563 
564  if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
565  ELOG("Could not get RandR primary output\n");
566  else DLOG("primary output is %08x\n", primary->output);
567  if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
569  return;
570  }
571  cts = res->config_timestamp;
572 
573  int len = xcb_randr_get_screen_resources_current_outputs_length(res);
574  randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
575 
576  /* Request information for each output */
577  xcb_randr_get_output_info_cookie_t ocookie[len];
578  for (int i = 0; i < len; i++)
579  ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
580 
581  /* Loop through all outputs available for this X11 screen */
582  for (int i = 0; i < len; i++) {
583  xcb_randr_get_output_info_reply_t *output;
584 
585  if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
586  continue;
587 
588  handle_output(conn, randr_outputs[i], output, cts, res);
589  free(output);
590  }
591 
592  /* Check for clones, disable the clones and reduce the mode to the
593  * lowest common mode */
594  TAILQ_FOREACH(output, &outputs, outputs) {
595  if (!output->active || output->to_be_disabled)
596  continue;
597  DLOG("output %p / %s, position (%d, %d), checking for clones\n",
598  output, output->name, output->rect.x, output->rect.y);
599 
600  for (other = output;
601  other != TAILQ_END(&outputs);
602  other = TAILQ_NEXT(other, outputs)) {
603  if (other == output || !other->active || other->to_be_disabled)
604  continue;
605 
606  if (other->rect.x != output->rect.x ||
607  other->rect.y != output->rect.y)
608  continue;
609 
610  DLOG("output %p has the same position, his mode = %d x %d\n",
611  other, other->rect.width, other->rect.height);
612  uint32_t width = min(other->rect.width, output->rect.width);
613  uint32_t height = min(other->rect.height, output->rect.height);
614 
615  if (update_if_necessary(&(output->rect.width), width) |
616  update_if_necessary(&(output->rect.height), height))
617  output->changed = true;
618 
619  update_if_necessary(&(other->rect.width), width);
620  update_if_necessary(&(other->rect.height), height);
621 
622  DLOG("disabling output %p (%s)\n", other, other->name);
623  other->to_be_disabled = true;
624 
625  DLOG("new output mode %d x %d, other mode %d x %d\n",
626  output->rect.width, output->rect.height,
627  other->rect.width, other->rect.height);
628  }
629  }
630 
631  /* Ensure that all outputs which are active also have a con. This is
632  * necessary because in the next step, a clone might get disabled. Example:
633  * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
634  * LVDS1 gets disabled. */
635  TAILQ_FOREACH(output, &outputs, outputs) {
636  if (output->active && output->con == NULL) {
637  DLOG("Need to initialize a Con for output %s\n", output->name);
638  output_init_con(output);
639  output->changed = false;
640  }
641  }
642 
643  /* Handle outputs which have a new mode or are disabled now (either
644  * because the user disabled them or because they are clones) */
645  TAILQ_FOREACH(output, &outputs, outputs) {
646  if (output->to_be_disabled) {
647  output->active = false;
648  DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name);
649 
650  first = get_first_output();
651 
652  /* TODO: refactor the following code into a nice function. maybe
653  * use an on_destroy callback which is implement differently for
654  * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
655  Con *first_content = output_get_content(first->con);
656 
657  if (output->con != NULL) {
658  /* We need to move the workspaces from the disappearing output to the first output */
659  /* 1: Get the con to focus next, if the disappearing ws is focused */
660  Con *next = NULL;
661  if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
662  DLOG("This output (%p) was focused! Getting next\n", output->con);
663  next = focused;
664  DLOG("next = %p\n", next);
665  }
666 
667  /* 2: iterate through workspaces and re-assign them, fixing the coordinates
668  * of floating containers as we go */
669  Con *current;
670  Con *old_content = output_get_content(output->con);
671  while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
672  current = TAILQ_FIRST(&(old_content->nodes_head));
673  if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
674  /* the workspace is empty and not focused, get rid of it */
675  DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
676  tree_close(current, DONT_KILL_WINDOW, false, false);
677  continue;
678  }
679  DLOG("Detaching current = %p / %s\n", current, current->name);
680  con_detach(current);
681  DLOG("Re-attaching current = %p / %s\n", current, current->name);
682  con_attach(current, first_content, false);
683  DLOG("Fixing the coordinates of floating containers\n");
684  Con *floating_con;
685  TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows)
686  floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
687  DLOG("Done, next\n");
688  }
689  DLOG("re-attached all workspaces\n");
690 
691  if (next) {
692  DLOG("now focusing next = %p\n", next);
693  con_focus(next);
695  }
696 
697  /* 3: move the dock clients to the first output */
698  Con *child;
699  TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
700  if (child->type != CT_DOCKAREA)
701  continue;
702  DLOG("Handling dock con %p\n", child);
703  Con *dock;
704  while (!TAILQ_EMPTY(&(child->nodes_head))) {
705  dock = TAILQ_FIRST(&(child->nodes_head));
706  Con *nc;
707  Match *match;
708  nc = con_for_window(first->con, dock->window, &match);
709  DLOG("Moving dock client %p to nc %p\n", dock, nc);
710  con_detach(dock);
711  DLOG("Re-attaching\n");
712  con_attach(dock, nc, false);
713  DLOG("Done\n");
714  }
715  }
716 
717  DLOG("destroying disappearing con %p\n", output->con);
718  tree_close(output->con, DONT_KILL_WINDOW, true, false);
719  DLOG("Done. Should be fine now\n");
720  output->con = NULL;
721  }
722 
723  output->to_be_disabled = false;
724  output->changed = false;
725  }
726 
727  if (output->changed) {
728  output_change_mode(conn, output);
729  output->changed = false;
730  }
731  }
732 
733  if (TAILQ_EMPTY(&outputs)) {
734  ELOG("No outputs found via RandR, disabling\n");
736  }
737 
738  /* Verifies that there is at least one active output as a side-effect. */
740 
741  /* Just go through each active output and assign one workspace */
742  TAILQ_FOREACH(output, &outputs, outputs) {
743  if (!output->active)
744  continue;
745  Con *content = output_get_content(output->con);
746  if (!TAILQ_EMPTY(&(content->nodes_head)))
747  continue;
748  DLOG("Should add ws for output %s\n", output->name);
749  init_ws_for_output(output, content);
750  }
751 
752  /* Focus the primary screen, if possible */
753  TAILQ_FOREACH(output, &outputs, outputs) {
754  if (!output->primary || !output->con)
755  continue;
756 
757  DLOG("Focusing primary output %s\n", output->name);
759  }
760 
761  /* render_layout flushes */
762  tree_render();
763 
764  FREE(res);
765  FREE(primary);
766 }
767 
768 /*
769  * We have just established a connection to the X server and need the initial
770  * XRandR information to setup workspaces for each screen.
771  *
772  */
773 void randr_init(int *event_base) {
774  const xcb_query_extension_reply_t *extreply;
775 
776  extreply = xcb_get_extension_data(conn, &xcb_randr_id);
777  if (!extreply->present) {
779  return;
780  } else randr_query_outputs();
781 
782  if (event_base != NULL)
783  *event_base = extreply->first_event;
784 
785  xcb_randr_select_input(conn, root,
786  XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
787  XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
788  XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
789  XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
790 
791  xcb_flush(conn);
792 }