linux-input 0.1.0

Rust bindings to Linux's input subsystems
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
#[derive(Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawDeviceId {
    pub bus: u16,
    pub vendor: u16,
    pub product: u16,
    pub version: u16
}

#[derive(Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawAbsInfo {
    /// Latest reported value for the axis.
    pub value: i32,
    /// Minimum value for the axis.
    pub minimum: i32,
    /// Maximum value for the axis.
    pub maximum: i32,
    /// Threshold for noise filtering.
    pub noise_threshold: i32,
    /// Deadzone.
    pub deadzone: i32,
    /// Resolution of the reported values.
    pub resolution: i32
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackTrigger {
    pub button: u16,
    pub interval: u16
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackReplay {
    pub length: u16,
    pub delay: u16
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackEnvelope {
    pub attack_length: u16,
    pub attack_level: u16,
    pub fade_length: u16,
    pub fade_level: u16
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackConstantEffect {
    pub level: u16,
    pub envelope: RawForceFeedbackEnvelope
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackRampEffect {
    pub start_level: i16,
    pub end_level: u16,
    pub envelope: RawForceFeedbackEnvelope
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackPeriodicEffect {
    pub waveform: u16,
    pub period: u16,
    pub magnitude: i16,
    pub offset: i16,
    pub phase: u16,
    pub envelope: RawForceFeedbackEnvelope,

    pub custom_length: u32,
    pub custom_data: *mut i16
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackConditionEffect {
    pub right_saturation: u16,
    pub left_saturation: u16,
    pub right_coefficient: i16,
    pub left_coefficient: i16,
    pub deadband: u16,
    pub center: i16
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct RawForceFeedbackRumbleEffect {
    pub strong_magnitude: u16,
    pub weak_magnitude: u16
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union RawForceFeedbackBody {
    pub constant: RawForceFeedbackConstantEffect,
    pub ramp: RawForceFeedbackRampEffect,
    pub periodic: RawForceFeedbackPeriodicEffect,
    pub condition: [RawForceFeedbackConditionEffect; 2],
    pub rumble: RawForceFeedbackRumbleEffect
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct RawForceFeedbackEffect {
    pub kind: u16,
    pub id: i16,
    pub direction: u16,
    pub trigger: RawForceFeedbackTrigger,
    pub replay: RawForceFeedbackReplay,
    pub body: RawForceFeedbackBody
}

pub const FF_RUMBLE: u16 = 0x50;
#[allow(dead_code)]
pub const FF_PERIODIC: u16 = 0x51;
#[allow(dead_code)]
pub const FF_CONSTANT: u16 = 0x52;
#[allow(dead_code)]
pub const FF_SPRING: u16 = 0x53;
#[allow(dead_code)]
pub const FF_FRICTION: u16 = 0x54;
#[allow(dead_code)]
pub const FF_DAMPER: u16 = 0x55;
#[allow(dead_code)]
pub const FF_INERTIA: u16 = 0x56;
#[allow(dead_code)]
pub const FF_RAMP: u16 = 0x57;

pub const FF_GAIN: u16 = 0x60;
#[allow(dead_code)]
pub const FF_AUTOCENTER: u16 = 0x61;

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct Timestamp {
    pub sec: libc::time_t,
    pub usec: libc::suseconds_t
}

impl Timestamp {
    /// Reads the current timestamp using the CLOCK_MONOTONIC source.
    pub fn get() -> Result< Self, std::io::Error > {
        let mut ts = libc::timespec {
            tv_sec: 0,
            tv_nsec: 0
        };

        let result = unsafe {
            libc::clock_gettime( libc::CLOCK_MONOTONIC, &mut ts )
        };

        if result < 0 {
            Err( std::io::Error::last_os_error() )
        } else {
            Ok( Timestamp {
                sec: ts.tv_sec,
                usec: ts.tv_nsec / 1000
            })
        }
    }

    pub fn as_f64( self ) -> f64 {
        self.sec as f64 + self.usec as f64 / 1000_000.0
    }
}

impl std::ops::Sub for Timestamp {
    type Output = std::time::Duration;
    fn sub( self, rhs: Timestamp ) -> Self::Output {
        std::time::Duration::new( self.sec as _, self.usec as _ ) - std::time::Duration::new( rhs.sec as _, rhs.usec as _ )
    }
}

#[derive(Clone, PartialEq, Eq, Default)]
#[repr(C)]
pub struct RawInputEvent {
    pub timestamp: Timestamp,
    pub kind: u16,
    pub code: u16,
    pub value: i32
}

define_enum! {
    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
    enum EventKind {
        Other( u16 ),
        Synchronization     = 0x00,
        Key                 = 0x01,
        RelativeAxis        = 0x02,
        AbsoluteAxis        = 0x03,
        Misc                = 0x04,
        Switch              = 0x05,
        LED                 = 0x11,
        Sound               = 0x12,
        AutoRepeat          = 0x14,
        ForceFeedback       = 0x15,
        Power               = 0x16,
        ForceFeedbackStatus = 0x17
    }
}

define_enum! {
    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
    enum Key {
        Other( u16 ),
        Escape = 1,
        Digit1 = 2,
        Digit2 = 3,
        Digit3 = 4,
        Digit4 = 5,
        Digit5 = 6,
        Digit6 = 7,
        Digit7 = 8,
        Digit8 = 9,
        Digit9 = 10,
        Digit0 = 11,
        Minus = 12,
        Equal = 13,
        Backspace = 14,
        Tab = 15,
        Q = 16,
        W = 17,
        E = 18,
        R = 19,
        T = 20,
        Y = 21,
        U = 22,
        I = 23,
        O = 24,
        P = 25,
        LeftBrace = 26,
        RightBrace = 27,
        Enter = 28,
        LeftCtrl = 29,
        A = 30,
        S = 31,
        D = 32,
        F = 33,
        G = 34,
        H = 35,
        J = 36,
        K = 37,
        L = 38,
        Semicolon = 39,
        Apostrophe = 40,
        Grave = 41,
        LeftShift = 42,
        Backslash = 43,
        Z = 44,
        X = 45,
        C = 46,
        V = 47,
        B = 48,
        N = 49,
        M = 50,
        Comma = 51,
        Dot = 52,
        Slash = 53,
        RightShift = 54,
        KeypadAsterisk = 55,
        LeftAlt = 56,
        Space = 57,
        CapsLock = 58,
        F1 = 59,
        F2 = 60,
        F3 = 61,
        F4 = 62,
        F5 = 63,
        F6 = 64,
        F7 = 65,
        F8 = 66,
        F9 = 67,
        F10 = 68,
        NumLock = 69,
        ScrollLock = 70,
        Keypad7 = 71,
        Keypad8 = 72,
        Keypad9 = 73,
        KeypadMinus = 74,
        Keypad4 = 75,
        Keypad5 = 76,
        Keypad6 = 77,
        KeypadPlus = 78,
        Keypad1 = 79,
        Keypad2 = 80,
        Keypad3 = 81,
        Keypad0 = 82,
        KeypadDot = 83,
        F11 = 87,
        F12 = 88,
        KeypadEnter = 96,
        RightCtrl = 97,
        KeypadSlash = 98,
        SysRq = 99,
        RightAlt = 100,
        Home = 102,
        Up = 103,
        PageUp = 104,
        Left = 105,
        Right = 106,
        End = 107,
        Down = 108,
        PageDown = 109,
        Insert = 110,
        Delete = 111,
        KeypadEqual = 117,
        KeypadPlusMinus = 118,
        Pause = 119,
        KeypadComma = 121,
        LeftMeta = 125,
        RightMeta = 126,
        MouseLeft = 0x110,
        MouseRight = 0x111,
        MouseMiddle = 0x112,
        MouseExtra1 = 0x113,
        MouseExtra2 = 0x114,
        MouseExtra3 = 0x115,
        MouseExtra4 = 0x116,
        MouseExtra5 = 0x117,

        // https://www.kernel.org/doc/html/v4.15/input/gamepad.html
        PadSouth = 0x130,
        PadEast = 0x131,
        PadNorth = 0x133,
        PadWest = 0x134,
        ShoulderLeft = 0x136,
        ShoulderRight = 0x137,
        ShoulderLeftLower = 0x138,
        ShoulderRightLower = 0x139,
        Select = 0x13a,
        Start = 0x13b,
        HomeButton = 0x13c,
        StickLeft = 0x13d,
        StickRight = 0x13e,
        PadUp = 0x220,
        PadDown = 0x221,
        PadLeft = 0x222,
        PadRight = 0x223,

        ButtonMisc = 0x100,
        TriggerHappy = 0x2c0
    }
}

define_enum! {
    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
    enum RelativeAxis {
        Other( u16 ),
        X = 0,
        Y = 1,
        Z = 2,
        RX = 3,
        RY = 4,
        RZ = 5,
        Wheel = 8,
        WheelHiRes = 11
    }
}

define_enum! {
    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
    enum AbsoluteAxis {
        Other( u16 ),
        X = 0,
        Y = 1,
        Z = 2,
        RX = 3,
        RY = 4,
        RZ = 5,
        Hat0X = 16,
        Hat0Y = 17,
        Hat1X = 18,
        Hat1Y = 19,
        Hat2X = 20,
        Hat2Y = 21,
        Misc = 40
    }
}

define_enum! {
    // Source: linux/input.h
    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
    enum Bus {
        Other( u16 ),
        PCI = 0x01,
        USB = 0x03,
        HIL = 0x04,
        Bluetooth = 0x05,
        Virtual = 0x06,
        ISA = 0x10,
        Host = 0x19
    }
}

define_enum! {
    #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
    enum ForceFeedback {
        Other( u16 ),
        Rumble = 0x50
    }
}

ioctl_write_int!( evdev_grab_or_release, b'E', 0x90 );
ioctl_read!( evdev_get_id, b'E', 0x02, RawDeviceId );
ioctl_write_ptr!( evdev_set_clock_id, b'E', 0xa0, libc::c_int );

ioctl_write_ptr!( evdev_start_force_feedback, b'E', 0x80, RawForceFeedbackEffect );
ioctl_write_int!( evdev_stop_force_feedback, b'E', 0x81 );
ioctl_read!( evdev_get_maximum_simultaneous_force_feedback_effect_count, b'E', 0x84, libc::c_int );

pub unsafe fn evdev_get_event_bits( fd: libc::c_int, kind: EventKind, data: *mut u8, length: usize ) -> nix::Result< libc::c_int > {
    let result = libc::ioctl( fd, request_code_read!( b'E', 0x20 + kind.raw() as usize, length ), data );
    nix::errno::Errno::result( result )
}

pub unsafe fn evdev_get_abs_info( fd: libc::c_int, axis: AbsoluteAxis ) -> nix::Result< RawAbsInfo > {
    let mut abs_info = std::mem::MaybeUninit::uninit();
    let result = libc::ioctl( fd, request_code_read!( b'E', 0x40 + axis.raw() as usize, std::mem::size_of::< RawAbsInfo >() ), abs_info.as_mut_ptr() );
    if result < 0 {
        return Err( nix::errno::Errno::last().into() );
    }

    Ok( abs_info.assume_init() )
}