evdevil 0.4.1

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
use std::{
    io,
    iter::zip,
    thread,
    time::{Duration, SystemTime},
};

use evdevil::{
    Evdev,
    bits::{BitSet, BitValue},
    event::{
        Abs, AbsEvent, EventKind, EventType, InputEvent, Key, KeyEvent, KeyState, Led, LedEvent,
        Rel, RelEvent, Syn, SynEvent,
    },
    uinput::UinputDevice,
};

use crate::Tester;

/// Sends `events` to the `uinput` device, and asserts that they arrive at the `evdev`.
#[track_caller]
fn roundtrip_raw(t: &mut Tester, events: &[InputEvent]) -> io::Result<()> {
    roundtrip_raw2(t.evdev.as_ref().unwrap(), &mut t.uinput, events)
}

#[track_caller]
fn roundtrip_raw2(
    evdev: &Evdev,
    uinput: &mut UinputDevice,
    events: &[InputEvent],
) -> io::Result<()> {
    uinput.write(events)?;

    for expected in events {
        let recv = evdev.raw_events().next().unwrap()?;
        if !events_eq(&recv, expected) {
            panic!("expected {expected:?} in evdev, got {recv:?}");
        }
    }
    let syn = evdev.raw_events().next().unwrap()?;
    assert_eq!(
        syn.event_type(),
        EventType::SYN,
        "expected SYN from evdev, got {syn:?}"
    );

    Ok(())
}

#[track_caller]
fn roundtrip_echo(t: &mut Tester, events: &[InputEvent]) -> io::Result<()> {
    roundtrip_raw(t, events)?;

    assert!(t.uinput.is_readable()?);
    for expected in events {
        let recv = t.uinput.events().next().unwrap()?;
        if !events_eq(&recv, expected) {
            panic!("expected {expected:?} in uinput device, got {recv:?}");
        }
    }
    Ok(())
}

#[track_caller]
fn evdev2uinput(t: &mut Tester, events: &[InputEvent]) -> io::Result<()> {
    t.evdev_mut().write(events)?;

    if !events.is_empty() {
        assert!(t.uinput.is_readable()?);
    }
    for expected in events {
        let recv = t.uinput.events().next().unwrap()?;
        if !events_eq(&recv, expected) {
            panic!("expected {expected:?} in uinput device, got {recv:?}");
        }
    }
    if t.uinput.is_readable()? {
        panic!("found pending event: {:?}", t.uinput.events().next());
    }

    Ok(())
}

fn events_eq(recv: &InputEvent, expected: &InputEvent) -> bool {
    if recv.event_type() != expected.event_type() || recv.raw_code() != expected.raw_code() {
        return false;
    }

    // Value is ignored for SYN events
    if recv.event_type() != EventType::SYN && recv.raw_value() != expected.raw_value() {
        return false;
    }
    true
}

#[track_caller]
fn check_events(actual: &[InputEvent], expected: &[InputEvent]) {
    assert_eq!(
        actual.len(),
        expected.len(),
        "expected {} events, got {actual:?}",
        expected.len()
    );
    if !zip(actual.iter(), expected.iter()).all(|(a, b)| events_eq(a, b)) {
        panic!("expected {expected:?}, got {actual:?}");
    }
}

#[test]
fn test_evdev_is_readable() -> io::Result<()> {
    let tester = Tester::get();

    assert!(!tester.evdev().is_readable()?);

    let event = RelEvent::new(Rel::DIAL, -42).into();
    tester.uinput.write(&[event])?;

    tester.evdev().block_until_readable()?;
    assert!(tester.evdev().is_readable()?);
    tester.evdev().block_until_readable()?;

    let recv = tester.evdev().raw_events().next().unwrap()?;
    if !events_eq(&recv, &event) {
        panic!("expected {event:?}, got {recv:?}");
    }

    // `EV_SYN`
    assert!(tester.evdev().is_readable()?);
    let ev = tester.evdev().raw_events().next().unwrap()?;
    assert_eq!(ev.event_type(), EventType::SYN);

    assert!(
        !tester.evdev().is_readable()?,
        "unexpected pending event: {:?}",
        tester.evdev().raw_events().next()
    );

    Ok(())
}

