evdevil 0.4.0

Bindings to Linux' input device APIs: evdev and uinput
Documentation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Input event types and enumerations.
//!
//! All device event iterators will yield [`InputEvent`], the base event type.
//!
//! Events carry the following information:
//!
//! - **Timestamp** ([`InputEvent::time`]): The time at which the event has been inserted into the
//!   kernel buffer. The default time source is the system's real-time clock, which is also used by
//!   [`SystemTime::now`]. It can be changed by calling [`Evdev::set_clockid`].
//! - **Event Type** ([`InputEvent::event_type`]): The broad category of event. The [`EventType`]
//!   determines which of the event wrappers listed below is responsible.
//! - **Event Code** ([`InputEvent::raw_code`]): A `u16` identifying the button, axis, or other
//!   object affected by the event. This is typically *wrapped* in a type like [`Syn`], [`Key`],
//!   [`Abs`] or [`Rel`] and accessed from a specific event wrapper.
//! - **Event Value** ([`InputEvent::raw_value`]): An `i32` describing *what* happened to the object
//!   identified by the code. This can be the new state of an [`Abs`] axis, an incremental movement
//!   of a [`Rel`] axis, the new state (pressed/released/repeated) of a [`Key`] or [`Switch`], or
//!   some other value.
//!
//! [`InputEvent::kind`] can be used to obtain an [`EventKind`], which can be conveniently `match`ed
//! on to handle the different types of events.
//!
//! Devices may emit a variety of different types of events, which are all wrapped by the following
//! Rust types in this module:
//!
//! - [`SynEvent`]
//! - [`KeyEvent`]
//! - [`RelEvent`]
//! - [`AbsEvent`]
//! - [`SwitchEvent`]
//! - [`MiscEvent`]
//! - [`LedEvent`]
//! - [`RepeatEvent`]
//! - [`SoundEvent`]
//! - [`UinputEvent`]
//! - [`ForceFeedbackEvent`]
//!
//! All of these event types can be converted to [`EventKind`] and [`InputEvent`] via [`From`] and
//! [`Into`], and can be [`Deref`]erenced to obtain the [`InputEvent`] they wrap.
//!
//! # Serialization and Parsing
//!
//! If the `serde` feature is enabled, implementations of [`Serialize`] and [`Deserialize`] will be
//! provided for the following types:
//!
//! - [`Abs`]
//! - [`Key`]
//! - [`Rel`]
//! - [`Misc`]
//! - [`Led`]
//! - [`Switch`]
//! - [`Sound`]
//!
//!
//! For human-readable formats, the serde representation will use the evdev constant name if the
//! value has one (eg. `KEY_F1`, `ABS_Y`, ...), and the raw [`u16`] code if it does not.
//! Deserialization from a human-readable format will accept either.
//!
//! Note that this means that when a new key or axis name is added in a later version of this crate,
//! older versions will not be able to deserialize it.
//! `evdevil` guarantees only old->new compatibility between non-breaking versions, not new->old.
//!
//!
//! Note also that some values have multiple names.
//! For example, [`Key::BTN_TRIGGER_HAPPY`] is the same value as [`Key::BTN_TRIGGER_HAPPY1`].
//! Deserialization will accept either name, but serialization has to pick a single name, which is
//! typically the first one with a given value, but is not generally guaranteed to be any specific
//! constant name.
//!
//! For non-human-readable formats, the raw numeric code is always used, and formats that aren't
//! self-describing (like [postcard]) are supported.
//! There is also no compatibility concern in case new key or axis names are added in later
//! versions.
//!
//! All of the above types also implement [`FromStr`], which accepts the same names as the
//! [`Deserialize`] implementation, and requires no Cargo feature to be enabled.
//!
//! Most evdev enumerations also define a `_MAX` and `_CNT` value in the Linux headers.
//! Since those values are routinely updated (incremented), they are not exposed by `evdevil` and
//! are also not accepted by the [`FromStr`] and [`Deserialize`] impls, to ensure that no silent
//! breakage occurs when these constants are changed.
//!
//! [`FromStr`]: std::str::FromStr
//! [`Evdev::set_clockid`]: crate::Evdev::set_clockid
//! [`Serialize`]: ::serde::Serialize
//! [`Deserialize`]: ::serde::Deserialize
//! [postcard]: https://github.com/jamesmunns/postcard

