upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/gameinput.md
diff options
context:
space:
mode:
authorkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 01:49:49 +0000
committerkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 02:54:55 +0000
commit0e59c75b7ba15013b5939b9177bfd2aa93c8f155 (patch)
tree545d9b6a58227fe1aa1f69b62e5e1fe5f6efd935 /docs/gameinput.md
parent67204bf8c0471335d9046890288b33e8d5e2875b (diff)
Runtime: GameInput shim on SDL3 (any controller; DualSense trigger effects, lightbar)
GameInputCreate and the v0 IGameInput/IGameInputDevice/IGameInputReading/IGameInputDispatcher surface a title uses for gamepads, served from SDL3 gamepads so DualSense, DualShock 4, Switch Pro and Xbox pads all work. Rumble goes to SDL_RumbleGamepad, impulse-trigger rumble to SDL_RumbleGamepadTriggers, and on a DualSense to adaptive-trigger effects through SDL_SendGamepadEffect (LEONIDA_DS_TRIGGERS=off|rumble|resistance). Lightbar via LEONIDA_DS_LIGHTBAR. SDL3 (zlib) comes from an installed package or a pinned, hash-checked release tarball via FetchContent; -DLEONIDA_SDL3=OFF skips the shim. gameinput_test covers the shim on SDL virtual joysticks; gameinput_probe is the manual check on a real pad. docs/gameinput.md has the mapping, settings and VERIFY list. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'docs/gameinput.md')
-rw-r--r--docs/gameinput.md156
1 files changed, 156 insertions, 0 deletions
diff --git a/docs/gameinput.md b/docs/gameinput.md
new file mode 100644
index 000000000..f97b0a1b3
--- /dev/null
+++ b/docs/gameinput.md
@@ -0,0 +1,156 @@
1# Controller input: the GameInput shim on SDL3
2
3The Xbox build reads controllers through Microsoft's GameInput API. `runtime/shims/gameinput.cpp` answers
4`GameInputCreate` with its own implementation of that API, backed by [SDL3](https://libsdl.org) gamepads, so
5every controller SDL supports works the way an Xbox pad would: DualSense, DualShock 4, Switch Pro, Xbox pads, and the
6rest of SDL's controller database. On a DualSense the impulse-trigger rumble becomes adaptive-trigger effects, and
7the lightbar colour can be set.
8
9Without the shim the loader would bind `GameInputCreate` to the host's `GameInput.dll` if there is one
10([#279](https://github.com/ItsNotRexed/Leonida/pull/279)). That works for Xbox pads only, and gives no DualSense
11features. The game executable is not available yet, so whether it imports GameInput this way, and under which
12module, is unverified (see [VERIFY](#verify)).
13
14## Building
15
16SDL3 is zlib-licensed. CMake uses an installed SDL3 3.2 or newer if `find_package(SDL3)` finds one (set `SDL3_DIR`
17or `CMAKE_PREFIX_PATH`). Otherwise it downloads the pinned release `SDL3-3.4.20.tar.gz` from SDL's GitHub releases
18once at configure time, checks its SHA-256, and builds it static with only the gamepad parts (no audio, video, GPU or
19render). That takes about a minute on the first build.
20
21- Offline: extract the same tarball and pass `-DFETCHCONTENT_SOURCE_DIR_SDL3=<path to SDL3-3.4.20>`.
22- Without the shim: `-DLEONIDA_SDL3=OFF`. `gta6rt` then has no `GameInputCreate`, as before.
23
24## Mapping
25
26GameInput's buttons are positional, as on an Xbox pad, and SDL's are too, so the face buttons go by position, not by
27label:
28
29| GameInput | SDL3 | DualSense | Switch Pro |
30|---|---|---|---|
31| `A` | south | Cross | B |
32| `B` | east | Circle | A |
33| `X` | west | Square | Y |
34| `Y` | north | Triangle | X |
35| `View` | back | Create | Minus |
36| `Menu` | start | Options | Plus |
37| shoulders, stick clicks, d-pad | same | L1/R1, L3/R3, d-pad | L/R, stick clicks, d-pad |
38| guide button callback | guide | PS | Home |
39
40The DualSense touchpad click, mute button and paddles are not in GameInput's gamepad state; they are in the
41controller-button array (`GetControllerButtonState`, indexed in SDL's `SDL_GamepadButton` order).
42
43- Sticks: -1..1 with +Y up (SDL reports +Y down; the shim flips it). Triggers: 0..1. No deadzone is applied, as on
44 console: the title applies its own.
45- `GetUiNavigationState` is derived from the pad: A accept, B cancel, X/Y context 1/2, stick clicks context 3/4, d-pad
46 or left stick past half for up/down/left/right, shoulders for page left/right, triggers past half for page up/down,
47 right stick past half for scrolling, Menu and View as themselves.
48- Controller axes (`GetControllerAxisState`): LX, LY, RX, RY, LT, RT in 0..1, sticks at rest at 0.5.
49- Every pad is reported as family `GameInputFamilyXboxOne` with its real USB vendor and product id.
50- Player LEDs (and SDL's default lightbar colour) follow connection order: the first pad is player 1, and a pad
51 plugged in later takes the lowest free slot.
52
53## Rumble and triggers
54
55`IGameInputDevice::SetRumbleState` takes four motor levels, 0..1. The two body motors go to `SDL_RumbleGamepad` on
56every pad that has rumble. The two trigger motors depend on the pad:
57
58- Xbox One and Series pads (and anything else SDL reports trigger rumble for): `SDL_RumbleGamepadTriggers`, the same
59 impulse triggers as on console.
60- DualSense: the trigger level becomes an adaptive-trigger effect sent with `SDL_SendGamepadEffect`, chosen by
61 `LEONIDA_DS_TRIGGERS`.
62- Others (DualShock 4, Switch Pro): the trigger levels are dropped and the device reports no trigger motors.
63
64**This is an approximation.** The Xbox title only sends an impulse-trigger vibration level per frame. The PS5 version
65of a game has effects authored per weapon and vehicle (weapon resistance, a click at a trigger position, and so on);
66none of that exists in the Xbox build, so the shim can only turn the level it gets into a generic effect. Expect it to
67feel like the Xbox signal played through the DualSense's trigger motors, not like the PS5 release.
68
69The DualSense effect is SDL's 47-byte DualSense effects block (`DS5EffectsState_t` in SDL's `SDL_hidapi_ps5.c`, the
70format `test/testcontroller.c` uses): byte 0 holds the valid flags `0x04 | 0x08` (right and left trigger effect),
71bytes 10-20 the right trigger effect and bytes 21-31 the left one. The effect modes are the three SDL's test program
72uses:
73
74| `LEONIDA_DS_TRIGGERS` | level 0 | level > 0 |
75|---|---|---|
76| `rumble` (default) | `05` (effect off) | `06 <freq> <level*255> 00`: vibration at `LEONIDA_DS_TRIGGER_FREQ` Hz from the start of the pull |
77| `resistance` | `05` | `01 00 <level*255>`: constant resistance over the whole pull |
78| `off` | nothing is sent; the triggers stay as they are | |
79
80An effect is sent only when its bytes change, so a steady level costs one report.
81
82## Environment variables
83
84| variable | default | effect |
85|---|---|---|
86| `LEONIDA_DS_TRIGGERS` | `rumble` | DualSense trigger effects: `off`, `rumble` or `resistance` (above) |
87| `LEONIDA_DS_TRIGGER_GAIN` | `1` | scales the trigger level before it becomes an effect, 0..4 |
88| `LEONIDA_DS_TRIGGER_FREQ` | `30` | vibration frequency of the `rumble` mode, 1..255 |
89| `LEONIDA_DS_LIGHTBAR` | unset | `RRGGBB` (hex) sets the DualSense/DualShock 4 lightbar when the pad connects; `off` turns it dark; unset keeps SDL's player colour |
90| `LEONIDA_RUMBLE` | `1` | `0` turns all rumble and trigger output off |
91| `LEONIDA_GAMEINPUT_THREAD` | `1` | `0` stops the background poll thread; only `rt::gameinput::poll()` then reads the pads (tests) |
92
93SDL's own hints (`SDL_JOYSTICK_*`, see SDL's `SDL_hints.h`) also apply, for example `SDL_JOYSTICK_HIDAPI_PS5=0` to
94leave a DualSense to the generic driver, or `SDL_GAMECONTROLLERCONFIG` to add a mapping for an unknown pad.
95
96## How it runs
97
98One thread owns SDL: a background thread started by the first `GameInputCreate` that polls every 2 ms. It opens new
99pads, closes removed ones, takes a reading of each, and applies rumble requests. Title calls never touch SDL: they read
100the latest reading or leave a rumble request, so a call costs a lock and a copy. Callbacks (device, reading, guide
101button) run on that thread, as GameInput runs them on its own threads. Once the title creates a dispatcher
102(`IGameInput::CreateDispatcher`), callbacks are queued instead, the dispatcher's wait handle is signalled, and
103`IGameInputDispatcher::Dispatch` runs them on the title's thread.
104
105The API surface:
106
107| interface | implemented | not implemented |
108|---|---|---|
109| `IGameInput` | `GetCurrentTimestamp` (µs), `GetCurrentReading`, `GetNextReading`, `RegisterReadingCallback`, `RegisterDeviceCallback` (blocking and async enumeration), `RegisterGuideButtonCallback`, `RegisterKeyboardLayoutCallback` (never fires), `StopCallback`, `UnregisterCallback`, `CreateDispatcher`, `FindDeviceFromId`, `EnableOemDeviceSupport` (no-op), `SetFocusPolicy` (no-op) | `GetPreviousReading` and `GetTemporalReading` (no history kept), `CreateAggregateDevice`, `FindDeviceFromObject/PlatformHandle/PlatformString` |
110| `IGameInputReading` | kind, sequence number, timestamp, device, gamepad state, UI navigation state, controller axes and buttons | raw report, keyboard, mouse, touch, motion, arcade/flight stick, racing wheel (all return nothing) |
111| `IGameInputDevice` | device info, status, battery, `SetRumbleState` | force feedback and haptic motors (none reported), raw device I/O, power off, input synchronization (no-ops) |
112| `IGameInputDispatcher` | `Dispatch`, `OpenWaitHandle` | the quota (the queue is short) |
113
114A device object stays alive for the whole process. When a pad is unplugged its status drops to
115`GameInputDeviceNoStatus` and its device callback runs; when the same pad is plugged in again (matched by SDL GUID and
116serial or device path) the title gets the same object back, connected again.
117
118## ABI
119
120`runtime/shims/gameinput_abi.h` declares the v0 GameInput API as Leonida's own code: the interfaces in vtable order,
121the enums the shim uses and the structs it has to lay out. It was written against Microsoft's public v0 header (the
122GDK version, also published as the `Microsoft.GameInput` NuGet package 0.2303.22621.3038). The header is not in the
123repository. Its struct offsets were checked by compiling both side by side with `offsetof` asserts for every field the
124shim fills (`GameInputDeviceInfo` 320 bytes with `deviceFamily` at 96 and the info pointers from 160,
125`GameInputGamepadState` 28 bytes, `GameInputControllerAxisInfo` 48, and the rest in `gameinput_abi.h`).
126
127## Tests
128
129- `gameinput_test` (CTest, no controller): turns SDL's real backends off and drives the shim through
130 `rt::shims::find` with SDL virtual joysticks, an Xbox-style pad with trigger motors, a DualSense (Sony
131 `054c:0ce6`) and a pad without trigger motors. It checks enumeration and device callbacks, the button, stick,
132 trigger and UI-navigation mapping, reading callbacks and sequence numbers, the guide button, motor and trigger
133 rumble as SDL receives them, the DualSense effect bytes in each `LEONIDA_DS_TRIGGERS` mode, the lightbar, a
134 dispatcher, async and blocking enumeration, unregistering, and unplugging and replugging.
135- `gameinput_probe [--seconds N] [--rumble]` (by hand, with a pad connected): lists the pads, plays each motor and
136 trigger for a second with `--rumble`, then prints the live reading.
137
138## VERIFY
139
140These need the game executable or a test on hardware:
141
142- **Which API version and module.** The shim is GameInput v0 and registers `GameInputCreate` under `gameinput.dll`
143 and `xgameruntime.dll`. GameInput v1 and later (NuGet 1.x and up) changed the interfaces; if the executable was
144 built against one of those, the vtables need a second set.
145- **IIDs.** `QueryInterface` answers the v0 IIDs from the public header.
146- **Reading history.** `GetNextReading` returns the newest reading when it is newer than the reference, and
147 `GetPreviousReading` has no history. A title that replays every input frame between two polls would miss the
148 intermediate states.
149- **UI navigation mapping** and the **controller-axis convention** (0..1 with sticks at 0.5) are this shim's choice,
150 not measured from a console.
151- **Battery capacity** is reported as a fraction (remaining 0..1 of full 1), not in device units.
152- **`UnregisterCallback` timeout** is not honoured; it waits for a callback that is running.
153- **Device family**: every pad is reported as `GameInputFamilyXboxOne`; a title that filters on family sees only
154 that.
155- **DualSense effect parameters**: the `06` (vibration) and `01` (resistance) byte meanings follow SDL's
156 `testcontroller.c`; how strong 255 feels and what frequency suits the Xbox signal needs a session on a real pad.