From dc01a173970ceccfefefc9e70e6cd31b1eeec43a Mon Sep 17 00:00:00 2001 From: Leonida local test Date: Sun, 22 Nov 2026 23:15:09 +0100 Subject: gameinput: report SDL's effect result from gameinput_probe (RT-160) `--force-feedback` now reads the shim's output state once the poll thread has picked the effect up, and says whether SDL created it and is running it, or why it did not, so a wheel owner can tell a silent drop from a real run. The shim records SDL_CreateHapticEffect's id, SDL_RunHapticEffect's result and SDL's error text per device; gameinput_test asserts the success and the refusal. AI-assisted --- runtime/test/gameinput_test.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 1389b8f6d..1fab89b5a 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -998,6 +998,7 @@ struct FakeHaptic { SDL_HAPTIC_SAWTOOTHUP | SDL_HAPTIC_SAWTOOTHDOWN | SDL_HAPTIC_RAMP | SDL_HAPTIC_SPRING | SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER | SDL_HAPTIC_INERTIA; int max_effects = 8; + int create_result = 1; // what create returns; negative is SDL refusing the effect int opens = 0, closes = 0, creates = 0, updates = 0, runs = 0, stops = 0, destroys = 0; int gain_calls = 0, last_gain = -1; uint32_t last_iterations = 0; @@ -1017,8 +1018,12 @@ int fake_max_effects(void* h) { return static_cast(h)->max_effects; int fake_create(void* h, const void* e) { FakeHaptic* f = static_cast(h); ++f->creates; + if (f->create_result < 0) { + SDL_SetError("fake: refused"); + return -1; + } f->last = *static_cast(e); - return 1; + return f->create_result; } bool fake_update(void* h, int, const void* e) { FakeHaptic* f = static_cast(h); @@ -1114,6 +1119,9 @@ void test_force_feedback(IGameInput* input) { const int runs0 = g_fake.runs; rt::gameinput::poll(); check(g_fake.creates == 1 && g_fake.runs == runs0 + 1, "force feedback: the poll thread creates and runs the effect"); + rt::gameinput::OutputState sent{}; + check(rt::gameinput::output_state(0, &sent) && sent.effect_id == 1 && sent.effect_ran && !sent.effect_error[0], + "force feedback: the output state reports the effect SDL created and is running"); check(g_fake.last.type == SDL_HAPTIC_CONSTANT && g_fake.last.constant.level == 16384, "force feedback: a constant 0.5 becomes SDL_HAPTIC_CONSTANT level 16384"); check(g_fake.last.constant.length == 60000 && g_fake.last_iterations == 0, @@ -1121,6 +1129,22 @@ void test_force_feedback(IGameInput* input) { rt::gameinput::poll(); check(g_fake.runs == runs0 + 1, "force feedback: an unchanged effect is not re-sent every poll"); + // SDL refusing an effect is visible, not swallowed: the output state carries the failure and SDL's error. + g_fake.create_result = -1; + IGameInputForceFeedbackEffect* refused = nullptr; + check(dev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast(&refused)) == S_OK && refused, + "force feedback: GameInput accepts an effect SDL will refuse"); + if (refused) { + rt::gameinput::poll(); + rt::gameinput::OutputState refused_out{}; + check(rt::gameinput::output_state(0, &refused_out) && refused_out.effect_id < 0 && !refused_out.effect_ran && + std::strstr(refused_out.effect_error, "fake: refused"), + "force feedback: SDL refusing the effect is recorded with its error"); + refused->Release(); + rt::gameinput::poll(); // the released effect leaves the device + } + g_fake.create_result = 1; + params.data.constant.magnitude.linearX = -1.0f; check(effect->SetParams(¶ms), "force feedback: SetParams"); rt::gameinput::poll(); -- cgit v1.2.3 From c5c9ff002ac564a687a3f3f4a418079e9881c8e0 Mon Sep 17 00:00:00 2001 From: Felix Croasdale <205819305+etcetera-yt@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:26:51 -0600 Subject: gameinput: the poll thread no longer revives a force-feedback effect being released (RT-262) ForceFeedbackEffect::Release() drops the count to 0 and deletes the effect; its destructor then waits for g().mu to unlink it from the device. In that window apply_effects (holding g().mu) could AddRef the dying effect from 0 to 1, sync it after it was freed, and delete it a second time with its own Release (@AAyberkU's finding). apply_effects now takes a reference only on a live effect (add_ref_if_live: a compare-exchange that never moves a count up from 0), so an effect whose last Release ran is skipped. gameinput_test: add_ref_if_live itself, and 20,000 effects created and released while a second thread runs the poll step. With the old AddRef that run ends in STATUS_HEAP_CORRUPTION (0xC0000374) every time here. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/gameinput.cpp | 12 ++++++++---- runtime/shims/gameinput.h | 11 +++++++++++ runtime/test/gameinput_test.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 69707f75a..440337d61 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -763,6 +763,9 @@ public: void sync(Device* device, uint64_t now, bool muted); // Poll thread, on unplug: the SDL device went with the pad, so forget its effect id without touching SDL. void orphan(); + // Poll thread, under g().mu: a reference unless the title's last Release already ran (the destructor then waits + // for g().mu to unlink the effect, so it is still in the device's list but must not be used or revived). + bool add_ref_if_live() { return rt::gameinput::add_ref_if_live(refs_); } private: std::atomic refs_{1}; @@ -1399,10 +1402,11 @@ void apply_effects(Device* d, uint64_t now) { { std::lock_guard lock(s.mu); live.reserve(d->effects.size()); - for (ForceFeedbackEffect* e : d->effects) { // referenced: the title may release one while this runs - e->AddRef(); - live.push_back(e); - } + // Referenced: the title may release one while this runs. An effect whose count already reached 0 is being + // destroyed (its destructor waits for this lock to unlink it); a plain AddRef revived it, and this loop's own + // Release below freed it a second time. + for (ForceFeedbackEffect* e : d->effects) + if (e->add_ref_if_live()) live.push_back(e); retire.swap(d->retired); motor_gain = d->motor_gain; muted = !s.config.rumble; diff --git a/runtime/shims/gameinput.h b/runtime/shims/gameinput.h index acee24e85..a32389a5e 100644 --- a/runtime/shims/gameinput.h +++ b/runtime/shims/gameinput.h @@ -1,12 +1,23 @@ // GameInput shim on SDL3: runtime-side controls and test seams. The GameInput API itself is reached the way a title // reaches it, through GameInputCreate in the shim registry (gameinput.dll / xgameruntime.dll). See docs/gameinput.md. #pragma once +#include #include #include #include namespace rt::gameinput { +// Takes a reference on a reference-counted object only while it is alive, that is while its count isn't 0. A thread +// that finds the object in a shared list (the poll thread and a device's force-feedback effects) then can't revive one +// whose last Release has already run and whose destructor is waiting for the lock to unlink it. False: leave it be. +template +bool add_ref_if_live(std::atomic& refs) { + Count n = refs.load(); + while (n != 0 && !refs.compare_exchange_weak(n, n + 1)) {} + return n != 0; +} + // How the impulse-trigger motors (GameInputRumbleParams::leftTrigger/rightTrigger) reach a DualSense. enum class DsTriggerMode { Off, Rumble, Resistance }; diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 1389b8f6d..2737b9fc9 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -12,12 +12,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include "gameinput.h" @@ -345,6 +347,14 @@ std::vector raw_bytes(IGameInputRawDeviceReport* r) { return v; } +// RT-262: the poll thread takes a reference on a force-feedback effect only while it is alive. +void test_add_ref_if_live() { + std::atomic live{1}, dying{0}; + check(rt::gameinput::add_ref_if_live(live) && live.load() == 2, "add_ref_if_live: a live count goes up by one"); + check(!rt::gameinput::add_ref_if_live(dying) && dying.load() == 0, + "add_ref_if_live: a count of 0 stays 0, so an effect being destroyed isn't revived"); +} + void test_raw_hid(IGameInput* input) { test_parse_report_descriptor(); rt::gameinput::set_hid_backend(&kFakeRawHid); @@ -1209,6 +1219,28 @@ void test_force_feedback(IGameInput* input) { rt::gameinput::poll(); check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); + // RT-262: the title releases effects while the poll thread applies them. The poll thread used to AddRef an effect + // whose count had already reached 0 (its destructor waiting for the lock), use it after it was freed, and free it + // a second time with its own Release. + { + std::atomic stop{false}; + std::thread poller([&] { + while (!stop.load()) rt::gameinput::poll(); + }); + int created = 0; + for (int i = 0; i < 20000; ++i) { + IGameInputForceFeedbackEffect* e = nullptr; + if (dev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast(&e)) == S_OK && e) { + ++created; + e->Release(); + } + } + stop = true; + poller.join(); + rt::gameinput::poll(); + check(created == 20000, "force feedback: 20,000 effects created and released while the poll thread runs"); + } + detach(wheel); rt::gameinput::poll(); check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0), @@ -1319,6 +1351,7 @@ int main() { set_env("LEONIDA_DS_TRIGGERS", nullptr); set_env("LEONIDA_DS_LIGHTBAR", nullptr); set_env("LEONIDA_RUMBLE", nullptr); + test_add_ref_if_live(); auto create = reinterpret_cast(rt::shims::find("gameinput.dll", "GameInputCreate")); check(create != nullptr, "gameinput.dll!GameInputCreate is registered"); -- cgit v1.2.3 From 399b30f69b7105593e92c06499cdd8cc618edbb9 Mon Sep 17 00:00:00 2001 From: Maxime Mansiet Date: Mon, 23 Nov 2026 02:59:55 +0100 Subject: gameinput: GetRawReport holds hid_mu while it reads the raw reports Signed-off-by: Maxime Mansiet --- docs/gameinput.md | 3 +- runtime/shims/gameinput.cpp | 1 + runtime/test/gameinput_test.cpp | 65 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/docs/gameinput.md b/docs/gameinput.md index fa216a2ee..ada315b09 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -415,7 +415,8 @@ rests on a transcription rather than on a side-by-side compile. - A fake backend (`set_hid_backend`) stands behind a virtual pad with pid.codes test ids. The test checks the report tables, the kind and status bits, a reading's raw report (newest declared id, without the id byte, an undeclared id ignored), creating, sending and reading back output and feature reports with their id byte, `SetRawData`'s exact - size, the item calls, `E_INVALIDARG` / `E_FAIL` / `E_NOTIMPL`, and the HID device closing on unplug. + size, the item calls, `E_INVALIDARG` / `E_FAIL` / `E_NOTIMPL`, the HID device closing on unplug, and a held + reading's `GetRawReport` waiting while a reconnect rebuilds the report tables. - `gameinput_probe [--seconds N] [--rumble]` (by hand, with a pad connected): lists the pads with each one's raw HID report tables (or why it has none), plays each motor and trigger for a second with `--rumble`, then prints the live reading and the newest raw input report. diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 69707f75a..7a915048f 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -647,6 +647,7 @@ bool Reading::GetRawReport(void** report) { if (!report) return false; *report = nullptr; if (!s_.has_raw) return false; + std::lock_guard lock(device_->hid_mu); const GameInputRawDeviceReportInfo* info = device_->find_raw(GameInputRawInputReport, s_.raw_id); if (!info) return false; *report = static_cast(new RawReport(device_, *info, s_.raw.data(), s_.raw.size())); diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 1389b8f6d..d410f20dd 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "gameinput.h" @@ -299,11 +300,16 @@ struct FakeRawHid { std::vector> writes, features_sent; std::vector feature_reply; bool fail = false; + HANDLE in_open = nullptr, release_open = nullptr; }; FakeRawHid g_raw; void* raw_open(const char*, uint16_t vendor, uint16_t product) { if (vendor != g_raw.vendor || product != g_raw.product) return nullptr; ++g_raw.opens; + if (g_raw.release_open) { + SetEvent(g_raw.in_open); + WaitForSingleObject(g_raw.release_open, 5000); + } return &g_raw; } void raw_close(void*) { ++g_raw.closes; } @@ -456,6 +462,64 @@ void test_raw_hid(IGameInput* input) { rt::gameinput::set_hid_backend(nullptr); } +void test_raw_report_during_reconnect(IGameInput* input) { + rt::gameinput::set_hid_backend(&kFakeRawHid); + Pad raw_pad; + attach(raw_pad, "Leonida virtual raw pad", g_raw.vendor, g_raw.product, false); + rt::gameinput::poll(); + g_raw.inputs.push_back({0x01, 0x44, 0x55, 0x66}); + rt::gameinput::poll(); + IGameInputReading* held = nullptr; + check(input->GetCurrentReading(GameInputKindRawDeviceReport, nullptr, &held) == S_OK && held, + "raw hid reconnect: a reading with a raw report to hold"); + detach(raw_pad); + rt::gameinput::poll(); + if (!held) { + rt::gameinput::set_hid_backend(nullptr); + return; + } + + g_raw.in_open = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_raw.release_open = CreateEventW(nullptr, TRUE, FALSE, nullptr); + HANDLE read_done = CreateEventW(nullptr, TRUE, FALSE, nullptr); + attach(raw_pad, "Leonida virtual raw pad", g_raw.vendor, g_raw.product, false); + std::thread poller([] { rt::gameinput::poll(); }); + const bool in_open = WaitForSingleObject(g_raw.in_open, 5000) == WAIT_OBJECT_0; + bool got = false; + void* report = nullptr; + std::thread reader([&] { + got = held->GetRawReport(&report); + SetEvent(read_done); + }); + const bool waited = WaitForSingleObject(read_done, 200) == WAIT_TIMEOUT; + SetEvent(g_raw.release_open); + poller.join(); + const bool finished = WaitForSingleObject(read_done, 5000) == WAIT_OBJECT_0; + reader.join(); + check(in_open && waited, "raw hid reconnect: GetRawReport waits while open_raw rebuilds the report tables"); + auto* rr = static_cast(report); + check(finished && got && rr && raw_bytes(rr) == std::vector({0x44, 0x55, 0x66}), + "raw hid reconnect: then it gives the held reading's report"); + if (rr) rr->Release(); + IGameInputDevice* dev = nullptr; + held->GetDevice(&dev); + void* created = nullptr; + check(dev && (dev->GetDeviceStatus() & GameInputDeviceRawIoEnabled) && + dev->CreateRawDeviceReport(2, GameInputRawOutputReport, &created) == S_OK && created, + "raw hid reconnect: and the reconnected pad has raw I/O again"); + if (created) static_cast(created)->Release(); + if (dev) dev->Release(); + held->Release(); + + CloseHandle(read_done); + CloseHandle(g_raw.in_open); + CloseHandle(g_raw.release_open); + g_raw.in_open = g_raw.release_open = nullptr; + detach(raw_pad); + rt::gameinput::poll(); + rt::gameinput::set_hid_backend(nullptr); +} + void test_motion(IGameInput* input) { const size_t events_before = g_device_events.size(); // other tests' events are still queued in the vector Pad gyro_pad, plain; @@ -1822,6 +1886,7 @@ int main() { test_force_feedback(input); test_haptic_motors(input); test_raw_hid(input); + test_raw_report_during_reconnect(input); input->Release(); std::printf("%d checks, %d failures\n", g_checks, g_failures); -- cgit v1.2.3 From 0519bc2c8108089c65652f550e8b950f9731eb33 Mon Sep 17 00:00:00 2001 From: Maxime Mansiet Date: Mon, 23 Nov 2026 03:02:17 +0100 Subject: gameinput: a reading callback's inputKind filter checks what the device supports Signed-off-by: Maxime Mansiet --- docs/gameinput.md | 8 +++++--- runtime/shims/gameinput.cpp | 2 +- runtime/test/gameinput_test.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/docs/gameinput.md b/docs/gameinput.md index fa216a2ee..7c8cefb74 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -222,7 +222,9 @@ intensity 0; waveform indices other than 0 are `E_INVALIDARG`. Pads whose SDL gamepad reports both a gyroscope and an accelerometer (a DualSense, a Switch Pro, Steam Input pads) get `GameInputKindMotion` in `supportedInput` and a `motionInfo` (`GameInputMotionInfo`, Microsoft's v0 layout); `IGameInputReading::GetMotionState` returns a `GameInputMotionState` on their readings, and `GetCurrentReading`/`GetNextReading` answer a -`GameInputKindMotion` query for such a pad and return `GAMEINPUT_E_READING_NOT_FOUND` for a pad without sensors. The +`GameInputKindMotion` query for such a pad and return `GAMEINPUT_E_READING_NOT_FOUND` for a pad without sensors. A +reading callback registered for `GameInputKindMotion` hears only such pads: like a device callback, it is limited to +devices that support at least one of the kinds it names (Microsoft's `RegisterReadingCallback` reference). The sensors are enabled when the pad is opened, at SDL's requested rate, and every poll reads the latest sample. - The state carries **signed fractions of the maxima `motionInfo` reports** (1.0 = full magnitude), which is the @@ -403,8 +405,8 @@ rests on a transcription rather than on a side-by-side compile. first twin's id equal to the single-pad id). Motion runs on a virtual pad with sensors (`nsensors` in the virtual-joystick description, fed with `SDL_SendJoystickVirtualSensorData`): the kind and `motionInfo` on a sensor pad and not on a plain one, no motion before the first sample, normalisation against the `motionInfo` maxima, the - accuracy values, the ±1 clamp, the ~8 ms cadence, and that a motion query on a pad without sensors is - `GAMEINPUT_E_READING_NOT_FOUND`. The keyboard pad is + accuracy values, the ±1 clamp, the ~8 ms cadence, that a motion query on a pad without sensors is + `GAMEINPUT_E_READING_NOT_FOUND`, and that a Motion reading callback hears only the sensor pad. The keyboard pad is driven through `rt::gameinput::set_keyboard_source` and `set_cursor_source` with fake keys, focus and cursor: every binding, two keys at once, each WASD direction, opposite keys cancelling, diagonals, the mouse moving the right stick, clamping and decaying to 0, the mouse-button triggers, no input out of focus, release of buttons, sticks and diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 69707f75a..dbd2cbe72 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -823,7 +823,7 @@ bool matches(const Callback& c, const Event& e) { if (!(c.kind & e.device->info.supportedInput) && c.kind != GameInputKindUnknown) return false; return e.only || ((e.current ^ e.previous) & c.status_filter); case CbType::Reading: - return (c.kind & kSupportedKinds) || c.kind == GameInputKindUnknown; + return c.kind == GameInputKindUnknown || (c.kind & e.device->info.supportedInput); default: return true; } diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 1389b8f6d..500490e33 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -241,6 +241,20 @@ GameInputMotionState motion_state(IGameInput* input, IGameInputDevice* dev, bool return ms; } +struct MotionReadings { + IGameInputDevice* gyro; + IGameInputDevice* plain; + int gyro_calls, plain_calls; +}; +void CALLBACK on_motion_reading(GameInputCallbackToken, void* context, IGameInputReading* reading, bool) { + auto* seen = static_cast(context); + IGameInputDevice* dev = nullptr; + if (reading) reading->GetDevice(&dev); + if (dev == seen->gyro) ++seen->gyro_calls; + if (dev == seen->plain) ++seen->plain_calls; + if (dev) dev->Release(); +} + // ---- raw HID (RT-175) ------------------------------------------------------------------------------------------- using rt::gameinput::RawReportSize; @@ -558,6 +572,19 @@ void test_motion(IGameInput* input) { } if (first) first->Release(); + MotionReadings seen{mdev, pdev, 0, 0}; + GameInputCallbackToken motion_token = 0; + check(input->RegisterReadingCallback(nullptr, GameInputKindMotion, 0.0f, &seen, on_motion_reading, &motion_token) == S_OK, + "motion: RegisterReadingCallback(GameInputKindMotion)"); + for (int i = 0; i < 4; ++i) { + SDL_SetJoystickVirtualButton(gyro_pad.js, SDL_GAMEPAD_BUTTON_SOUTH, i % 2 == 0); + SDL_SetJoystickVirtualButton(plain.js, SDL_GAMEPAD_BUTTON_SOUTH, i % 2 == 0); + rt::gameinput::poll(); + } + input->UnregisterCallback(motion_token, 0); + check(seen.gyro_calls > 0 && seen.plain_calls == 0, + "motion: a Motion reading callback fires for the gyro pad's readings, not for the plain pad's"); + detach(gyro_pad); detach(plain); rt::gameinput::poll(); -- cgit v1.2.3