pub(crate) mod codes;

#[cfg(any(test, feature = "serde"))]
mod serde;

use std::fmt;
use std::ops::Deref;
use std::time::{Duration, SystemTime};

use crate::ff::{self, EffectId};
use crate::raw::input::input_event;

pub use codes::{Abs, EventType, Key, Led, Misc, Rel, Repeat, Sound, Switch, Syn, UinputCode};

/// An input event received from or sent to an *evdev*.
///
/// Use [`InputEvent::kind`] to convert it to a `match`able enum.
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct InputEvent(input_event);

impl InputEvent {
    /// Creates an [`InputEvent`] from raw values.
    ///
    /// The timestamp of the event will be set to 0.
    /// When submitting events to the kernel, the time stamp will be replaced.
    #[inline]
    pub const fn new(ty: EventType, raw_code: u16, raw_value: i32) -> Self {
        Self(input_event {
            time: libc::timeval {
                tv_sec: 0,
                tv_usec: 0,
            },
            type_: ty.0,
            code: raw_code,
            value: raw_value,
        })
    }

    /// Creates an [`InputEvent`] with all fields zeroed out.
    ///
    /// Useful as a dummy or filler value that will be overwritten with a "real" event soon.
    ///
    /// This results in a [`Syn::REPORT`] event.
    #[inline]
    pub const fn zeroed() -> Self {
        Self(input_event {
            time: libc::timeval {
                tv_sec: 0,
                tv_usec: 0,
            },
            type_: 0,
            code: 0,
            value: 0,
        })
    }

    /// Changes the timestamp of `self` to the given [`SystemTime`].
    ///
    /// **Note**: [`InputEvent`] uses a `timeval` to store the timestamp, which has microsecond
    /// resolution, while [`SystemTime`] can represent nanoseconds on Unix.
    /// The value will be truncated or rounded to fit in the `timeval`.
    pub fn with_time(mut self, time: SystemTime) -> Self {
        let dur = if time >= SystemTime::UNIX_EPOCH {
            time.duration_since(SystemTime::UNIX_EPOCH).unwrap()
        } else {
            SystemTime::UNIX_EPOCH.duration_since(time).unwrap()
        };
        let sign = if time >= SystemTime::UNIX_EPOCH {
            1
        } else {
            -1
        };
        let sec = dur.as_secs();
        let usec = dur.subsec_micros();
        self.0.time.tv_sec = sec.try_into().unwrap();
        self.0.time.tv_sec *= sign;
        self.0.time.tv_usec = usec.try_into().unwrap();
        self
    }

    /// Returns the timestamp stored in the event.
    ///
    /// The clock source used to generate event timestamps can be changed by calling
    /// [`Evdev::set_clockid`].
    ///
    /// [`Evdev::set_clockid`]: crate::Evdev::set_clockid
    pub fn time(&self) -> SystemTime {
        match self.try_time() {
            Some(time) => time,
            None => {
                log::warn!(
                    "`input_event` timestamp out of range of `SystemTime`: tv_sec={} tv_usec={}",
                    self.0.time.tv_sec,
                    self.0.time.tv_usec,
                );
                SystemTime::UNIX_EPOCH
            }
        }
    }
    fn try_time(&self) -> Option<SystemTime> {
        let sec = self.0.time.tv_sec;
        let usec = self.0.time.tv_usec.clamp(0, 999_999);

        let dur = Duration::new(
            sec.unsigned_abs() as u64,
            (usec * 1000) as u32, // 999_999_000 fits in u32
        );

        if sec >= 0 {
            SystemTime::UNIX_EPOCH.checked_add(dur)
        } else {
            SystemTime::UNIX_EPOCH.checked_sub(dur)
        }
    }

    /// Returns the [`EventKind`] this [`InputEvent`] encodes.
    ///
    /// [`EventKind`] is a matchable, type-safe `enum` which is intended to be the primary way most
    /// applications examine input events.
    ///
    /// [`EventKind`] is `#[non_exhaustive]`, so matching on it requires a wildcard arm that will
    /// catch any events that don't have a specific [`EventKind`] variant.
    /// Future versions of `evdevil` might add new variants to catch those events.
    #[inline]
    pub fn kind(&self) -> EventKind {
        match self.event_type() {
            EventType::SYN => SynEvent(*self).into(),
            EventType::KEY => KeyEvent(*self).into(),
            EventType::REL => RelEvent(*self).into(),
            EventType::ABS => AbsEvent(*self).into(),
            EventType::SW => SwitchEvent(*self).into(),
            EventType::MSC => MiscEvent(*self).into(),
            EventType::LED => LedEvent(*self).into(),
            EventType::REP => RepeatEvent(*self).into(),
            EventType::SND => SoundEvent(*self).into(),
            EventType::UINPUT => UinputEvent(*self).into(),
            EventType::FF => ForceFeedbackEvent(*self).into(),
            _ => EventKind::Other(*self),
        }
    }