/// Tests that pressing and releasing a single key updates the kernel-side state and lets us receive
/// the event.
#[test]
fn test_single_key_event() -> io::Result<()> {
    let mut tester = Tester::get();

    assert_eq!(tester.evdev().key_state()?, BitSet::new());

    roundtrip_raw(
        &mut tester,
        &[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::PRESSED).into()],
    )?;

    assert_eq!(
        tester.evdev().key_state()?,
        BitSet::from_iter([Key::BTN_TRIGGER_HAPPY1])
    );

    roundtrip_raw(
        &mut tester,
        &[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED).into()],
    )?;
    assert_eq!(tester.evdev().key_state()?, BitSet::new());

    Ok(())
}

#[test]
#[cfg_attr(target_os = "freebsd", ignore = "test broken on FreeBSD")]
fn test_led() -> io::Result<()> {
    let mut tester = Tester::get();

    // evdev -> uinput
    evdev2uinput(&mut tester, &[LedEvent::new(Led::CAPSL, true).into()])?;
    assert_eq!(tester.evdev().led_state()?, BitSet::from_iter([Led::CAPSL]));
    evdev2uinput(&mut tester, &[LedEvent::new(Led::CAPSL, false).into()])?;
    assert_eq!(tester.evdev().led_state()?, BitSet::new());

    thread::sleep(Duration::from_millis(50));
    assert!(!tester.evdev().is_readable()?);
    assert!(!tester.uinput.is_readable()?);

    // For some reason, the kernel will insert "LED on" and "LED off" events before the next event
    // the uinput device emits, so drain them here.
    tester.uinput.write(&[RelEvent::new(Rel::DIAL, 7).into()])?;
    let mut ev = Vec::new();
    while tester.evdev().is_readable()? {
        ev.push(tester.evdev().raw_events().next().unwrap()?);
    }
    eprintln!("draining events: {ev:?}");

    // uinput -> evdev
    // If `uinput` sends an LED or SND event, the kernel will also echo it back to the `uinput` device.
    // Note that the `uinput` event buffer does not make use of the `SYN_*` mechanism.
    tester
        .uinput
        .write(&[LedEvent::new(Led::CAPSL, true).into()])?;

    roundtrip_echo(&mut tester, &[LedEvent::new(Led::CAPSL, true).into()])?;
    assert_eq!(tester.evdev().led_state()?, BitSet::from_iter([Led::CAPSL]));
    roundtrip_echo(&mut tester, &[LedEvent::new(Led::CAPSL, false).into()])?;
    assert_eq!(tester.evdev().led_state()?, BitSet::new());

    thread::sleep(Duration::from_millis(50));
    assert!(!tester.evdev().is_readable()?);
    assert!(!tester.uinput.is_readable()?);

    Ok(())
}

#[test]
fn test_abs_events() -> io::Result<()> {
    let mut tester = Tester::get();

    roundtrip_raw(&mut tester, &[AbsEvent::new(Abs::BRAKE, 100).into()])?;
    assert_eq!(tester.evdev().abs_info(Abs::BRAKE)?.value(), 100);

    roundtrip_raw(&mut tester, &[AbsEvent::new(Abs::BRAKE, 0).into()])?;
    assert_eq!(tester.evdev().abs_info(Abs::BRAKE)?.value(), 0);

    Ok(())
}

/// Tests that `EventReader` will fetch the current device state when created, and that it will emit
/// synthetic events to synchronize whoever consumes those events.
#[test]
fn test_reader_init_sync() -> io::Result<()> {
    let mut t = Tester::get();

    // Press the key and make sure the `EventReader` emits an event to sync even if the event is not
    // in the kernel queue anymore.
    roundtrip_raw(
        &mut t,
        &[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::PRESSED).into()],
    )?;

    t.evdev().set_nonblocking(true)?;
    t.with_reader(|_, reader| {
        let events = reader.events().collect::<io::Result<Vec<_>>>()?;
        check_events(
            &events,
            &[
                *KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::PRESSED),
                Syn::REPORT.into(),
            ],
        );

        Ok(())
    })?;
    t.evdev().set_nonblocking(false)?;

    roundtrip_raw(
        &mut t,
        &[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED).into()],
    )?;

    Ok(())
}

