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