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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,4 @@
use crate::convert::Convert;
use crate::glue;
use serde::{Deserialize, Serialize};
@ -24,8 +25,10 @@ pub enum RelAxis {
HWheelHiRes,
}
impl RelAxis {
pub(crate) fn from_raw(code: u16) -> Option<Self> {
impl Convert for RelAxis {
type Raw = u16;
fn from_raw(code: Self::Raw) -> Option<Self> {
let axis = match code as _ {
glue::REL_X => Self::X,
glue::REL_Y => Self::Y,
@ -45,7 +48,7 @@ impl RelAxis {
Some(axis)
}
pub(crate) fn to_raw(&self) -> u16 {
fn to_raw(&self) -> Option<Self::Raw> {
let code = match self {
Self::X => glue::REL_X,
Self::Y => glue::REL_Y,
@ -61,6 +64,6 @@ impl RelAxis {
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 serde::{Deserialize, Serialize};
@ -8,13 +9,25 @@ pub enum SyncEvent {
Mt,
}
impl SyncEvent {
pub(crate) fn to_raw(&self) -> u16 {
let code = match self {
impl Convert for SyncEvent {
type Raw = u16;
fn to_raw(&self) -> Option<Self::Raw> {
let raw = match self {
Self::All => glue::SYN_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::convert::Convert;
use crate::evdev::Evdev;
use crate::event::Event;
use crate::glue::{self, input_absinfo};
@ -25,20 +26,20 @@ impl Writer {
pub async fn write(&mut self, event: &Event) -> Result<(), Error> {
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 {
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 } => (
glue::EV_ABS,
Some(glue::ABS_MT_TOOL_TYPE as _),
value.to_raw(),
),
},
Event::Key(KeyEvent { down, key }) => (glue::EV_KEY, Some(key.to_raw()), *down as _),
Event::Sync(event) => (glue::EV_SYN, Some(event.to_raw()), 0),
Event::Key(KeyEvent { down, key }) => (glue::EV_KEY, key.to_raw(), Some(*down as _)),
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?;
}
@ -150,11 +151,16 @@ impl WriterBuilder {
pub fn rel<T: IntoIterator<Item = RelAxis>>(self, items: T) -> Result<Self, Error> {
for axis in items {
let axis = match axis.to_raw() {
Some(axis) => axis,
None => continue,
};
let ret = unsafe {
glue::libevdev_enable_event_code(
self.evdev.as_ptr(),
glue::EV_REL,
axis.to_raw() as _,
axis as _,
ptr::null(),
)
};
@ -215,11 +221,16 @@ impl WriterBuilder {
pub fn key<T: IntoIterator<Item = Key>>(self, items: T) -> Result<Self, Error> {
for key in items {
let key = match key.to_raw() {
Some(key) => key,
None => continue,
};
let ret = unsafe {
glue::libevdev_enable_event_code(
self.evdev.as_ptr(),
glue::EV_KEY,
key.to_raw() as _,
key as _,
ptr::null(),
)
};

View file

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

View file

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

View file

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