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