trandr.c - randr - print current monitor size
(HTM) git clone git://git.z3bra.org/randr.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
trandr.c (2957B)
---
1 /* See LICENSE file for copyright and license details. */
2
3 #include <err.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <xcb/xcb.h>
8 #include <xcb/xcb_cursor.h>
9 #include <xcb/randr.h>
10
11 #include "arg.h"
12
13 struct position {
14 int x, y;
15 };
16
17 enum {
18 MONITOR_CURSOR = 1,
19 MONITOR_WINDOW = 1<<2,
20 };
21
22 static xcb_connection_t *dpy;
23 static xcb_screen_t *scr;
24 static struct position pos = { 0, 0 };
25
26 char *fmt = "%.*s\t%d\t%d\t%d\t%d\t%d\n";
27 char *argv0;
28
29 int
30 cursor_position(xcb_window_t wid)
31 {
32 xcb_query_pointer_cookie_t c;
33 xcb_query_pointer_reply_t *r;
34
35 c = xcb_query_pointer(dpy, wid);
36 r = xcb_query_pointer_reply(dpy, c, NULL);
37 if (!r)
38 return -1;
39
40 pos.x = r->root_x;
41 pos.y = r->root_y;
42 free(r);
43
44 return 0;
45 }
46
47 int
48 window_position(xcb_window_t wid)
49 {
50 xcb_get_geometry_reply_t *r;
51
52 r = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, wid), NULL);
53 if (!r)
54 return -1;
55
56 pos.x = r->x;
57 pos.y = r->y;
58 free(r);
59
60 return 0;
61 }
62
63 int
64 listmonitors(xcb_window_t wid)
65 {
66 int i, n;
67 xcb_randr_output_t *monitors;
68 xcb_randr_get_screen_resources_reply_t *r;
69
70 r = xcb_randr_get_screen_resources_reply(dpy,
71 xcb_randr_get_screen_resources(dpy, wid), NULL);
72 if (!r)
73 return -1;
74
75 n = xcb_randr_get_screen_resources_outputs_length(r);
76 monitors = xcb_randr_get_screen_resources_outputs(r);
77
78 for (i = 0; i < n; i++) {
79 xcb_randr_get_output_info_reply_t *info;
80 info = xcb_randr_get_output_info_reply(dpy,
81 xcb_randr_get_output_info(dpy, monitors[i], XCB_CURRENT_TIME),
82 NULL);
83 if (!info)
84 continue;
85
86 xcb_randr_get_crtc_info_reply_t *crtc;
87 crtc = xcb_randr_get_crtc_info_reply(dpy,
88 xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME),
89 NULL);
90 if (crtc && (pos.x >= crtc->x && pos.x <= crtc->x + crtc->width)) {
91 printf(fmt,
92 xcb_randr_get_output_info_name_length(info),
93 xcb_randr_get_output_info_name(info),
94 crtc->width, crtc->height, crtc->x, crtc->y, crtc->status);
95 }
96
97 free(crtc);
98 free(info);
99 }
100
101 return 0;
102 }
103
104 void
105 usage()
106 {
107 fprintf(stderr, "usage: %s [-w wid]\n", argv0);
108 }
109
110 int
111 main(int argc, char *argv[])
112 {
113 int aflag = 0, wflag = 0;
114 xcb_window_t wid;
115
116 ARGBEGIN {
117 /* print all monitors (not used yet) */
118 case 'a':
119 aflag = 1;
120 break;
121 /* change output format */
122 case 'f':
123 fmt = EARGF(usage());
124 break;
125 /* return monitor for focused window rather than cursor */
126 case 'w':
127 wflag = 1;
128 wid = strtoul(EARGF(usage()), NULL, 16);
129 break;
130 case 'h':
131 usage();
132 return 0;
133 } ARGEND;
134
135 dpy = xcb_connect(NULL, NULL);
136 if (xcb_connection_has_error(dpy))
137 errx(1, "unable connect to the X server");
138
139 scr = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
140 if (!scr)
141 return -1;
142
143 else if (wflag)
144 window_position(wid);
145 else
146 cursor_position(scr->root);
147
148 scr = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
149 if (!scr)
150 errx(1, "unable to retrieve screen informations");
151
152 listmonitors(scr->root);
153
154 xcb_disconnect(dpy);
155 return 0;
156 }