]> git.sur5r.net Git - i3/i3lock/blob - xinerama.c
Implement Xinerama support (not used yet)
[i3/i3lock] / xinerama.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * © 2010-2012 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
16 #include "xcb.h"
17 #include "xinerama.h"
18
19 /* Number of Xinerama screens which are currently present. */
20 int xr_screens = 0;
21
22 /* The resolutions of the currently present Xinerama screens. */
23 Rect *xr_resolutions;
24
25 static bool xinerama_active;
26
27 void xinerama_init() {
28     if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
29         printf("Xinerama extension not found, disabling.\n");
30         return;
31     }
32
33     xcb_xinerama_is_active_cookie_t cookie;
34     xcb_xinerama_is_active_reply_t *reply;
35
36     cookie = xcb_xinerama_is_active(conn);
37     reply = xcb_xinerama_is_active_reply(conn, cookie, NULL);
38     if (!reply)
39         return;
40
41     if (!reply->state) {
42         free(reply);
43         return;
44     }
45
46     xinerama_active = true;
47 }
48
49 void xinerama_query_screens() {
50     if (!xinerama_active)
51         return;
52
53     xcb_xinerama_query_screens_cookie_t cookie;
54     xcb_xinerama_query_screens_reply_t *reply;
55     xcb_xinerama_screen_info_t *screen_info;
56
57     cookie = xcb_xinerama_query_screens_unchecked(conn);
58     reply = xcb_xinerama_query_screens_reply(conn, cookie, NULL);
59     if (!reply) {
60         fprintf(stderr, "Couldn't get Xinerama screens\n");
61         return;
62     }
63     screen_info = xcb_xinerama_query_screens_screen_info(reply);
64     int screens = xcb_xinerama_query_screens_screen_info_length(reply);
65
66     Rect *resolutions = malloc(screens * sizeof(Rect));
67     /* No memory? Just keep on using the old information. */
68     if (!resolutions) {
69         free(reply);
70         return;
71     }
72     xr_resolutions = resolutions;
73     xr_screens = screens;
74
75     for (int screen = 0; screen < xr_screens; screen++) {
76         xr_resolutions[screen].x = screen_info[screen].x_org;
77         xr_resolutions[screen].y = screen_info[screen].y_org;
78         xr_resolutions[screen].width = screen_info[screen].width;
79         xr_resolutions[screen].height = screen_info[screen].height;
80         printf("found Xinerama screen: %d x %d at %d x %d\n",
81                         screen_info[screen].width, screen_info[screen].height,
82                         screen_info[screen].x_org, screen_info[screen].y_org);
83     }
84
85     free(reply);
86 }