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
#![allow(dead_code, non_upper_case_globals)]
extern crate x11;
extern crate libc;
use ffi::*;
use libc::c_int;
use std::ffi::CString;
use std::{slice, ptr};
use x11::{xlib, xrandr};
pub mod ffi;
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[repr(C)]
pub struct Bgr8 {
pub b: u8,
pub g: u8,
pub r: u8,
_pad: u8,
}
enum Display {
Address(&'static str),
Default,
}
#[derive(Clone, Copy)]
enum Screen {
Specific(c_int),
Default,
}
#[derive(Clone, Copy)]
enum Window {
Window(xlib::Window),
Desktop,
}
struct WindowConnection {
display: *mut xlib::Display,
window: xlib::Window,
width: u32,
height: u32,
}
impl WindowConnection {
unsafe fn new(display: Display,
screen: Screen,
window: Window)
-> Result<WindowConnection, ()> {
let addr = if let Display::Address(address) = display {
match CString::new(address) {
Ok(s) => s.as_ptr(),
Err(_) => return Err(()),
}
} else {
ptr::null()
};
let display_ptr = xlib::XOpenDisplay(addr);
if !display_ptr.is_null() {
let screen_num = if let Screen::Specific(n) = screen {
n
} else {
xlib::XDefaultScreen(display_ptr)
};
let mut window_id = if let Window::Window(id) = window {
id
} else {
xlib::XRootWindow(display_ptr, screen_num)
};
let (mut window_width, mut window_height) = (0, 0);
let geo_result = xlib::XGetGeometry(display_ptr,
window_id,
&mut window_id,
&mut 0,
&mut 0,
&mut window_width,
&mut window_height,
&mut 0,
&mut 0);
if geo_result != 0 {
Ok(WindowConnection {
display: display_ptr,
window: window_id,
width: window_width,
height: window_height,
})
} else {
Err(())
}
} else {
Err(())
}
}
}
impl Drop for WindowConnection {
fn drop(&mut self) {
unsafe {
xlib::XCloseDisplay(self.display);
}
}
}
#[derive(Debug)]
pub enum CaptureError {
Fail(&'static str),
}
pub enum CaptureSource {
Region {
x: i32,
y: i32,
width: u32,
height: u32,
},
Monitor(usize),
}
#[derive(Clone, Copy)]
pub struct Geometry {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
}
#[derive(Debug)]
pub struct Image {
image: *mut xlib::XImage,
}
impl Image {
pub fn as_slice(&self) -> &[Bgr8] {
unsafe {
slice::from_raw_parts((*self.image).data as *const _,
(*self.image).width as usize * (*self.image).height as usize)
}
}
pub fn get_dimensions(&self) -> (u32, u32) {
unsafe { ((*self.image).width as u32, (*self.image).height as u32) }
}
}
impl Drop for Image {
fn drop(&mut self) {
unsafe {
xlib::XDestroyImage(self.image);
}
}
}
pub struct Capturer {
window_conn: WindowConnection,
geo: Geometry,
}
impl Capturer {
pub fn new(capture_src: CaptureSource) -> Result<Capturer, ()> {
match unsafe { WindowConnection::new(Display::Default, Screen::Default, Window::Desktop) } {
Ok(conn) => {
let (x, y, w, h) = match capture_src {
CaptureSource::Region { x, y, width, height } => (x, y, width, height),
CaptureSource::Monitor(mon_i) => {
let mut n_mons = 0;
let mons = unsafe {
xrandr::XRRGetMonitors(conn.display, conn.window, 1, &mut n_mons)
};
let mons = unsafe { slice::from_raw_parts_mut(mons, n_mons as usize) };
let mon = mons[mon_i];
(mon.x, mon.y, mon.width as u32, mon.height as u32)
}
};
Ok(Capturer {
window_conn: conn,
geo: Geometry {
width: w,
height: h,
x: x,
y: y,
},
})
}
Err(_) => Err(()),
}
}
pub fn get_geometry(&self) -> Geometry {
self.geo
}
pub fn capture_frame(&mut self) -> Result<Image, CaptureError> {
let image_ptr = unsafe {
xlib::XGetImage(self.window_conn.display,
self.window_conn.window,
self.geo.x,
self.geo.y,
self.geo.width,
self.geo.height,
AllPlanes,
ZPixmap)
};
if !image_ptr.is_null() {
let image = unsafe { &mut *image_ptr };
unsafe {
if image.depth == 24 && image.bits_per_pixel == 32 &&
image.red_mask == 0xFF0000 && image.green_mask == 0xFF00 &&
image.blue_mask == 0xFF {
let image = Image { image: image_ptr };
Ok(image)
} else {
xlib::XDestroyImage(image_ptr as *mut _);
Err(CaptureError::Fail("Wrong layout"))
}
}
} else {
Err(CaptureError::Fail("XGetImage returned null pointer"))
}
}
}