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

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

/// Common functions all Touch-Events implement.
pub trait TouchEventTrait: AsRaw<ffi::libinput_event_touch> + Context {
    ffi_func!(
    /// The event time for this event
    fn time, ffi::libinput_event_touch_get_time, u32);
    ffi_func!(
    /// The event time for this event in microseconds
    fn time_usec, ffi::libinput_event_touch_get_time_usec, u64);

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

impl<T: AsRaw<ffi::libinput_event_touch> + Context> TouchEventTrait for T {}

/// Touch slot related functions all TouchEvents implement, that can be mapped to slots.
///
/// A touch slot is grouping all events related to a single gesture together.
pub trait TouchEventSlot: AsRaw<ffi::libinput_event_touch> {
    ffi_func!(
    /// Get the seat slot of the touch event.
    ///
    /// A seat slot is a non-negative seat wide unique identifier of an active
    /// touch point.
    ///
    /// Events from single touch devices will be represented as one individual
    /// touch point per device.
    fn seat_slot, ffi::libinput_event_touch_get_seat_slot, u32);

    /// Get the slot of this touch event.
    ///
    /// See the kernel's multitouch protocol B documentation for more information.
    ///
    /// If the touch event has no assigned slot, for example if it is from a
    /// single touch device, this function returns `None`.
    fn slot(&self) -> Option<u32> {
        match unsafe { ffi::libinput_event_touch_get_slot(self.as_raw_mut()) } {
            x if x >= 0 => Some(x as u32),
            -1 => None,
            _ => panic!("libinput_event_touch_get_slot returned undocumentated value!"),
        }
    }
}

/// Position related functions all TouchEvents implement, that have a screen
/// position assigned to them.
pub trait TouchEventPosition: AsRaw<ffi::libinput_event_touch> {
    ffi_func!(
    /// Return the current absolute x coordinate of the touch event, in mm from
    /// the top left corner of the device.
    ///
    /// To get the corresponding output screen coordinate, use `x_transformed`.
    fn x, ffi::libinput_event_touch_get_x, f64);
    ffi_func!(
    /// Return the current absolute y coordinate of the touch event, in mm from
    /// the top left corner of the device.
    ///
    /// To get the corresponding output screen coordinate, use `y_transformed`.
    fn y, ffi::libinput_event_touch_get_y, f64);

    /// Return the current absolute x coordinate of the touch event, transformed
    /// to screen coordinates.
    ///
    /// ## Arguments
    ///
    /// - width - The current output screen width
    fn x_transformed(&self, width: u32) -> f64 {
        unsafe { ffi::libinput_event_touch_get_x_transformed(self.as_raw_mut(), width) }
    }

    /// Return the current absolute y coordinate of the touch event, transformed
    /// to screen coordinates.
    ///
    /// ## Arguments
    ///
    /// - height - The current output screen height
    fn y_transformed(&self, height: u32) -> f64 {
        unsafe { ffi::libinput_event_touch_get_y_transformed(self.as_raw_mut(), height) }
    }
}

/// A touch related `Event`
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum TouchEvent {
    /// An event related to resting the finger on the screen
    Down(TouchDownEvent),
    /// An event related to lifting the finger on the screen
    Up(TouchUpEvent),
    /// An event related to moving a finger on the screen
    Motion(TouchMotionEvent),
    /// An event cancelling previous events on this slot
    Cancel(TouchCancelEvent),
    /// Signals the end of a set of touchpoints at one device sample time.
    Frame(TouchFrameEvent),
}

impl EventTrait for TouchEvent {
    #[doc(hidden)]
    fn as_raw_event(&self) -> *mut ffi::libinput_event {
        match *self {
            TouchEvent::Down(ref event) => event.as_raw_event(),
            TouchEvent::Up(ref event) => event.as_raw_event(),
            TouchEvent::Motion(ref event) => event.as_raw_event(),
            TouchEvent::Cancel(ref event) => event.as_raw_event(),
            TouchEvent::Frame(ref event) => event.as_raw_event(),
        }
    }
}

impl FromRaw<ffi::libinput_event_touch> for TouchEvent {
    unsafe fn from_raw(event: *mut ffi::libinput_event_touch, context: &::context::Libinput) -> Self {
        let base = ffi::libinput_event_touch_get_base_event(event);
        match ffi::libinput_event_get_type(base) {
            ffi::libinput_event_type_LIBINPUT_EVENT_TOUCH_DOWN => {
                TouchEvent::Down(TouchDownEvent::from_raw(event, context))
            }
            ffi::libinput_event_type_LIBINPUT_EVENT_TOUCH_UP => {
                TouchEvent::Up(TouchUpEvent::from_raw(event, context))
            }
            ffi::libinput_event_type_LIBINPUT_EVENT_TOUCH_MOTION => {
                TouchEvent::Motion(TouchMotionEvent::from_raw(event, context))
            }
            ffi::libinput_event_type_LIBINPUT_EVENT_TOUCH_CANCEL => {
                TouchEvent::Cancel(TouchCancelEvent::from_raw(event, context))
            }
            ffi::libinput_event_type_LIBINPUT_EVENT_TOUCH_FRAME => {
                TouchEvent::Frame(TouchFrameEvent::from_raw(event, context))
            }
            _ => unreachable!(),
        }
    }
}

impl AsRaw<ffi::libinput_event_touch> for TouchEvent {
    fn as_raw(&self) -> *const ffi::libinput_event_touch {
        match *self {
            TouchEvent::Down(ref event) => event.as_raw(),
            TouchEvent::Up(ref event) => event.as_raw(),
            TouchEvent::Motion(ref event) => event.as_raw(),
            TouchEvent::Cancel(ref event) => event.as_raw(),
            TouchEvent::Frame(ref event) => event.as_raw(),
        }
    }
}

impl Context for TouchEvent {
    fn context(&self) -> &::Libinput {
        match *self {
            TouchEvent::Down(ref event) => event.context(),
            TouchEvent::Up(ref event) => event.context(),
            TouchEvent::Motion(ref event) => event.context(),
            TouchEvent::Cancel(ref event) => event.context(),
            TouchEvent::Frame(ref event) => event.context(),
        }
    }
}

ffi_event_struct!(
/// An event related to resting the finger on the screen
struct TouchDownEvent, ffi::libinput_event_touch, ffi::libinput_event_touch_get_base_event);

impl TouchEventSlot for TouchDownEvent {}

impl TouchEventPosition for TouchDownEvent {}

ffi_event_struct!(
/// An event related to lifting the finger on the screen
struct TouchUpEvent, ffi::libinput_event_touch, ffi::libinput_event_touch_get_base_event);

impl TouchEventSlot for TouchUpEvent {}

ffi_event_struct!(
/// An event related to moving a finger on the screen
struct TouchMotionEvent, ffi::libinput_event_touch, ffi::libinput_event_touch_get_base_event);

impl TouchEventSlot for TouchMotionEvent {}

impl TouchEventPosition for TouchMotionEvent {}

ffi_event_struct!(
/// An event cancelling previous events on this slot
struct TouchCancelEvent, ffi::libinput_event_touch, ffi::libinput_event_touch_get_base_event);

impl TouchEventSlot for TouchCancelEvent {}

ffi_event_struct!(
/// Signals the end of a set of touchpoints at one device sample time.
struct TouchFrameEvent, ffi::libinput_event_touch, ffi::libinput_event_touch_get_base_event);