    /// Returns the [`EventType`] of this event.
    #[inline]
    pub fn event_type(&self) -> EventType {
        EventType(self.0.type_)
    }

    /// Returns the raw *event code* field.
    ///
    /// The *code* of an [`InputEvent`] generally describes what entity the event is describing.
    /// Depending on the type of event, it can describe a key, axis, sound, LED, or other entity.
    #[inline]
    pub fn raw_code(&self) -> u16 {
        self.0.code
    }

    /// Returns the raw *event value* field.
    ///
    /// The *value* of an [`InputEvent`] describes the new state of the key, axis, LED, or other
    /// entity.
    #[inline]
    pub fn raw_value(&self) -> i32 {
        self.0.value
    }
}

impl fmt::Debug for InputEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind() {
            EventKind::Other(_) => f
                .debug_struct("InputEvent")
                .field("time", &self.time())
                .field("type", &self.event_type())
                .field("code", &self.raw_code())
                .field("value", &self.raw_value())
                .finish(),
            kind => kind.fmt(f),
        }
    }
}

macro_rules! event_wrappers {
    ( $(
        $(#[$attr:meta])*
        pub struct $name:ident in $variant:ident;
    )* ) => {
        $(
            $( #[$attr] )*
            #[derive(Clone, Copy, PartialEq, Eq)]
            pub struct $name(InputEvent);

            impl From<$name> for EventKind {
                #[inline]
                fn from(value: $name) -> Self {
                    Self::$variant(value)
                }
            }

            impl From<$name> for InputEvent {
                #[inline]
                fn from(value: $name) -> Self {
                    value.0
                }
            }

            impl Deref for $name {
                type Target = InputEvent;

                #[inline]
                fn deref(&self) -> &InputEvent {
                    &self.0
                }
            }
        )*

        /// Enumeration of event types.
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        #[non_exhaustive]
        pub enum EventKind {
            $(
                $( #[$attr] )*
                $variant($name),
            )*

            /// Fallback variant for unknown events.
            ///
            /// This cannot be matched on by user code. Future versions of `evdevil` might add new
            /// variants to [`EventKind`] that match events previously captured as `Other`.
            #[non_exhaustive] // prevents construction and use in patterns
            Other(InputEvent),
        }

        impl From<EventKind> for InputEvent {
            #[inline]
            fn from(kind: EventKind) -> InputEvent {
                match kind {
                    $(
                        EventKind::$variant(it) => *it,
                    )*
                    EventKind::Other(ev) => ev,
                }
            }
        }
    };
}

event_wrappers! {
    /// A synchronization event.
    pub struct SynEvent in Syn;
    /// A key press/release/repeat event.
    pub struct KeyEvent in Key;
    /// A relative axis change.
    pub struct RelEvent in Rel;
    /// An absolute axis change.
    pub struct AbsEvent in Abs;
    /// A switch state change.
    pub struct SwitchEvent in Switch;
    /// Miscellaneous management events.
    pub struct MiscEvent in Misc;
    /// Reports or changes the state of device LEDs.
    pub struct LedEvent in Led;
    /// The key repeat settings have been changed.
    ///
    /// **Note**: This event is *not* used to signal key repeats.
    /// Key repeat events are sent as [`KeyEvent`]s with a value of 2 ([`KeyState::REPEAT`]).
    pub struct RepeatEvent in Repeat;
    /// Plays simple sounds on the device.
    pub struct SoundEvent in Sound;
    /// Internal events sent to [`UinputDevice`]s.
    ///
    /// When an [`Evdev`][crate::Evdev] holder uploads or erases a force-feedback effect, the
    /// [`UinputDevice`] will be notified by receiving a [`UinputEvent`].
    /// It should then pass that event to [`UinputDevice::ff_upload`] or [`UinputDevice::ff_erase`]
    /// to perform the requested operation.
    ///
    /// [`UinputDevice`]: crate::uinput::UinputDevice
    /// [`UinputDevice::ff_upload`]: crate::uinput::UinputDevice::ff_upload
    /// [`UinputDevice::ff_erase`]: crate::uinput::UinputDevice::ff_erase
    pub struct UinputEvent in Uinput;
    /// Starts or stops previously uploaded force-feedback effects.
    pub struct ForceFeedbackEvent in ForceFeedback;
}

impl SynEvent {
    #[inline]
    pub fn new(syn: Syn) -> Self {
        Self(InputEvent::new(EventType::SYN, syn.0, 0))
    }

    /// Returns the event code as a [`Syn`] (the specific kind of `SYN` event).
    #[inline]
    pub fn syn(&self) -> Syn {
        Syn(self.raw_code())
    }
}
impl fmt::Debug for SynEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SynEvent")
            .field("time", &self.time())
            .field("syn", &self.syn())
            .field("value", &self.raw_value())
            .finish()
    }
}
impl From<Syn> for SynEvent {
    #[inline]
    fn from(syn: Syn) -> Self {
        Self::new(syn)
    }
}
impl From<Syn> for EventKind {
    #[inline]
    fn from(value: Syn) -> Self {
        Self::Syn(value.into())
    }
}
impl From<Syn> for InputEvent {
    #[inline]
    fn from(value: Syn) -> Self {
        SynEvent::new(value).into()
    }
}

impl KeyEvent {
    #[inline]
    pub fn new(key: Key, state: KeyState) -> Self {
        Self(InputEvent::new(EventType::KEY, key.0, state.0))
    }

    /// Returns the [`Key`] code that has been pressed/released/repeated.
    #[inline]
    pub fn key(&self) -> Key {
        Key(self.raw_code())
    }

    /// Returns the state of the key.
    ///
    /// This will be [`KeyState::RELEASED`] if the key has been released, [`KeyState::PRESSED`] if
    /// it has just been pressed, and [`KeyState::REPEAT`] if the key has been held down after being
    /// pressed.
    #[inline]
    pub fn state(&self) -> KeyState {
        KeyState(self.raw_value())
    }
}
impl fmt::Debug for KeyEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KeyEvent")
            .field("time", &self.time())
            .field("key", &self.key())
            .field("state", &self.state())
            .finish()
    }
}