/// Events to send to overflow the kernel buffer.
///
/// On Linux, the buffer appears to be around 128 events large.
/// Send an odd number of events to ensure any power-of-two shenanigans are partially filled.
const OVERFLOW_COUNT: usize = 555;

/// Tests that the kernel buffer will overflow and drop events as expected.
#[test]
fn test_overflow() -> io::Result<()> {
    let tester = Tester::get();

    // Use REL events, since they don't get processed, deduplicated, or ignored by the kernel.
    // Kernel buffer appears to be around 128 events large.
    let events = vec![RelEvent::new(Rel::DIAL, 1).into(); OVERFLOW_COUNT];
    tester.uinput.write(&events)?;

    assert!(tester.evdev().is_readable()?);

    tester.evdev().set_nonblocking(true)?;
    let mut count = 0;
    loop {
        let event = match tester.evdev().raw_events().next().unwrap() {
            Ok(e) => e,
            Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                panic!("kernel buffer did not overflow (received {count} events)")
            }
            Err(e) => return Err(e),
        };
        match event.kind() {
            EventKind::Rel(e) if e.rel() == Rel::DIAL => count += 1,
            EventKind::Syn(e) if e.syn() == Syn::REPORT => count += 1,
            EventKind::Syn(e) if e.syn() == Syn::DROPPED => {
                println!("{count} events before SYN_DROPPED");
                assert!(count < OVERFLOW_COUNT);
                break;
            }
            _ => {
                panic!("unexpected event after {count} expected ones: {event:?}");
            }
        }
    }

    for res in tester.evdev().raw_events() {
        match res {
            Ok(_) => {}
            Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
            Err(e) => return Err(e),
        }
    }
    tester.evdev().set_nonblocking(false)?;

    Ok(())
}

/// Tests that `EventReader` will correctly resync when events are dropped.
#[test]
fn test_overflow_resync() -> io::Result<()> {
    let mut tester = Tester::get();

    // Start out with the key pressed.
    roundtrip_raw(
        &mut tester,
        &[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::PRESSED).into()],
    )?;

    tester.with_reader(|uinput, reader| {
        assert_eq!(
            reader.evdev().key_state()?,
            BitSet::from_iter([Key::BTN_TRIGGER_HAPPY1])
        );

        // Discard the initial events.
        reader.update()?;

        // Release the key without overflowing.
        uinput.write(&[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED).into()])?;
        // Update via next
        reader.evdev().set_nonblocking(true)?;
        let events = reader.events().collect::<io::Result<Vec<_>>>()?;
        check_events(
            &events,
            &[
                *KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED),
                *SynEvent::new(Syn::REPORT),
            ],
        );
        reader.evdev().set_nonblocking(false)?;
        assert_eq!(reader.evdev().key_state()?, BitSet::new());
        assert_eq!(reader.key_state(), &BitSet::new());

        // Press the key without overflowing.
        uinput.write(&[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::PRESSED).into()])?;
        // Update via `update`
        reader.update()?;
        assert_eq!(
            reader.evdev().key_state()?,
            BitSet::from_iter([Key::BTN_TRIGGER_HAPPY1])
        );
        assert_eq!(
            reader.key_state(),
            &BitSet::from_iter([Key::BTN_TRIGGER_HAPPY1])
        );

        // Overflow the buffer, release the key, then overflow it again.
        let events = vec![RelEvent::new(Rel::DIAL, 1).into(); OVERFLOW_COUNT];
        uinput.write(&events)?;

        uinput.write(&[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED).into()])?;

        let events = vec![RelEvent::new(Rel::DIAL, 1).into(); OVERFLOW_COUNT];
        uinput.write(&events)?;

        // Kernel key state should now be released:
        assert_eq!(reader.evdev().key_state()?, BitSet::new());
        // But reader key state should still be pressed:
        assert_eq!(
            reader.key_state(),
            &BitSet::from_iter([Key::BTN_TRIGGER_HAPPY1])
        );

        // Now use the reader to pull events until we see the key press.
        log::info!("waiting until we see the key press (should complete instantly)");
        reader.evdev().set_nonblocking(true)?;
        let mut ev = Vec::new();
        for res in &mut *reader {
            let event = match res {
                Ok(ev) => ev,
                Err(e) => {
                    reader.evdev().set_nonblocking(false)?;
                    return Err(e);
                }
            };
            if event.event_type() == EventType::REL || event.event_type() == EventType::SYN {
                continue;
            }
            ev.push(event);
        }
        reader.evdev().set_nonblocking(false)?;
        check_events(
            &ev,
            &[*KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED)],
        );

        // All events should have non-zero timestamps that monotonically increase
        for win in ev.windows(2) {
            let [a, b] = win else { panic!() };
            assert_ne!(a.time(), SystemTime::UNIX_EPOCH);
            assert_ne!(b.time(), SystemTime::UNIX_EPOCH);
            assert!(a.time() <= b.time());
        }

        // Reader key state should now be up-to-date
        assert_eq!(reader.key_state(), &BitSet::new());

        // Empty the rest of the kernel buffer.
        reader.update()?;

        Ok(())
    })?;

    Ok(())
}

