input_linux/
events.rs

1use core::convert::TryFrom;
2use core::mem::{transmute, size_of};
3use core::hint::unreachable_unchecked;
4use crate::sys::input_event;
5use crate::{
6    EventTime, RangeError, KeyState,
7    EventKind, SynchronizeKind, Key, RelativeAxis, AbsoluteAxis,
8    SwitchKind, MiscKind, LedKind, AutorepeatKind, SoundKind, UInputKind,
9    ForceFeedbackKind, ForceFeedbackStatusKind,
10};
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14#[repr(C)]
15#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
16#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
17/// Synchronization events are used by evdev to group events or convey other
18/// out-of-band information.
19pub struct SynchronizeEvent {
20    /// The timestamp associated with the event.
21    pub time: EventTime,
22    event: EventKind,
23    /// The type of synchronization event.
24    pub kind: SynchronizeKind,
25    /// An associated value with the event.
26    pub value: i32,
27}
28
29#[repr(C)]
30#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
31#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
32/// An event that indicates the state of a key has changed.
33pub struct KeyEvent {
34    /// The timestamp associated with the event.
35    pub time: EventTime,
36    event: EventKind,
37    /// The key that triggered the event.
38    pub key: Key,
39    /// The value of the event.
40    pub value: KeyState,
41}
42
43#[repr(C)]
44#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
45#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
46/// Events that occur when the state of a relative axis is changed.
47pub struct RelativeEvent {
48    /// The timestamp associated with the event.
49    pub time: EventTime,
50    event: EventKind,
51    /// The axis associated with the event.
52    pub axis: RelativeAxis,
53    /// The relative distance of the axis event.
54    pub value: i32,
55}
56
57#[repr(C)]
58#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
59#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
60/// Events that occur when the state of an absolute axis is changed.
61pub struct AbsoluteEvent {
62    /// The timestamp associated with the event.
63    pub time: EventTime,
64    event: EventKind,
65    /// The axis associated with the event.
66    pub axis: AbsoluteAxis,
67    /// The absolute value of the axis.
68    pub value: i32,
69}
70
71#[repr(C)]
72#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
73#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
74/// Special switch events.
75pub struct SwitchEvent {
76    /// The timestamp associated with the event.
77    pub time: EventTime,
78    event: EventKind,
79    /// The switch that triggered the event.
80    pub switch: SwitchKind,
81    /// The state of the switch.
82    pub value: i32,
83}
84
85#[repr(C)]
86#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
87#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
88/// Miscellaneous events.
89pub struct MiscEvent {
90    /// The timestamp associated with the event.
91    pub time: EventTime,
92    event: EventKind,
93    /// The kind of miscellaneous event.
94    pub kind: MiscKind,
95    /// The state/value of the event.
96    pub value: i32,
97}
98
99#[repr(C)]
100#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
101#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
102/// An event that indicates whether the specified LED should turn on/off.
103pub struct LedEvent {
104    /// The timestamp associated with the event.
105    pub time: EventTime,
106    event: EventKind,
107    /// The led associated with the event.
108    pub led: LedKind,
109    /// The state of the led.
110    pub value: i32,
111}
112
113#[repr(C)]
114#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
115#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
116/// An event that configures the autorepeat behaviour of the input device.
117pub struct AutorepeatEvent {
118    /// The timestamp associated with the event.
119    pub time: EventTime,
120    event: EventKind,
121    /// The kind of autorepeat event.
122    pub kind: AutorepeatKind,
123    /// The value of the event.
124    pub value: i32,
125}
126
127#[repr(C)]
128#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
129#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
130/// An event that indicates the device should play a specified sound.
131pub struct SoundEvent {
132    /// The timestamp associated with the event.
133    pub time: EventTime,
134    event: EventKind,
135    /// The sound to play.
136    pub sound: SoundKind,
137    /// The value or state associated with the sound.
138    pub value: i32,
139}
140
141#[repr(C)]
142#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
143#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
144/// An event that configures a force feedback effect.
145pub struct ForceFeedbackEvent {
146    /// The timestamp associated with the event.
147    pub time: EventTime,
148    event: EventKind,
149    /// The force effect.
150    pub kind: ForceFeedbackKind,
151    /// The value or state associated with the effect.
152    pub value: i32,
153}
154
155#[repr(C)]
156#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
157#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
158/// An event that indicates the status of a force feedback effect.
159pub struct ForceFeedbackStatusEvent {
160    /// The timestamp associated with the event.
161    pub time: EventTime,
162    event: EventKind,
163    /// The kind of status update.
164    pub kind: ForceFeedbackStatusKind,
165    /// The value or state associated with the update.
166    pub value: i32,
167}
168
169#[repr(C)]
170#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
171#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
172/// A special event type used to send force-feedback events to uinput.
173pub struct UInputEvent {
174    /// The timestamp associated with the event.
175    pub time: EventTime,
176    event: EventKind,
177    /// The code of the event.
178    pub code: UInputKind,
179    /// The unique request ID.
180    pub value: i32,
181}
182
183#[repr(C)]
184#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
185#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
186/// A generic event.
187pub struct InputEvent {
188    /// The timestamp associated with the event.
189    pub time: EventTime,
190    /// The type of event that occurred.
191    pub kind: EventKind,
192    /// The code of the event.
193    ///
194    /// The meaning of this code depends on the `kind` of event. Using the typed
195    /// events via [`Event`] and [`EventRef`] is recommended.
196    pub code: u16,
197    /// The value of the event.
198    ///
199    /// The interpretation of this value depends on the `kind` of event.
200    pub value: i32,
201}
202
203impl InputEvent {
204    /// Constructs an empty event.
205    pub const fn zeroed() -> Self {
206        Self {
207            time: EventTime::new(0, 0),
208            kind: EventKind::Synchronize,
209            code: SynchronizeKind::Report.code(),
210            value: 0,
211        }
212    }
213
214    /// Reinterprets a raw [`input_event`].
215    pub fn with_raw(event: input_event) -> Result<Self, RangeError> {
216        EventKind::from_type(event.type_).map(move |_| unsafe {
217            Self::with_raw_unchecked(event)
218        })
219    }
220
221    /// Reinterprets a raw [`input_event`].
222    ///
223    /// # Safety
224    ///
225    /// The input event must have a valid [event kind](EventKind).
226    pub unsafe fn with_raw_unchecked(event: input_event) -> Self {
227        unsafe {
228            transmute(event)
229        }
230    }
231
232    /// Reinterpret this event as an array of bytes.
233    pub fn into_bytes(self) -> [u8; size_of::<InputEvent>()] {
234        unsafe {
235            transmute(self)
236        }
237    }
238
239    /// Reinterpret this event as a byte slice.
240    pub fn as_bytes(&self) -> &[u8; size_of::<InputEvent>()] {
241        unsafe {
242            transmute(self)
243        }
244    }
245
246    /// Reinterpret this event as a mutable byte slice.
247    pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8; size_of::<InputEvent>()] {
248        transmute(self)
249    }
250}
251
252#[test]
253fn input_event_zeroed() {
254    use core::mem::MaybeUninit;
255
256    let event = InputEvent::zeroed().into_raw();
257    let zeroed: input_event = unsafe { MaybeUninit::zeroed().assume_init() };
258    assert_eq!(event, zeroed);
259}
260
261impl SynchronizeEvent {
262    /// Synchronization event
263    pub const fn report(time: EventTime) -> Self {
264        Self::new(time, SynchronizeKind::Report, 0)
265    }
266}
267
268macro_rules! event_impl {
269    (@impl_event InputEvent $($tt:tt)*) => { };
270    (@impl_event $name:ident $code:ident $kind:path, $codekind:ident, $valuekind:ident) => {
271        impl GenericEvent for $name {
272            fn event_kind(&self) -> EventKind { $kind }
273            fn time(&self) -> &EventTime { &self.time }
274            fn code(&self) -> u16 { self.$code as _ }
275            fn value(&self) -> i32 { self.value.into() }
276
277            fn with_event(event: InputEvent) -> Result<Self, RangeError> {
278                $codekind::from_code(event.code)
279                    .and_then(move |_| if event.kind == $kind {
280                        Ok(unsafe { Self::with_event_unchecked(event) })
281                    } else {
282                        Err(Default::default())
283                    })
284            }
285
286            unsafe fn with_event_unchecked(event: InputEvent) -> Self {
287                transmute(event)
288            }
289
290            fn from_ref(event: &InputEvent) -> Result<&Self, RangeError> {
291                $codekind::from_code(event.code)
292                    .and_then(move |_| if event.kind == $kind {
293                        Ok(unsafe { Self::from_event(event) })
294                    } else {
295                        Err(Default::default())
296                    })
297            }
298
299            fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError> {
300                $codekind::from_code(event.code)
301                    .and_then(move |_| if event.kind == $kind {
302                        Ok(unsafe { Self::from_event_mut(event) })
303                    } else {
304                        Err(Default::default())
305                    })
306            }
307        }
308
309        impl<'a> TryFrom<&'a InputEvent> for &'a $name {
310            type Error = RangeError;
311
312            fn try_from(event: &'a InputEvent) -> Result<Self, Self::Error> {
313                $name::from_ref(event)
314            }
315        }
316
317        impl TryFrom<InputEvent> for $name {
318            type Error = RangeError;
319
320            fn try_from(event: InputEvent) -> Result<Self, Self::Error> {
321                $name::from_ref(&event).map(|&e| e)
322            }
323        }
324
325        impl<'a> TryFrom<&'a mut InputEvent> for &'a mut $name {
326            type Error = RangeError;
327
328            fn try_from(event: &'a mut InputEvent) -> Result<Self, Self::Error> {
329                $name::from_mut(event)
330            }
331        }
332
333        impl<'a> From<&'a $name> for &'a InputEvent {
334            fn from(event: &'a $name) -> Self {
335                event.as_event()
336            }
337        }
338
339        impl<'a> From<&'a $name> for InputEvent {
340            fn from(event: &'a $name) -> Self {
341                event.as_event().clone()
342            }
343        }
344
345        impl From<$name> for InputEvent {
346            fn from(event: $name) -> Self {
347                From::from(&event)
348            }
349        }
350
351        impl AsRef<InputEvent> for $name {
352            fn as_ref(&self) -> &InputEvent {
353                self.as_event()
354            }
355        }
356
357        impl<'a> $name {
358            /// Creates a new event from the given code and value.
359            pub const fn new(time: EventTime, $code: $codekind, value: $valuekind) -> Self {
360                $name {
361                    time,
362                    event: $kind,
363                    $code: $code,
364                    value,
365                }
366            }
367
368            /// Reinterpret a generic event without checking for validity.
369            pub unsafe fn from_event<E: AsRef<input_event>>(event: &E) -> &Self {
370                let raw = event.as_ref();
371                transmute(raw)
372            }
373
374            /// Reinterpret a mutable generic event without checking for validity.
375            pub unsafe fn from_event_mut(event: &mut InputEvent) -> &mut Self {
376                transmute(event)
377            }
378
379            /// A generic input event.
380            pub fn into_event(self) -> InputEvent {
381                unsafe {
382                    transmute(self)
383                }
384            }
385
386            /// A generic input event reference.
387            pub fn as_event(&self) -> &InputEvent {
388                unsafe {
389                    transmute(self)
390                }
391            }
392
393            /// A mutable generic input event reference.
394            pub unsafe fn as_event_mut(&mut self) -> &mut InputEvent {
395                transmute(self)
396            }
397        }
398    };
399    (@impl $name:ident $code:ident $kind:path, $codekind:ident, $valuekind:ident) => {
400        event_impl! {
401            @impl_event $name $code $kind, $codekind, $valuekind
402        }
403
404        impl AsRef<input_event> for $name {
405            fn as_ref(&self) -> &input_event {
406                let raw = self as *const _ as *const _;
407                unsafe { &*raw }
408            }
409        }
410    };
411    ($(struct $name:ident : $kind:path { $code:ident: $codekind:ident, value: $valuekind:ident })*) => {
412        $(
413            event_impl! {
414                @impl $name $code $kind, $codekind, $valuekind
415            }
416        )*
417    };
418}
419
420/// A generic linux input event.
421pub trait GenericEvent: AsRef<InputEvent> + AsRef<input_event> {
422    /// The event kind.
423    fn event_kind(&self) -> EventKind;
424    /// The timestamp associated with the event.
425    fn time(&self) -> &EventTime;
426    /// The type code value of the event.
427    fn code(&self) -> u16;
428    /// The value associated with the event.
429    fn value(&self) -> i32;
430
431    /// Interprets a generic event into a concrete event type.
432    fn with_event(event: InputEvent) -> Result<Self, RangeError> where
433        Self: Clone,
434    {
435        Self::from_ref(&event).cloned()
436    }
437
438    /// Interprets a generic event into a concrete event type.
439    ///
440    /// # Safety
441    ///
442    /// The event must match `Self`'s [event kind](Self::event_kind) and have a valid [code](Self::code).
443    unsafe fn with_event_unchecked(event: InputEvent) -> Self where
444        Self: Clone,
445    {
446        match Self::from_ref(&event) {
447            Ok(event) => event.clone(),
448            Err(..) => unreachable_unchecked(),
449        }
450    }
451
452    /// Interprets a generic event reference into a concrete event type.
453    fn from_ref(event: &InputEvent) -> Result<&Self, RangeError>;
454    /// Interprets a mutable generic event reference into a concrete event type.
455    fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError>;
456}
457
458event_impl! {
459    struct SynchronizeEvent : EventKind::Synchronize { kind: SynchronizeKind, value: i32 }
460    struct KeyEvent : EventKind::Key { key: Key, value: KeyState }
461    struct RelativeEvent : EventKind::Relative { axis: RelativeAxis, value: i32 }
462    struct AbsoluteEvent : EventKind::Absolute { axis: AbsoluteAxis, value: i32 }
463    struct SwitchEvent : EventKind::Switch { switch: SwitchKind, value: i32 }
464    struct MiscEvent : EventKind::Misc { kind: MiscKind, value: i32 }
465    struct LedEvent : EventKind::Led { led: LedKind, value: i32 }
466    struct AutorepeatEvent : EventKind::Autorepeat { kind: AutorepeatKind, value: i32 }
467    struct SoundEvent : EventKind::Sound { sound: SoundKind, value: i32 }
468    struct ForceFeedbackEvent : EventKind::ForceFeedback { kind: ForceFeedbackKind, value: i32 }
469    struct ForceFeedbackStatusEvent : EventKind::ForceFeedbackStatus { kind: ForceFeedbackStatusKind, value: i32 }
470    struct UInputEvent : EventKind::UInput { code: UInputKind, value: i32 }
471    struct InputEvent : Unknown { code: u16, value: i32 }
472}
473
474impl GenericEvent for InputEvent {
475    fn event_kind(&self) -> EventKind { self.kind }
476    fn time(&self) -> &EventTime { &self.time }
477    fn code(&self) -> u16 { self.code }
478    fn value(&self) -> i32 { self.value }
479
480    fn from_ref(event: &InputEvent) -> Result<&Self, RangeError> {
481        Ok(event)
482    }
483
484    fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError> {
485        Ok(event)
486    }
487
488    fn with_event(event: InputEvent) -> Result<Self, RangeError> {
489        Ok(event)
490    }
491
492    unsafe fn with_event_unchecked(event: InputEvent) -> Self {
493        event
494    }
495}
496
497impl AsRef<InputEvent> for InputEvent {
498    fn as_ref(&self) -> &InputEvent {
499        self
500    }
501}
502
503impl InputEvent {
504    /// Reinterprets a raw [`input_event`] reference.
505    pub fn from_raw(event: &input_event) -> Result<&Self, RangeError> {
506        EventKind::from_type(event.type_).map(|_| unsafe {
507            Self::from_raw_unchecked(event)
508        })
509    }
510
511    /// Reinterprets a raw [`input_event`] reference.
512    ///
513    /// # Safety
514    ///
515    /// The input event must have a valid [event kind](EventKind).
516    pub unsafe fn from_raw_unchecked(event: &input_event) -> &Self {
517        transmute(event)
518    }
519
520    /// Reinterprets a raw [`input_event`] into a mutable reference.
521    pub fn from_raw_mut(event: &mut input_event) -> Result<&mut Self, RangeError> {
522        EventKind::from_type(event.type_).map(move |_| unsafe {
523            Self::from_raw_mut_unchecked(event)
524        })
525    }
526
527    /// Reinterprets a raw [`input_event`] into a mutable reference.
528    ///
529    /// # Safety
530    ///
531    /// The input event must have a valid [event kind](EventKind).
532    pub unsafe fn from_raw_mut_unchecked(event: &mut input_event) -> &mut Self {
533        transmute(event)
534    }
535
536    /// Reinterprets the event as a raw [`input_event`].
537    pub fn into_raw(self) -> input_event {
538        unsafe {
539            transmute(self)
540        }
541    }
542
543    /// Reinterprets the event as a raw [`input_event`] reference.
544    pub fn as_raw(&self) -> &input_event {
545        unsafe {
546            transmute(self)
547        }
548    }
549
550    /// Reinterprets the event as a mutable raw [`input_event`].
551    pub unsafe fn as_raw_mut(&mut self) -> &mut input_event {
552        transmute(self)
553    }
554}
555
556impl From<InputEvent> for input_event {
557    fn from(event: InputEvent) -> Self {
558        event.into_raw()
559    }
560}
561
562impl<'a> From<&'a InputEvent> for &'a input_event {
563    fn from(event: &'a InputEvent) -> Self {
564        event.as_raw()
565    }
566}
567
568impl TryFrom<input_event> for InputEvent {
569    type Error = RangeError;
570
571    fn try_from(event: input_event) -> Result<Self, Self::Error> {
572        InputEvent::with_raw(event)
573    }
574}
575
576impl<'a> TryFrom<&'a input_event> for &'a InputEvent {
577    type Error = RangeError;
578
579    fn try_from(event: &'a input_event) -> Result<Self, Self::Error> {
580        InputEvent::from_raw(event)
581    }
582}
583
584impl<'a> TryFrom<&'a mut input_event> for &'a mut InputEvent {
585    type Error = RangeError;
586
587    fn try_from(event: &'a mut input_event) -> Result<Self, Self::Error> {
588        InputEvent::from_raw_mut(event)
589    }
590}
591
592macro_rules! input_event_enum {
593    ($($variant:ident($ty:ident),)*) => {
594        /// An owned and typed input event.
595        #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
596        #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
597        pub enum Event {
598        $(
599            #[allow(missing_docs)]
600            $variant($ty),
601        )*
602            /// Unknown event type.
603            Unknown(InputEvent),
604        }
605
606        /// A reference to an input event.
607        #[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
608        pub enum EventRef<'a> {
609        $(
610            #[allow(missing_docs)]
611            $variant(&'a $ty),
612        )*
613            /// Unknown event type.
614            Unknown(&'a InputEvent),
615        }
616
617        /// A mutable reference to an input event.
618        #[derive(PartialEq, Eq, Hash, Debug)]
619        pub enum EventMut<'a> {
620        $(
621            #[allow(missing_docs)]
622            $variant(&'a mut $ty),
623        )*
624            /// Unknown event type.
625            Unknown(&'a mut InputEvent),
626        }
627
628        impl Event {
629            /// Converts a generic [`InputEvent`] to a typed event.
630            pub fn new(event: InputEvent) -> Result<Self, RangeError> {
631                match event.kind {
632                $(
633                    EventKind::$variant => $ty::with_event(event).map(Event::$variant),
634                )*
635                    _ => Ok(Event::Unknown(event)),
636                }
637            }
638
639            /// Converts a generic [`InputEvent`] to a typed event.
640            ///
641            /// Unlike [`Event::new`] this will fall back to [`Event::Unknown`] when an event fails to validate.
642            pub fn with_event(event: InputEvent) -> Self {
643                Self::new(event)
644                    .unwrap_or_else(|_| Event::Unknown(event))
645            }
646
647            /// Extracts the contained event.
648            pub fn into_event(self) -> InputEvent {
649                match self {
650                $(
651                    Event::$variant(e) => e.into_event(),
652                )*
653                    Event::Unknown(event) => event,
654                }
655            }
656
657            /// Borrows the event.
658            pub fn as_event(&self) -> &InputEvent {
659                match *self {
660                $(
661                    Event::$variant(ref event) => event.as_event(),
662                )*
663                    Event::Unknown(ref event) => event,
664                }
665            }
666
667            /// Borrows the event.
668            pub fn to_ref(&self) -> EventRef {
669                match *self {
670                $(
671                    Event::$variant(ref event) => EventRef::$variant(event),
672                )*
673                    Event::Unknown(ref event) => EventRef::Unknown(event),
674                }
675            }
676
677            /// Mutably borrows the event.
678            pub fn to_mut(&mut self) -> EventMut {
679                match *self {
680                $(
681                    Event::$variant(ref mut event) => EventMut::$variant(event),
682                )*
683                    Event::Unknown(ref mut event) => EventMut::Unknown(event),
684                }
685            }
686        }
687
688        impl<'a> EventRef<'a> {
689            /// Wraps the generic [`InputEvent`] into an event.
690            pub fn new(event: &'a InputEvent) -> Result<Self, RangeError> {
691                match event.kind {
692                $(
693                    EventKind::$variant => $ty::from_ref(event).map(EventRef::$variant),
694                )*
695                    _ => Ok(EventRef::Unknown(event)),
696                }
697            }
698
699            /// Clones the referenced event.
700            pub fn to_owned(self) -> Event {
701                match self {
702                $(
703                    EventRef::$variant(event) => Event::$variant(event.clone()),
704                )*
705                    EventRef::Unknown(event) => Event::Unknown(event.clone()),
706                }
707            }
708
709            /// Reborrows the event.
710            pub fn to_ref(&self) -> EventRef {
711                unsafe {
712                    transmute(*self)
713                }
714            }
715
716            /// Borrows the referenced event.
717            pub fn as_event(self) -> &'a InputEvent {
718                match self {
719                $(
720                    EventRef::$variant(e) => e.as_event(),
721                )*
722                    EventRef::Unknown(event) => event,
723                }
724            }
725        }
726
727        impl<'a> EventMut<'a> {
728            /// Wraps the generic [`InputEvent`] into a mutable event.
729            pub fn new(event: &'a mut InputEvent) -> Result<Self, RangeError> {
730                match event.kind {
731                $(
732                    EventKind::$variant => $ty::from_mut(event).map(EventMut::$variant),
733                )*
734                    _ => Ok(EventMut::Unknown(event)),
735                }
736            }
737
738            /// Clones the referenced event.
739            pub fn to_owned(self) -> Event {
740                match self {
741                $(
742                    EventMut::$variant(event) => Event::$variant(event.clone()),
743                )*
744                    EventMut::Unknown(event) => Event::Unknown(event.clone()),
745                }
746            }
747
748            /// Borrows the referenced event.
749            pub fn into_ref(self) -> EventRef<'a> {
750                match self {
751                $(
752                    EventMut::$variant(event) => EventRef::$variant(event),
753                )*
754                    EventMut::Unknown(event) => EventRef::Unknown(event),
755                }
756            }
757
758            /// Borrows the referenced event.
759            pub fn to_ref(&self) -> EventRef {
760                match self {
761                $(
762                    EventMut::$variant(event) => EventRef::$variant(event),
763                )*
764                    EventMut::Unknown(event) => EventRef::Unknown(event),
765                }
766            }
767
768            /// Reborrows the event.
769            pub fn to_mut(&mut self) -> EventMut {
770                match self {
771                $(
772                    EventMut::$variant(event) => EventMut::$variant(event),
773                )*
774                    EventMut::Unknown(event) => EventMut::Unknown(event),
775                }
776            }
777
778            /// Borrows the referenced event.
779            pub fn as_event(&self) -> &InputEvent {
780                match self {
781                $(
782                    EventMut::$variant(event) => event.as_event(),
783                )*
784                    EventMut::Unknown(event) => event,
785                }
786            }
787
788            /// Mutably borrows the referenced event.
789            pub unsafe fn as_event_mut(self) -> &'a mut InputEvent {
790                match self {
791                $(
792                    EventMut::$variant(e) => e.as_event_mut(),
793                )*
794                    EventMut::Unknown(event) => event,
795                }
796            }
797        }
798
799        $(
800        impl From<$ty> for Event {
801            fn from(event: $ty) -> Self {
802                Event::$variant(event)
803            }
804        }
805        )*
806
807        $(
808        impl<'a> From<&'a $ty> for EventRef<'a> {
809            fn from(event: &'a $ty) -> Self {
810                EventRef::$variant(event)
811            }
812        }
813        )*
814
815        $(
816        impl<'a> From<&'a mut $ty> for EventMut<'a> {
817            fn from(event: &'a mut $ty) -> Self {
818                EventMut::$variant(event)
819            }
820        }
821        )*
822
823        impl From<Event> for InputEvent {
824            fn from(event: Event) -> Self {
825                event.into_event()
826            }
827        }
828
829        impl From<InputEvent> for Event {
830            fn from(event: InputEvent) -> Self {
831                Event::with_event(event)
832            }
833        }
834
835        impl<'a> From<&'a Event> for EventRef<'a> {
836            fn from(event: &'a Event) -> Self {
837                event.to_ref()
838            }
839        }
840
841        impl<'a> From<&'a mut Event> for EventMut<'a> {
842            fn from(event: &'a mut Event) -> Self {
843                event.to_mut()
844            }
845        }
846
847        impl<'a> From<EventRef<'a>> for &'a InputEvent {
848            fn from(event: EventRef<'a>) -> Self {
849                event.as_event()
850            }
851        }
852
853        impl<'a> From<EventMut<'a>> for &'a InputEvent {
854            fn from(event: EventMut<'a>) -> Self {
855                event.into_ref().as_event()
856            }
857        }
858
859        impl<'a> From<EventMut<'a>> for EventRef<'a> {
860            fn from(event: EventMut<'a>) -> Self {
861                event.into_ref()
862            }
863        }
864
865        impl<'a, 'b> From<&'a EventMut<'b>> for &'a InputEvent {
866            fn from(event: &'a EventMut<'b>) -> Self {
867                event.as_event()
868            }
869        }
870
871        impl<'a, 'b> From<&'a EventMut<'b>> for EventRef<'a> {
872            fn from(event: &'a EventMut<'b>) -> Self {
873                event.to_ref()
874            }
875        }
876
877        impl AsRef<InputEvent> for Event {
878            fn as_ref(&self) -> &InputEvent {
879                self.as_event()
880            }
881        }
882
883        impl<'a> AsRef<InputEvent> for EventRef<'a> {
884            fn as_ref(&self) -> &InputEvent {
885                self.as_event()
886            }
887        }
888
889        impl<'a> AsRef<InputEvent> for EventMut<'a> {
890            fn as_ref(&self) -> &InputEvent {
891                self.as_event()
892            }
893        }
894    };
895}
896
897input_event_enum! {
898    Synchronize(SynchronizeEvent),
899    Key(KeyEvent),
900    Relative(RelativeEvent),
901    Absolute(AbsoluteEvent),
902    Switch(SwitchEvent),
903    Misc(MiscEvent),
904    Led(LedEvent),
905    Autorepeat(AutorepeatEvent),
906    Sound(SoundEvent),
907    ForceFeedback(ForceFeedbackEvent),
908    ForceFeedbackStatus(ForceFeedbackStatusEvent),
909    UInput(UInputEvent),
910}