1
//! POD types for the gamepad / game-controller surface
2
//! (SUPER_PLAN_2 §1 feature 6 + research/03 §"Feature 6").
3
//!
4
//! Cross-platform controller input: `gilrs` on the desktop
5
//! (Windows / Linux / macOS), iOS `GCController` + Android `InputDevice`
6
//! on mobile (research/03). Defined here in `azul-core` so the manager +
7
//! accessors cross the FFI without `azul-layout` as a dependency; the
8
//! stateful side lives in `azul_layout::managers::gamepad::GamepadManager`.
9
//!
10
//! Poll model, like the sensors: the backend keeps a [`GamepadState`]
11
//! snapshot per connected pad current, and a callback reads the latest each
12
//! frame (`CallbackInfo::get_gamepad_state`) to drive movement / menus.
13
//! Button + axis naming follows the SDL / gilrs "standard gamepad" mapping,
14
//! so the face buttons are Xbox-style: South = A, East = B, West = X,
15
//! North = Y.
16

            
17
/// A connected gamepad's id — stable for the lifetime of the connection,
18
/// assigned by the backend on connect. (gilrs `GamepadId` / the platform
19
/// device id, normalised to a `u32`.)
20
#[repr(C)]
21
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22
pub struct GamepadId {
23
    pub id: u32,
24
}
25

            
26
/// A standard-layout gamepad button. Face buttons are Xbox-style by
27
/// position (South = A / Cross, East = B / Circle, West = X / Square,
28
/// North = Y / Triangle), so layouts stay consistent across vendors.
29
///
30
/// The discriminant order is also the bit position in
31
/// [`GamepadState::buttons`] — don't reorder without bumping the ABI.
32
#[repr(C)]
33
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34
pub enum GamepadButton {
35
    /// Bottom face button (A / Cross).
36
    South,
37
    /// Right face button (B / Circle).
38
    East,
39
    /// Top face button (Y / Triangle).
40
    North,
41
    /// Left face button (X / Square).
42
    West,
43
    /// Left shoulder button (L1 / LB).
44
    LeftBumper,
45
    /// Right shoulder button (R1 / RB).
46
    RightBumper,
47
    /// Left trigger as a digital press (L2 / LT). Analog value: `LeftZ`.
48
    LeftTrigger,
49
    /// Right trigger as a digital press (R2 / RT). Analog value: `RightZ`.
50
    RightTrigger,
51
    /// Select / Back / Share.
52
    Select,
53
    /// Start / Options / Menu.
54
    Start,
55
    /// Vendor / guide button (Xbox / PS / Home).
56
    Mode,
57
    /// Left stick click (L3).
58
    LeftThumb,
59
    /// Right stick click (R3).
60
    RightThumb,
61
    /// D-pad up.
62
    DPadUp,
63
    /// D-pad down.
64
    DPadDown,
65
    /// D-pad left.
66
    DPadLeft,
67
    /// D-pad right.
68
    DPadRight,
69
    // APPENDED at the end for ABI stability — the bitset in
70
    // `GamepadState::buttons` is indexed by DISCRIMINANT, so inserting any of
71
    // these in the middle would silently renumber every bit above it and turn
72
    // a saved keybinding into a different button.
73
    /// A miscellaneous button the vendor did not standardise: the Xbox Series
74
    /// share button, the `DualSense` create button, the Switch capture button.
75
    Misc1,
76
    /// Rear paddle 1 (Xbox Elite, `DualSense` Edge, Steam Deck).
77
    Paddle1,
78
    /// Rear paddle 2.
79
    Paddle2,
80
    /// Rear paddle 3.
81
    Paddle3,
82
    /// Rear paddle 4.
83
    Paddle4,
84
    /// The touchpad pressed as a button (`DualShock` 4, `DualSense`). Distinct
85
    /// from a touch ON the pad, which is `GamepadState::touchpad`.
86
    Touchpad,
87
}
88

            
89
/// A gamepad analog axis. Stick axes are in `[-1, 1]` (right / up positive);
90
/// trigger axes ([`GamepadAxis::LeftZ`] / [`GamepadAxis::RightZ`]) in
91
/// `[0, 1]`.
92
#[repr(C)]
93
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
94
pub enum GamepadAxis {
95
    /// Left stick horizontal (left −1 … right +1).
96
    LeftStickX,
97
    /// Left stick vertical (down −1 … up +1).
98
    LeftStickY,
99
    /// Right stick horizontal.
100
    RightStickX,
101
    /// Right stick vertical.
102
    RightStickY,
103
    /// Left trigger pressure (0 … 1).
104
    LeftZ,
105
    /// Right trigger pressure (0 … 1).
106
    RightZ,
107
}
108

            
109
/// Snapshot of one gamepad's state. Buttons are a bitset (bit `n` = the
110
/// [`GamepadButton`] with discriminant `n`); axes are explicit fields. All
111
/// POD / `Copy`, so it crosses the FFI by value.
112
#[repr(C)]
113
#[derive(Debug, Clone, Copy, PartialEq)]
114
pub struct GamepadState {
115
    // Field order is by DECREASING ALIGNMENT, not by topic. `#[repr(C)]`
116
    // lays these out literally, so an align-1 bool between align-4 floats
117
    // costs 3 bytes of padding each time — the FFI checker flags it, and at
118
    // one GamepadState per pad per frame it is not free.
119
    /// Which pad this snapshot is for.
120
    pub id: GamepadId,
121
    /// Pressed-button bitset — bit `n` set ⇔ the `GamepadButton` with
122
    /// discriminant `n` is held. Read via [`GamepadState::is_pressed`].
123
    pub buttons: u32,
124
    /// Left stick X in `[-1, 1]`.
125
    pub left_stick_x: f32,
126
    /// Left stick Y in `[-1, 1]`.
127
    pub left_stick_y: f32,
128
    /// Right stick X in `[-1, 1]`.
129
    pub right_stick_x: f32,
130
    /// Right stick Y in `[-1, 1]`.
131
    pub right_stick_y: f32,
132
    /// Left trigger pressure in `[0, 1]`.
133
    pub left_z: f32,
134
    /// Right trigger pressure in `[0, 1]`.
135
    pub right_z: f32,
136
    /// Battery charge in `[0, 1]`, or `-1.0` when the pad does not report it.
137
    ///
138
    /// A sentinel rather than an `Option` because this struct is `#[repr(C)]`
139
    /// and crosses the C ABI, where a niche-optimised Option would not be
140
    /// stable. Wired pads report `-1.0`.
141
    pub battery: f32,
142
    /// Where a finger is on the pad's touch surface, if it has one and a
143
    /// finger is down (`DualShock` 4, `DualSense`, Steam Deck).
144
    ///
145
    /// `x`/`y` normalized `[0, 1]` across the surface; `active` false when
146
    /// nothing is touching. Not a `TouchPoint`: the pad surface is not the
147
    /// screen, so its coordinates are not window coordinates and must not be
148
    /// mistaken for them.
149
    ///
150
    /// ORIGIN IS BOTTOM-LEFT: `y` grows upward, like the thumbstick axes
151
    /// beside it and unlike a window's y-down coordinates. Stated because the
152
    /// underlying hardware disagrees with itself - a `DualShock`'s raw HID
153
    /// report counts y downward while the Game Controller framework normalizes
154
    /// it upward - so a producer needs to be told which one this field is.
155
    pub touchpad_x: f32,
156
    /// See [`GamepadState::touchpad_x`].
157
    pub touchpad_y: f32,
158
    /// The SECOND finger on the touch surface (`DualShock` 4 and `DualSense`
159
    /// track two), same coordinates as [`GamepadState::touchpad_x`]. Valid
160
    /// only while `touchpad2_active`; a pinch on the pad reads both slots.
161
    pub touchpad2_x: f32,
162
    /// See [`GamepadState::touchpad2_x`].
163
    pub touchpad2_y: f32,
164
    /// Angular velocity from the pad's own gyroscope, in **rad/s**.
165
    ///
166
    /// Present on `DualShock` 4, `DualSense`, Switch Pro and Steam Deck. This is
167
    /// the CONTROLLER's motion, not the device's — a phone's `SensorKind`
168
    /// readings describe the phone, these describe the thing in your hands,
169
    /// and a game that aims with gyro needs the latter.
170
    pub gyro_x: f32,
171
    /// See [`GamepadState::gyro_x`].
172
    pub gyro_y: f32,
173
    /// See [`GamepadState::gyro_x`].
174
    pub gyro_z: f32,
175
    /// Acceleration from the pad's own accelerometer, in **m/s²**.
176
    pub accel_x: f32,
177
    /// See [`GamepadState::accel_x`].
178
    pub accel_y: f32,
179
    /// See [`GamepadState::accel_x`].
180
    pub accel_z: f32,
181
    /// `false` once the pad disconnects (the manager keeps the last slot so
182
    /// a callback can observe the disconnect).
183
    pub connected: bool,
184
    /// Whether a finger is on the pad's touch surface.
185
    pub touchpad_active: bool,
186
    /// Whether a second finger is on the pad's touch surface
187
    /// (`touchpad2_x` / `touchpad2_y`).
188
    pub touchpad2_active: bool,
189
}
190

            
191
impl GamepadButton {
192
    /// This button's bit in [`GamepadState::buttons`].
193
    #[must_use]
194
664
    pub const fn bit(self) -> u32 {
195
664
        1u32 << (self as u32)
196
664
    }
197
}
198

            
199
impl Default for GamepadState {
200
11
    fn default() -> Self {
201
11
        Self {
202
11
            id: GamepadId { id: 0 },
203
11
            connected: false,
204
11
            buttons: 0,
205
11
            left_stick_x: 0.0,
206
11
            left_stick_y: 0.0,
207
11
            right_stick_x: 0.0,
208
11
            right_stick_y: 0.0,
209
11
            left_z: 0.0,
210
11
            right_z: 0.0,
211
11
            // -1.0, not 0.0: zero is a real reading meaning "flat", and a pad
212
11
            // that does not report battery must not look like a dead one.
213
11
            battery: -1.0,
214
11
            touchpad_x: 0.0,
215
11
            touchpad_y: 0.0,
216
11
            touchpad2_x: 0.0,
217
11
            touchpad2_y: 0.0,
218
11
            touchpad_active: false,
219
11
            touchpad2_active: false,
220
11
            gyro_x: 0.0,
221
11
            gyro_y: 0.0,
222
11
            gyro_z: 0.0,
223
11
            accel_x: 0.0,
224
11
            accel_y: 0.0,
225
11
            accel_z: 0.0,
226
11
        }
227
11
    }
228
}
229

            
230
impl GamepadState {
231
    /// An empty (disconnected) state for `id` — all buttons up, axes zero.
232
    #[must_use]
233
1351
    pub const fn empty(id: GamepadId) -> Self {
234
1351
        Self {
235
1351
            id,
236
1351
            connected: false,
237
1351
            buttons: 0,
238
1351
            left_stick_x: 0.0,
239
1351
            left_stick_y: 0.0,
240
1351
            right_stick_x: 0.0,
241
1351
            right_stick_y: 0.0,
242
1351
            left_z: 0.0,
243
1351
            right_z: 0.0,
244
1351
            // -1.0 = "does not report", so an absent battery is not mistaken
245
1351
            // for a flat one. See the field docs.
246
1351
            battery: -1.0,
247
1351
            touchpad_x: 0.0,
248
1351
            touchpad_y: 0.0,
249
1351
            touchpad2_x: 0.0,
250
1351
            touchpad2_y: 0.0,
251
1351
            touchpad_active: false,
252
1351
            touchpad2_active: false,
253
1351
            gyro_x: 0.0,
254
1351
            gyro_y: 0.0,
255
1351
            gyro_z: 0.0,
256
1351
            accel_x: 0.0,
257
1351
            accel_y: 0.0,
258
1351
            accel_z: 0.0,
259
1351
        }
260
1351
    }
261

            
262
    /// Whether `button` is currently held.
263
    #[must_use]
264
513
    pub const fn is_pressed(&self, button: GamepadButton) -> bool {
265
513
        self.buttons & button.bit() != 0
266
513
    }
267

            
268
    /// The current value of `axis` (sticks `[-1, 1]`, triggers `[0, 1]`).
269
    #[must_use]
270
186
    pub const fn axis(&self, axis: GamepadAxis) -> f32 {
271
186
        match axis {
272
31
            GamepadAxis::LeftStickX => self.left_stick_x,
273
31
            GamepadAxis::LeftStickY => self.left_stick_y,
274
31
            GamepadAxis::RightStickX => self.right_stick_x,
275
31
            GamepadAxis::RightStickY => self.right_stick_y,
276
31
            GamepadAxis::LeftZ => self.left_z,
277
31
            GamepadAxis::RightZ => self.right_z,
278
        }
279
186
    }
280
}
281

            
282
// FFI Option wrapper for `CallbackInfo::get_gamepad_state(id) ->
283
// Option<GamepadState>` (mirrors `OptionSensorReading`).
284
impl_option!(
285
    GamepadState,
286
    OptionGamepadState,
287
    [Debug, Clone, Copy, PartialEq]
288
);
289

            
290
#[cfg(test)]
291
#[path = "gamepad_test.rs"]
292
mod gamepad_test;