Files
DomiStream/crates/domistream/src/input/mod.rs
T

168 lines
5.1 KiB
Rust

//! Input and gamepad packets (`docs/PROTOCOL.md` section 4).
//!
//! An `INPUT_DATA` message is `0x0206 (LE) ‖ length (LE) ‖ size (BE) ‖ input type (LE) ‖ data`; see the field
//! tables and the test vectors in PROTOCOL.md. [`encode_input`] produces the whole message, ready for the control
//! frame; [`parse_input`] is its inverse (for round-trip tests, fuzzing and the Lumen host).
pub mod gamepad;
pub use gamepad::{ButtonFlags, ControllerCapabilities, ControllerKind, GamepadState};
/// Maximum size of one `INPUT_DATA` message, in bytes.
pub const MAX_INPUT_MESSAGE_BYTES: usize = 1024;
/// Bytes before the input data (message header 4 + size 4 + input type 4).
pub const INPUT_HEADER_LEN: usize = 12;
/// Input type codes (little-endian on the wire).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum InputType {
/// Key pressed.
KeyDown = 0x0000_0003,
/// Key released.
KeyUp = 0x0000_0004,
/// Absolute mouse position.
MouseMoveAbs = 0x0000_0005,
/// Relative mouse move.
MouseMoveRel = 0x0000_0007,
/// Mouse button pressed.
MouseButtonDown = 0x0000_0008,
/// Mouse button released.
MouseButtonUp = 0x0000_0009,
/// Vertical scroll.
MouseScroll = 0x0000_000A,
/// Gamepad state.
ControllerMulti = 0x0000_000C,
/// Haptics (layout UNKNOWN U21).
Haptics = 0x0000_000D,
/// UTF-8 text (layout UNKNOWN U21).
Utf8Text = 0x0000_0017,
/// Horizontal scroll.
MouseHScroll = 0x5500_0001,
/// Touch (layout UNKNOWN U21).
Touch = 0x5500_0002,
/// Pen (layout UNKNOWN U21).
Pen = 0x5500_0003,
/// A controller was plugged in.
ControllerArrival = 0x5500_0004,
/// Controller touchpad (layout UNKNOWN U21).
ControllerTouch = 0x5500_0005,
/// Controller motion sensors (layout UNKNOWN U21).
ControllerMotion = 0x5500_0006,
/// Controller battery (layout UNKNOWN U21).
ControllerBattery = 0x5500_0007,
}
impl TryFrom<u32> for InputType {
type Error = crate::Error;
/// # Errors
/// [`crate::Error::UnexpectedValue`] for an unknown code. Phase A: always [`crate::Error::Unimplemented`].
fn try_from(code: u32) -> crate::Result<Self> {
let _ = code;
Err(crate::Error::Unimplemented)
}
}
/// Mouse buttons.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum MouseButton {
/// Left.
Left = 1,
/// Middle.
Middle = 2,
/// Right.
Right = 3,
/// Side (back).
Side = 4,
/// Extra (forward).
Extra = 5,
}
/// One input event with a known layout.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InputEvent {
/// Relative mouse move.
MouseMoveRel {
/// Horizontal delta.
dx: i16,
/// Vertical delta.
dy: i16,
},
/// Absolute mouse position within the client's viewport (the LG Magic Remote pointer).
MouseMoveAbs {
/// X in the viewport.
x: i16,
/// Y in the viewport.
y: i16,
/// Viewport width as the client reports it.
width: u16,
/// Viewport height as the client reports it.
height: u16,
},
/// Mouse button.
MouseButton {
/// Pressed (true) or released.
pressed: bool,
/// Which button.
button: MouseButton,
},
/// Keyboard key.
Key {
/// Pressed (true) or released.
pressed: bool,
/// `flags` byte (bit meanings UNKNOWN U20).
flags: u8,
/// Key code, Windows virtual-key style in the low byte (high byte UNKNOWN U20).
key_code: u16,
/// `modifiers` byte (bit meanings UNKNOWN U20).
modifiers: u8,
},
/// Vertical scroll (positive = up), in wheel units (120 = one notch).
Scroll {
/// Amount.
amount: i16,
},
/// Horizontal scroll.
HScroll {
/// Amount.
amount: i16,
},
/// Full gamepad state.
Gamepad(GamepadState),
/// A controller appeared.
ControllerArrival {
/// Controller number.
controller: u8,
/// What it is.
kind: ControllerKind,
/// What it can do.
capabilities: ControllerCapabilities,
/// Which buttons it has.
supported_buttons: ButtonFlags,
},
}
/// Serializes an event into a whole `INPUT_DATA` message.
///
/// # Errors
/// [`crate::Error::UnexpectedValue`] for a controller number above 15. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn encode_input(event: &InputEvent) -> crate::Result<Vec<u8>> {
let _ = event;
Err(crate::Error::Unimplemented)
}
/// Parses a whole `INPUT_DATA` message (inverse of [`encode_input`]).
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_INPUT_MESSAGE_BYTES`], [`crate::Error::Truncated`] under
/// [`INPUT_HEADER_LEN`], [`crate::Error::Malformed`] when the length or size field does not match, the data length
/// is wrong for the type, or a constant differs; [`crate::Error::Unsupported`] for a type whose layout is UNKNOWN.
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn parse_input(message: &[u8]) -> crate::Result<InputEvent> {
let _ = message;
Err(crate::Error::Unimplemented)
}