From 213f87b871bb9ef35a57401da87e73c1c8ba663f Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 11:46:55 +0100 Subject: [PATCH] [domistream] Input API: mouse, keyboard and gamepad events with their INPUT_DATA codec --- crates/domistream/src/input/gamepad.rs | 115 +++++++++++++++++ crates/domistream/src/input/mod.rs | 167 +++++++++++++++++++++++++ crates/domistream/src/lib.rs | 1 + 3 files changed, 283 insertions(+) create mode 100644 crates/domistream/src/input/gamepad.rs create mode 100644 crates/domistream/src/input/mod.rs diff --git a/crates/domistream/src/input/gamepad.rs b/crates/domistream/src/input/gamepad.rs new file mode 100644 index 0000000..7a500c2 --- /dev/null +++ b/crates/domistream/src/input/gamepad.rs @@ -0,0 +1,115 @@ +//! Gamepad state, buttons and capabilities (`docs/PROTOCOL.md` section 4, CONTROLLER_MULTI and ARRIVAL). + +/// Data length of `CONTROLLER_MULTI` (26 bytes after the input type). +pub const CONTROLLER_MULTI_DATA_LEN: usize = 26; +/// Highest controller number accepted (16 controllers, matching a 16-bit active mask). +pub const MAX_CONTROLLER_NUMBER: u8 = 15; + +/// Button bits: low 16 bits in the first button field, high 16 bits in the second. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub struct ButtonFlags(pub u32); + +impl ButtonFlags { + /// D-pad up. + pub const DPAD_UP: u32 = 0x0001; + /// D-pad down. + pub const DPAD_DOWN: u32 = 0x0002; + /// D-pad left. + pub const DPAD_LEFT: u32 = 0x0004; + /// D-pad right. + pub const DPAD_RIGHT: u32 = 0x0008; + /// Start / Menu. + pub const START: u32 = 0x0010; + /// Back / View. + pub const BACK: u32 = 0x0020; + /// Left stick press. + pub const LEFT_STICK: u32 = 0x0040; + /// Right stick press. + pub const RIGHT_STICK: u32 = 0x0080; + /// Left shoulder. + pub const LEFT_BUTTON: u32 = 0x0100; + /// Right shoulder. + pub const RIGHT_BUTTON: u32 = 0x0200; + /// Home / Guide. + pub const HOME: u32 = 0x0400; + /// A. + pub const A: u32 = 0x1000; + /// B. + pub const B: u32 = 0x2000; + /// X. + pub const X: u32 = 0x4000; + /// Y. + pub const Y: u32 = 0x8000; + /// Paddle 1. + pub const PADDLE1: u32 = 0x01_0000; + /// Paddle 2. + pub const PADDLE2: u32 = 0x02_0000; + /// Paddle 3. + pub const PADDLE3: u32 = 0x04_0000; + /// Paddle 4. + pub const PADDLE4: u32 = 0x08_0000; + /// Touchpad click. + pub const TOUCHPAD: u32 = 0x10_0000; + /// Misc (share / capture). + pub const MISC: u32 = 0x20_0000; +} + +/// Controller type in `CONTROLLER_ARRIVAL`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(u8)] +pub enum ControllerKind { + /// Unknown. + Unknown = 0x00, + /// Xbox-style. + Xbox = 0x01, + /// PlayStation-style. + PlayStation = 0x02, + /// Nintendo-style. + Nintendo = 0x03, +} + +/// Capability bits in `CONTROLLER_ARRIVAL`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub struct ControllerCapabilities(pub u8); + +impl ControllerCapabilities { + /// Analog triggers. + pub const ANALOG_TRIGGERS: u8 = 0x01; + /// Rumble. + pub const RUMBLE: u8 = 0x02; + /// Trigger rumble. + pub const TRIGGER_RUMBLE: u8 = 0x04; + /// Touchpad. + pub const TOUCHPAD: u8 = 0x08; + /// Accelerometer. + pub const ACCELEROMETER: u8 = 0x10; + /// Gyroscope. + pub const GYRO: u8 = 0x20; + /// Battery state. + pub const BATTERY: u8 = 0x40; + /// RGB LED. + pub const RGB_LED: u8 = 0x80; +} + +/// One `CONTROLLER_MULTI` state. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub struct GamepadState { + /// Controller number, 0..=15. + pub controller: u8, + /// Bit per connected controller. A cleared bit for `controller` means it was unplugged. + pub active_mask: u16, + /// Pressed buttons. + pub buttons: ButtonFlags, + /// Left trigger, 0..=255. + pub left_trigger: u8, + /// Right trigger, 0..=255. + pub right_trigger: u8, + /// Left stick X. + pub left_stick_x: i16, + /// Left stick Y. + pub left_stick_y: i16, + /// Right stick X. + pub right_stick_x: i16, + /// Right stick Y. + pub right_stick_y: i16, +} diff --git a/crates/domistream/src/input/mod.rs b/crates/domistream/src/input/mod.rs new file mode 100644 index 0000000..b3ebdbe --- /dev/null +++ b/crates/domistream/src/input/mod.rs @@ -0,0 +1,167 @@ +//! 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 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 { + 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> { + 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 { + let _ = message; + Err(crate::Error::Unimplemented) +} diff --git a/crates/domistream/src/lib.rs b/crates/domistream/src/lib.rs index e17e060..f1a0105 100644 --- a/crates/domistream/src/lib.rs +++ b/crates/domistream/src/lib.rs @@ -39,6 +39,7 @@ pub mod enet; pub mod entropy; pub mod error; pub mod http; +pub mod input; pub mod keys; pub mod pairing; pub mod rtsp;