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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Tablet pad event types

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

mod mode_group;
pub use self::mode_group::*;

/// Common functions all TabletPad-Events implement.
pub trait TabletPadEventTrait: AsRaw<ffi::libinput_event_tablet_pad> + Context {
    ffi_func!(
    /// The event time for this event
    fn time, ffi::libinput_event_tablet_pad_get_time, u32);
    ffi_func!(
    /// The event time for this event in microseconds
    fn time_usec, ffi::libinput_event_tablet_pad_get_time_usec, u64);
    ffi_func!(
    /// Returns the mode the button, ring, or strip that triggered this event is in, at the time of the event.
    ///
    /// The mode is a virtual grouping of functionality, usually based on some
    /// visual feedback like LEDs on the pad. See [Tablet pad modes](https://wayland.freedesktop.org/libinput/doc/latest/tablet-support.html#tablet-pad-modes)
    /// for details. Mode indices start at 0, a device that does not support modes
    /// always returns 0.
    ///
    /// Mode switching is controlled by libinput and more than one mode may exist
    /// on the tablet. This function returns the mode that this event's button,
    /// ring or strip is logically in. If the button is a mode toggle button and
    /// the button event caused a new mode to be toggled, the mode returned is the
    /// new mode the button is in.
    ///
    /// Note that the returned mode is the mode valid as of the time of the event.
    /// The returned mode may thus be different to the mode returned by
    /// `TabletPadModeGroup::mode`. See `TabletPadModeGroup::mode` for details.
    fn mode, ffi::libinput_event_tablet_pad_get_mode, u32);

    /// Returns the mode group that the button, ring, or strip that triggered this
    /// event is considered in.
    ///
    /// The mode is a virtual grouping of functionality, usually based on some
    /// visual feedback like LEDs on the pad. See [Tablet pad modes](https://wayland.freedesktop.org/libinput/doc/latest/tablet-support.html#tablet-pad-modes) for details.
    fn mode_group(&self) -> TabletPadModeGroup {
        unsafe {
            TabletPadModeGroup::from_raw(
                ffi::libinput_event_tablet_pad_get_mode_group(self.as_raw_mut()),
                self.context(),
            )
        }
    }

    /// Convert into a general `TabletPadEvent` again
    fn into_tablet_pad_event(self) -> TabletPadEvent
    where
        Self: Sized,
    {
        unsafe { TabletPadEvent::from_raw(self.as_raw_mut(), self.context()) }
    }
}

impl<T: AsRaw<ffi::libinput_event_tablet_pad> + Context> TabletPadEventTrait for T {}

/// A tablet-pad related `Event`
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum TabletPadEvent {
    /// A button pressed on a device with the `DeviceCapability::TabletPad`
    /// capability.
    ///
    /// This event is not to be confused with the button events emitted by tools
    /// on a tablet. See `TabletToolButtonEvent`.
    Button(TabletPadButtonEvent),
    /// A status change on a tablet ring with the `DeviceCapability::TabletPad`
    /// capability.
    Ring(TabletPadRingEvent),
    /// A status change on a strip on a device with the
    /// `DeviceCapability::TabletPad` capability.
    Strip(TabletPadStripEvent),
}

impl EventTrait for TabletPadEvent {
    #[doc(hidden)]
    fn as_raw_event(&self) -> *mut ffi::libinput_event {
        match *self {
            TabletPadEvent::Button(ref event) => event.as_raw_event(),
            TabletPadEvent::Ring(ref event) => event.as_raw_event(),
            TabletPadEvent::Strip(ref event) => event.as_raw_event(),
        }
    }
}

impl FromRaw<ffi::libinput_event_tablet_pad> for TabletPadEvent {
    unsafe fn from_raw(event: *mut ffi::libinput_event_tablet_pad, context: &::Libinput) -> Self {
        let base = ffi::libinput_event_tablet_pad_get_base_event(event);
        match ffi::libinput_event_get_type(base) {
            ffi::libinput_event_type_LIBINPUT_EVENT_TABLET_PAD_BUTTON => {
                TabletPadEvent::Button(TabletPadButtonEvent::from_raw(event, context))
            }
            ffi::libinput_event_type_LIBINPUT_EVENT_TABLET_PAD_RING => {
                TabletPadEvent::Ring(TabletPadRingEvent::from_raw(event, context))
            }
            ffi::libinput_event_type_LIBINPUT_EVENT_TABLET_PAD_STRIP => {
                TabletPadEvent::Strip(TabletPadStripEvent::from_raw(event, context))
            }
            _ => unreachable!(),
        }
    }
}

impl AsRaw<ffi::libinput_event_tablet_pad> for TabletPadEvent {
    fn as_raw(&self) -> *const ffi::libinput_event_tablet_pad {
        match *self {
            TabletPadEvent::Button(ref event) => event.as_raw(),
            TabletPadEvent::Ring(ref event) => event.as_raw(),
            TabletPadEvent::Strip(ref event) => event.as_raw(),
        }
    }
}

impl Context for TabletPadEvent {
    fn context(&self) -> &::Libinput {
        match *self {
            TabletPadEvent::Button(ref event) => event.context(),
            TabletPadEvent::Ring(ref event) => event.context(),
            TabletPadEvent::Strip(ref event) => event.context(),
        }
    }
}

