ironrdp_pdu/input/
mouse.rs

1use bitflags::bitflags;
2use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct MousePdu {
6    pub flags: PointerFlags,
7    pub number_of_wheel_rotation_units: i16,
8    pub x_position: u16,
9    pub y_position: u16,
10}
11
12impl MousePdu {
13    const NAME: &'static str = "MousePdu";
14
15    const FIXED_PART_SIZE: usize = 2 /* flags */ + 2 /* x */ + 2 /* y */;
16}
17
18impl Encode for MousePdu {
19    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
20        ensure_fixed_part_size!(in: dst);
21
22        let wheel_negative_bit = if self.number_of_wheel_rotation_units < 0 {
23            PointerFlags::WHEEL_NEGATIVE.bits()
24        } else {
25            PointerFlags::empty().bits()
26        };
27
28        let wheel_rotations_bits = u16::from(self.number_of_wheel_rotation_units as u8); // truncate
29
30        let flags = self.flags.bits() | wheel_negative_bit | wheel_rotations_bits;
31
32        dst.write_u16(flags);
33        dst.write_u16(self.x_position);
34        dst.write_u16(self.y_position);
35
36        Ok(())
37    }
38
39    fn name(&self) -> &'static str {
40        Self::NAME
41    }
42
43    fn size(&self) -> usize {
44        Self::FIXED_PART_SIZE
45    }
46}
47
48impl<'de> Decode<'de> for MousePdu {
49    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
50        ensure_fixed_part_size!(in: src);
51
52        let flags_raw = src.read_u16();
53
54        let flags = PointerFlags::from_bits_truncate(flags_raw);
55
56        let wheel_rotations_bits = flags_raw as u8; // truncate
57
58        let number_of_wheel_rotation_units = if flags.contains(PointerFlags::WHEEL_NEGATIVE) {
59            -i16::from(wheel_rotations_bits)
60        } else {
61            i16::from(wheel_rotations_bits)
62        };
63
64        let x_position = src.read_u16();
65        let y_position = src.read_u16();
66
67        Ok(Self {
68            flags,
69            number_of_wheel_rotation_units,
70            x_position,
71            y_position,
72        })
73    }
74}
75bitflags! {
76    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
77    pub struct PointerFlags: u16 {
78        const WHEEL_NEGATIVE = 0x0100;
79        const VERTICAL_WHEEL = 0x0200;
80        const HORIZONTAL_WHEEL = 0x0400;
81        const MOVE = 0x0800;
82        const LEFT_BUTTON = 0x1000;
83        const RIGHT_BUTTON = 0x2000;
84        const MIDDLE_BUTTON_OR_WHEEL = 0x4000;
85        const DOWN = 0x8000;
86    }
87}