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
//! Switch event types

use super::EventTrait;
use {AsRaw, Context, FromRaw};
use ffi;

/// Common functions all Switch-Events implement.
pub trait SwitchEventTrait: AsRaw<ffi::libinput_event_switch> + Context {
    ffi_func!(
    /// The event time for this event
    fn time, ffi::libinput_event_switch_get_time, u32);
    ffi_func!(
    /// The event time for this event in microseconds
    fn time_usec, ffi::libinput_event_switch_get_time_usec, u64);

    /// Convert into a general `SwitchEvent` again
    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 {}

/// A switch related `Event`
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum SwitchEvent {
    /// An event related a switch, that was toggled
    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(),
        }
    }
}

/// Types of Switches
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum Switch {
    /// The laptop lid was closed when the `SwitchState` is
    /// `On`, or was opened when it is `Off`
    Lid = ffi::libinput_switch_LIBINPUT_SWITCH_LID,
    /// This switch indicates whether the device is in normal laptop mode
    /// or behaves like a tablet-like device where the primary
    /// interaction is usually a touch screen. When in tablet mode, the
    /// keyboard and touchpad are usually inaccessible.
    ///
    /// If the switch is in state `SwitchState::Off`, the
    /// device is in laptop mode. If the switch is in state
    /// `SwitchState::On`, the device is in tablet mode and the
    /// keyboard or touchpad may not be  accessible.
    ///
    /// It is up to the caller to identify which devices are inaccessible
    /// `in tablet mode.
    TabletMode = ffi::libinput_switch_LIBINPUT_SWITCH_TABLET_MODE,
}

/// State of a Switch
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SwitchState {
    /// Switch is off
    Off,
    /// Switch is on
    On,
}

ffi_event_struct!(
/// An event related a switch, that was toggled
struct SwitchToggleEvent, ffi::libinput_event_switch, ffi::libinput_event_switch_get_base_event);

impl SwitchToggleEvent {
    /// Return the switch that triggered this event.
    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'"),
        }
    }

    /// Return the switch state that triggered this event.
    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'"),
        }
    }
}