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
use std::os::raw::c_int;
use std::ptr;
use std::ptr::NonNull;
use crate::event::InputEvent;
use crate::looper::ForeignLooper;
#[derive(Debug)]
pub struct InputQueue {
ptr: NonNull<ffi::AInputQueue>,
}
unsafe impl Send for InputQueue {}
unsafe impl Sync for InputQueue {}
#[derive(Debug)]
pub struct InputQueueError;
impl InputQueue {
pub unsafe fn from_ptr(ptr: NonNull<ffi::AInputQueue>) -> Self {
Self { ptr }
}
pub fn ptr(&self) -> NonNull<ffi::AInputQueue> {
self.ptr
}
pub fn get_event(&self) -> Option<InputEvent> {
unsafe {
let mut out_event = ptr::null_mut();
if ffi::AInputQueue_getEvent(self.ptr.as_ptr(), &mut out_event) < 0 {
None
} else {
debug_assert!(!out_event.is_null());
Some(InputEvent::from_ptr(NonNull::new_unchecked(out_event)))
}
}
}
pub fn has_events(&self) -> Result<bool, InputQueueError> {
unsafe {
match ffi::AInputQueue_hasEvents(self.ptr.as_ptr()) {
0 => Ok(false),
1 => Ok(true),
x if x < 0 => Err(InputQueueError),
x => unreachable!("AInputQueue_hasEvents returned {}", x),
}
}
}
pub fn pre_dispatch(&self, event: InputEvent) -> Option<InputEvent> {
unsafe {
if ffi::AInputQueue_preDispatchEvent(self.ptr.as_ptr(), event.ptr().as_ptr()) == 0 {
Some(event)
} else {
None
}
}
}
pub fn finish_event(&self, event: InputEvent, handled: bool) {
unsafe {
ffi::AInputQueue_finishEvent(self.ptr.as_ptr(), event.ptr().as_ptr(), handled as c_int);
}
}
pub fn attach_looper(&self, looper: &ForeignLooper, id: i32) {
unsafe {
ffi::AInputQueue_attachLooper(
self.ptr.as_ptr(),
looper.ptr().as_ptr(),
id,
None,
std::ptr::null_mut(),
);
}
}
pub fn detach_looper(&self) {
unsafe {
ffi::AInputQueue_detachLooper(self.ptr.as_ptr());
}
}
}