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