116 lines
3.3 KiB
Rust
116 lines
3.3 KiB
Rust
//! 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,
|
|
}
|