#[test]
#[cfg_attr(
    target_os = "freebsd",
    ignore = "event masks are not supported on FreeBSD"
)]
fn test_event_mask_state() -> io::Result<()> {
    let mut tester = Tester::get();

    let event_mask = tester.evdev().event_mask()?;

    tester.evdev_mut().set_event_mask(&BitSet::new())?;

    tester
        .uinput
        .write(&[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::PRESSED).into()])?;
    let keys = tester.evdev().key_state()?;
    assert_eq!(keys, BitSet::from_iter([Key::BTN_TRIGGER_HAPPY1]));

    tester
        .uinput
        .write(&[KeyEvent::new(Key::BTN_TRIGGER_HAPPY1, KeyState::RELEASED).into()])?;

    tester.evdev_mut().set_event_mask(&event_mask)?;
    Ok(())
}

#[test]
#[cfg_attr(
    target_os = "freebsd",
    ignore = "event masks are not supported on FreeBSD"
)]
fn test_event_mask() -> io::Result<()> {
    let mut tester = Tester::get();

    let event_mask = tester.evdev().event_mask()?;

    tester
        .evdev_mut()
        .set_event_mask(&BitSet::from_iter([EventType::REL]))?;
    assert_eq!(
        tester.evdev().event_mask()?,
        BitSet::from_iter([EventType::REL]),
    );

    roundtrip_raw(&mut tester, &[RelEvent::new(Rel::DIAL, 1).into()])?;

    tester.evdev_mut().set_event_mask(&BitSet::new())?;
    assert_eq!(tester.evdev().event_mask()?, BitSet::new());
    assert!(!tester.evdev().is_readable()?);

    // `REL_DIAL` events shouldn't arrive.
    tester.uinput.write(&[RelEvent::new(Rel::DIAL, 1).into()])?;
    assert!(!tester.evdev().is_readable()?);

    tester.evdev_mut().set_event_mask(&event_mask)?;
    Ok(())
}

