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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use sys::input_event;
use ::{
    EventTime, RangeError, KeyState,
    EventKind, SynchronizeKind, Key, RelativeAxis, AbsoluteAxis,
    SwitchKind, MiscKind, LedKind, AutorepeatKind, SoundKind, UInputKind,
};

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct SynchronizeEvent {
    pub time: EventTime,
    event: EventKind,
    pub kind: SynchronizeKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct KeyEvent {
    pub time: EventTime,
    event: EventKind,
    pub key: Key,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct RelativeEvent {
    pub time: EventTime,
    event: EventKind,
    pub axis: RelativeAxis,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct AbsoluteEvent {
    pub time: EventTime,
    event: EventKind,
    pub axis: AbsoluteAxis,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct SwitchEvent {
    pub time: EventTime,
    event: EventKind,
    pub switch: SwitchKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct MiscEvent {
    pub time: EventTime,
    event: EventKind,
    pub kind: MiscKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct LedEvent {
    pub time: EventTime,
    event: EventKind,
    pub led: LedKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct AutorepeatEvent {
    pub time: EventTime,
    event: EventKind,
    pub kind: AutorepeatKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct SoundEvent {
    pub time: EventTime,
    event: EventKind,
    pub sound: SoundKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct UInputEvent {
    pub time: EventTime,
    event: EventKind,
    pub code: UInputKind,
    pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct InputEvent {
    pub time: EventTime,
    pub kind: EventKind,
    pub code: u16,
    pub value: i32,
}

impl KeyEvent {
    pub fn key_state(&self) -> KeyState {
        KeyState::from(self.value)
    }
}

macro_rules! event_impl {
    (@impl_event InputEvent $($tt:tt)*) => { };
    (@impl_event $name:ident $code:ident $kind:path, $codekind:ident) => {
        impl GenericEvent for $name {
            fn event_kind(&self) -> EventKind { $kind }
            fn time(&self) -> &EventTime { &self.time }
            fn code(&self) -> u16 { self.$code as _ }
            fn value(&self) -> i32 { self.value }

            fn from_ref(event: &InputEvent) -> Result<&Self, RangeError> {
                $codekind::from_code(event.code)
                    .and_then(move |_| if event.kind == $kind {
                        Ok(unsafe { Self::from_event(event) })
                    } else {
                        Err(Default::default())
                    })
            }

            fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError> {
                $codekind::from_code(event.code)
                    .and_then(move |_| if event.kind == $kind {
                        Ok(unsafe { Self::from_event_mut(event) })
                    } else {
                        Err(Default::default())
                    })
            }
        }

        impl<'a> From<&'a $name> for &'a InputEvent {
            fn from(event: &'a $name) -> Self {
                event.as_event()
            }
        }

        impl<'a> From<&'a $name> for InputEvent {
            fn from(event: &'a $name) -> Self {
                event.as_event().clone()
            }
        }

        impl From<$name> for InputEvent {
            fn from(event: $name) -> Self {
                From::from(&event)
            }
        }

        impl AsRef<InputEvent> for $name {
            fn as_ref(&self) -> &InputEvent {
                self.as_event()
            }
        }

        impl<'a> $name {
            pub fn new(time: EventTime, $code: $codekind, value: i32) -> Self {
                $name {
                    time: time,
                    event: $kind,
                    $code: $code,
                    value: value,
                }
            }

            pub unsafe fn from_event<E: AsRef<input_event>>(event: &E) -> &Self {
                let raw = event.as_ref() as *const _ as *const _;
                &*raw
            }

            pub unsafe fn from_event_mut(event: &mut InputEvent) -> &mut Self {
                let raw = event as *mut _ as *mut _;
                &mut *raw
            }

            pub fn as_event(&self) -> &InputEvent {
                let raw = self as *const _ as *const _;
                unsafe { &*raw }
            }

            pub unsafe fn as_event_mut(&mut self) -> &mut InputEvent {
                let raw = self as *mut _ as *mut _;
                &mut *raw
            }
        }
    };
    (@impl $name:ident $code:ident $kind:path, $codekind:ident) => {
        event_impl! {
            @impl_event $name $code $kind, $codekind
        }

        impl AsRef<input_event> for $name {
            fn as_ref(&self) -> &input_event {
                let raw = self as *const _ as *const _;
                unsafe { &*raw }
            }
        }
    };
    ($(struct $name:ident : $kind:path { $code:ident: $codekind:ident })*) => {
        $(
            event_impl! {
                @impl $name $code $kind, $codekind
            }
        )*
    };
}

pub trait GenericEvent: AsRef<InputEvent> + AsRef<input_event> {
    fn event_kind(&self) -> EventKind;
    fn time(&self) -> &EventTime;
    fn code(&self) -> u16;
    fn value(&self) -> i32;

    fn from_ref(event: &InputEvent) -> Result<&Self, RangeError>;
    fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError>;
}

event_impl! {
    struct SynchronizeEvent : EventKind::Synchronize { kind: SynchronizeKind }
    struct KeyEvent : EventKind::Key { key: Key }
    struct RelativeEvent : EventKind::Relative { axis: RelativeAxis }
    struct AbsoluteEvent : EventKind::Absolute { axis: AbsoluteAxis }
    struct SwitchEvent : EventKind::Switch { switch: SwitchKind }
    struct MiscEvent : EventKind::Misc { kind: MiscKind }
    struct LedEvent : EventKind::Led { led: LedKind }
    struct AutorepeatEvent : EventKind::Autorepeat { kind: AutorepeatKind }
    struct SoundEvent : EventKind::Sound { sound: SoundKind }
    struct UInputEvent : EventKind::UInput { code: UInputKind }
    struct InputEvent : Unknown { code: u16 }
}

impl GenericEvent for InputEvent {
    fn event_kind(&self) -> EventKind { self.kind }
    fn time(&self) -> &EventTime { &self.time }
    fn code(&self) -> u16 { self.code }
    fn value(&self) -> i32 { self.value }

    fn from_ref(event: &InputEvent) -> Result<&Self, RangeError> {
        Ok(event)
    }

    fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError> {
        Ok(event)
    }
}

impl AsRef<InputEvent> for InputEvent {
    fn as_ref(&self) -> &InputEvent {
        self
    }
}

impl InputEvent {
    pub fn from_raw(event: &input_event) -> Result<&Self, RangeError> {
        EventKind::from_type(event.type_).map(|_| {
            let raw = event as *const _ as *const _;
            unsafe { &*raw }
        })
    }

    pub fn from_raw_mut(event: &mut input_event) -> Result<&mut Self, RangeError> {
        EventKind::from_type(event.type_).map(|_| {
            let raw = event as *mut _ as *mut _;
            unsafe { &mut *raw }
        })
    }

    pub fn as_raw(&self) -> &input_event {
        let raw = self as *const _ as *const _;
        unsafe { &*raw }
    }

    pub unsafe fn as_raw_mut(&mut self) -> &mut input_event {
        let raw = self as *mut _ as *mut _;
        &mut *raw
    }
}

macro_rules! input_event_enum {
    ($($variant:ident($ty:ident),)*) => {
        #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
        #[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
        pub enum Event {
        $(
            $variant($ty),
        )*
            Unknown(InputEvent),
        }

        #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
        pub enum EventRef<'a> {
        $(
            $variant(&'a $ty),
        )*
            Unknown(&'a InputEvent),
        }

        #[derive(PartialEq, Eq, Hash, Debug)]
        pub enum EventMut<'a> {
        $(
            $variant(&'a mut $ty),
        )*
            Unknown(&'a mut InputEvent),
        }

        impl Event {
            pub fn new(event: InputEvent) -> Result<Self, RangeError> {
                match event.kind {
                $(
                    EventKind::$variant => $ty::from_ref(&event).map(Clone::clone).map(Event::$variant),
                )*
                    _ => Ok(Event::Unknown(event)),
                }
            }
        }

        impl<'a> EventRef<'a> {
            pub fn new(event: &'a InputEvent) -> Result<Self, RangeError> {
                match event.kind {
                $(
                    EventKind::$variant => $ty::from_ref(event).map(EventRef::$variant),
                )*
                    _ => Ok(EventRef::Unknown(event)),
                }
            }
        }

        impl<'a> EventMut<'a> {
            pub fn new(event: &'a mut InputEvent) -> Result<Self, RangeError> {
                match event.kind {
                $(
                    EventKind::$variant => $ty::from_mut(event).map(EventMut::$variant),
                )*
                    _ => Ok(EventMut::Unknown(event)),
                }
            }
        }

        $(
        impl From<$ty> for Event {
            fn from(event: $ty) -> Self {
                Event::$variant(event)
            }
        }
        )*

        $(
        impl<'a> From<&'a $ty> for EventRef<'a> {
            fn from(event: &'a $ty) -> Self {
                EventRef::$variant(event)
            }
        }
        )*

        $(
        impl<'a> From<&'a mut $ty> for EventMut<'a> {
            fn from(event: &'a mut $ty) -> Self {
                EventMut::$variant(event)
            }
        }
        )*

        impl From<Event> for InputEvent {
            fn from(event: Event) -> Self {
                match event {
                $(
                    Event::$variant(ref event) => <&InputEvent as From<_>>::from(event).clone(),
                )*
                    Event::Unknown(event) => event,
                }
            }
        }

        impl<'a> From<&'a Event> for EventRef<'a> {
            fn from(event: &'a Event) -> Self {
                match *event {
                $(
                    Event::$variant(ref event) => EventRef::$variant(event),
                )*
                    Event::Unknown(ref event) => EventRef::Unknown(event),
                }
            }
        }

        impl<'a> From<&'a mut Event> for EventMut<'a> {
            fn from(event: &'a mut Event) -> Self {
                match *event {
                $(
                    Event::$variant(ref mut event) => EventMut::$variant(event),
                )*
                    Event::Unknown(ref mut event) => EventMut::Unknown(event),
                }
            }
        }

        impl<'a> From<EventRef<'a>> for &'a InputEvent {
            fn from(event: EventRef<'a>) -> Self {
                match event {
                $(
                    EventRef::$variant(event) => event.as_ref(),
                )*
                    EventRef::Unknown(event) => event,
                }
            }
        }

        impl<'a> From<&'a EventMut<'a>> for &'a InputEvent {
            fn from(event: &'a EventMut<'a>) -> Self {
                match *event {
                $(
                    EventMut::$variant(ref event) => event.as_ref(),
                )*
                    EventMut::Unknown(ref event) => event,
                }
            }
        }

        impl AsRef<InputEvent> for Event {
            fn as_ref(&self) -> &InputEvent {
                match *self {
                $(
                    Event::$variant(ref event) => event.as_ref(),
                )*
                    Event::Unknown(ref event) => event.as_ref(),
                }
            }
        }

        impl<'a> AsRef<InputEvent> for EventRef<'a> {
            fn as_ref(&self) -> &InputEvent {
                From::from(*self)
            }
        }

        impl<'a> AsRef<InputEvent> for EventMut<'a> {
            fn as_ref(&self) -> &InputEvent {
                From::from(self)
            }
        }
    };
}

input_event_enum! {
    Synchronize(SynchronizeEvent),
    Key(KeyEvent),
    Relative(RelativeEvent),
    Absolute(AbsoluteEvent),
    Switch(SwitchEvent),
    Misc(MiscEvent),
    Led(LedEvent),
    Autorepeat(AutorepeatEvent),
    Sound(SoundEvent),
    UInput(UInputEvent),
}