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