ffi_enum! {
    /// State of a [`Key`], stored as the value of a [`KeyEvent`].
    ///
    /// Returned by [`KeyEvent::state`].
    pub enum KeyState: i32 {
        /// The key used to be pressed and has now been released.
        RELEASED = 0,
        /// The key used to be released and has now been pressed.
        PRESSED = 1,
        /// The key is pressed, and has been held down long enough to generate a repeat event.
        REPEAT = 2,
    }
}
impl fmt::Debug for KeyState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.variant_name() {
            Some(name) => f.write_str(name),
            None => write!(f, "KeyState({:#?})", self.0),
        }
    }
}

impl RelEvent {
    #[inline]
    pub fn new(rel: Rel, value: i32) -> Self {
        Self(InputEvent::new(EventType::REL, rel.0, value))
    }

    /// Returns the [`Rel`] axis identifier of this event.
    #[inline]
    pub fn rel(&self) -> Rel {
        Rel(self.raw_code())
    }

    /// Returns the value by which the axis has moved.
    #[inline]
    pub fn value(&self) -> i32 {
        self.raw_value()
    }
}
impl fmt::Debug for RelEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RelEvent")
            .field("time", &self.time())
            .field("rel", &self.rel())
            .field("value", &self.value())
            .finish()
    }
}

impl AbsEvent {
    #[inline]
    pub fn new(abs: Abs, value: i32) -> Self {
        Self(InputEvent::new(EventType::ABS, abs.0, value))
    }

    #[inline]
    pub fn abs(&self) -> Abs {
        Abs(self.raw_code())
    }

    #[inline]
    pub fn value(&self) -> i32 {
        self.raw_value()
    }
}
impl fmt::Debug for AbsEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AbsEvent")
            .field("time", &self.time())
            .field("abs", &self.abs())
            .field("value", &self.value())
            .finish()
    }
}

