Input mapping

This commit is contained in:
FyloZ 2023-04-25 21:50:32 -04:00
parent 811aa8d117
commit 61a7ce3904
Signed by: william
GPG Key ID: 835378AE9AF4AE97
6 changed files with 255 additions and 25 deletions

View File

@ -0,0 +1,12 @@
use crate::input::events::{KvmButtonState, KvmEvent, KvmEventTrait};
pub struct KvmKeyboardEvent {
pub key_code: u32,
pub state: KvmButtonState,
}
impl KvmEventTrait for KvmKeyboardEvent {
fn into_kvm_event(self) -> KvmEvent {
KvmEvent::Keyboard(self)
}
}

View File

@ -1 +1,38 @@
use std::fmt::{Display, Formatter};
use crate::input::events::keyboard::KvmKeyboardEvent;
use crate::input::events::pointer::KvmPointerEvent;
pub mod pointer; pub mod pointer;
pub mod keyboard;
pub enum KvmEvent {
Pointer(KvmPointerEvent),
Keyboard(KvmKeyboardEvent),
}
pub enum KvmButtonState {
Pressed,
Released,
}
pub trait KvmEventTrait {
fn into_kvm_event(self) -> KvmEvent;
}
impl Display for KvmEvent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
KvmEvent::Pointer(_) => write!(f, "Pointer"),
KvmEvent::Keyboard(_) => write!(f, "Keyboard")
}
}
}
impl Display for KvmButtonState {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
KvmButtonState::Pressed => write!(f, "Pressed"),
KvmButtonState::Released => write!(f, "Released")
}
}
}

View File

@ -1,29 +1,54 @@
pub enum PointerEvent { use crate::input::events::{KvmButtonState, KvmEvent, KvmEventTrait};
Motion(PointerMotionEvent),
Button(PointerButtonEvent), pub enum KvmPointerEvent {
Scroll(PointerScrollEvent), Motion(KvmPointerMotionEvent),
Button(KvmPointerButtonEvent),
Scroll(KvmPointerScrollEvent),
} }
pub enum Axis { pub struct KvmPointerButtonEvent {
Horizontal = 0,
Vertical = 1,
}
pub struct PointerButtonEvent {
pub button: u32, pub button: u32,
pub seat_button_count: u32, pub state: KvmButtonState,
pub button_state: u32,
} }
pub struct PointerMotionEvent { pub struct KvmPointerMotionEvent {
pub dx: f64, pub dx: f64,
pub dx_unaccelerated: f64, pub dx_unaccelerated: f64,
pub dy: f64, pub dy: f64,
pub dy_unaccelerated: f64, pub dy_unaccelerated: f64,
} }
pub struct PointerScrollEvent { pub struct KvmPointerScrollEvent {
pub axis: Axis, pub horizontal_scroll_value: f64,
pub scroll_value: f64, pub vertical_scroll_value: f64,
pub scroll_value_v120: f64,
} }
pub trait KvmPointerEventTrait {
fn into_kvm_pointer_event(self) -> KvmPointerEvent;
}
impl KvmEventTrait for KvmPointerEvent {
fn into_kvm_event(self) -> KvmEvent {
KvmEvent::Pointer(self)
}
}
macro_rules! kvm_event_traits {
($e:ident, $s:ident) => {
impl KvmEventTrait for $s {
fn into_kvm_event(self) -> KvmEvent {
self.into_kvm_pointer_event().into_kvm_event()
}
}
impl KvmPointerEventTrait for $s {
fn into_kvm_pointer_event(self) -> KvmPointerEvent {
KvmPointerEvent::$e(self)
}
}
}
}
kvm_event_traits!(Motion, KvmPointerMotionEvent);
kvm_event_traits!(Button, KvmPointerButtonEvent);
kvm_event_traits!(Scroll, KvmPointerScrollEvent);

View File

