upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/gameinput_test.cpp
diff options
context:
space:
mode:
authorJack UK <janzeiser@web.de>2026-11-22 14:26:06 +0100
committerJack UK <janzeiser@web.de>2026-11-22 14:26:06 +0100
commitc608a6567940a1ed2c2cc144163e76c348e7fa12 (patch)
tree7a14b49ceaae9fa84a31fb46b5e764b531682a15 /runtime/test/gameinput_test.cpp
parent41f448261e1de8a410b4d760e69a7e5656cef858 (diff)
GameInput: motion support via SDL sensors (#660 follow-up)
Expose SDL3's gyroscope and accelerometer through the GameInput shim: - gameinput_abi.h: GameInputMotionAccuracy, GameInputMotionInfo and GameInputMotionState with the v0 layouts (Microsoft's public docs), size asserts included. - The shim enables both sensors when a pad with them is opened, reads the latest sample each poll, and reports GameInputKindMotion plus a GameInputMotionInfo on such pads. GetMotionState returns the state as signed fractions of the motionInfo maxima (2 g accel, 2000 deg/s gyro, the shim's choice since SDL exposes no sensor range; VERIFY). All-zero accel means no sample yet, so readings before the device's first sample carry no motion. Magnetic field and orientation stay zero and Unknown: SDL has no compass and nothing synthesises a quaternion. Pads with sensors stream a reading every ~8 ms so motion is not stuck at the last button event; other pads keep the old change-driven behaviour. - GetCurrentReading and GetNextReading with a device filter now reject kinds the device does not support (READING_NOT_FOUND), matching the v0 docs and the device-less path, so a motion query on a plain pad fails instead of returning a reading without motion. - gameinput_test: a virtual pad with SDL sensors covers kind/motionInfo presence, no-motion-before-first-sample, normalisation, accuracy values, the +-1 clamp, the cadence, and the plain-pad rejection. gameinput_probe prints the live accel and gyro fractions. - docs/gameinput.md: a Motion section, the API table, and VERIFY notes for the chosen maxima and the unprovided members. Tested: gameinput_test 185/185 and ctest 90/90 (RelWithDebInfo) on Windows 11 Pro 10.0.26200; SDL3 3.4.20 via -DLEONIDA_SDL3_FETCH=ON. Follows: #660. AI-assisted (ZCode agent).
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp136
1 files changed, 135 insertions, 1 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index a0bcaf444..d1923ceaf 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -80,7 +80,7 @@ bool SDLCALL on_effect(void* u, const void* data, int size) {
80} 80}
81void SDLCALL on_player(void* u, int index) { static_cast<Pad*>(u)->player = index; } 81void SDLCALL on_player(void* u, int index) { static_cast<Pad*>(u)->player = index; }
82 82
83void attach(Pad& pad, const char* name, uint16_t vendor, uint16_t product, bool trigger_motors) { 83void attach(Pad& pad, const char* name, uint16_t vendor, uint16_t product, bool trigger_motors, bool sensors = false) {
84 SDL_VirtualJoystickDesc d; 84 SDL_VirtualJoystickDesc d;
85 SDL_INIT_INTERFACE(&d); 85 SDL_INIT_INTERFACE(&d);
86 d.type = SDL_JOYSTICK_TYPE_GAMEPAD; 86 d.type = SDL_JOYSTICK_TYPE_GAMEPAD;
@@ -97,6 +97,11 @@ void attach(Pad& pad, const char* name, uint16_t vendor, uint16_t product, bool
97 d.SetLED = on_led; 97 d.SetLED = on_led;
98 d.SendEffect = on_effect; 98 d.SendEffect = on_effect;
99 d.SetPlayerIndex = on_player; 99 d.SetPlayerIndex = on_player;
100 static const SDL_VirtualJoystickSensorDesc kSensors[] = {{SDL_SENSOR_GYRO, 250.0f}, {SDL_SENSOR_ACCEL, 250.0f}};
101 if (sensors) {
102 d.nsensors = 2;
103 d.sensors = kSensors;
104 }
100 pad.id = SDL_AttachVirtualJoystick(&d); 105 pad.id = SDL_AttachVirtualJoystick(&d);
101 pad.js = pad.id ? SDL_OpenJoystick(pad.id) : nullptr; 106 pad.js = pad.id ? SDL_OpenJoystick(pad.id) : nullptr;
102 if (pad.js) { // triggers at rest: the full-range virtual axis maps -32768 to 0 107 if (pad.js) { // triggers at rest: the full-range virtual axis maps -32768 to 0
@@ -206,6 +211,134 @@ bool sticks_at_rest(const GameInputGamepadState& st) {
206 st.rightThumbstickY == 0 && st.leftTrigger == 0 && st.rightTrigger == 0; 211 st.rightThumbstickY == 0 && st.leftTrigger == 0 && st.rightTrigger == 0;
207} 212}
208 213
214// ---- motion: SDL sensors through the shim ----
215void send_motion(Pad& pad, float ax, float ay, float az, float gx, float gy, float gz) {
216 const Uint64 ts = SDL_GetTicksNS();
217 const float accel[3] = {ax, ay, az};
218 const float gyro[3] = {gx, gy, gz};
219 SDL_SendJoystickVirtualSensorData(pad.js, SDL_SENSOR_ACCEL, ts, accel, 3);
220 SDL_SendJoystickVirtualSensorData(pad.js, SDL_SENSOR_GYRO, ts, gyro, 3);
221}
222
223GameInputMotionState motion_state(IGameInput* input, IGameInputDevice* dev, bool* ok = nullptr) {
224 GameInputMotionState ms{};
225 IGameInputReading* r = nullptr;
226 const bool got = input->GetCurrentReading(GameInputKindMotion, dev, &r) == S_OK && r && r->GetMotionState(&ms);
227 if (r) r->Release();
228 if (ok) *ok = got;
229 return ms;
230}
231
232void test_motion(IGameInput* input) {
233 const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector
234 Pad gyro_pad, plain;
235 attach(gyro_pad, "Leonida virtual gyro pad", 0x054C, 0x0CE6, false, true); // Sony ids, sensors on
236 attach(plain, "Leonida virtual plain pad", 0x045E, 0x02FF, false);
237 rt::gameinput::poll();
238 check(rt::gameinput::connected_count() == 2, "motion: both pads connected");
239 // The connect events for these two pads are the first two CONNECTED events after `events_before`, deduplicated
240 // by device: earlier test sections leave their device callbacks registered, so every connect is delivered more
241 // than once, and a disconnect from a previous test may be queued in between.
242 std::vector<IGameInputDevice*> found;
243 for (size_t i = events_before; i < g_device_events.size() && found.size() < 2; ++i) {
244 const DeviceEvent& e = g_device_events[i];
245 if (!(e.current & GameInputDeviceConnected)) continue;
246 if (std::find(found.begin(), found.end(), e.device) != found.end()) continue;
247 found.push_back(e.device);
248 }
249 IGameInputDevice* mdev = found.size() > 0 ? found[0] : nullptr; // attach order: gyro pad first
250 IGameInputDevice* pdev = found.size() > 1 ? found[1] : nullptr;
251 if (mdev) mdev->AddRef();
252 if (pdev) pdev->AddRef();
253 check(mdev && pdev, "motion: both device objects arrived");
254 if (!mdev || !pdev) return;
255
256 const GameInputDeviceInfo* mi = mdev->GetDeviceInfo();
257 const GameInputDeviceInfo* pi = pdev->GetDeviceInfo();
258 check((mi->supportedInput & GameInputKindMotion) != 0, "motion: the gyro pad reports GameInputKindMotion");
259 check((pi->supportedInput & GameInputKindMotion) == 0, "motion: the plain pad does not");
260 const GameInputMotionInfo* mmotion = static_cast<const GameInputMotionInfo*>(mi->motionInfo);
261 check(mmotion != nullptr && mmotion->maxAcceleration == 19.6133f && mmotion->maxAngularVelocity == 34.9066f &&
262 mmotion->maxMagneticFieldStrength == 0.0f,
263 "motion: motionInfo carries the shim's maxima (magnetometer unavailable)");
264 check(pi->motionInfo == nullptr, "motion: the plain pad's motionInfo is null");
265
266 // Before the first sensor sample arrives, SDL answers reads with zeros and the shim reports no motion at all.
267 IGameInputReading* r = nullptr;
268 GameInputMotionState unused{};
269 check(input->GetCurrentReading(GameInputKindGamepad, mdev, &r) == S_OK && r && !r->GetMotionState(&unused),
270 "motion: before the first sample, GetMotionState is false even on a sensor pad");
271 if (r) r->Release();
272
273 // 9.80665 m/s^2 is half of the 2 g maximum; 17.4533 rad/s is half of 2000 deg/s. A reading with the sample needs
274 // either a gamepad change or the ~8 ms motion cadence, so the test waits out the window before polling.
275 send_motion(gyro_pad, 0.0f, 0.0f, 9.80665f, 17.4533f, 0.0f, 0.0f);
276 Sleep(12);
277 rt::gameinput::poll();
278 bool got = false;
279 const GameInputMotionState ms = motion_state(input, mdev, &got);
280 check(got, "motion: GetMotionState true on a motion reading");
281 check(approx(ms.accelerationZ, 0.5f) && approx(ms.angularVelocityX, 0.5f),
282 "motion: accel and gyro normalised against the motionInfo maxima");
283 check(ms.accelerometerAccuracy == GameInputMotionAccuracyHigh && ms.gyroscopeAccuracy == GameInputMotionAccuracyHigh,
284 "motion: live sensors report High accuracy");
285 check(ms.magnetometerAccuracy == GameInputMotionAccuracyUnknown &&
286 ms.orientationAccuracy == GameInputMotionAccuracyUnknown,
287 "motion: sensors the shim does not provide report Unknown");
288
289 r = nullptr;
290 check(input->GetCurrentReading(GameInputKindMotion, pdev, &r) == GAMEINPUT_E_READING_NOT_FOUND && !r,
291 "motion: a motion query on the plain pad is READING_NOT_FOUND");
292 check(input->GetCurrentReading(GameInputKindGamepad, mdev, &r) == S_OK && r && !r->GetMotionState(nullptr) &&
293 (r->GetInputKind() & GameInputKindMotion),
294 "motion: GetMotionState(nullptr) is false; the gyro pad's reading kind includes Motion");
295 if (r) r->Release();
296 unused = {};
297 check(input->GetCurrentReading(GameInputKindGamepad, pdev, &r) == S_OK && r &&
298 !(r->GetInputKind() & GameInputKindMotion) && !r->GetMotionState(&unused),
299 "motion: the plain pad's reading has no motion kind and GetMotionState is false");
300 if (r) r->Release();
301
302 send_motion(gyro_pad, 100.0f, -100.0f, 0.0f, 0.0f, 0.0f, -100.0f);
303 Sleep(12);
304 rt::gameinput::poll();
305 const GameInputMotionState sat = motion_state(input, mdev);
306 check(sat.accelerationX == 1.0f && sat.accelerationY == -1.0f && sat.angularVelocityZ == -1.0f,
307 "motion: out-of-range sensor values clamp to the +-1 fraction range");
308
309 // The cadence: with the buttons idle, a changed gyro yields a new reading only once per ~8 ms window, not one
310 // per poll, so the history still spans the half second it is documented for.
311 send_motion(gyro_pad, 0.0f, 0.0f, 9.80665f, 0.0f, 0.0f, 0.0f);
312 rt::gameinput::poll();
313 IGameInputReading* first = nullptr;
314 check(input->GetCurrentReading(GameInputKindMotion, mdev, &first) == S_OK && first,
315 "motion: a reading exists after a sensor update");
316 send_motion(gyro_pad, 0.0f, 0.0f, 9.80665f, 34.9066f, 0.0f, 0.0f); // gravity stays: zero accel means "no sample"
317 rt::gameinput::poll();
318 IGameInputReading* none = nullptr;
319 check(input->GetNextReading(first, GameInputKindMotion, mdev, &none) == GAMEINPUT_E_READING_NOT_FOUND && !none,
320 "motion: no new reading within the 8 ms cadence window");
321 Sleep(12);
322 rt::gameinput::poll();
323 IGameInputReading* next = nullptr;
324 check(input->GetNextReading(first, GameInputKindMotion, mdev, &next) == S_OK && next,
325 "motion: after the window the latest sensor state is in a new reading");
326 if (next) {
327 GameInputMotionState later{};
328 check(next->GetMotionState(&later) && approx(later.angularVelocityX, 1.0f),
329 "motion: the new reading carries the newest gyro sample");
330 next->Release();
331 }
332 if (first) first->Release();
333
334 detach(gyro_pad);
335 detach(plain);
336 rt::gameinput::poll();
337 if (mdev) mdev->Release();
338 if (pdev) pdev->Release();
339 check(rt::gameinput::connected_count() == 0, "motion: both pads closed again");
340}
341
209// WASD, the mouse and its buttons. Runs in the focused mode with focus and nothing held. 342// WASD, the mouse and its buttons. Runs in the focused mode with focus and nothing held.
210void test_keyboard_pad_sticks(IGameInput* input, IGameInputDevice* kb) { 343void test_keyboard_pad_sticks(IGameInput* input, IGameInputDevice* kb) {
211 rt::gameinput::poll(); 344 rt::gameinput::poll();
@@ -965,6 +1098,7 @@ int main() {
965 1098
966 test_twins(input); 1099 test_twins(input);
967 test_keyboard_pad(input); 1100 test_keyboard_pad(input);
1101 test_motion(input);
968 input->Release(); 1102 input->Release();
969 1103
970 std::printf("%d checks, %d failures\n", g_checks, g_failures); 1104 std::printf("%d checks, %d failures\n", g_checks, g_failures);