Debian Codegraph @

src/device_state.rs

raw symbols SCIP
1 use crate::compat::input_absinfo;
2 use crate::{constants::*, raw_stream::RawDevice};
3 use crate::{AttributeSet, AttributeSetRef, EventSummary, InputEvent, KeyCode};
4 use std::time::SystemTime;
5
6 /// A **cached** representation of device state at a certain time.
7 #[derive(Debug)]
8 pub struct DeviceState {
9 /// The state corresponds to kernel state at this timestamp.
10 pub(crate) timestamp: SystemTime,
11 /// Set = key pressed
12 pub(crate) key_vals: Option<AttributeSet<KeyCode>>,
13 pub(crate) abs_vals: Option<Box<[input_absinfo; AbsoluteAxisCode::COUNT]>>,
14 /// Set = switch enabled (closed)
15 pub(crate) switch_vals: Option<AttributeSet<SwitchCode>>,
16 /// Set = LED lit
17 pub(crate) led_vals: Option<AttributeSet<LedCode>>,
18 }
19
20 // manual Clone impl for clone_from optimization
21 impl Clone for DeviceState {
22 fn clone(&self) -> Self {
23 Self {
24 timestamp: self.timestamp,
25 key_vals: self.key_vals.clone(),
26 abs_vals: self.abs_vals.clone(),
27 switch_vals: self.switch_vals.clone(),
28 led_vals: self.led_vals.clone(),
29 }
30 }
31 fn clone_from(&mut self, other: &Self) {
32 self.timestamp.clone_from(&other.timestamp);
33 self.key_vals.clone_from(&other.key_vals);
34 self.abs_vals.clone_from(&other.abs_vals);
35 self.switch_vals.clone_from(&other.switch_vals);
36 self.led_vals.clone_from(&other.led_vals);
37 }
38 }
39
40 impl DeviceState {
41 /// Create an empty `DeviceState`. The `{abs,key,etc}_vals` for the returned state will return
42 /// `Some` if `supported_events()` contains that `EventType`.
43 pub(crate) fn new(device: &RawDevice) -> Self {
44 let supports = device.supported_events();
45
46 let key_vals = if supports.contains(EventType::KEY) {
47 Some(AttributeSet::new())
48 } else {
49 None
50 };
51 let abs_vals = if supports.contains(EventType::ABSOLUTE) {
52 Some(Box::new(crate::raw_stream::ABS_VALS_INIT))
53 } else {
54 None
55 };
56 let switch_vals = if supports.contains(EventType::SWITCH) {
57 Some(AttributeSet::new())
58 } else {
59 None
60 };
61 let led_vals = if supports.contains(EventType::LED) {
62 Some(AttributeSet::new())
63 } else {
64 None
65 };
66
67 DeviceState {
68 timestamp: std::time::UNIX_EPOCH,
69 key_vals,
70 abs_vals,
71 switch_vals,
72 led_vals,
73 }
74 }
75 /// Returns the time when this snapshot was taken.
76 pub fn timestamp(&self) -> SystemTime {
77 self.timestamp
78 }
79
80 /// Returns the set of keys pressed when the snapshot was taken.
81 ///
82 /// Returns `None` if keys are not supported by this device.
83 pub fn key_vals(&self) -> Option<&AttributeSetRef<KeyCode>> {
84 self.key_vals.as_deref()
85 }
86
87 /// Returns the set of absolute axis measurements when the snapshot was taken.
88 ///
89 /// Returns `None` if not supported by this device.
90 pub fn abs_vals(&self) -> Option<&[input_absinfo]> {
91 self.abs_vals.as_deref().map(|v| &v[..])
92 }
93
94 /// Returns the set of switches triggered when the snapshot was taken.
95 ///
96 /// Returns `None` if switches are not supported by this device.
97 pub fn switch_vals(&self) -> Option<&AttributeSetRef<SwitchCode>> {
98 self.switch_vals.as_deref()
99 }
100
101 /// Returns the set of LEDs turned on when the snapshot was taken.
102 ///
103 /// Returns `None` if LEDs are not supported by this device.
104 pub fn led_vals(&self) -> Option<&AttributeSetRef<LedCode>> {
105 self.led_vals.as_deref()
106 }
107
108 #[inline]
109 pub(crate) fn process_event(&mut self, ev: InputEvent) {
110 match ev.destructure() {
111 EventSummary::Key(_, code, _) => {
112 let keys = self
113 .key_vals
114 .as_deref_mut()
115 .expect("got a key event despite not supporting keys");
116 keys.set(code, ev.value() != 0);
117 }
118 EventSummary::AbsoluteAxis(_, axis, _) => {
119 let axes = self
120 .abs_vals
121 .as_deref_mut()
122 .expect("got an abs event despite not supporting absolute axes");
123 axes[axis.0 as usize].value = ev.value();
124 }
125 _ => {}
126 }
127 }
128 }