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
use super::EventTrait;
use {AsRaw, Context, FromRaw};
use ffi;
pub trait SwitchEventTrait: AsRaw<ffi::libinput_event_switch> + Context {
ffi_func!(
fn time, ffi::libinput_event_switch_get_time, u32);
ffi_func!(
fn time_usec, ffi::libinput_event_switch_get_time_usec, u64);
fn into_switch_event(self) -> SwitchEvent
where
Self: Sized,
{
unsafe { SwitchEvent::from_raw(self.as_raw_mut(), self.context()) }
}
}
impl<T: AsRaw<ffi::libinput_event_switch> + Context> SwitchEventTrait for T {}
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum SwitchEvent {
Toggle(SwitchToggleEvent),
}
impl EventTrait for SwitchEvent {
#[doc(hidden)]
fn as_raw_event(&self) -> *mut ffi::libinput_event {
match *self {
SwitchEvent::Toggle(ref event) => event.as_raw_event(),
}
}
}
impl FromRaw<ffi::libinput_event_switch> for SwitchEvent {
unsafe fn from_raw(event: *mut ffi::libinput_event_switch, context: &::context::Libinput) -> Self {
let base = ffi::libinput_event_switch_get_base_event(event);
match ffi::libinput_event_get_type(base) {
ffi::libinput_event_type_LIBINPUT_EVENT_SWITCH_TOGGLE => {
SwitchEvent::Toggle(SwitchToggleEvent::from_raw(event, context))
}
_ => unreachable!(),
}
}
}
impl AsRaw<ffi::libinput_event_switch> for SwitchEvent {
fn as_raw(&self) -> *const ffi::libinput_event_switch {
match *self {
SwitchEvent::Toggle(ref event) => event.as_raw(),
}
}
}
impl Context for SwitchEvent {
fn context(&self) -> &::Libinput {
match *self {
SwitchEvent::Toggle(ref event) => event.context(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum Switch {
Lid = ffi::libinput_switch_LIBINPUT_SWITCH_LID,
TabletMode = ffi::libinput_switch_LIBINPUT_SWITCH_TABLET_MODE,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SwitchState {
Off,
On,
}
ffi_event_struct!(
struct SwitchToggleEvent, ffi::libinput_event_switch, ffi::libinput_event_switch_get_base_event);
impl SwitchToggleEvent {
pub fn switch(&self) -> Switch {
match unsafe { ffi::libinput_event_switch_get_switch(self.as_raw_mut()) } {
ffi::libinput_switch_LIBINPUT_SWITCH_LID => Switch::Lid,
ffi::libinput_switch_LIBINPUT_SWITCH_TABLET_MODE => Switch::TabletMode,
_ => panic!("libinput returned invalid 'libinput_switch'"),
}
}
pub fn switch_state(&self) -> SwitchState {
match unsafe { ffi::libinput_event_switch_get_switch_state(self.as_raw_mut()) } {
ffi::libinput_switch_state_LIBINPUT_SWITCH_STATE_OFF => SwitchState::Off,
ffi::libinput_switch_state_LIBINPUT_SWITCH_STATE_ON => SwitchState::On,
_ => panic!("libinput returned invalid 'libinput_switch_state'"),
}
}
}