]> git.sur5r.net Git - i3/i3lock/blob - xinerama.c
i3lock: Stop leaking the image_path dup. (#93)
[i3/i3lock] / xinerama.c
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
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     free(reply);
50 }
51
52 void xinerama_query_screens(void) {
53     if (!xinerama_active)
54         return;
55
56     xcb_xinerama_query_screens_cookie_t cookie;
57     xcb_xinerama_query_screens_reply_t *reply;
58     xcb_xinerama_screen_info_t *screen_info;
59
60     cookie = xcb_xinerama_query_screens_unchecked(conn);
61     reply = xcb_xinerama_query_screens_reply(conn, cookie, NULL);
62     if (!reply) {
63         if (debug_mode)
64             fprintf(stderr, "Couldn't get Xinerama screens\n");
65         return;
66     }
67     screen_info = xcb_xinerama_query_screens_screen_info(reply);
68     int screens = xcb_xinerama_query_screens_screen_info_length(reply);
69
70     Rect *resolutions = malloc(screens * sizeof(Rect));
71     /* No memory? Just keep on using the old information. */
72     if (!resolutions) {
73         free(reply);
74         return;
75     }
76     xr_resolutions = resolutions;
77     xr_screens = screens;
78
79     for (int screen = 0; screen < xr_screens; screen++) {
80         xr_resolutions[screen].x = screen_info[screen].x_org;
81         xr_resolutions[screen].y = screen_info[screen].y_org;
82         xr_resolutions[screen].width = screen_info[screen].width;
83         xr_resolutions[screen].height = screen_info[screen].height;
84         DEBUG("found Xinerama screen: %d x %d at %d x %d\n",
85               screen_info[screen].width, screen_info[screen].height,
86               screen_info[screen].x_org, screen_info[screen].y_org);
87     }
88
89     free(reply);
90 }