-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathxwrap.rs
327 lines (286 loc) · 8.58 KB
/
xwrap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::ffi;
use std::mem;
use std::os::raw;
use std::ptr;
use std::slice;
use image::Pixel;
use image::Rgba;
use image::RgbaImage;
use libc;
use x11::xlib;
use x11::xrandr;
use crate::util;
pub const ALL_PLANES: libc::c_ulong = !0;
pub struct Display {
handle: *mut xlib::Display,
}
pub struct Image {
handle: *mut xlib::XImage,
}
pub struct ScreenRectIter<'a> {
dpy: &'a Display,
res: *mut xrandr::XRRScreenResources,
crtcs: &'a [xrandr::RRCrtc],
i: usize,
}
impl Display {
pub fn open(name: Option<ffi::CString>) -> Option<Display> {
unsafe {
let name = match name {
None => ptr::null(),
Some(cstr) => cstr.as_ptr(),
};
let d = xlib::XOpenDisplay(name);
if d.is_null() {
return None;
}
Some(Display { handle: d })
}
}
pub fn get_default_root(&self) -> xlib::Window {
unsafe { xlib::XDefaultRootWindow(self.handle) }
}
pub fn get_window_rect(&self, window: xlib::Window) -> util::Rect {
unsafe {
let mut attrs = mem::MaybeUninit::uninit();
xlib::XGetWindowAttributes(self.handle, window, attrs.as_mut_ptr());
let attrs = attrs.assume_init();
let mut root = 0;
let mut parent = 0;
let mut children: *mut xlib::Window = ptr::null_mut();
let mut nchildren = 0;
xlib::XQueryTree(
self.handle,
window,
&mut root,
&mut parent,
&mut children,
&mut nchildren,
);
if !children.is_null() {
xlib::XFree(children as *mut raw::c_void);
}
let mut x = attrs.x;
let mut y = attrs.y;
if parent != 0 {
let mut child = 0;
xlib::XTranslateCoordinates(
self.handle,
parent,
root,
attrs.x,
attrs.y,
&mut x,
&mut y,
&mut child,
);
}
util::Rect {
x: x,
y: y,
w: attrs.width,
h: attrs.height,
}
}
}
pub fn get_image(
&self,
window: xlib::Window,
rect: util::Rect,
plane_mask: libc::c_ulong,
format: libc::c_int,
) -> Option<Image> {
unsafe {
let image = xlib::XGetImage(
self.handle,
window,
rect.x,
rect.y,
rect.w as libc::c_uint,
rect.h as libc::c_uint,
plane_mask,
format,
);
if image.is_null() {
return None;
}
Some(Image::from_raw_ximage(image))
}
}
pub fn get_screen_rects(&self, root: xlib::Window) -> Option<ScreenRectIter<'_>> {
unsafe {
let xrr_res = xrandr::XRRGetScreenResourcesCurrent(self.handle, root);
if xrr_res.is_null() {
return None;
}
Some(ScreenRectIter {
dpy: &self,
res: xrr_res,
crtcs: slice::from_raw_parts((*xrr_res).crtcs, (*xrr_res).ncrtc as usize),
i: 0,
})
}
}
pub fn get_cursor_position(&self, window: xlib::Window) -> Option<util::Point> {
let mut x = 0;
let mut y = 0;
unsafe {
if xlib::XQueryPointer(
self.handle,
window,
&mut 0,
&mut 0,
&mut x,
&mut y,
&mut 0,
&mut 0,
&mut 0,
) == 0
{
return None;
}
}
Some(util::Point { x, y })
}
}
impl Drop for Display {
fn drop(&mut self) {
unsafe {
xlib::XCloseDisplay(self.handle);
}
}
}
impl Image {
pub fn from_raw_ximage(ximage: *mut xlib::XImage) -> Image {
Image { handle: ximage }
}
pub fn into_image_buffer(&self) -> Option<RgbaImage> {
unsafe {
// Extract values from the XImage into our own scope
macro_rules! get {
($($a:ident),+) => ($(let $a = (*self.handle).$a;)+);
}
get!(
width,
height,
byte_order,
depth,
bytes_per_line,
bits_per_pixel,
red_mask,
green_mask,
blue_mask
);
// Pixel size
let stride = match (depth, bits_per_pixel) {
(24, 24) => 3,
(24, 32) | (32, 32) => 4,
_ => return None,
};
// Compute subpixel offsets into each pixel according the the bitmasks X gives us
// Only 8 bit, byte-aligned values are supported
// Truncate masks to the lower 32 bits as that is the maximum pixel size
macro_rules! channel_offset {
($mask:expr) => {
match (byte_order, $mask & 0xFFFFFFFF) {
(0, 0xFF) | (1, 0xFF000000) => 0,
(0, 0xFF00) | (1, 0xFF0000) => 1,
(0, 0xFF0000) | (1, 0xFF00) => 2,
(0, 0xFF000000) | (1, 0xFF) => 3,
_ => return None,
}
};
}
let red_offset = channel_offset!(red_mask);
let green_offset = channel_offset!(green_mask);
let blue_offset = channel_offset!(blue_mask);
let alpha_offset = channel_offset!(!(red_mask | green_mask | blue_mask));
// Wrap the pixel buffer into a slice
let size = (bytes_per_line * height) as usize;
let data = slice::from_raw_parts((*self.handle).data as *const u8, size);
// Finally, generate the image object
Some(RgbaImage::from_fn(width as u32, height as u32, |x, y| {
macro_rules! subpixel {
($channel_offset:ident) => {
data[(y * bytes_per_line as u32 + x * stride as u32 + $channel_offset)
as usize]
};
}
Rgba::from_channels(
subpixel!(red_offset),
subpixel!(green_offset),
subpixel!(blue_offset),
// Make the alpha channel fully opaque if none is provided
if depth == 24 {
0xFF
} else {
subpixel!(alpha_offset)
},
)
}))
}
}
}
impl Drop for Image {
fn drop(&mut self) {
unsafe {
xlib::XDestroyImage(self.handle);
}
}
}
impl<'a> Iterator for ScreenRectIter<'a> {
type Item = util::Rect;
fn next(&mut self) -> Option<Self::Item> {
if self.i >= self.crtcs.len() {
return None;
}
unsafe {
// TODO Handle failure here?
let crtc = xrandr::XRRGetCrtcInfo((*self.dpy).handle, self.res, self.crtcs[self.i]);
let x = (*crtc).x;
let y = (*crtc).y;
let w = (*crtc).width;
let h = (*crtc).height;
xrandr::XRRFreeCrtcInfo(crtc);
self.i += 1;
//Some((w as i32, h as i32, x as i32, y as i32))
Some(util::Rect {
x: x,
y: y,
w: w as i32,
h: h as i32,
})
}
}
}
impl<'a> Drop for ScreenRectIter<'a> {
fn drop(&mut self) {
unsafe {
xrandr::XRRFreeScreenResources(self.res);
}
}
}
pub fn parse_geometry(g: ffi::CString) -> util::Rect {
unsafe {
let mut x = 0;
let mut y = 0;
let mut w = 0;
let mut h = 0;
xlib::XParseGeometry(
g.as_ptr() as *const raw::c_char,
&mut x,
&mut y,
&mut w,
&mut h,
);
util::Rect {
x: x,
y: y,
w: w as i32,
h: h as i32,
}
}
}