impl SwitchEvent {
    #[inline]
    pub fn new(switch: Switch, on: bool) -> Self {
        Self(InputEvent::new(
            EventType::SW,
            switch.0,
            if on { 1 } else { 0 },
        ))
    }

    #[inline]
    pub fn switch(&self) -> Switch {
        Switch(self.raw_code())
    }

    #[inline]
    pub fn is_pressed(&self) -> bool {
        self.raw_value() != 0
    }
}
impl fmt::Debug for SwitchEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SwitchEvent")
            .field("time", &self.time())
            .field("switch", &self.switch())
            .field("pressed", &self.is_pressed())
            .finish()
    }
}

impl MiscEvent {
    #[inline]
    pub fn new(misc: Misc, value: i32) -> Self {
        Self(InputEvent::new(EventType::MSC, misc.0, value))
    }

    /// Returns the event code (the type of *misc* event).
    #[inline]
    pub fn misc(&self) -> Misc {
        Misc(self.raw_code())
    }
}
impl fmt::Debug for MiscEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MiscEvent")
            .field("time", &self.time())
            .field("misc", &self.misc())
            .field("value", &self.raw_value())
            .finish()
    }
}

impl LedEvent {
    #[inline]
    pub fn new(led: Led, on: bool) -> Self {
        Self(InputEvent::new(
            EventType::LED,
            led.0,
            if on { 1 } else { 0 },
        ))
    }

    #[inline]
    pub fn led(&self) -> Led {
        Led(self.raw_code())
    }

    #[inline]
    pub fn is_on(&self) -> bool {
        !self.is_off()
    }

    #[inline]
    pub fn is_off(&self) -> bool {
        self.0.raw_value() == 0
    }
}
impl fmt::Debug for LedEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LedEvent")
            .field("time", &self.time())
            .field("led", &self.led())
            .field("state", &self.is_on())
            .finish()
    }
}

impl RepeatEvent {
    #[inline]
    pub fn new(repeat: Repeat, value: u32) -> Self {
        Self(InputEvent::new(EventType::REP, repeat.0, value as i32))
    }

    /// Returns the type of [`Repeat`] setting to be adjusted or reported by this event.
    #[inline]
    pub fn repeat(&self) -> Repeat {
        Repeat(self.raw_code())
    }

    #[inline]
    pub fn value(&self) -> u32 {
        self.raw_value() as u32
    }
}
impl fmt::Debug for RepeatEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RepeatEvent")
            .field("time", &self.time())
            .field("repeat", &self.repeat())
            .field("value", &self.raw_value())
            .finish()
    }
}

impl SoundEvent {
    #[inline]
    pub fn new(sound: Sound, playing: bool) -> Self {
        Self(InputEvent::new(
            EventType::SND,
            sound.0,
            if playing { 1 } else { 0 },
        ))
    }

    /// Returns the [`Sound`] this event is requesting to play or stop.
    #[inline]
    pub fn sound(&self) -> Sound {
        Sound(self.raw_code())
    }

    #[inline]
    pub fn is_playing(&self) -> bool {
        self.raw_value() != 0
    }
}
impl fmt::Debug for SoundEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SoundEvent")
            .field("time", &self.time())
            .field("sound", &self.sound())
            .field("value", &self.raw_value())
            .finish()
    }
}

impl UinputEvent {
    #[inline]
    pub fn code(&self) -> UinputCode {
        UinputCode(self.raw_code())
    }
}
impl fmt::Debug for UinputEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UinputEvent")
            .field("time", &self.time())
            .field("code", &self.code())
            .field("value", &self.raw_value())
            .finish()
    }
}

impl ForceFeedbackEvent {
    /// Creates a [`ForceFeedbackEvent`] that controls an effect.
    ///
    /// The effect has to be uploaded via [`Evdev::upload_ff_effect`] first.
    ///
    /// [`Evdev::upload_ff_effect`]: crate::Evdev::upload_ff_effect
    #[inline]
    pub fn control_effect(effect: EffectId, active: bool) -> Self {
        Self(InputEvent::new(
            EventType::FF,
            effect.0 as u16,
            if active { 1 } else { 0 },
        ))
    }

    /// Creates a [`ForceFeedbackEvent`] that controls the master effect gain.
    ///
    /// The `gain` value encodes the gain as a fraction of 65535.
    ///
    /// This only does something if the device advertises support for [`ff::Feature::GAIN`].
    #[inline]
    pub fn control_gain(gain: u16) -> Self {
        Self(InputEvent::new(
            EventType::FF,
            ff::Feature::GAIN.0,
            gain.into(),
        ))
    }

