use crate::device::InputDeviceInfo;
use crate::event_handler::DISGUISED_EVENT_OFFSETTER;
use evdev::{EventType, InputEvent, KeyCode as Key};
use std::rc::Rc;
#[derive(Debug, Clone)]
pub enum Event {
KeyEvent(Rc<InputDeviceInfo>, KeyEvent),
RelativeEvent(Rc<InputDeviceInfo>, RelativeEvent),
OtherEvents(InputEvent),
OverrideTimeout,
Tick,
}
impl Event {
#[cfg(test)]
pub fn key_release(code: Key) -> Event {
Event::KeyEvent(crate::tests::get_input_device_info(), KeyEvent::new(code, KeyValue::Release))
}
#[cfg(test)]
pub fn key_press(code: Key) -> Event {
Event::KeyEvent(crate::tests::get_input_device_info(), KeyEvent::new(code, KeyValue::Press))
}
#[cfg(test)]
pub fn key_repeat(code: Key) -> Event {
Event::KeyEvent(crate::tests::get_input_device_info(), KeyEvent::new(code, KeyValue::Repeat))
}
#[cfg(test)]
pub fn relative(code: u16, value: i32) -> Event {
Event::RelativeEvent(crate::tests::get_input_device_info(), RelativeEvent { code, value })
}
}
#[derive(Debug, Clone)]
pub struct KeyEvent {
pub key: Key,
value: KeyValue,
}
#[derive(Debug, Clone)]
pub struct RelativeEvent {
pub code: u16,
pub value: i32,
}
#[derive(Debug, Copy, Clone)]
pub enum KeyValue {
Press,
Release,
Repeat,
}
impl Event {
pub fn new(device: Rc<InputDeviceInfo>, event: InputEvent) -> Event {
let event = match event.event_type() {
EventType::KEY => Event::KeyEvent(device, KeyEvent::new_with(event.code(), event.value())),
EventType::RELATIVE => Event::RelativeEvent(device, RelativeEvent::new_with(event.code(), event.value())),
_ => Event::OtherEvents(event),
};
event
}
}
impl KeyEvent {
pub fn new(key: Key, value: KeyValue) -> KeyEvent {
KeyEvent { key, value }
}
pub fn new_with(code: u16, value: i32) -> KeyEvent {
let key = Key::new(code);
let value = KeyValue::new(value).unwrap();
KeyEvent::new(key, value)
}
pub fn code(&self) -> u16 {
self.key.code()
}
pub fn value(&self) -> i32 {
self.value.value()
}
}
impl RelativeEvent {
pub fn new_with(code: u16, value: i32) -> RelativeEvent {
RelativeEvent { code, value }
}
pub fn to_disguised_key(&self) -> u16 {
match self.value {
1..=i32::MAX => (self.code * 2) + DISGUISED_EVENT_OFFSETTER,
i32::MIN..=-1 => (self.code * 2) + 1 + DISGUISED_EVENT_OFFSETTER,
0 => {
println!("This event has a value of zero : {self:?}");
(self.code * 2) + DISGUISED_EVENT_OFFSETTER
}
}
}
}
impl KeyValue {
fn new(value: i32) -> Option<KeyValue> {
let event_value = match value {
0 => KeyValue::Release,
1 => KeyValue::Press,
2 => KeyValue::Repeat,
_ => return None,
};
Some(event_value)
}
fn value(&self) -> i32 {
match self {
KeyValue::Release => 0,
KeyValue::Press => 1,
KeyValue::Repeat => 2,
}
}
}