| 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 |
|
| 7 |
#[derive(Debug)] |
| 8 |
pub struct DeviceState { |
| 9 |
|
| 10 |
pub(crate) timestamp: SystemTime, |
| 11 |
|
| 12 |
pub(crate) key_vals: Option<AttributeSet<KeyCode>>, |
| 13 |
pub(crate) abs_vals: Option<Box<[input_absinfo; AbsoluteAxisCode::COUNT]>>, |
| 14 |
|
| 15 |
pub(crate) switch_vals: Option<AttributeSet<SwitchCode>>, |
| 16 |
|
| 17 |
pub(crate) led_vals: Option<AttributeSet<LedCode>>, |
| 18 |
} |
| 19 |
|
| 20 |
|
| 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 |
|
| 42 |
|
| 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 |
|
| 76 |
pub fn timestamp(&self) -> SystemTime { |
| 77 |
self.timestamp |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
pub fn key_vals(&self) -> Option<&AttributeSetRef<KeyCode>> { |
| 84 |
self.key_vals.as_deref() |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
pub fn abs_vals(&self) -> Option<&[input_absinfo]> { |
| 91 |
self.abs_vals.as_deref().map(|v| &v[..]) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub fn switch_vals(&self) -> Option<&AttributeSetRef<SwitchCode>> { |
| 98 |
self.switch_vals.as_deref() |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
} |