2 * vim:ts=4:sw=4:expandtab
4 * © 2010 Michael Stapelberg
6 * See LICENSE for licensing information
14 #include <xcb/xinerama.h>
15 #include <xcb/randr.h>
21 /* Number of Xinerama screens which are currently present. */
24 /* The resolutions of the currently present Xinerama screens. */
25 Rect *xr_resolutions = NULL;
27 static bool xinerama_active;
28 static bool has_randr = false;
29 static bool has_randr_1_5 = false;
30 extern bool debug_mode;
32 void _xinerama_init(void);
34 void randr_init(int *event_base, xcb_window_t root) {
35 const xcb_query_extension_reply_t *extreply;
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");
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);
49 DEBUG("Could not query RandR version: X11 error code %d\n", err->error_code);
56 has_randr_1_5 = (randr_version->major_version >= 1) &&
57 (randr_version->minor_version >= 5);
61 if (event_base != NULL)
62 *event_base = extreply->first_event;
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);
73 void _xinerama_init(void) {
74 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
75 DEBUG("Xinerama extension not found, disabling.\n");
79 xcb_xinerama_is_active_cookie_t cookie;
80 xcb_xinerama_is_active_reply_t *reply;
82 cookie = xcb_xinerama_is_active(conn);
83 reply = xcb_xinerama_is_active_reply(conn, cookie, NULL);
92 xinerama_active = true;
97 * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
100 static bool _randr_query_monitors_15(xcb_window_t root) {
101 #if XCB_RANDR_MINOR_VERSION < 5
104 /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
105 if (!has_randr_1_5) {
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);
115 DEBUG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
117 /* Fall back to RandR ≤ 1.4 */
121 int screens = xcb_randr_get_monitors_monitors_length(monitors);
122 DEBUG("%d RandR monitors found (timestamp %d)\n",
123 screens, monitors->timestamp);
125 Rect *resolutions = malloc(screens * sizeof(Rect));
126 /* No memory? Just keep on using the old information. */
132 xcb_randr_monitor_info_iterator_t iter;
134 for (iter = xcb_randr_get_monitors_monitors_iterator(monitors), screen = 0;
136 xcb_randr_monitor_info_next(&iter), screen++) {
137 const xcb_randr_monitor_info_t *monitor_info = iter.data;
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);
147 free(xr_resolutions);
148 xr_resolutions = resolutions;
149 xr_screens = screens;
157 * randr_query_outputs_14 uses RandR ≤ 1.4 to update outputs.
160 static bool _randr_query_outputs_14(xcb_window_t root) {
164 DEBUG("Querying outputs using RandR ≤ 1.4\n");
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);
170 xcb_randr_get_screen_resources_current_reply_t *res =
171 xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL);
173 DEBUG("Could not query screen resources.\n");
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;
181 const int len = xcb_randr_get_screen_resources_current_outputs_length(res);
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);
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);
191 Rect *resolutions = malloc(len * sizeof(Rect));
192 /* No memory? Just keep on using the old information. */
198 /* Loop through all outputs available for this X11 screen */
201 for (int i = 0; i < len; i++) {
202 xcb_randr_get_output_info_reply_t *output;
204 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL) {
208 if (output->crtc == XCB_NONE) {
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);
222 resolutions[screen].x = crtc->x;
223 resolutions[screen].y = crtc->y;
224 resolutions[screen].width = crtc->width;
225 resolutions[screen].height = crtc->height;
227 DEBUG("found RandR output: %d x %d at %d x %d\n",
228 crtc->width, crtc->height,
237 free(xr_resolutions);
238 xr_resolutions = resolutions;
244 void _xinerama_query_screens(void) {
245 if (!xinerama_active) {
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);
256 DEBUG("Couldn't get Xinerama screens: X11 error code %d\n", err->error_code);
260 screen_info = xcb_xinerama_query_screens_screen_info(reply);
261 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
263 Rect *resolutions = malloc(screens * sizeof(Rect));
264 /* No memory? Just keep on using the old information. */
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);
280 free(xr_resolutions);
281 xr_resolutions = resolutions;
282 xr_screens = screens;
287 void randr_query(xcb_window_t root) {
288 if (_randr_query_monitors_15(root)) {
292 if (_randr_query_outputs_14(root)) {
296 _xinerama_query_screens();