]> git.sur5r.net Git - i3/i3/blob - src/randr.c
Check if output is disabled in handle_output()
[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     if (output->connection == XCB_RANDR_CONNECTION_DISCONNECTED) {
559         DLOG("Disabling output %s: it is disconnected\n", new->name);
560         new->to_be_disabled = true;
561         return;
562     }
563
564     bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
565                    update_if_necessary(&(new->rect.y), crtc->y) |
566                    update_if_necessary(&(new->rect.width), crtc->width) |
567                    update_if_necessary(&(new->rect.height), crtc->height);
568     free(crtc);
569     new->active = (new->rect.width != 0 && new->rect.height != 0);
570     if (!new->active) {
571         DLOG("width/height 0/0, disabling output\n");
572         return;
573     }
574
575     DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
576          new->rect.x, new->rect.y);
577
578     /* If we don’t need to change an existing output or if the output
579      * does not exist in the first place, the case is simple: we either
580      * need to insert the new output or we are done. */
581     if (!updated || !existing) {
582         if (!existing) {
583             if (new->primary)
584                 TAILQ_INSERT_HEAD(&outputs, new, outputs);
585             else
586                 TAILQ_INSERT_TAIL(&outputs, new, outputs);
587         }
588         return;
589     }
590
591     new->changed = true;
592 }
593
594 /*
595  * (Re-)queries the outputs via RandR and stores them in the list of outputs.
596  *
597  */
598 void randr_query_outputs(void) {
599     Output *output, *other, *first;
600     xcb_randr_get_output_primary_cookie_t pcookie;
601     xcb_randr_get_screen_resources_current_cookie_t rcookie;
602     resources_reply *res;
603
604     /* timestamp of the configuration so that we get consistent replies to all
605      * requests (if the configuration changes between our different calls) */
606     xcb_timestamp_t cts;
607
608     /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
609     xcb_randr_output_t *randr_outputs;
610
611     if (randr_disabled)
612         return;
613
614     /* Get screen resources (primary output, crtcs, outputs, modes) */
615     rcookie = xcb_randr_get_screen_resources_current(conn, root);
616     pcookie = xcb_randr_get_output_primary(conn, root);
617
618     if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
619         ELOG("Could not get RandR primary output\n");
620     else
621         DLOG("primary output is %08x\n", primary->output);
622     if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
623         disable_randr(conn);
624         return;
625     }
626     cts = res->config_timestamp;
627
628     int len = xcb_randr_get_screen_resources_current_outputs_length(res);
629     randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
630
631     /* Request information for each output */
632     xcb_randr_get_output_info_cookie_t ocookie[len];
633     for (int i = 0; i < len; i++)
634         ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
635
636     /* Loop through all outputs available for this X11 screen */
637     for (int i = 0; i < len; i++) {
638         xcb_randr_get_output_info_reply_t *output;
639
640         if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
641             continue;
642
643         handle_output(conn, randr_outputs[i], output, cts, res);
644         free(output);
645     }
646
647     /* Check for clones, disable the clones and reduce the mode to the
648      * lowest common mode */
649     TAILQ_FOREACH(output, &outputs, outputs) {
650         if (!output->active || output->to_be_disabled)
651             continue;
652         DLOG("output %p / %s, position (%d, %d), checking for clones\n",
653              output, output->name, output->rect.x, output->rect.y);
654
655         for (other = output;
656              other != TAILQ_END(&outputs);
657              other = TAILQ_NEXT(other, outputs)) {
658             if (other == output || !other->active || other->to_be_disabled)
659                 continue;
660
661             if (other->rect.x != output->rect.x ||
662                 other->rect.y != output->rect.y)
663                 continue;
664
665             DLOG("output %p has the same position, his mode = %d x %d\n",
666                  other, other->rect.width, other->rect.height);
667             uint32_t width = min(other->rect.width, output->rect.width);
668             uint32_t height = min(other->rect.height, output->rect.height);
669
670             if (update_if_necessary(&(output->rect.width), width) |
671                 update_if_necessary(&(output->rect.height), height))
672                 output->changed = true;
673
674             update_if_necessary(&(other->rect.width), width);
675             update_if_necessary(&(other->rect.height), height);
676
677             DLOG("disabling output %p (%s)\n", other, other->name);
678             other->to_be_disabled = true;
679
680             DLOG("new output mode %d x %d, other mode %d x %d\n",
681                  output->rect.width, output->rect.height,
682                  other->rect.width, other->rect.height);
683         }
684     }
685
686     /* Ensure that all outputs which are active also have a con. This is
687      * necessary because in the next step, a clone might get disabled. Example:
688      * LVDS1 active, VGA1 gets activated as a clone of LVDS1 (has no con).
689      * LVDS1 gets disabled. */
690     TAILQ_FOREACH(output, &outputs, outputs) {
691         if (output->active && output->con == NULL) {
692             DLOG("Need to initialize a Con for output %s\n", output->name);
693             output_init_con(output);
694             output->changed = false;
695         }
696     }
697
698     /* Handle outputs which have a new mode or are disabled now (either
699      * because the user disabled them or because they are clones) */
700     TAILQ_FOREACH(output, &outputs, outputs) {
701         if (output->to_be_disabled) {
702             output->active = false;
703             DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name);
704
705             first = get_first_output();
706
707             /* TODO: refactor the following code into a nice function. maybe
708              * use an on_destroy callback which is implement differently for
709              * different container types (CT_CONTENT vs. CT_DOCKAREA)? */
710             Con *first_content = output_get_content(first->con);
711
712             if (output->con != NULL) {
713                 /* We need to move the workspaces from the disappearing output to the first output */
714                 /* 1: Get the con to focus next, if the disappearing ws is focused */
715                 Con *next = NULL;
716                 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
717                     DLOG("This output (%p) was focused! Getting next\n", output->con);
718                     next = focused;
719                     DLOG("next = %p\n", next);
720                 }
721
722                 /* 2: iterate through workspaces and re-assign them, fixing the coordinates
723                  * of floating containers as we go */
724                 Con *current;
725                 Con *old_content = output_get_content(output->con);
726                 while (!TAILQ_EMPTY(&(old_content->nodes_head))) {
727                     current = TAILQ_FIRST(&(old_content->nodes_head));
728                     if (current != next && TAILQ_EMPTY(&(current->focus_head))) {
729                         /* the workspace is empty and not focused, get rid of it */
730                         DLOG("Getting rid of current = %p / %s (empty, unfocused)\n", current, current->name);
731                         tree_close(current, DONT_KILL_WINDOW, false, false);
732                         continue;
733                     }
734                     DLOG("Detaching current = %p / %s\n", current, current->name);
735                     con_detach(current);
736                     DLOG("Re-attaching current = %p / %s\n", current, current->name);
737                     con_attach(current, first_content, false);
738                     DLOG("Fixing the coordinates of floating containers\n");
739                     Con *floating_con;
740                     TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows)
741                     floating_fix_coordinates(floating_con, &(output->con->rect), &(first->con->rect));
742                     DLOG("Done, next\n");
743                 }
744                 DLOG("re-attached all workspaces\n");
745
746                 if (next) {
747                     DLOG("now focusing next = %p\n", next);
748                     con_focus(next);
749                     workspace_show(con_get_workspace(next));
750                 }
751
752                 /* 3: move the dock clients to the first output */
753                 Con *child;
754                 TAILQ_FOREACH(child, &(output->con->nodes_head), nodes) {
755                     if (child->type != CT_DOCKAREA)
756                         continue;
757                     DLOG("Handling dock con %p\n", child);
758                     Con *dock;
759                     while (!TAILQ_EMPTY(&(child->nodes_head))) {
760                         dock = TAILQ_FIRST(&(child->nodes_head));
761                         Con *nc;
762                         Match *match;
763                         nc = con_for_window(first->con, dock->window, &match);
764                         DLOG("Moving dock client %p to nc %p\n", dock, nc);
765                         con_detach(dock);
766                         DLOG("Re-attaching\n");
767                         con_attach(dock, nc, false);
768                         DLOG("Done\n");
769                     }
770                 }
771
772                 DLOG("destroying disappearing con %p\n", output->con);
773                 tree_close(output->con, DONT_KILL_WINDOW, true, false);
774                 DLOG("Done. Should be fine now\n");
775                 output->con = NULL;
776             }
777
778             output->to_be_disabled = false;
779             output->changed = false;
780         }
781
782         if (output->changed) {
783             output_change_mode(conn, output);
784             output->changed = false;
785         }
786     }
787
788     if (TAILQ_EMPTY(&outputs)) {
789         ELOG("No outputs found via RandR, disabling\n");
790         disable_randr(conn);
791     }
792
793     /* Verifies that there is at least one active output as a side-effect. */
794     get_first_output();
795
796     /* Just go through each active output and assign one workspace */
797     TAILQ_FOREACH(output, &outputs, outputs) {
798         if (!output->active)
799             continue;
800         Con *content = output_get_content(output->con);
801         if (!TAILQ_EMPTY(&(content->nodes_head)))
802             continue;
803         DLOG("Should add ws for output %s\n", output->name);
804         init_ws_for_output(output, content);
805     }
806
807     /* Focus the primary screen, if possible */
808     TAILQ_FOREACH(output, &outputs, outputs) {
809         if (!output->primary || !output->con)
810             continue;
811
812         DLOG("Focusing primary output %s\n", output->name);
813         con_focus(con_descend_focused(output->con));
814     }
815
816     /* render_layout flushes */
817     tree_render();
818
819     FREE(res);
820     FREE(primary);
821 }
822
823 /*
824  * We have just established a connection to the X server and need the initial
825  * XRandR information to setup workspaces for each screen.
826  *
827  */
828 void randr_init(int *event_base) {
829     const xcb_query_extension_reply_t *extreply;
830
831     extreply = xcb_get_extension_data(conn, &xcb_randr_id);
832     if (!extreply->present) {
833         disable_randr(conn);
834         return;
835     } else
836         randr_query_outputs();
837
838     if (event_base != NULL)
839         *event_base = extreply->first_event;
840
841     xcb_randr_select_input(conn, root,
842                            XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
843                                XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
844                                XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
845                                XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
846
847     xcb_flush(conn);
848 }