From dd3fce25711451b8e2aecdee8f655a76b9f981de Mon Sep 17 00:00:00 2001 From: Andrew Lytvynov Date: Wed, 6 Oct 2021 09:39:37 -0700 Subject: [PATCH] Add mouse wheel support to PointerEvent (#3) --- src/core/event.rs | 15 +++++++++++++ src/core/global.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/core/event.rs b/src/core/event.rs index 7560033..3a4d851 100644 --- a/src/core/event.rs +++ b/src/core/event.rs @@ -130,6 +130,17 @@ pub enum PointerButton { Middle = 3, } +#[repr(u8)] +#[derive(Eq, PartialEq, TryFromPrimitive, Copy, Clone)] +pub enum PointerWheel { + /// No wheel scroll. + None = 0, + /// Vertical wheel scroll. + Vertical = 1, + /// Horizontal wheel scroll. + Horizontal = 2, +} + /// A mouse pointer event pub struct PointerEvent { /// horizontal position from top left angle of the window @@ -140,6 +151,10 @@ pub struct PointerEvent { pub button: PointerButton, /// true if it's a down press action pub down: bool, + /// Which direction the wheel was scrolled. + pub wheel: PointerWheel, + /// How much was the wheel scrolled. + pub wheel_delta: i16, } /// Keyboard event diff --git a/src/core/global.rs b/src/core/global.rs index c05a6c6..fba2079 100644 --- a/src/core/global.rs +++ b/src/core/global.rs @@ -1,6 +1,8 @@ use crate::core::capability; use crate::core::capability::{capability_set, Capability}; -use crate::core::event::{BitmapEvent, RdpEvent}; +use crate::core::event::{ + BitmapEvent, PointerWheel, RdpEvent, +}; use crate::core::gcc::KeyboardLayout; use crate::core::mcs; use crate::core::tpkt; @@ -377,6 +379,58 @@ pub fn ts_pointer_event(flags: Option, x: Option, y: Option) -> T } } +impl From for TSInputEvent { + fn from(pointer: PointerEvent) -> Self { + // Pointer are sent to global channel + // Compute flags + let mut flags = match pointer.button { + PointerButton::Left => PointerFlag::PtrflagsButton1 as u16, + PointerButton::Right => PointerFlag::PtrflagsButton2 as u16, + PointerButton::Middle => PointerFlag::PtrflagsButton3 as u16, + _ => 0, + }; + // Clamp wheel delta to the allowed range. + let wheel_delta: i16 = if pointer.wheel_delta > 0x00FF { + 0x00FF + } else if pointer.wheel_delta < -0x00FF { + -0x0FF + } else { + pointer.wheel_delta + }; + let wheel_delta_flag = if wheel_delta > 0 { + wheel_delta as u16 + } else if wheel_delta < 0 { + -wheel_delta as u16 | PointerFlag::PtrflagsWheelNegative as u16 + } else { + 0 + }; + flags |= match pointer.wheel { + PointerWheel::Vertical => PointerFlag::PtrflagsWheel as u16 | wheel_delta_flag, + PointerWheel::Horizontal => PointerFlag::PtrflagsHwheel as u16 | wheel_delta_flag, + _ => 0, + }; + if pointer.button == PointerButton::None && pointer.wheel == PointerWheel::None { + flags |= PointerFlag::PtrflagsMove as u16; + } + + if pointer.down { + flags |= PointerFlag::PtrflagsDown as u16; + } + + ts_pointer_event(Some(flags), Some(pointer.x), Some(pointer.y)) + } +} + +impl From for TSInputEvent { + fn from(key: KeyboardEvent) -> Self { + let mut flags: u16 = 0; + if !key.down { + flags |= KeyboardFlag::KbdflagsRelease as u16; + } + ts_keyboard_event(Some(flags), Some(key.code)) + } +} + #[repr(u16)] pub enum KeyboardFlag { KbdflagsExtended = 0x0100,