#[test]
#[cfg_attr(
    target_os = "freebsd",
    ignore = "event masks are not supported on FreeBSD"
)]
fn test_rel_mask() -> io::Result<()> {
    let mut tester = Tester::get();

    let rel_mask = tester.evdev().rel_mask()?;
    assert!((0..=Rel::MAX.raw()).all(|rel| rel_mask.contains(Rel::from_raw(rel))));

    tester
        .evdev_mut()
        .set_rel_mask(&BitSet::from_iter([Rel::DIAL]))?;
    assert_eq!(tester.evdev().rel_mask()?, BitSet::from_iter([Rel::DIAL]),);

    roundtrip_raw(&mut tester, &[RelEvent::new(Rel::DIAL, 1).into()])?;

    tester.evdev_mut().set_rel_mask(&BitSet::new())?;
    assert_eq!(tester.evdev().rel_mask()?, BitSet::new());
    assert!(!tester.evdev().is_readable()?);

    // `REL_DIAL` events shouldn't arrive.
    tester.uinput.write(&[RelEvent::new(Rel::DIAL, 1).into()])?;
    assert!(!tester.evdev().is_readable()?);

    tester.evdev_mut().set_rel_mask(&rel_mask)?;
    Ok(())
}

#[test]
fn reader_reports_clear() -> io::Result<()> {
    let mut tester = Tester::get();

    tester.evdev().set_nonblocking(true)?;
    tester.with_reader(|uinput, reader| {
        uinput.write(&[RelEvent::new(Rel::DIAL, 1).into()])?;
        let report = reader.reports().next().unwrap()?;
        let events = report.iter().collect::<Vec<InputEvent>>();
        assert_eq!(report.len(), events.len());
        check_events(
            &events,
            &[RelEvent::new(Rel::DIAL, 1).into(), Syn::REPORT.into()],
        );

        // Fetching a report should remove its events from the queue, so there should be no more
        // reports now.
        assert!(reader.reports().next().is_none());

        // Using the events iterator should also not yield the old events anymore.
        let ev = reader.events().next();
        assert!(ev.is_none(), "{ev:?}");

        Ok(())
    })?;
    tester.evdev().set_nonblocking(false)?;
    Ok(())
}

/// Tests that multiple `Report`s can coexist and contain the right data.
#[test]
fn reader_reports_collect() -> io::Result<()> {
    let mut tester = Tester::get();

    tester.evdev().set_nonblocking(true)?;
    tester.with_reader(|uinput, reader| {
        uinput.write(&[RelEvent::new(Rel::DIAL, 1).into()])?;
        uinput.write(&[RelEvent::new(Rel::DIAL, 2).into()])?;
        let reports = reader.reports().collect::<io::Result<Vec<_>>>()?;
        assert_eq!(reports.len(), 2);
        check_events(
            &reports[0].iter().by_ref().collect::<Vec<_>>(),
            &[RelEvent::new(Rel::DIAL, 1).into(), Syn::REPORT.into()],
        );
        check_events(
            &reports[1].iter().by_ref().collect::<Vec<_>>(),
            &[RelEvent::new(Rel::DIAL, 2).into(), Syn::REPORT.into()],
        );

        Ok(())
    })?;
    tester.evdev().set_nonblocking(false)?;
    Ok(())
}

/// Tests that we can create and drop the `Events` iterator without affecting the `EventReader`.
#[test]
fn reader_events_stateless() -> io::Result<()> {
    let mut tester = Tester::get();

    tester.with_reader(|uinput, reader| {
        reader.evdev().set_nonblocking(true)?;
        let mut events = reader.events();

        let next = events.next();
        assert!(next.is_none(), "{next:?}");

        uinput.write(&[
            RelEvent::new(Rel::DIAL, 1).into(),
            RelEvent::new(Rel::DIAL, 2).into(),
        ])?;

        // `Events` instance was created before the events were written.
        check_events(
            &[events.next().expect("expected `REL_DIAL`")?],
            &[RelEvent::new(Rel::DIAL, 1).into()],
        );

        let mut events = reader.events();
        check_events(
            &[events.next().expect("expected `REL_DIAL`")?],
            &[RelEvent::new(Rel::DIAL, 2).into()],
        );
        check_events(
            &[events.next().expect("expected `SYN_REPORT`")?],
            &[Syn::REPORT.into()],
        );

        reader.evdev().set_nonblocking(false)?;
        Ok(())
    })
}