Skip to content

Commit

Permalink
Add mouse wheel support to PointerEvent (citronneur#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Lytvynov authored and qwqVictor committed Apr 19, 2024
1 parent 09ee53b commit dd3fce2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
56 changes: 55 additions & 1 deletion src/core/global.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -377,6 +379,58 @@ pub fn ts_pointer_event(flags: Option<u16>, x: Option<u16>, y: Option<u16>) -> T
}
}

impl From<PointerEvent> 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<KeyboardEvent> 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,
Expand Down

0 comments on commit dd3fce2

Please sign in to comment.