-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathraw_ffi.rs
343 lines (299 loc) · 9.77 KB
/
raw_ffi.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use std::ptr::NonNull;
#[cfg(not(all(test, unix)))]
use libc::c_void;
#[cfg(unix)]
pub(crate) use libc::iovec;
use libc::{c_char, c_int, c_uint};
// As defined in xcb_windefs.h
#[cfg(not(unix))]
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub(crate) struct iovec {
pub(crate) iov_base: *mut c_void,
pub(crate) iov_len: c_int,
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_connection_t {
_unused: [u8; 0],
}
#[derive(Debug)]
pub(crate) struct XCBConnectionWrapper {
ptr: NonNull<xcb_connection_t>,
should_drop: bool,
}
// libxcb is fully thread-safe (well, except for xcb_disconnect()), so the following is
// actually fine and safe:
unsafe impl Send for XCBConnectionWrapper {}
unsafe impl Sync for XCBConnectionWrapper {}
impl Drop for XCBConnectionWrapper {
fn drop(&mut self) {
if self.should_drop {
unsafe {
xcb_disconnect(self.ptr.as_ptr());
}
}
}
}
impl XCBConnectionWrapper {
pub(crate) unsafe fn new(ptr: *mut xcb_connection_t, should_drop: bool) -> Self {
Self {
ptr: NonNull::new_unchecked(ptr),
should_drop,
}
}
pub(crate) fn as_ptr(&self) -> *mut xcb_connection_t {
self.ptr.as_ptr()
}
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_generic_event_t {
pub(crate) response_type: u8,
pub(crate) pad0: u8,
pub(crate) sequence: u16,
pub(crate) pad: [u32; 7],
pub(crate) full_sequence: u32,
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_generic_error_t {
pub(crate) response_type: u8,
pub(crate) error_code: u8,
pub(crate) sequence: u16,
pub(crate) resource_id: u32,
pub(crate) minor_code: u16,
pub(crate) major_code: u8,
pub(crate) pad0: u8,
pub(crate) pad: [u32; 5],
pub(crate) full_sequence: u32,
}
#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_void_cookie_t {
pub(crate) sequence: c_uint,
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_extension_t {
pub(crate) name: *const c_char,
pub(crate) global_id: c_int,
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_protocol_request_t {
pub(crate) count: usize,
pub(crate) ext: *mut xcb_extension_t,
pub(crate) opcode: u8,
pub(crate) isvoid: u8,
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub(crate) struct xcb_setup_t {
_unused: [u8; 0],
}
pub(crate) mod connection_errors {
use std::os::raw::c_int;
pub(crate) const ERROR: c_int = 1;
pub(crate) const EXT_NOTSUPPORTED: c_int = 2;
pub(crate) const MEM_INSUFFICIENT: c_int = 3;
pub(crate) const REQ_LEN_EXCEED: c_int = 4;
pub(crate) const PARSE_ERR: c_int = 5;
pub(crate) const INVALID_SCREEN: c_int = 6;
pub(crate) const FDPASSING_FAILED: c_int = 7;
}
pub(crate) mod send_request_flags {
use libc::c_int;
pub(crate) const CHECKED: c_int = 1;
pub(crate) const RAW: c_int = 2;
//pub(crate) const DISCARD_REPLY: c_int = 4;
pub(crate) const REPLY_FDS: c_int = 8;
}
#[cfg(not(test))]
#[link(name = "xcb")]
extern "C" {
// From xcb.h
pub(crate) fn xcb_flush(c: *mut xcb_connection_t) -> c_int;
pub(crate) fn xcb_get_maximum_request_length(c: *mut xcb_connection_t) -> u32;
pub(crate) fn xcb_prefetch_maximum_request_length(c: *mut xcb_connection_t);
pub(crate) fn xcb_wait_for_event(c: *mut xcb_connection_t) -> *mut xcb_generic_event_t;
pub(crate) fn xcb_poll_for_event(c: *mut xcb_connection_t) -> *mut xcb_generic_event_t;
pub(crate) fn xcb_request_check(
c: *mut xcb_connection_t,
void_cookie: xcb_void_cookie_t,
) -> *mut xcb_generic_error_t;
pub(crate) fn xcb_discard_reply64(c: *mut xcb_connection_t, sequence: u64);
pub(crate) fn xcb_get_setup(c: *mut xcb_connection_t) -> *const xcb_setup_t;
#[cfg(unix)]
pub(crate) fn xcb_get_file_descriptor(c: *mut xcb_connection_t) -> c_int;
pub(crate) fn xcb_connection_has_error(c: *mut xcb_connection_t) -> c_int;
pub(crate) fn xcb_disconnect(c: *mut xcb_connection_t);
pub(crate) fn xcb_connect(
displayname: *const c_char,
screenp: *mut c_int,
) -> *mut xcb_connection_t;
pub(crate) fn xcb_generate_id(c: *mut xcb_connection_t) -> u32;
// From xcbext.h
pub(crate) fn xcb_send_request64(
c: *mut xcb_connection_t,
flags: c_int,
vector: *mut iovec,
request: *const xcb_protocol_request_t,
) -> u64;
#[cfg(unix)]
pub(crate) fn xcb_send_request_with_fds64(
c: *mut xcb_connection_t,
flags: c_int,
vector: *mut iovec,
request: *const xcb_protocol_request_t,
num_fds: c_uint,
fds: *mut c_int,
) -> u64;
pub(crate) fn xcb_wait_for_reply64(
c: *mut xcb_connection_t,
request: u64,
e: *mut *mut xcb_generic_error_t,
) -> *mut c_void;
pub(crate) fn xcb_poll_for_reply64(
c: *mut xcb_connection_t,
request: u64,
reply: *mut *mut c_void,
error: *mut *mut xcb_generic_error_t,
) -> c_int;
}
#[cfg(test)]
mod mock {
use std::ffi::CStr;
use libc::{c_char, c_int, c_uint, c_void};
use super::{
iovec, xcb_connection_t, xcb_generic_error_t, xcb_generic_event_t, xcb_protocol_request_t,
xcb_setup_t, xcb_void_cookie_t,
};
use crate::protocol::xproto::{ImageOrder, Setup};
use crate::x11_utils::Serialize;
#[repr(C)]
struct ConnectionMock {
error: c_int,
setup: Vec<u8>,
}
// From xcb.h
pub(crate) unsafe fn xcb_flush(_c: *mut xcb_connection_t) -> c_int {
unimplemented!();
}
pub(crate) unsafe fn xcb_get_maximum_request_length(_c: *mut xcb_connection_t) -> u32 {
unimplemented!();
}
pub(crate) unsafe fn xcb_prefetch_maximum_request_length(_c: *mut xcb_connection_t) {
unimplemented!();
}
pub(crate) unsafe fn xcb_wait_for_event(_c: *mut xcb_connection_t) -> *mut xcb_generic_event_t {
unimplemented!();
}
pub(crate) unsafe fn xcb_poll_for_event(_c: *mut xcb_connection_t) -> *mut xcb_generic_event_t {
unimplemented!();
}
pub(crate) unsafe fn xcb_request_check(
_c: *mut xcb_connection_t,
_void_cookie: xcb_void_cookie_t,
) -> *mut xcb_generic_error_t {
unimplemented!();
}
pub(crate) unsafe fn xcb_discard_reply64(_c: *mut xcb_connection_t, _sequence: u64) {
unimplemented!();
}
pub(crate) unsafe fn xcb_get_setup(c: *mut xcb_connection_t) -> *const xcb_setup_t {
// The pointer is suitable aligned since our xcb_connect() mock above created it
#[allow(clippy::cast_ptr_alignment)]
((*(c as *const ConnectionMock)).setup.as_ptr() as _)
}
#[cfg(unix)]
pub(crate) unsafe fn xcb_get_file_descriptor(_c: *mut xcb_connection_t) -> c_int {
unimplemented!();
}
pub(crate) unsafe fn xcb_connection_has_error(c: *mut xcb_connection_t) -> c_int {
// The pointer is suitable aligned since our xcb_connect() mock above created it
#[allow(clippy::cast_ptr_alignment)]
(*(c as *const ConnectionMock)).error
}
pub(crate) unsafe fn xcb_disconnect(c: *mut xcb_connection_t) {
// The pointer is suitable aligned since our xcb_connect() mock above created it
#[allow(clippy::cast_ptr_alignment)]
let _ = Box::from_raw(c as *mut ConnectionMock);
}
pub(crate) unsafe fn xcb_connect(
displayname: *const c_char,
screenp: *mut c_int,
) -> *mut xcb_connection_t {
// Test that the provided displayname is correct
if CStr::from_ptr(displayname).to_str().unwrap() != "display name" {
panic!("Did not get the expected displayname");
}
std::ptr::write(screenp, 0);
let length_field = 10;
let setup = Setup {
status: 0,
protocol_major_version: 0,
protocol_minor_version: 0,
length: length_field,
release_number: 0,
resource_id_base: 0,
resource_id_mask: 0,
motion_buffer_size: 0,
maximum_request_length: 0,
image_byte_order: ImageOrder::LSBFirst,
bitmap_format_bit_order: ImageOrder::LSBFirst,
bitmap_format_scanline_unit: 0,
bitmap_format_scanline_pad: 0,
min_keycode: 0,
max_keycode: 0,
vendor: Default::default(),
pixmap_formats: Default::default(),
roots: Default::default(),
};
let setup = setup.serialize();
assert_eq!(setup.len(), 4 * length_field as usize);
let mock = ConnectionMock { error: 0, setup };
Box::into_raw(Box::new(mock)) as _
}
pub(crate) unsafe fn xcb_generate_id(_c: *mut xcb_connection_t) -> u32 {
unimplemented!();
}
// From xcbext.h
pub(crate) unsafe fn xcb_send_request64(
_c: *mut xcb_connection_t,
_flags: c_int,
_vector: *mut iovec,
_request: *const xcb_protocol_request_t,
) -> u64 {
unimplemented!();
}
#[cfg(unix)]
pub(crate) unsafe fn xcb_send_request_with_fds64(
_c: *mut xcb_connection_t,
_flags: c_int,
_vector: *mut iovec,
_request: *const xcb_protocol_request_t,
_num_fds: c_uint,
_fds: *mut c_int,
) -> u64 {
unimplemented!();
}
pub(crate) unsafe fn xcb_wait_for_reply64(
_c: *mut xcb_connection_t,
_request: u64,
_e: *mut *mut xcb_generic_error_t,
) -> *mut c_void {
unimplemented!();
}
pub(crate) unsafe fn xcb_poll_for_reply64(
_c: *mut xcb_connection_t,
_request: u64,
_reply: *mut *mut c_void,
_error: *mut *mut xcb_generic_error_t,
) -> c_int {
unimplemented!();
}
}
#[cfg(test)]
pub(crate) use mock::*;