Pi66

pind
Simple Rust hotkey daemon.
git clone https://git.pi66.xyz/pind

libs/evdev/src/event_variants.rs

  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
//! The event_variants module contains new-type wrappers around [`InputEvent`]
//! for each known [`EventType`].
//!
//! These event variants guarantee that the underlying `InputEvent` has the
//! corresponding type. They may also contain additional methods for the
//! specific type and convenient shortcut methods for event creation.
//! An `InputEvent` can be converted to the corresponding event variant with
//! the [`InputEvent::destructure()`] method. Each event variant implements
//! `Into<InputEvent>` and `Deref<Target=InputEvent>` for easy back conversion.

use std::fmt;
use std::ops::Deref;
use std::time::SystemTime;

use crate::compat::input_event;
use crate::constants::{
    AbsoluteAxisCode, FFStatusCode, LedCode, MiscCode, OtherCode, PowerCode, RelativeAxisCode,
    RepeatCode, SoundCode, SwitchCode, SynchronizationCode, UInputCode,
};
use crate::scancodes::KeyCode;
use crate::{systime_to_timeval, EventType, FFEffectCode};
use crate::{EventSummary, InputEvent};

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// A bookkeeping event. Usually not important to applications.
/// [`EventType::SYNCHRONIZATION`]
pub struct SynchronizationEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::KEY`]
pub struct KeyEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::RELATIVE`]
pub struct RelativeAxisEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::ABSOLUTE`]
pub struct AbsoluteAxisEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::MISC`]
pub struct MiscEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::SWITCH`]
pub struct SwitchEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::LED`]
pub struct LedEvent(InputEvent);
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::SOUND`]
pub struct SoundEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::REPEAT`]
pub struct RepeatEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::FORCEFEEDBACK`]
pub struct FFEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::POWER`]
pub struct PowerEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::FORCEFEEDBACKSTATUS`]
pub struct FFStatusEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// [`EventType::UINPUT`]
pub struct UInputEvent(InputEvent);

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
/// An event not covered by any other variant.
pub struct OtherEvent(pub(crate) InputEvent);

macro_rules! input_event_newtype {
    ($name:ty) => {
        impl AsRef<input_event> for $name {
            fn as_ref(&self) -> &input_event {
                &self.0.as_ref()
            }
        }
        impl AsRef<InputEvent> for $name {
            fn as_ref(&self) -> &InputEvent {
                &self.0
            }
        }
        // never implement the other direction!
        impl From<$name> for InputEvent {
            fn from(event: $name) -> Self {
                event.0
            }
        }
        impl Deref for $name {
            type Target = InputEvent;
            fn deref(&self) -> &InputEvent {
                &self.0
            }
        }
    };
    (<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi>:</mi><mi>t</mi><mi>y</mi><mo>&#x0002C;</mo></mrow></math>evdev_type:path, $kind:path) => {
        impl $name {
            pub fn new(<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>k</mi><mi>i</mi><mi>n</mi><mi>d</mi><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>o</mi><mi>d</mi><mi>e</mi><mo stretchy="false">&#x00029;</mo><mi>:</mi></mrow></math>kind, value: i32) -> Self {
                let raw = input_event {
                    time: libc::timeval {
                        tv_sec: 0,
                        tv_usec: 0,
                    },
                    type_: $evdev_type.0,
                    code,
                    value,
                };
                Self::from_raw(raw)
            }
            pub fn new_now(<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>k</mi><mi>i</mi><mi>n</mi><mi>d</mi><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>o</mi><mi>d</mi><mi>e</mi><mo stretchy="false">&#x00029;</mo><mi>:</mi></mrow></math>kind, value: i32) -> Self {
                let raw = input_event {
                    time: systime_to_timeval(&SystemTime::now()),
                    type_: $evdev_type.0,
                    code,
                    value,
                };
                Self::from_raw(raw)
            }
            pub fn destructure(&self) -> ($kind, i32) {
                (self.code(), self.value())
            }
            pub fn code(&self) -> $kind {
                $kind(self.0.code())
            }
            // must be kept internal
            fn from_raw(raw: input_event) -> Self {
                match EventType(raw.type_) {
                    $evdev_type => Self(InputEvent(raw)),
                    _ => unreachable!(),
                }
            }
            // must be kept internal
            pub(crate) fn from_event(event: InputEvent) -> Self {
                match event.event_type() {
                    $evdev_type => Self(event),
                    _ => unreachable!(),
                }
            }
        }
        impl fmt::Debug for $name {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                let mut debug = f.debug_struct(stringify!($name));
                debug.field("time", &self.timestamp());
                debug.field("code", &self.code());
                debug.field("value", &self.value()).finish()
            }
        }
        input_event_newtype!($name);
    };
    (<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi>:</mi><mi>t</mi><mi>y</mi><mo>&#x0002C;</mo></mrow></math>evdev_type:path, <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>k</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>:</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>h</mi><mo>&#x0002C;</mo></mrow></math>summary:path) => {
        impl From<$name> for EventSummary {
            fn from(event: $name) -> EventSummary {
                let (kind, value) = event.destructure();
                $summary(event, kind, value)
            }
        }

        input_event_newtype!(<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>&#x0002C;</mo></mrow></math>evdev_type, $kind);
    };
}
input_event_newtype!(
    SynchronizationEvent,
    EventType::SYNCHRONIZATION,
    SynchronizationCode,
    EventSummary::Synchronization
);
input_event_newtype!(KeyEvent, EventType::KEY, KeyCode, EventSummary::Key);
input_event_newtype!(
    RelativeAxisEvent,
    EventType::RELATIVE,
    RelativeAxisCode,
    EventSummary::RelativeAxis
);
input_event_newtype!(
    AbsoluteAxisEvent,
    EventType::ABSOLUTE,
    AbsoluteAxisCode,
    EventSummary::AbsoluteAxis
);
input_event_newtype!(MiscEvent, EventType::MISC, MiscCode, EventSummary::Misc);
input_event_newtype!(
    SwitchEvent,
    EventType::SWITCH,
    SwitchCode,
    EventSummary::Switch
);
input_event_newtype!(LedEvent, EventType::LED, LedCode, EventSummary::Led);
input_event_newtype!(SoundEvent, EventType::SOUND, SoundCode, EventSummary::Sound);
input_event_newtype!(
    RepeatEvent,
    EventType::REPEAT,
    RepeatCode,
    EventSummary::Repeat
);
input_event_newtype!(
    FFEvent,
    EventType::FORCEFEEDBACK,
    FFEffectCode,
    EventSummary::ForceFeedback
);
input_event_newtype!(PowerEvent, EventType::POWER, PowerCode, EventSummary::Power);
input_event_newtype!(
    FFStatusEvent,
    EventType::FORCEFEEDBACKSTATUS,
    FFStatusCode,
    EventSummary::ForceFeedbackStatus
);
input_event_newtype!(
    UInputEvent,
    EventType::UINPUT,
    UInputCode,
    EventSummary::UInput
);
input_event_newtype!(OtherEvent);

impl OtherEvent {
    pub fn kind(&self) -> OtherCode {
        OtherCode(self.event_type().0, self.code())
    }
    pub fn destructure(&self) -> (OtherCode, i32) {
        (self.kind(), self.value())
    }
}
impl From<OtherEvent> for EventSummary {
    fn from(event: OtherEvent) -> Self {
        let (kind, value) = event.destructure();
        EventSummary::Other(event, kind, value)
    }
}
impl fmt::Debug for OtherEvent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut debug = f.debug_struct("OtherEvent");
        debug.field("time", &self.timestamp());
        debug.field("type", &self.event_type());
        debug.field("code", &self.code());
        debug.field("value", &self.value()).finish()
    }
}