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