1use super::EventTrait;
4use crate::{ffi, AsRaw, Context, FromRaw, Libinput};
5
6pub trait SwitchEventTrait: AsRaw<ffi::libinput_event_switch> + Context {
8 ffi_func!(
9 fn time, ffi::libinput_event_switch_get_time, u32);
11 ffi_func!(
12 fn time_usec, ffi::libinput_event_switch_get_time_usec, u64);
14
15 fn into_switch_event(self) -> SwitchEvent
17 where
18 Self: Sized,
19 {
20 unsafe { SwitchEvent::from_raw(self.as_raw_mut(), self.context()) }
21 }
22}
23
24impl<T: AsRaw<ffi::libinput_event_switch> + Context> SwitchEventTrait for T {}
25
26#[derive(Debug, PartialEq, Eq, Hash)]
28#[non_exhaustive]
29pub enum SwitchEvent {
30 Toggle(SwitchToggleEvent),
32}
33
34impl EventTrait for SwitchEvent {
35 #[doc(hidden)]
36 fn as_raw_event(&self) -> *mut ffi::libinput_event {
37 match self {
38 SwitchEvent::Toggle(event) => event.as_raw_event(),
39 }
40 }
41}
42
43impl FromRaw<ffi::libinput_event_switch> for SwitchEvent {
44 unsafe fn try_from_raw(
45 event: *mut ffi::libinput_event_switch,
46 context: &Libinput,
47 ) -> Option<Self> {
48 let base = ffi::libinput_event_switch_get_base_event(event);
49 match ffi::libinput_event_get_type(base) {
50 ffi::libinput_event_type_LIBINPUT_EVENT_SWITCH_TOGGLE => Some(SwitchEvent::Toggle(
51 SwitchToggleEvent::try_from_raw(event, context)?,
52 )),
53 _ => None,
54 }
55 }
56 unsafe fn from_raw(event: *mut ffi::libinput_event_switch, context: &Libinput) -> Self {
57 Self::try_from_raw(event, context).expect("Unknown switch event type")
58 }
59}
60
61impl AsRaw<ffi::libinput_event_switch> for SwitchEvent {
62 fn as_raw(&self) -> *const ffi::libinput_event_switch {
63 match self {
64 SwitchEvent::Toggle(event) => event.as_raw(),
65 }
66 }
67}
68
69impl Context for SwitchEvent {
70 fn context(&self) -> &Libinput {
71 match self {
72 SwitchEvent::Toggle(event) => event.context(),
73 }
74 }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79#[repr(u32)]
80#[non_exhaustive]
81pub enum Switch {
82 Lid = ffi::libinput_switch_LIBINPUT_SWITCH_LID,
85 TabletMode = ffi::libinput_switch_LIBINPUT_SWITCH_TABLET_MODE,
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102pub enum SwitchState {
103 Off,
105 On,
107}
108
109ffi_event_struct!(
110struct SwitchToggleEvent, ffi::libinput_event_switch, ffi::libinput_event_switch_get_base_event);
112
113impl SwitchToggleEvent {
114 pub fn switch(&self) -> Option<Switch> {
118 match unsafe { ffi::libinput_event_switch_get_switch(self.as_raw_mut()) } {
119 ffi::libinput_switch_LIBINPUT_SWITCH_LID => Some(Switch::Lid),
120 ffi::libinput_switch_LIBINPUT_SWITCH_TABLET_MODE => Some(Switch::TabletMode),
121 _x => {
122 #[cfg(feature = "log")]
123 log::warn!("Unknown Switch type returned by libinput: {}", _x);
124 None
125 }
126 }
127 }
128
129 pub fn switch_state(&self) -> SwitchState {
131 match unsafe { ffi::libinput_event_switch_get_switch_state(self.as_raw_mut()) } {
132 ffi::libinput_switch_state_LIBINPUT_SWITCH_STATE_OFF => SwitchState::Off,
133 ffi::libinput_switch_state_LIBINPUT_SWITCH_STATE_ON => SwitchState::On,
134 _ => panic!("libinput returned invalid 'libinput_switch_state'"),
135 }
136 }
137}