ffi_event_struct!(
/// A button pressed on a device with the `DeviceCapability::TabletPad`
/// capability.
///
/// This event is not to be confused with the button events emitted by tools
/// on a tablet. See `TabletToolButtonEvent`.
struct TabletPadButtonEvent, ffi::libinput_event_tablet_pad, ffi::libinput_event_tablet_pad_get_base_event);

impl TabletPadButtonEvent {
    ffi_func!(
    /// Return the button number that triggered this event, starting at 0.
    ///
    /// Note that the number returned is a generic sequential button number and
    /// not a semantic button code as defined in linux/input.h.
    /// [See Tablet pad button numbers](https://wayland.freedesktop.org/libinput/doc/latest/tablet-support.html#tablet-pad-buttons)
    /// for more details.
    pub fn button_number, ffi::libinput_event_tablet_pad_get_button_number, u32);

    /// Return the button state of the event.
    pub fn button_state(&self) -> ButtonState {
        match unsafe { ffi::libinput_event_tablet_pad_get_button_state(self.as_raw_mut()) } {
            ffi::libinput_button_state_LIBINPUT_BUTTON_STATE_PRESSED => ButtonState::Pressed,
            ffi::libinput_button_state_LIBINPUT_BUTTON_STATE_RELEASED => ButtonState::Released,
            _ => panic!("libinput returned invalid 'libinput_button_state'"),
        }
    }
}

/// The source for a `TabletPadRingEvent` event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RingAxisSource {
    /// An unknown source
    Unknown,
    /// Finger source
    Finger,
}

ffi_event_struct!(
/// A status change on a tablet ring with the `DeviceCapability::TabletPad`
/// capability.
struct TabletPadRingEvent, ffi::libinput_event_tablet_pad, ffi::libinput_event_tablet_pad_get_base_event);

impl TabletPadRingEvent {
    ffi_func!(
    /// Returns the number of the ring that has changed state, with 0 being the
    /// first ring.
    ///
    /// On tablets with only one ring, this function always returns 0.
    pub fn number, ffi::libinput_event_tablet_pad_get_ring_number, u32);
    ffi_func!(
    /// Returns the current position of the ring, in degrees counterclockwise from
    /// the northern-most point of the ring in the tablet's current logical
    /// orientation.
    ///
    /// If the source is `RingAxisSource::Finger`, libinput sends a  terminating
    /// event with a ring value of -1 when the finger is lifted from the ring. A
    /// caller may use this information to e.g. determine if kinetic scrolling
    /// should be triggered.
    pub fn position, ffi::libinput_event_tablet_pad_get_ring_position, f64);

    /// Returns the source of the interaction with the ring.
    ///
    /// If the source is `RingAxisSource::Finger`, libinput sends a ring position
    /// value of -1 to terminate the current interaction.
    pub fn source(&self) -> RingAxisSource {
        match unsafe { ffi::libinput_event_tablet_pad_get_ring_source(self.as_raw_mut()) } {
            ffi::libinput_tablet_pad_ring_axis_source_LIBINPUT_TABLET_PAD_RING_SOURCE_UNKNOWN => {
                RingAxisSource::Unknown
            }
            ffi::libinput_tablet_pad_ring_axis_source_LIBINPUT_TABLET_PAD_RING_SOURCE_FINGER => {
                RingAxisSource::Finger
            }
            _ => panic!("libinput returned invalid 'libinput_tablet_pad_ring_axis_source'"),
        }
    }
}

/// The source for a `TabletPadStripEvent` event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StripAxisSource {
    /// An unknown source
    Unknown,
    /// Finger source
    Finger,
}

ffi_event_struct!(
/// A status change on a strip on a device with the `DeviceCapability::TabletPad`
/// capability.
struct TabletPadStripEvent, ffi::libinput_event_tablet_pad, ffi::libinput_event_tablet_pad_get_base_event);

impl TabletPadStripEvent {
    ffi_func!(
    /// Returns the number of the strip that has changed state, with 0 being the
    /// first strip.
    ///
    /// On tablets with only one strip, this function always returns 0.
    pub fn number, ffi::libinput_event_tablet_pad_get_strip_number, u32);
    ffi_func!(
    /// Returns the current position of the strip, normalized to the range [0, 1],
    /// with 0 being the top/left-most point in the tablet's current logical
    /// orientation.
    ///
    /// If the source is `StripAxisSource::Finger`, libinput sends a terminating
    /// event with a ring value of -1 when the finger is lifted from the ring. A
    /// caller may use this information to e.g. determine if kinetic scrolling
    /// should be triggered.
    pub fn position, ffi::libinput_event_tablet_pad_get_strip_position, f64);

    /// Returns the source of the interaction with the strip.
    ///
    /// If the source is `StripAxisSource::Finger`, libinput sends a strip
    /// position value of -1 to terminate the current interaction
    pub fn source(&self) -> StripAxisSource {
        match unsafe { ffi::libinput_event_tablet_pad_get_strip_source(self.as_raw_mut()) } {
            ffi::libinput_tablet_pad_strip_axis_source_LIBINPUT_TABLET_PAD_STRIP_SOURCE_UNKNOWN => {
                StripAxisSource::Unknown
            }
            ffi::libinput_tablet_pad_strip_axis_source_LIBINPUT_TABLET_PAD_STRIP_SOURCE_FINGER => {
                StripAxisSource::Finger
            }
            _ => panic!("libinput returned invalid 'libinput_tablet_pad_strip_axis_source'"),
        }
    }
}