@ -0,0 +1,129 @@
use input::{Event};
use input::event::{EventTrait, KeyboardEvent, PointerEvent};
use input::event::keyboard::{KeyboardEventTrait, KeyState};
use input::event::pointer::{Axis, ButtonState, PointerButtonEvent, PointerMotionAbsoluteEvent, PointerMotionEvent, PointerScrollContinuousEvent, PointerScrollEvent, PointerScrollFingerEvent, PointerScrollWheelEvent};
use crate::input::events::{KvmButtonState, KvmEvent, KvmEventTrait};
use crate::input::events::keyboard::KvmKeyboardEvent;
use crate::input::events::pointer::{KvmPointerButtonEvent, KvmPointerEvent, KvmPointerEventTrait, KvmPointerMotionEvent, KvmPointerScrollEvent};
use crate::input::mappings::MappingError::{UnknownEvent, UnsupportedEvent};
#[derive(Debug)]
pub enum MappingError {
UnsupportedEvent(Event),
UnknownEvent(Event),
}
pub trait FromLibinputEvent<T>
where Self: Sized, T: EventTrait {
fn from_libinput(t: T) -> Result<Self, MappingError>;
}
impl FromLibinputEvent<Event> for KvmEvent {
fn from_libinput(event: Event) -> Result<Self, MappingError> {
match event {
Event::Keyboard(event) => get_event::<KeyboardEvent, KvmKeyboardEvent>(event),
Event::Pointer(event) => get_event::<PointerEvent, KvmPointerEvent>(event),
_ => Err(UnsupportedEvent(event))
}
}
}
fn get_event<T, U>(event: T) -> Result<KvmEvent, MappingError>
where T: EventTrait, U: FromLibinputEvent<T> + KvmEventTrait {
Ok(U::from_libinput(event)?.into_kvm_event())
}
impl FromLibinputEvent<KeyboardEvent> for KvmKeyboardEvent {
fn from_libinput(event: KeyboardEvent) -> Result<Self, MappingError> {
let key_code = event.key();
let state = KvmButtonState::from_key_state(event.key_state());
Ok(KvmKeyboardEvent { key_code, state })
}
}
impl FromLibinputEvent<PointerEvent> for KvmPointerEvent {
fn from_libinput(event: PointerEvent) -> Result<Self, MappingError> {
#[allow(deprecated)]
match event {
PointerEvent::Motion(event) => get_pointer_event::<PointerMotionEvent, KvmPointerMotionEvent>(event),
PointerEvent::MotionAbsolute(event) => get_pointer_event::<PointerMotionAbsoluteEvent, KvmPointerMotionEvent>(event),
PointerEvent::Button(event) => get_pointer_event::<PointerButtonEvent, KvmPointerButtonEvent>(event),
PointerEvent::ScrollWheel(event) => get_pointer_event::<PointerScrollWheelEvent, KvmPointerScrollEvent>(event),
PointerEvent::ScrollFinger(event) => get_pointer_event::<PointerScrollFingerEvent, KvmPointerScrollEvent>(event),
PointerEvent::ScrollContinuous(event) => get_pointer_event::<PointerScrollContinuousEvent, KvmPointerScrollEvent>(event),
PointerEvent::Axis(event) => Err(UnsupportedEvent(event.into_event())),
_ => Err(UnknownEvent(event.into_event()))
}
}
}
fn get_pointer_event<T, U>(event: T) -> Result<KvmPointerEvent, MappingError>
where T: EventTrait, U: FromLibinputEvent<T> + KvmPointerEventTrait {
Ok(U::from_libinput(event)?.into_kvm_pointer_event())
}
impl FromLibinputEvent<PointerMotionEvent> for KvmPointerMotionEvent {
fn from_libinput(event: PointerMotionEvent) -> Result<Self, MappingError> {
Ok(KvmPointerMotionEvent {
dx: event.dx(),
dx_unaccelerated: event.dx_unaccelerated(),
dy: event.dy(),
dy_unaccelerated: event.dy_unaccelerated(),
})
}
}
impl FromLibinputEvent<PointerMotionAbsoluteEvent> for KvmPointerMotionEvent {
fn from_libinput(event: PointerMotionAbsoluteEvent) -> Result<Self, MappingError> {
Err(UnsupportedEvent(event.into_event())) // TODO
}
}
impl FromLibinputEvent<PointerButtonEvent> for KvmPointerButtonEvent {
fn from_libinput(event: PointerButtonEvent) -> Result<Self, MappingError> {
let button = event.button();
let state = KvmButtonState::from_button_state(event.button_state());
Ok(KvmPointerButtonEvent { button, state })
}
}
impl<T> FromLibinputEvent<T> for KvmPointerScrollEvent
where T: EventTrait + PointerScrollEvent {
fn from_libinput(event: T) -> Result<Self, MappingError> {
fn get_axis_value<T>(event: &T, axis: Axis) -> f64
where T: PointerScrollEvent {
if event.has_axis(axis) {
return event.scroll_value(axis);
}
0f64
}
let horizontal_scroll_value = get_axis_value(&event, Axis::Horizontal);
let vertical_scroll_value = get_axis_value(&event, Axis::Vertical);
Ok(KvmPointerScrollEvent {
horizontal_scroll_value,
vertical_scroll_value,
})
}
}
impl KvmButtonState {
pub fn from_key_state(state: KeyState) -> Self {
match state {
KeyState::Released => Self::Released,
KeyState::Pressed => Self::Pressed,
}
}
pub fn from_button_state(state: ButtonState) -> Self {
match state {
ButtonState::Released => Self::Released,
ButtonState::Pressed => Self::Pressed,
}
}
}

