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 where Self: Sized, T: EventTrait { fn from_libinput(t: T) -> Result; } impl FromLibinputEvent for KvmEvent { fn from_libinput(event: Event) -> Result { match event { Event::Keyboard(event) => get_event::(event), Event::Pointer(event) => get_event::(event), _ => Err(UnsupportedEvent(event)) } } } fn get_event(event: T) -> Result where T: EventTrait, U: FromLibinputEvent + KvmEventTrait { Ok(U::from_libinput(event)?.into_kvm_event()) } impl FromLibinputEvent for KvmKeyboardEvent { fn from_libinput(event: KeyboardEvent) -> Result { let key_code = event.key(); let state = KvmButtonState::from_key_state(event.key_state()); Ok(KvmKeyboardEvent { key_code, state }) } } impl FromLibinputEvent for KvmPointerEvent { fn from_libinput(event: PointerEvent) -> Result { #[allow(deprecated)] match event { PointerEvent::Motion(event) => get_pointer_event::(event), PointerEvent::MotionAbsolute(event) => get_pointer_event::(event), PointerEvent::Button(event) => get_pointer_event::(event), PointerEvent::ScrollWheel(event) => get_pointer_event::(event), PointerEvent::ScrollFinger(event) => get_pointer_event::(event), PointerEvent::ScrollContinuous(event) => get_pointer_event::(event), PointerEvent::Axis(event) => Err(UnsupportedEvent(event.into_event())), _ => Err(UnknownEvent(event.into_event())) } } } fn get_pointer_event(event: T) -> Result where T: EventTrait, U: FromLibinputEvent + KvmPointerEventTrait { Ok(U::from_libinput(event)?.into_kvm_pointer_event()) } impl FromLibinputEvent for KvmPointerMotionRelativeEvent { fn from_libinput(event: PointerMotionEvent) -> Result { Ok(KvmPointerMotionRelativeEvent { dx: event.dx(), dx_unaccelerated: event.dx_unaccelerated(), dy: event.dy(), dy_unaccelerated: event.dy_unaccelerated(), }) } } impl FromLibinputEvent for KvmPointerMotionAbsoluteEvent { fn from_libinput(event: PointerMotionAbsoluteEvent) -> Result { 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 for KvmPointerButtonEvent { fn from_libinput(event: PointerButtonEvent) -> Result { let button = event.button(); let state = KvmButtonState::from_button_state(event.button_state()); Ok(KvmPointerButtonEvent { button, state }) } } impl FromLibinputEvent for KvmPointerScrollEvent where T: EventTrait + PointerScrollEvent { fn from_libinput(event: T) -> Result { fn get_axis_value(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, } } }