summaryrefslogtreecommitdiff
path: root/randr.c
diff options
context:
space:
mode:
Diffstat (limited to 'randr.c')
-rw-r--r--randr.c297
1 files changed, 297 insertions, 0 deletions
diff --git a/randr.c b/randr.c
new file mode 100644
index 0000000..4b1ea97
--- /dev/null
+++ b/randr.c
@@ -0,0 +1,297 @@
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * © 2010 Michael Stapelberg
5 *
6 * See LICENSE for licensing information
7 *
8 */
9#include <stdbool.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <math.h>
13#include <xcb/xcb.h>
14#include <xcb/xinerama.h>
15#include <xcb/randr.h>
16
17#include "i3lock.h"
18#include "xcb.h"
19#include "randr.h"
20
21/* Number of Xinerama screens which are currently present. */
22int xr_screens = 0;
23
24/* The resolutions of the currently present Xinerama screens. */
25Rect *xr_resolutions = NULL;
26
27static bool xinerama_active;
28static bool has_randr = false;
29static bool has_randr_1_5 = false;
30extern bool debug_mode;
31
32void _xinerama_init(void);
33
34void randr_init(int *event_base, xcb_window_t root) {
35 const xcb_query_extension_reply_t *extreply;
36
37 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
38 if (!extreply->present) {
39 DEBUG("RandR is not present, falling back to Xinerama.\n");
40 _xinerama_init();
41 return;
42 }
43
44 xcb_generic_error_t *err;
45 xcb_randr_query_version_reply_t *randr_version =
46 xcb_randr_query_version_reply(
47 conn, xcb_randr_query_version(conn, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION), &err);
48 if (err != NULL) {
49 DEBUG("Could not query RandR version: X11 error code %d\n", err->error_code);
50 _xinerama_init();
51 return;
52 }
53
54 has_randr = true;
55
56 has_randr_1_5 = (randr_version->major_version >= 1) &&
57 (randr_version->minor_version >= 5);
58
59 free(randr_version);
60
61 if (event_base != NULL)
62 *event_base = extreply->first_event;
63
64 xcb_randr_select_input(conn, root,
65 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
66 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
67 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
68 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
69
70 xcb_flush(conn);
71}
72
73void _xinerama_init(void) {
74 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
75 DEBUG("Xinerama extension not found, disabling.\n");
76 return;
77 }
78
79 xcb_xinerama_is_active_cookie_t cookie;
80 xcb_xinerama_is_active_reply_t *reply;
81
82 cookie = xcb_xinerama_is_active(conn);
83 reply = xcb_xinerama_is_active_reply(conn, cookie, NULL);
84 if (!reply)
85 return;
86
87 if (!reply->state) {
88 free(reply);
89 return;
90 }
91
92 xinerama_active = true;
93 free(reply);
94}
95
96/*
97 * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
98 *
99 */
100static bool _randr_query_monitors_15(xcb_window_t root) {
101#if XCB_RANDR_MINOR_VERSION < 5
102 return false;
103#else
104 /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
105 if (!has_randr_1_5) {
106 return false;
107 }
108 /* RandR 1.5 available at run-time (supported by the server) */
109 DEBUG("Querying monitors using RandR 1.5\n");
110 xcb_generic_error_t *err;
111 xcb_randr_get_monitors_reply_t *monitors =
112 xcb_randr_get_monitors_reply(
113 conn, xcb_randr_get_monitors(conn, root, true), &err);
114 if (err != NULL) {
115 DEBUG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
116 free(err);
117 /* Fall back to RandR ≤ 1.4 */
118 return false;
119 }
120
121 int screens = xcb_randr_get_monitors_monitors_length(monitors);
122 DEBUG("%d RandR monitors found (timestamp %d)\n",
123 screens, monitors->timestamp);
124
125 Rect *resolutions = malloc(screens * sizeof(Rect));
126 /* No memory? Just keep on using the old information. */
127 if (!resolutions) {
128 free(monitors);
129 return true;
130 }
131
132 xcb_randr_monitor_info_iterator_t iter;
133 int screen;
134 for (iter = xcb_randr_get_monitors_monitors_iterator(monitors), screen = 0;
135 iter.rem;
136 xcb_randr_monitor_info_next(&iter), screen++) {
137 const xcb_randr_monitor_info_t *monitor_info = iter.data;
138
139 resolutions[screen].x = monitor_info->x;
140 resolutions[screen].y = monitor_info->y;
141 resolutions[screen].width = monitor_info->width;
142 resolutions[screen].height = monitor_info->height;
143 DEBUG("found RandR monitor: %d x %d at %d x %d\n",
144 monitor_info->width, monitor_info->height,
145 monitor_info->x, monitor_info->y);
146 }
147 free(xr_resolutions);
148 xr_resolutions = resolutions;
149 xr_screens = screens;
150
151 free(monitors);
152 return true;
153#endif
154}
155
156/*
157 * randr_query_outputs_14 uses RandR ≤ 1.4 to update outputs.
158 *
159 */
160static bool _randr_query_outputs_14(xcb_window_t root) {
161 if (!has_randr) {
162 return false;
163 }
164 DEBUG("Querying outputs using RandR ≤ 1.4\n");
165
166 /* Get screen resources (primary output, crtcs, outputs, modes) */
167 xcb_randr_get_screen_resources_current_cookie_t rcookie;
168 rcookie = xcb_randr_get_screen_resources_current(conn, root);
169
170 xcb_randr_get_screen_resources_current_reply_t *res =
171 xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL);
172 if (res == NULL) {
173 DEBUG("Could not query screen resources.\n");
174 return false;
175 }
176
177 /* timestamp of the configuration so that we get consistent replies to all
178 * requests (if the configuration changes between our different calls) */
179 const xcb_timestamp_t cts = res->config_timestamp;
180
181 const int len = xcb_randr_get_screen_resources_current_outputs_length(res);
182
183 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */
184 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
185
186 /* Request information for each output */
187 xcb_randr_get_output_info_cookie_t ocookie[len];
188 for (int i = 0; i < len; i++) {
189 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
190 }
191 Rect *resolutions = malloc(len * sizeof(Rect));
192 /* No memory? Just keep on using the old information. */
193 if (!resolutions) {
194 free(res);
195 return true;
196 }
197
198 /* Loop through all outputs available for this X11 screen */
199 int screen = 0;
200
201 for (int i = 0; i < len; i++) {
202 xcb_randr_get_output_info_reply_t *output;
203
204 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL) {
205 continue;
206 }
207
208 if (output->crtc == XCB_NONE) {
209 free(output);
210 continue;
211 }
212
213 xcb_randr_get_crtc_info_cookie_t icookie;
214 xcb_randr_get_crtc_info_reply_t *crtc;
215 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
216 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
217 DEBUG("Skipping output: could not get CRTC (0x%08x)\n", output->crtc);
218 free(output);
219 continue;
220 }
221
222 resolutions[screen].x = crtc->x;
223 resolutions[screen].y = crtc->y;
224 resolutions[screen].width = crtc->width;
225 resolutions[screen].height = crtc->height;
226
227 DEBUG("found RandR output: %d x %d at %d x %d\n",
228 crtc->width, crtc->height,
229 crtc->x, crtc->y);
230
231 screen++;
232
233 free(crtc);
234
235 free(output);
236 }
237 free(xr_resolutions);
238 xr_resolutions = resolutions;
239 xr_screens = screen;
240 free(res);
241 return true;
242}
243
244void _xinerama_query_screens(void) {
245 if (!xinerama_active) {
246 return;
247 }
248
249 xcb_xinerama_query_screens_cookie_t cookie;
250 xcb_xinerama_query_screens_reply_t *reply;
251 xcb_xinerama_screen_info_t *screen_info;
252 xcb_generic_error_t *err;
253 cookie = xcb_xinerama_query_screens_unchecked(conn);
254 reply = xcb_xinerama_query_screens_reply(conn, cookie, &err);
255 if (!reply) {
256 DEBUG("Couldn't get Xinerama screens: X11 error code %d\n", err->error_code);
257 free(err);
258 return;
259 }
260 screen_info = xcb_xinerama_query_screens_screen_info(reply);
261 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
262
263 Rect *resolutions = malloc(screens * sizeof(Rect));
264 /* No memory? Just keep on using the old information. */
265 if (!resolutions) {
266 free(reply);
267 return;
268 }
269
270 for (int screen = 0; screen < xr_screens; screen++) {
271 resolutions[screen].x = screen_info[screen].x_org;
272 resolutions[screen].y = screen_info[screen].y_org;
273 resolutions[screen].width = screen_info[screen].width;
274 resolutions[screen].height = screen_info[screen].height;
275 DEBUG("found Xinerama screen: %d x %d at %d x %d\n",
276 screen_info[screen].width, screen_info[screen].height,
277 screen_info[screen].x_org, screen_info[screen].y_org);
278 }
279
280 free(xr_resolutions);
281 xr_resolutions = resolutions;
282 xr_screens = screens;
283
284 free(reply);
285}
286
287void randr_query(xcb_window_t root) {
288 if (_randr_query_monitors_15(root)) {
289 return;
290 }
291
292 if (_randr_query_outputs_14(root)) {
293 return;
294 }
295
296 _xinerama_query_screens();
297}