]> git.sur5r.net Git - i3/i3/blob - src/randr.c
refactor some places to use con_descend_focused instead of duplicating code
[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_mode_info_t mode_info;
25 typedef xcb_randr_get_screen_resources_current_reply_t resources_reply;
26
27 /* Pointer to the result of the query for primary output */
28 xcb_randr_get_output_primary_reply_t *primary;
29
30 /* Stores all outputs available in your current session. */
31 struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs);
32
33 static bool randr_disabled = false;
34
35 /*
36  * Get a specific output by its internal X11 id. Used by randr_query_outputs
37  * to check if the output is new (only in the first scan) or if we are
38  * re-scanning.
39  *
40  */
41 static Output *get_output_by_id(xcb_randr_output_t id) {
42     Output *output;
43     TAILQ_FOREACH(output, &outputs, outputs)
44         if (output->id == id)
45             return output;
46
47     return NULL;
48 }
49
50 /*
51  * Returns the output with the given name if it is active (!) or NULL.
52  *
53  */
54 Output *get_output_by_name(const char *name) {
55     Output *output;
56     TAILQ_FOREACH(output, &outputs, outputs)
57         if (output->active &&
58             strcasecmp(output->name, name) == 0)
59             return output;
60
61     return NULL;
62 }
63
64 /*
65  * Returns the first output which is active.
66  *
67  */
68 Output *get_first_output() {
69     Output *output;
70
71     TAILQ_FOREACH(output, &outputs, outputs)
72         if (output->active)
73             return output;
74
75     return NULL;
76 }
77
78 /*
79  * Returns the active (!) output which contains the coordinates x, y or NULL
80  * if there is no output which contains these coordinates.
81  *
82  */
83 Output *get_output_containing(int x, int y) {
84     Output *output;
85     TAILQ_FOREACH(output, &outputs, outputs) {
86         if (!output->active)
87             continue;
88         DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
89                         x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height);
90         if (x >= output->rect.x && x < (output->rect.x + output->rect.width) &&
91             y >= output->rect.y && y < (output->rect.y + output->rect.height))
92             return output;
93     }
94
95     return NULL;
96 }
97
98 /*
99  * Gets the output which is the last one in the given direction, for example
100  * the output on the most bottom when direction == D_DOWN, the output most
101  * right when direction == D_RIGHT and so on.
102  *
103  * This function always returns a output.
104  *
105  */
106 Output *get_output_most(direction_t direction, Output *current) {
107     Output *output, *candidate = NULL;
108     int position = 0;
109     TAILQ_FOREACH(output, &outputs, outputs) {
110         if (!output->active)
111             continue;
112
113         /* Repeated calls of WIN determine the winner of the comparison */
114         #define WIN(variable, condition) \
115             if (variable condition) { \
116                 candidate = output; \
117                 position = variable; \
118             } \
119             break;
120
121         if (((direction == D_UP) || (direction == D_DOWN)) &&
122             (current->rect.x != output->rect.x))
123             continue;
124
125         if (((direction == D_LEFT) || (direction == D_RIGHT)) &&
126             (current->rect.y != output->rect.y))
127             continue;
128
129         switch (direction) {
130             case D_UP:
131                 WIN(output->rect.y, <= position);
132             case D_DOWN:
133                 WIN(output->rect.y, >= position);
134             case D_LEFT:
135                 WIN(output->rect.x, <= position);
136             case D_RIGHT:
137                 WIN(output->rect.x, >= position);
138         }
139     }
140
141     assert(candidate != NULL);
142
143     return candidate;
144 }
145
146 #if 0
147 /*
148  * Initializes the specified output, assigning the specified workspace to it.
149  *
150  */
151 void initialize_output(xcb_connection_t *conn, Output *output, Workspace *workspace) {
152         i3Font *font = load_font(conn, config.font);
153
154         workspace->output = output;
155         output->current_workspace = workspace;
156
157         /* Copy rect for the workspace */
158         memcpy(&(workspace->rect), &(output->rect), sizeof(Rect));
159
160         /* Map clients on the workspace, if any */
161         workspace_map_clients(conn, workspace);
162
163         /* Create a bar window on each output */
164         if (!config.disable_workspace_bar) {
165                 Rect bar_rect = {output->rect.x,
166                                  output->rect.y + output->rect.height - (font->height + 6),
167                                  output->rect.x + output->rect.width,
168                                  font->height + 6};
169                 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
170                 uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
171                 output->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, true, mask, values);
172                 output->bargc = xcb_generate_id(conn);
173                 xcb_create_gc(conn, output->bargc, output->bar, 0, 0);
174         }
175
176         SLIST_INIT(&(output->dock_clients));
177
178         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
179         DLOG("initialized output at (%d, %d) with %d x %d\n",
180                         output->rect.x, output->rect.y, output->rect.width, output->rect.height);
181
182         DLOG("assigning configured workspaces to this output...\n");
183         Workspace *ws;
184         TAILQ_FOREACH(ws, workspaces, workspaces) {
185                 if (ws == workspace)
186                         continue;
187                 if (ws->preferred_output == NULL ||
188                     get_output_by_name(ws->preferred_output) != output)
189                         continue;
190
191                 DLOG("assigning ws %d\n", ws->num + 1);
192                 workspace_assign_to(ws, output, true);
193         }
194 }
195 #endif
196
197 /*
198  * Disables RandR support by creating exactly one output with the size of the
199  * X11 screen.
200  *
201  */
202 void disable_randr(xcb_connection_t *conn) {
203     xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
204
205     DLOG("RandR extension unusable, disabling.\n");
206
207     Output *s = scalloc(sizeof(Output));
208
209     s->active = true;
210     s->rect.x = 0;
211     s->rect.y = 0;
212     s->rect.width = root_screen->width_in_pixels;
213     s->rect.height = root_screen->height_in_pixels;
214     s->name = "xroot-0";
215     output_init_con(s);
216
217     TAILQ_INSERT_TAIL(&outputs, s, outputs);
218
219     randr_disabled = true;
220 }
221
222 /*
223  * Initializes a CT_OUTPUT Con (searches existing ones from inplace restart
224  * before) to use for the given Output.
225  *
226  * XXX: for assignments, we probably need to move workspace creation from here
227  * to after the loop in randr_query_outputs().
228  *
229  */
230 void output_init_con(Output *output) {
231     Con *con = NULL, *current;
232     bool reused = false;
233
234     DLOG("init_con for output %s\n", output->name);
235
236     /* Search for a Con with that name directly below the root node. There
237      * might be one from a restored layout. */
238     TAILQ_FOREACH(current, &(croot->nodes_head), nodes) {
239         if (strcmp(current->name, output->name) != 0)
240             continue;
241
242         con = current;
243         reused = true;
244         DLOG("Using existing con %p / %s\n", con, con->name);
245         break;
246     }
247
248     if (con == NULL) {
249         con = con_new(croot);
250         FREE(con->name);
251         con->name = sstrdup(output->name);
252         con->type = CT_OUTPUT;
253     }
254     con->rect = output->rect;
255     output->con = con;
256
257     char *name;
258     asprintf(&name, "[i3 con] output %s", con->name);
259     x_set_name(con, name);
260     free(name);
261
262     if (reused) {
263         DLOG("Not adding workspace, this was a reused con\n");
264         return;
265     }
266     DLOG("Now adding a workspace\n");
267
268     /* add a workspace to this output */
269     Con *ws = con_new(NULL);
270     ws->type = CT_WORKSPACE;
271
272     /* get the next unused workspace number */
273     DLOG("Getting next unused workspace\n");
274     int c = 0;
275     bool exists = true;
276     while (exists) {
277         Con *out, *current;
278
279         c++;
280
281         FREE(ws->name);
282         asprintf(&(ws->name), "%d", c);
283
284         exists = false;
285         TAILQ_FOREACH(out, &(croot->nodes_head), nodes) {
286             TAILQ_FOREACH(current, &(out->nodes_head), nodes) {
287                 if (strcasecmp(current->name, ws->name) != 0)
288                     continue;
289
290                 exists = true;
291                 break;
292             }
293         }
294
295         DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
296     }
297     ws->num = c;
298     con_attach(ws, con, false);
299
300     asprintf(&name, "[i3 con] workspace %s", ws->name);
301     x_set_name(ws, name);
302     free(name);
303
304     ws->fullscreen_mode = CF_OUTPUT;
305     ws->orientation = HORIZ;
306
307     /* TODO: Set focus in main.c */
308     con_focus(ws);
309 }
310
311 /*
312  * This function needs to be called when changing the mode of an output when
313  * it already has some workspaces (or a bar window) assigned.
314  *
315  * It reconfigures the bar window for the new mode, copies the new rect into
316  * each workspace on this output and forces all windows on the affected
317  * workspaces to be reconfigured.
318  *
319  * It is necessary to call render_layout() afterwards.
320  *
321  */
322 static void output_change_mode(xcb_connection_t *conn, Output *output) {
323     //i3Font *font = load_font(conn, config.font);
324
325     DLOG("Output mode changed, updating rect\n");
326     assert(output->con != NULL);
327     output->con->rect = output->rect;
328 #if 0
329     Rect bar_rect = {output->rect.x,
330                      output->rect.y + output->rect.height - (font->height + 6),
331                      output->rect.x + output->rect.width,
332                      font->height + 6};
333
334     xcb_set_window_rect(conn, output->bar, bar_rect);
335
336         /* go through all workspaces and set force_reconfigure */
337         TAILQ_FOREACH(ws, workspaces, workspaces) {
338                 if (ws->output != output)
339                         continue;
340
341                 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
342                         client->force_reconfigure = true;
343                         if (!client_is_floating(client))
344                                 continue;
345                         /* For floating clients we need to translate the
346                          * coordinates (old workspace to new workspace) */
347                         DLOG("old: (%x, %x)\n", client->rect.x, client->rect.y);
348                         client->rect.x -= ws->rect.x;
349                         client->rect.y -= ws->rect.y;
350                         client->rect.x += ws->output->rect.x;
351                         client->rect.y += ws->output->rect.y;
352                         DLOG("new: (%x, %x)\n", client->rect.x, client->rect.y);
353                 }
354
355                 /* Update dimensions from output */
356                 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
357
358                 /* Update the dimensions of a fullscreen client, if any */
359                 if (ws->fullscreen_client != NULL) {
360                         DLOG("Updating fullscreen client size\n");
361                         client = ws->fullscreen_client;
362                         Rect r = ws->rect;
363                         xcb_set_window_rect(conn, client->frame, r);
364
365                         r.x = 0;
366                         r.y = 0;
367                         xcb_set_window_rect(conn, client->child, r);
368                 }
369         }
370 #endif
371 }
372
373 /*
374  * Gets called by randr_query_outputs() for each output. The function adds new
375  * outputs to the list of outputs, checks if the mode of existing outputs has
376  * been changed or if an existing output has been disabled. It will then change
377  * either the "changed" or the "to_be_deleted" flag of the output, if
378  * appropriate.
379  *
380  */
381 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id,
382                           xcb_randr_get_output_info_reply_t *output,
383                           xcb_timestamp_t cts, resources_reply *res) {
384     /* each CRT controller has a position in which we are interested in */
385     crtc_info *crtc;
386
387     Output *new = get_output_by_id(id);
388     bool existing = (new != NULL);
389     if (!existing)
390         new = scalloc(sizeof(Output));
391     new->id = id;
392     new->primary = (primary && primary->output == id);
393     FREE(new->name);
394     asprintf(&new->name, "%.*s",
395             xcb_randr_get_output_info_name_length(output),
396             xcb_randr_get_output_info_name(output));
397
398     DLOG("found output with name %s\n", new->name);
399
400     /* Even if no CRTC is used at the moment, we store the output so that
401      * we do not need to change the list ever again (we only update the
402      * position/size) */
403     if (output->crtc == XCB_NONE) {
404         if (!existing) {
405             if (new->primary)
406                 TAILQ_INSERT_HEAD(&outputs, new, outputs);
407             else TAILQ_INSERT_TAIL(&outputs, new, outputs);
408         } else if (new->active)
409             new->to_be_disabled = true;
410         return;
411     }
412
413     xcb_randr_get_crtc_info_cookie_t icookie;
414     icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
415     if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
416         DLOG("Skipping output %s: could not get CRTC (%p)\n",
417              new->name, crtc);
418         free(new);
419         return;
420     }
421
422     bool updated = update_if_necessary(&(new->rect.x), crtc->x) |
423                    update_if_necessary(&(new->rect.y), crtc->y) |
424                    update_if_necessary(&(new->rect.width), crtc->width) |
425                    update_if_necessary(&(new->rect.height), crtc->height);
426     free(crtc);
427     new->active = (new->rect.width != 0 && new->rect.height != 0);
428     if (!new->active) {
429         DLOG("width/height 0/0, disabling output\n");
430         return;
431     }
432
433     DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
434                                 new->rect.x, new->rect.y);
435
436     /* If we don’t need to change an existing output or if the output
437      * does not exist in the first place, the case is simple: we either
438      * need to insert the new output or we are done. */
439     if (!updated || !existing) {
440         if (!existing) {
441             if (new->primary)
442                 TAILQ_INSERT_HEAD(&outputs, new, outputs);
443             else TAILQ_INSERT_TAIL(&outputs, new, outputs);
444         }
445         return;
446     }
447
448     new->changed = true;
449 }
450
451 /*
452  * (Re-)queries the outputs via RandR and stores them in the list of outputs.
453  *
454  */
455 void randr_query_outputs() {
456     Output *output, *other, *first;
457     xcb_randr_get_output_primary_cookie_t pcookie;
458     xcb_randr_get_screen_resources_current_cookie_t rcookie;
459     resources_reply *res;
460
461     /* timestamp of the configuration so that we get consistent replies to all
462      * requests (if the configuration changes between our different calls) */
463     xcb_timestamp_t cts;
464
465     /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
466     xcb_randr_output_t *randr_outputs;
467
468     if (randr_disabled)
469         return;
470
471     /* Get screen resources (primary output, crtcs, outputs, modes) */
472     rcookie = xcb_randr_get_screen_resources_current(conn, root);
473     pcookie = xcb_randr_get_output_primary(conn, root);
474
475     if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL)
476         ELOG("Could not get RandR primary output\n");
477     else DLOG("primary output is %08x\n", primary->output);
478     if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
479         disable_randr(conn);
480         return;
481     }
482     cts = res->config_timestamp;
483
484     int len = xcb_randr_get_screen_resources_current_outputs_length(res);
485     randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
486
487     /* Request information for each output */
488     xcb_randr_get_output_info_cookie_t ocookie[len];
489     for (int i = 0; i < len; i++)
490         ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
491
492     /* Loop through all outputs available for this X11 screen */
493     for (int i = 0; i < len; i++) {
494         xcb_randr_get_output_info_reply_t *output;
495
496         if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
497             continue;
498
499         handle_output(conn, randr_outputs[i], output, cts, res);
500         free(output);
501     }
502
503     /* Check for clones, disable the clones and reduce the mode to the
504      * lowest common mode */
505     TAILQ_FOREACH(output, &outputs, outputs) {
506         if (!output->active || output->to_be_disabled)
507             continue;
508         DLOG("output %p / %s, position (%d, %d), checking for clones\n",
509                 output, output->name, output->rect.x, output->rect.y);
510
511         for (other = output;
512              other != TAILQ_END(&outputs);
513              other = TAILQ_NEXT(other, outputs)) {
514             if (other == output || !other->active || other->to_be_disabled)
515                 continue;
516
517             if (other->rect.x != output->rect.x ||
518                 other->rect.y != output->rect.y)
519                 continue;
520
521             DLOG("output %p has the same position, his mode = %d x %d\n",
522                             other, other->rect.width, other->rect.height);
523             uint32_t width = min(other->rect.width, output->rect.width);
524             uint32_t height = min(other->rect.height, output->rect.height);
525
526             if (update_if_necessary(&(output->rect.width), width) |
527                 update_if_necessary(&(output->rect.height), height))
528                 output->changed = true;
529
530             update_if_necessary(&(other->rect.width), width);
531             update_if_necessary(&(other->rect.height), height);
532
533             DLOG("disabling output %p (%s)\n", other, other->name);
534             other->to_be_disabled = true;
535
536             DLOG("new output mode %d x %d, other mode %d x %d\n",
537                             output->rect.width, output->rect.height,
538                             other->rect.width, other->rect.height);
539         }
540     }
541
542     /* Handle outputs which have a new mode or are disabled now (either
543      * because the user disabled them or because they are clones) */
544     TAILQ_FOREACH(output, &outputs, outputs) {
545         if (output->to_be_disabled) {
546             output->active = false;
547             DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name);
548
549             if ((first = get_first_output()) == NULL)
550                     die("No usable outputs available\n");
551
552             if (output->con != NULL) {
553                 /* We need to move the workspaces from the disappearing output to the first output */
554                 /* 1: Get the con to focus next, if the disappearing ws is focused */
555                 Con *next = NULL;
556                 if (TAILQ_FIRST(&(croot->focus_head)) == output->con) {
557                     DLOG("This output (%p) was focused! Getting next\n", output->con);
558                     next = con_next_focused(output->con);
559                     DLOG("next = %p\n", next);
560                 }
561
562                 /* 2: iterate through workspaces and re-assign them */
563                 Con *current;
564                 while (!TAILQ_EMPTY(&(output->con->nodes_head))) {
565                     current = TAILQ_FIRST(&(output->con->nodes_head));
566                     DLOG("Detaching current = %p / %s\n", current, current->name);
567                     con_detach(current);
568                     DLOG("Re-attaching current = %p / %s\n", current, current->name);
569                     con_attach(current, first->con, false);
570                     DLOG("Done, next\n");
571                 }
572                 DLOG("re-attached all workspaces\n");
573
574                 if (next) {
575                     DLOG("now focusing next = %p\n", next);
576                     con_focus(next);
577                 }
578
579                 DLOG("destroying disappearing con %p\n", output->con);
580                 tree_close(output->con, false, true);
581                 DLOG("Done. Should be fine now\n");
582                 output->con = NULL;
583             }
584
585             output->to_be_disabled = false;
586             output->changed = false;
587         }
588
589         if (output->active && output->con == NULL) {
590             DLOG("Need to initialize a Con for output %s\n", output->name);
591             output_init_con(output);
592             output->changed = false;
593         }
594
595         if (output->changed) {
596             output_change_mode(conn, output);
597             output->changed = false;
598         }
599     }
600
601     if (TAILQ_EMPTY(&outputs)) {
602         ELOG("No outputs found via RandR, disabling\n");
603         disable_randr(conn);
604     }
605
606     //ewmh_update_workarea();
607
608 #if 0
609     /* Just go through each active output and associate one workspace */
610     TAILQ_FOREACH(output, &outputs, outputs) {
611             if (!output->active || output->current_workspace != NULL)
612                     continue;
613             ws = get_first_workspace_for_output(output);
614             initialize_output(conn, output, ws);
615     }
616 #endif
617
618     /* Focus the primary screen, if possible */
619     TAILQ_FOREACH(output, &outputs, outputs) {
620         if (!output->primary || !output->con)
621             continue;
622
623         DLOG("Focusing primary output %s\n", output->name);
624         con_focus(con_descend_focused(output->con));
625     }
626
627     /* render_layout flushes */
628     tree_render();
629
630     FREE(res);
631     FREE(primary);
632 }
633
634 /*
635  * We have just established a connection to the X server and need the initial
636  * XRandR information to setup workspaces for each screen.
637  *
638  */
639 void randr_init(int *event_base) {
640     const xcb_query_extension_reply_t *extreply;
641
642     extreply = xcb_get_extension_data(conn, &xcb_randr_id);
643     if (!extreply->present)
644         disable_randr(conn);
645     else randr_query_outputs();
646
647     if (event_base != NULL)
648         *event_base = extreply->first_event;
649
650     xcb_randr_select_input(conn, root,
651             XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
652             XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
653             XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
654             XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
655
656     xcb_flush(conn);
657 }