    /// Creates a [`ForceFeedbackEvent`] that controls the autocenter strength.
    ///
    /// The `autocenter` value encodes the autocenter power as a fraction of 65535.
    ///
    /// This only does something if the device advertises support for [`ff::Feature::AUTOCENTER`].
    #[inline]
    pub fn control_autocenter(autocenter: u16) -> Self {
        Self(InputEvent::new(
            EventType::FF,
            ff::Feature::AUTOCENTER.0,
            autocenter.into(),
        ))
    }

    #[inline]
    pub fn code(&self) -> Option<ForceFeedbackCode> {
        const FF_GAIN: u16 = ff::Feature::GAIN.0;
        const FF_AUTOCENTER: u16 = ff::Feature::AUTOCENTER.0;

        match self.raw_code() {
            id if id < FF_GAIN => Some(ForceFeedbackCode::ControlEffect(EffectId(id as i16))),
            FF_GAIN => Some(ForceFeedbackCode::SetGain),
            FF_AUTOCENTER => Some(ForceFeedbackCode::SetAutocenter),
            _ => None,
        }
    }
}

impl fmt::Debug for ForceFeedbackEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ForceFeedbackEvent")
            .field("time", &self.time())
            .field("code", &self.code())
            .field("value", &self.raw_value())
            .finish()
    }
}

/// Code of a [`ForceFeedbackEvent`].
///
/// Returned by [`ForceFeedbackEvent::code`].
///
/// Describes what the intent of the event is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ForceFeedbackCode {
    /// Controls the force feedback effect with the given [`EffectId`].
    ///
    /// This may be sent with an unassigned or invalid [`EffectId`]. Consumers should ignore events
    /// in that case.
    ///
    /// The event value controls whether the effect should play (0 or 1).
    ControlEffect(EffectId),

    /// Set the master gain of the device.
    ///
    /// The event value encodes the gain as a fraction of 65535.
    SetGain,

    /// Set the autocenter value of the device.
    ///
    /// The event value encodes the autocenter power as a fraction of 65535.
    SetAutocenter,
}

ffi_enum! {
    /// Multi-touch contact tool.
    ///
    /// This is used as the value of [`Abs::MT_TOOL_TYPE`] events.
    pub enum MtToolType: i32 {
        FINGER = 0x00,
        PEN    = 0x01,
        PALM   = 0x02,
        DIAL   = 0x0a,
    }
}

impl fmt::Debug for MtToolType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.variant_name() {
            Some(name) => write!(f, "MT_TOOL_{name}"),
            None => write!(f, "MtToolType({:#?})", self.0),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn timestamps() {
        const EV: InputEvent = InputEvent::zeroed();

        let epoch = EV.with_time(SystemTime::UNIX_EPOCH);
        assert_eq!(epoch.0.time.tv_sec, 0);
        assert_eq!(epoch.0.time.tv_usec, 0);

        // `timeval` stores a `time_t` and a `suseconds_t`; the latter is guaranteed to be signed and
        // capable of storing `-1`. We match the glibc behavior where we require the value to be in
        // the valid range; if it isn't, it is clamped.
        let mut negative_micros = EV;
        negative_micros.0.time.tv_usec = -1;
        assert_eq!(
            negative_micros.time(),
            SystemTime::UNIX_EPOCH,
            "should saturate to `UNIX_EPOCH`",
        );
        assert_eq!(
            negative_micros.time(),
            EV.with_time(SystemTime::UNIX_EPOCH).time(),
        );

        let mut before_epoch = EV;
        before_epoch.0.time.tv_sec = -1;
        assert_eq!(
            before_epoch.time(),
            SystemTime::UNIX_EPOCH - Duration::from_secs(1),
        );
        assert_eq!(
            before_epoch.time(),
            EV.with_time(SystemTime::UNIX_EPOCH - Duration::from_secs(1))
                .time()
        );

        let mut after_epoch = EV;
        after_epoch.0.time.tv_sec = 1_000_000;
        assert_eq!(
            after_epoch.time(),
            SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000),
        );
        assert_eq!(
            after_epoch.time(),
            EV.with_time(SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000))
                .time()
        );
    }
}