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
use super::EventTrait;
use {AsRaw, FromRaw};
use ffi;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyState {
Pressed,
Released,
}
pub trait KeyboardEventTrait: AsRaw<ffi::libinput_event_keyboard> {
ffi_func!(
fn time, ffi::libinput_event_keyboard_get_time, u32);
ffi_func!(
fn time_usec, ffi::libinput_event_keyboard_get_time_usec, u64);
ffi_func!(
fn key, ffi::libinput_event_keyboard_get_key, u32);
fn key_state(&self) -> KeyState {
match unsafe { ffi::libinput_event_keyboard_get_key_state(self.as_raw() as *mut _) } {
ffi::libinput_key_state::LIBINPUT_KEY_STATE_PRESSED => KeyState::Pressed,
ffi::libinput_key_state::LIBINPUT_KEY_STATE_RELEASED => KeyState::Released,
}
}
fn into_keyboard_event(self) -> KeyboardEvent
where Self: Sized
{
unsafe { KeyboardEvent::from_raw(self.as_raw_mut()) }
}
}
impl<T: AsRaw<ffi::libinput_event_keyboard>> KeyboardEventTrait for T {}
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum KeyboardEvent {
Key(KeyboardKeyEvent),
}
impl EventTrait for KeyboardEvent {
#[doc(hidden)]
fn as_raw_event(&self) -> *mut ffi::libinput_event {
match *self {
KeyboardEvent::Key(ref event) => event.as_raw_event(),
}
}
}
impl FromRaw<ffi::libinput_event_keyboard> for KeyboardEvent {
unsafe fn from_raw(event: *mut ffi::libinput_event_keyboard) -> Self {
let base = ffi::libinput_event_keyboard_get_base_event(event);
match ffi::libinput_event_get_type(base) {
ffi::libinput_event_type::LIBINPUT_EVENT_KEYBOARD_KEY => {
KeyboardEvent::Key(KeyboardKeyEvent::from_raw(event))
}
_ => unreachable!(),
}
}
}
impl AsRaw<ffi::libinput_event_keyboard> for KeyboardEvent {
fn as_raw(&self) -> *const ffi::libinput_event_keyboard {
match *self {
KeyboardEvent::Key(ref event) => event.as_raw(),
}
}
}
ffi_event_struct!(
struct KeyboardKeyEvent, ffi::libinput_event_keyboard, ffi::libinput_event_keyboard_get_base_event);
impl KeyboardKeyEvent {
ffi_func!(
pub fn seat_key_count, ffi::libinput_event_keyboard_get_seat_key_count, u32);
}