Refactor keys, buttons, raw codes

This commit is contained in:
Jan Trefil 2023-09-02 14:42:08 +02:00
parent 0c16cdd144
commit fed31254d6
14 changed files with 1090 additions and 1057 deletions

View file

@ -1,3 +1,4 @@
use crate::convert::Convert;
use crate::glue; use crate::glue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -53,9 +54,11 @@ pub enum AbsAxis {
MtToolY, MtToolY,
} }
impl AbsAxis { impl Convert for AbsAxis {
pub(crate) fn from_raw(code: u16) -> Option<Self> { type Raw = u16;
let axis = match code as _ {
fn from_raw(raw: Self::Raw) -> Option<Self> {
let axis = match raw as _ {
glue::ABS_X => Self::X, glue::ABS_X => Self::X,
glue::ABS_Y => Self::Y, glue::ABS_Y => Self::Y,
glue::ABS_Z => Self::Z, glue::ABS_Z => Self::Z,
@ -104,7 +107,7 @@ impl AbsAxis {
Some(axis) Some(axis)
} }
pub(crate) fn to_raw(&self) -> Option<u16> { fn to_raw(&self) -> Option<Self::Raw> {
let code = match self { let code = match self {
Self::X => glue::ABS_X, Self::X => glue::ABS_X,
Self::Y => glue::ABS_Y, Self::Y => glue::ABS_Y,
@ -174,9 +177,11 @@ pub enum ToolType {
Dial, Dial,
} }
impl ToolType { impl Convert for ToolType {
pub(crate) fn from_raw(value: i32) -> Option<Self> { type Raw = i32;
let value = match value as _ {
fn from_raw(raw: Self::Raw) -> Option<Self> {
let r#type = match raw as _ {
glue::MT_TOOL_FINGER => Self::Finger, glue::MT_TOOL_FINGER => Self::Finger,
glue::MT_TOOL_PEN => Self::Pen, glue::MT_TOOL_PEN => Self::Pen,
glue::MT_TOOL_PALM => Self::Palm, glue::MT_TOOL_PALM => Self::Palm,
@ -184,10 +189,10 @@ impl ToolType {
_ => return None, _ => return None,
}; };
Some(value) Some(r#type)
} }
pub(crate) fn to_raw(&self) -> i32 { fn to_raw(&self) -> Option<Self::Raw> {
let value = match self { let value = match self {
Self::Finger => glue::MT_TOOL_FINGER, Self::Finger => glue::MT_TOOL_FINGER,
Self::Pen => glue::MT_TOOL_PEN, Self::Pen => glue::MT_TOOL_PEN,
@ -195,6 +200,6 @@ impl ToolType {
Self::Dial => glue::MT_TOOL_DIAL, Self::Dial => glue::MT_TOOL_DIAL,
}; };
value as _ Some(value as _)
} }
} }

View file

@ -0,0 +1,7 @@
pub trait Convert: Sized {
type Raw;
fn from_raw(raw: Self::Raw) -> Option<Self>;
fn to_raw(&self) -> Option<Self::Raw>;
}

View file

@ -3,6 +3,7 @@ mod caps;
pub use caps::{AbsCaps, KeyCaps, RelCaps}; pub use caps::{AbsCaps, KeyCaps, RelCaps};
use crate::abs::{AbsAxis, AbsEvent, ToolType}; use crate::abs::{AbsAxis, AbsEvent, ToolType};
use crate::convert::Convert;
use crate::evdev::Evdev; use crate::evdev::Evdev;
use crate::event::Event; use crate::event::Event;
use crate::glue; use crate::glue;

View file

@ -1,4 +1,5 @@
use crate::abs::{AbsAxis, AbsInfo}; use crate::abs::{AbsAxis, AbsInfo};
use crate::convert::Convert;
use crate::glue; use crate::glue;
use crate::interceptor::Interceptor; use crate::interceptor::Interceptor;
use crate::key::Key; use crate::key::Key;

View file

@ -4,6 +4,8 @@ mod keyboard;
pub use button::Button; pub use button::Button;
pub use keyboard::Keyboard; pub use keyboard::Keyboard;
use crate::convert::Convert;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
@ -18,8 +20,10 @@ pub enum Key {
Button(Button), Button(Button),
} }
impl Key { impl Convert for Key {
pub(crate) fn from_raw(code: u16) -> Option<Self> { type Raw = u16;
fn from_raw(code: Self::Raw) -> Option<Self> {
if let Some(key) = Keyboard::from_raw(code) { if let Some(key) = Keyboard::from_raw(code) {
return Some(Self::Key(key)); return Some(Self::Key(key));
} }
@ -31,7 +35,7 @@ impl Key {
None None
} }
pub(crate) fn to_raw(&self) -> u16 { fn to_raw(&self) -> Option<u16> {
match self { match self {
Self::Key(key) => key.to_raw(), Self::Key(key) => key.to_raw(),
Self::Button(button) => button.to_raw(), Self::Button(button) => button.to_raw(),

View file

@ -1,3 +1,4 @@
use crate::convert::Convert;
use crate::glue; use crate::glue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -114,9 +115,11 @@ pub enum Button {
TriggerHappy40, TriggerHappy40,
} }
impl Button { impl Convert for Button {
pub(crate) fn to_raw(&self) -> u16 { type Raw = u16;
let code = match self {
fn to_raw(&self) -> Option<Self::Raw> {
let raw = match self {
Self::B0 => glue::BTN_0, Self::B0 => glue::BTN_0,
Self::B1 => glue::BTN_1, Self::B1 => glue::BTN_1,
Self::B2 => glue::BTN_2, Self::B2 => glue::BTN_2,
@ -227,11 +230,11 @@ impl Button {
Self::TriggerHappy40 => glue::BTN_TRIGGER_HAPPY40, Self::TriggerHappy40 => glue::BTN_TRIGGER_HAPPY40,
}; };
code as _ Some(raw as _)
} }
pub(crate) fn from_raw(code: u16) -> Option<Self> { fn from_raw(raw: Self::Raw) -> Option<Self> {
let button = match code as _ { let button = match raw as _ {
glue::BTN_0 => Self::B0, glue::BTN_0 => Self::B0,
glue::BTN_1 => Self::B1, glue::BTN_1 => Self::B1,
glue::BTN_2 => Self::B2, glue::BTN_2 => Self::B2,

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,7 @@ pub mod rel;
pub mod sync; pub mod sync;
pub mod writer; pub mod writer;
mod convert;
mod evdev; mod evdev;
mod glue; mod glue;
mod registry; mod registry;

View file

@ -1,3 +1,4 @@
use crate::convert::Convert;
use crate::glue; use crate::glue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -24,8 +25,10 @@ pub enum RelAxis {
HWheelHiRes, HWheelHiRes,
} }
impl RelAxis { impl Convert for RelAxis {
pub(crate) fn from_raw(code: u16) -> Option<Self> { type Raw = u16;
fn from_raw(code: Self::Raw) -> Option<Self> {
let axis = match code as _ { let axis = match code as _ {
glue::REL_X => Self::X, glue::REL_X => Self::X,
glue::REL_Y => Self::Y, glue::REL_Y => Self::Y,
@ -45,7 +48,7 @@ impl RelAxis {
Some(axis) Some(axis)
} }
pub(crate) fn to_raw(&self) -> u16 { fn to_raw(&self) -> Option<Self::Raw> {
let code = match self { let code = match self {
Self::X => glue::REL_X, Self::X => glue::REL_X,
Self::Y => glue::REL_Y, Self::Y => glue::REL_Y,
@ -61,6 +64,6 @@ impl RelAxis {
Self::HWheelHiRes => glue::REL_HWHEEL_HI_RES, Self::HWheelHiRes => glue::REL_HWHEEL_HI_RES,
}; };
code as _ Some(code as _)
} }
} }

View file

@ -1,3 +1,4 @@
use crate::convert::Convert;
use crate::glue; use crate::glue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -8,13 +9,25 @@ pub enum SyncEvent {
Mt, Mt,
} }
impl SyncEvent { impl Convert for SyncEvent {
pub(crate) fn to_raw(&self) -> u16 { type Raw = u16;
let code = match self {
fn to_raw(&self) -> Option<Self::Raw> {
let raw = match self {
Self::All => glue::SYN_REPORT, Self::All => glue::SYN_REPORT,
Self::Mt => glue::SYN_MT_REPORT, Self::Mt => glue::SYN_MT_REPORT,
}; };
code as _ Some(raw as _)
}
fn from_raw(raw: Self::Raw) -> Option<Self> {
let event = match raw as _ {
glue::SYN_REPORT => SyncEvent::All,
glue::SYN_MT_REPORT => SyncEvent::Mt,
_ => return None,
};
Some(event)
} }
} }

View file

@ -1,4 +1,5 @@
use crate::abs::{AbsAxis, AbsEvent, AbsInfo}; use crate::abs::{AbsAxis, AbsEvent, AbsInfo};
use crate::convert::Convert;
use crate::evdev::Evdev; use crate::evdev::Evdev;
use crate::event::Event; use crate::event::Event;
use crate::glue::{self, input_absinfo}; use crate::glue::{self, input_absinfo};
@ -25,20 +26,20 @@ impl Writer {
pub async fn write(&mut self, event: &Event) -> Result<(), Error> { pub async fn write(&mut self, event: &Event) -> Result<(), Error> {
let (r#type, code, value) = match event { let (r#type, code, value) = match event {
Event::Rel(RelEvent { axis, value }) => (glue::EV_REL, Some(axis.to_raw()), *value), Event::Rel(RelEvent { axis, value }) => (glue::EV_REL, axis.to_raw(), Some(*value)),
Event::Abs(event) => match event { Event::Abs(event) => match event {
AbsEvent::Axis { axis, value } => (glue::EV_ABS, axis.to_raw(), *value), AbsEvent::Axis { axis, value } => (glue::EV_ABS, axis.to_raw(), Some(*value)),
AbsEvent::MtToolType { value } => ( AbsEvent::MtToolType { value } => (
glue::EV_ABS, glue::EV_ABS,
Some(glue::ABS_MT_TOOL_TYPE as _), Some(glue::ABS_MT_TOOL_TYPE as _),
value.to_raw(), value.to_raw(),
), ),
}, },
Event::Key(KeyEvent { down, key }) => (glue::EV_KEY, Some(key.to_raw()), *down as _), Event::Key(KeyEvent { down, key }) => (glue::EV_KEY, key.to_raw(), Some(*down as _)),
Event::Sync(event) => (glue::EV_SYN, Some(event.to_raw()), 0), Event::Sync(event) => (glue::EV_SYN, event.to_raw(), Some(0)),
}; };
if let Some(code) = code { if let (Some(code), Some(value)) = (code, value) {
self.write_raw(r#type as _, code, value).await?; self.write_raw(r#type as _, code, value).await?;
} }
@ -150,11 +151,16 @@ impl WriterBuilder {
pub fn rel<T: IntoIterator<Item = RelAxis>>(self, items: T) -> Result<Self, Error> { pub fn rel<T: IntoIterator<Item = RelAxis>>(self, items: T) -> Result<Self, Error> {
for axis in items { for axis in items {
let axis = match axis.to_raw() {
Some(axis) => axis,
None => continue,
};
let ret = unsafe { let ret = unsafe {
glue::libevdev_enable_event_code( glue::libevdev_enable_event_code(
self.evdev.as_ptr(), self.evdev.as_ptr(),
glue::EV_REL, glue::EV_REL,
axis.to_raw() as _, axis as _,
ptr::null(), ptr::null(),
) )
}; };
@ -215,11 +221,16 @@ impl WriterBuilder {
pub fn key<T: IntoIterator<Item = Key>>(self, items: T) -> Result<Self, Error> { pub fn key<T: IntoIterator<Item = Key>>(self, items: T) -> Result<Self, Error> {
for key in items { for key in items {
let key = match key.to_raw() {
Some(key) => key,
None => continue,
};
let ret = unsafe { let ret = unsafe {
glue::libevdev_enable_event_code( glue::libevdev_enable_event_code(
self.evdev.as_ptr(), self.evdev.as_ptr(),
glue::EV_KEY, glue::EV_KEY,
key.to_raw() as _, key as _,
ptr::null(), ptr::null(),
) )
}; };

View file

@ -8,7 +8,7 @@ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
pub struct Version(u16); pub struct Version(u16);
impl Version { impl Version {
pub const CURRENT: Self = Self(3); pub const CURRENT: Self = Self(4);
} }
impl Display for Version { impl Display for Version {

View file

@ -51,7 +51,6 @@ pub enum SwitchKey {
BrightnessMax, BrightnessMax,
BrightnessMin, BrightnessMin,
BrightnessToggle, BrightnessToggle,
BrightnessZero,
BrightnessDown, BrightnessDown,
BrightnessUp, BrightnessUp,
BrlDot1, BrlDot1,
@ -107,7 +106,6 @@ pub enum SwitchKey {
Delete, Delete,
DeleteFile, DeleteFile,
Digits, Digits,
Direction,
Directory, Directory,
DisplayOff, DisplayOff,
DisplayToggle, DisplayToggle,
@ -322,19 +320,33 @@ pub enum SwitchKey {
Muhenkan, Muhenkan,
Mute, Mute,
N, N,
#[serde(rename = "0")]
N0, N0,
#[serde(rename = "1")]
N1, N1,
#[serde(rename = "102nd")]
N102nd, N102nd,
#[serde(rename = "10-channels-down")]
N10ChannelsDown, N10ChannelsDown,
#[serde(rename = "10-channels-up")]
N10ChannelsUp, N10ChannelsUp,
#[serde(rename = "2")]
N2, N2,
#[serde(rename = "3")]
N3, N3,
#[serde(rename = "3d-mode")]
N3dMode, N3dMode,
#[serde(rename = "4")]
N4, N4,
#[serde(rename = "5")]
N5, N5,
#[serde(rename = "6")]
N6, N6,
#[serde(rename = "7")]
N7, N7,
#[serde(rename = "8")]
N8, N8,
#[serde(rename = "9")]
N9, N9,
New, New,
News, News,
@ -422,8 +434,6 @@ pub enum SwitchKey {
Sat2, Sat2,
Save, Save,
Scale, Scale,
Screen,
Screenlock,
Screensaver, Screensaver,
ScrollDown, ScrollDown,
ScrollLock, ScrollLock,
@ -489,7 +499,6 @@ pub enum SwitchKey {
VolumeUp, VolumeUp,
W, W,
WakeUp, WakeUp,
Wimax,
Wlan, Wlan,
WordProcessor, WordProcessor,
WpsButton, WpsButton,
@ -502,7 +511,6 @@ pub enum SwitchKey {
Yen, Yen,
Z, Z,
ZenkakuHankaku, ZenkakuHankaku,
Zoom,
ZoomIn, ZoomIn,
ZoomOut, ZoomOut,
ZoomReset, ZoomReset,
@ -621,7 +629,6 @@ pub enum SwitchKey {
impl Into<Key> for SwitchKey { impl Into<Key> for SwitchKey {
fn into(self) -> Key { fn into(self) -> Key {
match self { match self {
// Keys.
// Keys. // Keys.
Self::A => Key::Key(Keyboard::A), Self::A => Key::Key(Keyboard::A),
Self::Ab => Key::Key(Keyboard::Ab), Self::Ab => Key::Key(Keyboard::Ab),
@ -656,7 +663,6 @@ impl Into<Key> for SwitchKey {
Self::BrightnessMax => Key::Key(Keyboard::BrightnessMax), Self::BrightnessMax => Key::Key(Keyboard::BrightnessMax),
Self::BrightnessMin => Key::Key(Keyboard::BrightnessMin), Self::BrightnessMin => Key::Key(Keyboard::BrightnessMin),
Self::BrightnessToggle => Key::Key(Keyboard::BrightnessToggle), Self::BrightnessToggle => Key::Key(Keyboard::BrightnessToggle),
Self::BrightnessZero => Key::Key(Keyboard::BrightnessZero),
Self::BrightnessDown => Key::Key(Keyboard::BrightnessDown), Self::BrightnessDown => Key::Key(Keyboard::BrightnessDown),
Self::BrightnessUp => Key::Key(Keyboard::BrightnessUp), Self::BrightnessUp => Key::Key(Keyboard::BrightnessUp),
Self::BrlDot1 => Key::Key(Keyboard::BrlDot1), Self::BrlDot1 => Key::Key(Keyboard::BrlDot1),
@ -712,7 +718,6 @@ impl Into<Key> for SwitchKey {
Self::Delete => Key::Key(Keyboard::Delete), Self::Delete => Key::Key(Keyboard::Delete),
Self::DeleteFile => Key::Key(Keyboard::DeleteFile), Self::DeleteFile => Key::Key(Keyboard::DeleteFile),
Self::Digits => Key::Key(Keyboard::Digits), Self::Digits => Key::Key(Keyboard::Digits),
Self::Direction => Key::Key(Keyboard::Direction),
Self::Directory => Key::Key(Keyboard::Directory), Self::Directory => Key::Key(Keyboard::Directory),
Self::DisplayOff => Key::Key(Keyboard::DisplayOff), Self::DisplayOff => Key::Key(Keyboard::DisplayOff),
Self::DisplayToggle => Key::Key(Keyboard::DisplayToggle), Self::DisplayToggle => Key::Key(Keyboard::DisplayToggle),
@ -847,7 +852,7 @@ impl Into<Key> for SwitchKey {
Self::Kp9 => Key::Key(Keyboard::Kp9), Self::Kp9 => Key::Key(Keyboard::Kp9),
Self::KpAsterisk => Key::Key(Keyboard::KpAsterisk), Self::KpAsterisk => Key::Key(Keyboard::KpAsterisk),
Self::KpComma => Key::Key(Keyboard::KpComma), Self::KpComma => Key::Key(Keyboard::KpComma),
Self::KpDott => Key::Key(Keyboard::KpDott), Self::KpDott => Key::Key(Keyboard::KpDot),
Self::KpEnter => Key::Key(Keyboard::KpEnter), Self::KpEnter => Key::Key(Keyboard::KpEnter),
Self::KpEqual => Key::Key(Keyboard::KpEqual), Self::KpEqual => Key::Key(Keyboard::KpEqual),
Self::KpJpComma => Key::Key(Keyboard::KpJpComma), Self::KpJpComma => Key::Key(Keyboard::KpJpComma),
@ -1027,8 +1032,6 @@ impl Into<Key> for SwitchKey {
Self::Sat2 => Key::Key(Keyboard::Sat2), Self::Sat2 => Key::Key(Keyboard::Sat2),
Self::Save => Key::Key(Keyboard::Save), Self::Save => Key::Key(Keyboard::Save),
Self::Scale => Key::Key(Keyboard::Scale), Self::Scale => Key::Key(Keyboard::Scale),
Self::Screen => Key::Key(Keyboard::Screen),
Self::Screenlock => Key::Key(Keyboard::Screenlock),
Self::Screensaver => Key::Key(Keyboard::Screensaver), Self::Screensaver => Key::Key(Keyboard::Screensaver),
Self::ScrollDown => Key::Key(Keyboard::ScrollDown), Self::ScrollDown => Key::Key(Keyboard::ScrollDown),
Self::ScrollLock => Key::Key(Keyboard::ScrollLock), Self::ScrollLock => Key::Key(Keyboard::ScrollLock),
@ -1094,7 +1097,6 @@ impl Into<Key> for SwitchKey {
Self::VolumeUp => Key::Key(Keyboard::VolumeUp), Self::VolumeUp => Key::Key(Keyboard::VolumeUp),
Self::W => Key::Key(Keyboard::W), Self::W => Key::Key(Keyboard::W),
Self::WakeUp => Key::Key(Keyboard::WakeUp), Self::WakeUp => Key::Key(Keyboard::WakeUp),
Self::Wimax => Key::Key(Keyboard::Wimax),
Self::Wlan => Key::Key(Keyboard::Wlan), Self::Wlan => Key::Key(Keyboard::Wlan),
Self::WordProcessor => Key::Key(Keyboard::WordProcessor), Self::WordProcessor => Key::Key(Keyboard::WordProcessor),
Self::WpsButton => Key::Key(Keyboard::WpsButton), Self::WpsButton => Key::Key(Keyboard::WpsButton),
@ -1107,7 +1109,6 @@ impl Into<Key> for SwitchKey {
Self::Yen => Key::Key(Keyboard::Yen), Self::Yen => Key::Key(Keyboard::Yen),
Self::Z => Key::Key(Keyboard::Z), Self::Z => Key::Key(Keyboard::Z),
Self::ZenkakuHankaku => Key::Key(Keyboard::ZenkakuHankaku), Self::ZenkakuHankaku => Key::Key(Keyboard::ZenkakuHankaku),
Self::Zoom => Key::Key(Keyboard::Zoom),
Self::ZoomIn => Key::Key(Keyboard::ZoomIn), Self::ZoomIn => Key::Key(Keyboard::ZoomIn),
Self::ZoomOut => Key::Key(Keyboard::ZoomOut), Self::ZoomOut => Key::Key(Keyboard::ZoomOut),
Self::ZoomReset => Key::Key(Keyboard::ZoomReset), Self::ZoomReset => Key::Key(Keyboard::ZoomReset),

View file

@ -33,7 +33,6 @@
- `brightness-max` - `brightness-max`
- `brightness-min` - `brightness-min`
- `brightness-toggle` - `brightness-toggle`
- `brightness-zero`
- `brightness-down` - `brightness-down`
- `brightness-up` - `brightness-up`
- `brl-dot1` - `brl-dot1`
@ -89,7 +88,6 @@
- `delete` - `delete`
- `delete-file` - `delete-file`
- `digits` - `digits`
- `direction`
- `directory` - `directory`
- `display-off` - `display-off`
- `display-toggle` - `display-toggle`
@ -304,20 +302,20 @@
- `muhenkan` - `muhenkan`
- `mute` - `mute`
- `n` - `n`
- `n0` - `0`
- `n1` - `1`
- `n102nd` - `102nd`
- `n10-channels-down` - `10-channels-down`
- `n10-channels-up` - `10-channels-up`
- `n2` - `2`
- `n3` - `3`
- `n3d-mode` - `3d-mode`
- `n4` - `4`
- `n5` - `5`
- `n6` - `6`
- `n7` - `7`
- `n8` - `8`
- `n9` - `9`
- `new` - `new`
- `news` - `news`
- `next` - `next`
@ -404,8 +402,6 @@
- `sat2` - `sat2`
- `save` - `save`
- `scale` - `scale`
- `screen`
- `screenlock`
- `screensaver` - `screensaver`
- `scroll-down` - `scroll-down`
- `scroll-lock` - `scroll-lock`
@ -471,7 +467,6 @@
- `volume-up` - `volume-up`
- `w` - `w`
- `wake-up` - `wake-up`
- `wimax`
- `wlan` - `wlan`
- `word-processor` - `word-processor`
- `wps-button` - `wps-button`
@ -484,7 +479,6 @@
- `yen` - `yen`
- `z` - `z`
- `zenkaku-hankaku` - `zenkaku-hankaku`
- `zoom`
- `zoom-in` - `zoom-in`
- `zoom-out` - `zoom-out`
- `zoom-reset` - `zoom-reset`