sweetacid_evdev/
device_state.rs1use crate::{constants::*, raw_stream::RawDevice};
2use crate::{AttributeSet, AttributeSetRef, InputEvent, InputEventKind, Key};
3use std::time::SystemTime;
4
5#[derive(Debug)]
7pub struct DeviceState {
8 pub(crate) timestamp: SystemTime,
10 pub(crate) key_vals: Option<AttributeSet<Key>>,
12 pub(crate) abs_vals: Option<Box<[libc::input_absinfo; AbsoluteAxisType::COUNT]>>,
13 pub(crate) switch_vals: Option<AttributeSet<SwitchType>>,
15 pub(crate) led_vals: Option<AttributeSet<LedType>>,
17}
18
19impl Clone for DeviceState {
21 fn clone(&self) -> Self {
22 Self {
23 timestamp: self.timestamp,
24 key_vals: self.key_vals.clone(),
25 abs_vals: self.abs_vals.clone(),
26 switch_vals: self.switch_vals.clone(),
27 led_vals: self.led_vals.clone(),
28 }
29 }
30 fn clone_from(&mut self, other: &Self) {
31 self.timestamp.clone_from(&other.timestamp);
32 self.key_vals.clone_from(&other.key_vals);
33 self.abs_vals.clone_from(&other.abs_vals);
34 self.switch_vals.clone_from(&other.switch_vals);
35 self.led_vals.clone_from(&other.led_vals);
36 }
37}
38
39impl DeviceState {
40 pub(crate) fn new(device: &RawDevice) -> Self {
43 let supports = device.supported_events();
44
45 let key_vals = if supports.contains(EventType::KEY) {
46 Some(AttributeSet::new())
47 } else {
48 None
49 };
50 let abs_vals = if supports.contains(EventType::ABSOLUTE) {
51 Some(Box::new(crate::raw_stream::ABS_VALS_INIT))
52 } else {
53 None
54 };
55 let switch_vals = if supports.contains(EventType::SWITCH) {
56 Some(AttributeSet::new())
57 } else {
58 None
59 };
60 let led_vals = if supports.contains(EventType::LED) {
61 Some(AttributeSet::new())
62 } else {
63 None
64 };
65
66 DeviceState {
67 timestamp: std::time::UNIX_EPOCH,
68 key_vals,
69 abs_vals,
70 switch_vals,
71 led_vals,
72 }
73 }
74 pub fn timestamp(&self) -> SystemTime {
76 self.timestamp
77 }
78
79 pub fn key_vals(&self) -> Option<&AttributeSetRef<Key>> {
83 self.key_vals.as_deref()
84 }
85
86 pub fn abs_vals(&self) -> Option<&[libc::input_absinfo]> {
90 self.abs_vals.as_deref().map(|v| &v[..])
91 }
92
93 pub fn switch_vals(&self) -> Option<&AttributeSetRef<SwitchType>> {
97 self.switch_vals.as_deref()
98 }
99
100 pub fn led_vals(&self) -> Option<&AttributeSetRef<LedType>> {
104 self.led_vals.as_deref()
105 }
106
107 #[inline]
108 pub(crate) fn process_event(&mut self, ev: InputEvent) {
109 match ev.kind() {
110 InputEventKind::Key(code) => {
111 let keys = self
112 .key_vals
113 .as_deref_mut()
114 .expect("got a key event despite not supporting keys");
115 keys.set(code, ev.value() != 0);
116 }
117 InputEventKind::AbsAxis(axis) => {
118 let axes = self
119 .abs_vals
120 .as_deref_mut()
121 .expect("got an abs event despite not supporting absolute axes");
122 axes[axis.0 as usize].value = ev.value();
123 }
124 _ => {}
125 }
126 }
127}