1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::io;

use bitflags::bitflags;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use super::InputEventError;
use crate::PduParsing;

#[derive(Debug, Clone, PartialEq)]
pub struct MouseXPdu {
    pub flags: PointerFlags,
    pub x_position: u16,
    pub y_position: u16,
}

impl PduParsing for MouseXPdu {
    type Error = InputEventError;

    fn from_buffer(mut stream: impl io::Read) -> Result<Self, Self::Error> {
        let flags = PointerFlags::from_bits_truncate(stream.read_u16::<LittleEndian>()?);
        let x_position = stream.read_u16::<LittleEndian>()?;
        let y_position = stream.read_u16::<LittleEndian>()?;

        Ok(Self {
            flags,
            x_position,
            y_position,
        })
    }

    fn to_buffer(&self, mut stream: impl io::Write) -> Result<(), Self::Error> {
        stream.write_u16::<LittleEndian>(self.flags.bits())?;
        stream.write_u16::<LittleEndian>(self.x_position)?;
        stream.write_u16::<LittleEndian>(self.y_position)?;

        Ok(())
    }

    fn buffer_length(&self) -> usize {
        6
    }
}

bitflags! {
    pub struct PointerFlags: u16 {
        const DOWN = 0x8000;
        const BUTTON1 = 0x0001;
        const BUTTON2 = 0x0002;
    }
}