View File

@ -1,5 +1,3 @@
mod events;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io; use std::io;
use std::os::fd::OwnedFd; use std::os::fd::OwnedFd;
@ -7,7 +5,19 @@ use std::os::unix::fs::OpenOptionsExt;
use std::path::Path; use std::path::Path;
use input::{Libinput, LibinputInterface}; use input::{Libinput, LibinputInterface};
use input::Event;
use input::Event::{Keyboard, Pointer};
use input::event::keyboard::{KeyboardEventTrait, KeyState};
use input::event::{KeyboardEvent, PointerEvent};
use input::event::PointerEvent::{Button, Motion};
use libc::{O_RDONLY, O_RDWR, O_WRONLY}; use libc::{O_RDONLY, O_RDWR, O_WRONLY};
use crate::input::events::keyboard::KvmKeyboardEvent;
use crate::input::events::KvmEvent;
use crate::input::events::pointer::{KvmPointerButtonEvent, KvmPointerMotionEvent};
use crate::input::mappings::FromLibinputEvent;
mod events;
mod mappings;
const UDEV_SEAT: &str = "seat0"; const UDEV_SEAT: &str = "seat0";
@ -29,14 +39,31 @@ impl LibinputInterface for Interface {
} }
} }
pub(crate) fn read_inputs() -> io::Result<()> { pub fn listen_inputs() -> io::Result<()> {
let input = prepare_libinput();
read_inputs(input)
}
fn prepare_libinput() -> Libinput {
let mut input = Libinput::new_with_udev(Interface); let mut input = Libinput::new_with_udev(Interface);
input.udev_assign_seat(UDEV_SEAT).unwrap(); input.udev_assign_seat(UDEV_SEAT).unwrap();
input
}
fn read_inputs(mut input: Libinput) -> io::Result<()> {
loop { loop {
input.dispatch().unwrap(); input.dispatch().unwrap();
for event in &mut input { for event in &mut input {
// event. let mapping_result = KvmEvent::from_libinput(event);
println!("Got event: {:?}", event); if mapping_result.is_err() {
println!("Error: {:?}", &mapping_result.err());
} else {
let kvm_event = mapping_result.unwrap();
match kvm_event {
KvmEvent::Keyboard(event) => println!("Key event: {} ({})", event.key_code, event.state),
_ => {}
}
}
} }
} }
} }

View File

@ -5,7 +5,7 @@ use std::net::SocketAddr;
use messages::client_registration::ClientRegistration; use messages::client_registration::ClientRegistration;
use messages::serialization::{DeserializeMessage, read_message_data, SerializeMessage}; use messages::serialization::{DeserializeMessage, read_message_data, SerializeMessage};
use crate::input::read_inputs; use crate::input::{listen_inputs};
use crate::net::tcp_server::{NextIntent, TcpClient, TcpServer}; use crate::net::tcp_server::{NextIntent, TcpClient, TcpServer};
mod net; mod net;
@ -18,7 +18,7 @@ fn main() -> io::Result<()> {
// //
// server.listen()?; // server.listen()?;
read_inputs()?; listen_inputs()?;
Ok(()) Ok(())
} }