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
use crate::{constants::*, raw_stream::RawDevice};
use crate::{AttributeSet, AttributeSetRef, InputEvent, InputEventKind, Key};
use std::time::SystemTime;
#[derive(Debug)]
pub struct DeviceState {
pub(crate) timestamp: SystemTime,
pub(crate) key_vals: Option<AttributeSet<Key>>,
pub(crate) abs_vals: Option<Box<[libc::input_absinfo; AbsoluteAxisType::COUNT]>>,
pub(crate) switch_vals: Option<AttributeSet<SwitchType>>,
pub(crate) led_vals: Option<AttributeSet<LedType>>,
}
impl Clone for DeviceState {
fn clone(&self) -> Self {
Self {
timestamp: self.timestamp,
key_vals: self.key_vals.clone(),
abs_vals: self.abs_vals.clone(),
switch_vals: self.switch_vals.clone(),
led_vals: self.led_vals.clone(),
}
}
fn clone_from(&mut self, other: &Self) {
self.timestamp.clone_from(&other.timestamp);
self.key_vals.clone_from(&other.key_vals);
self.abs_vals.clone_from(&other.abs_vals);
self.switch_vals.clone_from(&other.switch_vals);
self.led_vals.clone_from(&other.led_vals);
}
}
impl DeviceState {
pub(crate) fn new(device: &RawDevice) -> Self {
let supports = device.supported_events();
let key_vals = if supports.contains(EventType::KEY) {
Some(AttributeSet::new())
} else {
None
};
let abs_vals = if supports.contains(EventType::ABSOLUTE) {
Some(Box::new(crate::raw_stream::ABS_VALS_INIT))
} else {
None
};
let switch_vals = if supports.contains(EventType::SWITCH) {
Some(AttributeSet::new())
} else {
None
};
let led_vals = if supports.contains(EventType::LED) {
Some(AttributeSet::new())
} else {
None
};
DeviceState {
timestamp: std::time::UNIX_EPOCH,
key_vals,
abs_vals,
switch_vals,
led_vals,
}
}
pub fn timestamp(&self) -> SystemTime {
self.timestamp
}
pub fn key_vals(&self) -> Option<&AttributeSetRef<Key>> {
self.key_vals.as_deref()
}
pub fn abs_vals(&self) -> Option<&[libc::input_absinfo]> {
self.abs_vals.as_deref().map(|v| &v[..])
}
pub fn switch_vals(&self) -> Option<&AttributeSetRef<SwitchType>> {
self.switch_vals.as_deref()
}
pub fn led_vals(&self) -> Option<&AttributeSetRef<LedType>> {
self.led_vals.as_deref()
}
#[inline]
pub(crate) fn process_event(&mut self, ev: InputEvent) {
match ev.kind() {
InputEventKind::Key(code) => {
let keys = self
.key_vals
.as_deref_mut()
.expect("got a key event despite not supporting keys");
keys.set(code, ev.value() != 0);
}
InputEventKind::AbsAxis(axis) => {
let axes = self
.abs_vals
.as_deref_mut()
.expect("got an abs event despite not supporting absolute axes");
axes[axis.0 as usize].value = ev.value();
}
_ => {}
}
}
}