vigem_rust/controller/
x360.rs

1use bitflags::bitflags;
2
3bitflags! {
4    /// Represents the digital buttons on a virtual Xbox 360 controller.
5    ///
6    /// # Example
7    /// ```
8    /// use vigem_rust::X360Button;
9    ///
10    /// let buttons = X360Button::A | X360Button::LEFT_SHOULDER;
11    /// assert!(buttons.contains(X360Button::A));
12    /// ```
13    #[repr(transparent)]
14    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
15    pub struct X360Button: u16 {
16        const DPAD_UP          = 0x0001;
17        const DPAD_DOWN        = 0x0002;
18        const DPAD_LEFT        = 0x0004;
19        const DPAD_RIGHT       = 0x0008;
20        const START            = 0x0010;
21        const BACK             = 0x0020;
22        const LEFT_THUMB       = 0x0040;
23        const RIGHT_THUMB      = 0x0080;
24        const LEFT_SHOULDER    = 0x0100;
25        const RIGHT_SHOULDER   = 0x0200;
26        const GUIDE            = 0x0400;
27        const A                = 0x1000;
28        const B                = 0x2000;
29        const X                = 0x4000;
30        const Y                = 0x8000;
31    }
32}
33
34/// Represents the full input state of a virtual Xbox 360 controller.
35///
36/// An instance of this struct is sent to the bus via `TargetHandle::update` to
37/// update the controller's state.
38///
39/// # Examples
40///
41/// ```no_run
42/// # use vigem_rust::{Client, X360Report, X360Button};
43/// # let client = Client::connect().unwrap();
44/// # let x360 = client.new_x360_target().plugin().unwrap();
45/// # x360.wait_for_ready().unwrap();
46/// let mut report = X360Report::default();
47///
48/// // Press the A and Start buttons
49/// report.buttons = X360Button::A | X360Button::START;
50///
51/// // Move the left thumbstick halfway to the right
52/// report.thumb_lx = 16384;
53///
54/// // Pull the right trigger all the way
55/// report.right_trigger = 255;
56///
57/// x360.update(&report).unwrap();
58/// ```
59#[repr(C)]
60#[derive(Debug, Clone, Copy, Default)]
61pub struct X360Report {
62    /// A bitmask of the digital buttons.
63    pub buttons: X360Button,
64    /// Left trigger value (0-255).
65    pub left_trigger: u8,
66    /// Right trigger value (0-255).
67    pub right_trigger: u8,
68    /// Left thumbstick X-axis (-32768 to 32767). 0 is center.
69    pub thumb_lx: i16,
70    /// Left thumbstick Y-axis (-32768 to 32767). 0 is center.
71    pub thumb_ly: i16,
72    /// Right thumbstick X-axis (-32768 to 32767). 0 is center.
73    pub thumb_rx: i16,
74    /// Right thumbstick Y-axis (-32768 to 32767). 0 is center.
75    pub thumb_ry: i16,
76}
77
78#[repr(C)]
79#[derive(Debug, Clone, Copy, Default)]
80pub(crate) struct XusbSubmitReport {
81    pub size: u32,
82    pub serial_no: u32,
83    pub report: X360Report,
84}
85
86/// A notification received from the bus for an Xbox 360 target.
87///
88/// This contains feedback from the system or a game, such as rumble commands
89/// or the player index assigned to the controller. You can receive these by
90/// calling `TargetHandle::<Xbox360>::register_notification`.
91///
92/// # Examples
93/// ```no_run
94/// # use vigem_rust::{Client, target::Xbox360};
95/// # use std::time::Duration;
96/// # let client = Client::connect().unwrap();
97/// # let x360 = client.new_x360_target().plugin().unwrap();
98/// let notifications = x360.register_notification().unwrap();
99///
100/// // In a real application, you might check for notifications on a separate thread.
101/// if let Ok(Ok(notification)) = notifications.try_recv() {
102///     println!(
103///         "Received notification: Player LED = {}, Large Motor = {}",
104///         notification.led_number,
105///         notification.large_motor
106///     );
107/// }
108/// ```
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110pub struct X360Notification {
111    /// Rumble strength for the large motor (0-255).
112    pub large_motor: u8,
113    /// Rumble strength for the small motor (0-255).
114    pub small_motor: u8,
115    /// The player number (0-3) assigned to the controller, indicated by the LED.
116    /// This is the most reliable way to determine the controller's player index.
117    pub led_number: u8,
118}