NetworkKVM/server/src/input/mappings.rs
2023-05-25 13:13:06 -04:00

135 lines
5.3 KiB
Rust

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, KvmPointerMotionAbsoluteEvent, KvmPointerMotionRelativeEvent, 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, KvmPointerMotionRelativeEvent>(event),
PointerEvent::MotionAbsolute(event) => get_pointer_event::<PointerMotionAbsoluteEvent, KvmPointerMotionAbsoluteEvent>(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 KvmPointerMotionRelativeEvent {
fn from_libinput(event: PointerMotionEvent) -> Result<Self, MappingError> {
Ok(KvmPointerMotionRelativeEvent {
dx: event.dx(),
dx_unaccelerated: event.dx_unaccelerated(),
dy: event.dy(),
dy_unaccelerated: event.dy_unaccelerated(),
})
}
}
impl FromLibinputEvent<PointerMotionAbsoluteEvent> for KvmPointerMotionAbsoluteEvent {
fn from_libinput(event: PointerMotionAbsoluteEvent) -> Result<Self, MappingError> {
Ok(KvmPointerMotionAbsoluteEvent {
x: event.absolute_x_transformed(1920),
x_mm: event.absolute_x(),
y: event.absolute_y_transformed(1080),
y_mm: event.absolute_y(),
})
}
}
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,
}
}
}