From a3b1719c61b4b916964368bf9968e5f822cd50ca Mon Sep 17 00:00:00 2001 From: Leonida local test Date: Sun, 22 Nov 2026 18:26:28 +0100 Subject: gameinput: report and drive force-feedback and haptic motors through SDL3 haptics (RT-160) IGameInputDevice answered the force-feedback and haptic half of the API with nothing: no motors in GameInputDeviceInfo, E_INVALIDARG from CreateForceFeedbackEffect, false from IsForceFeedbackMotorPoweredOn, a no-op SetForceFeedbackMotorGain and E_INVALIDARG from SetHapticMotorState. A pad SDL opens as a haptic device now reports one force-feedback motor with the effect kinds SDL lists, and a pad with trigger motors reports two haptic motors. Force feedback goes to SDL_CreateHapticEffect/RunHapticEffect on the poll thread, so title calls still never touch SDL; the eleven GameInput effect kinds are SDL's eleven force-feedback effects. The trigger haptic motors play their one waveform as an intensity on the existing trigger output. A pad with neither keeps the old answers, and LEONIDA_RUMBLE=0 silences both. gameinput_test drives all of it through a fake haptic backend, because SDL's virtual joysticks report no haptics. AI-assisted. --- runtime/test/gameinput_test.cpp | 323 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index f525d1cb9..106f68fe4 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -718,6 +718,327 @@ void test_twins(IGameInput* input) { check(rt::gameinput::connected_count() == 0, "twins: removed at the end"); } +// ---- force feedback and haptic motors: a fake SDL haptic backend ---- +// SDL's virtual joysticks report no haptics, so the shim's haptic calls go through this table instead. It records what +// the shim asked SDL to do and reports the capabilities a force-feedback wheel has. +struct FakeHaptic { + uint32_t features = SDL_HAPTIC_CONSTANT | SDL_HAPTIC_SINE | SDL_HAPTIC_SQUARE | SDL_HAPTIC_TRIANGLE | + 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 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; + SDL_HapticEffect last{}; +}; +FakeHaptic g_fake; +bool g_fake_open = false; // pads attached while this is set are opened as haptic devices + +void* fake_open(void*) { + if (!g_fake_open) return nullptr; + ++g_fake.opens; + return &g_fake; +} +void fake_close(void*) { ++g_fake.closes; } +uint32_t fake_features(void* h) { return static_cast(h)->features; } +int fake_axes(void*) { return 1; } +int fake_max_effects(void* h) { return static_cast(h)->max_effects; } +bool fake_supported(void*, const void*) { return true; } +int fake_create(void* h, const void* e) { + FakeHaptic* f = static_cast(h); + ++f->creates; + f->last = *static_cast(e); + return 1; +} +bool fake_update(void* h, int, const void* e) { + FakeHaptic* f = static_cast(h); + ++f->updates; + f->last = *static_cast(e); + return true; +} +bool fake_run(void* h, int, uint32_t iterations) { + FakeHaptic* f = static_cast(h); + ++f->runs; + f->last_iterations = iterations; + return true; +} +bool fake_stop(void* h, int) { + ++static_cast(h)->stops; + return true; +} +void fake_destroy(void* h, int) { ++static_cast(h)->destroys; } +bool fake_gain(void* h, int gain) { + FakeHaptic* f = static_cast(h); + ++f->gain_calls; + f->last_gain = gain; + return true; +} +const rt::gameinput::HapticApi kFakeHaptic{fake_open, fake_close, fake_features, fake_axes, fake_max_effects, + fake_supported, fake_create, fake_update, fake_run, fake_stop, + fake_destroy, fake_gain}; + +// The motors of one device, and the effect object its force-feedback motor hands out. Runs with no other pad. +void test_force_feedback(IGameInput* input) { + rt::gameinput::set_haptic_source(&kFakeHaptic); + g_fake = {}; + g_fake_open = true; + Pad wheel; + attach(wheel, "Leonida virtual wheel", 0x046D, 0xC24F, false); + const size_t before = g_device_events.size(); + rt::gameinput::poll(); + IGameInputDevice* dev = g_device_events.size() > before ? g_device_events.back().device : nullptr; + check(dev && rt::gameinput::connected_count() == 1 && g_fake.opens == 1, + "force feedback: the pad's joystick is opened as an SDL haptic device"); + if (!dev) return; + dev->AddRef(); + rt::gameinput::OutputState out{}; + check(rt::gameinput::output_state(0, &out) && out.force_feedback, "force feedback: the output state says so"); + IGameInputReading* r = current(input, dev); + check(r != nullptr, "force feedback: the pad still reads normally with a haptic device open"); + if (r) r->Release(); + + const GameInputDeviceInfo* info = dev->GetDeviceInfo(); + check(info->forceFeedbackMotorCount == 1 && info->forceFeedbackMotorInfo != nullptr, + "force feedback: the device reports one force-feedback motor"); + const GameInputForceFeedbackMotorInfo* motor = + static_cast(info->forceFeedbackMotorInfo); + check(motor->supportedAxes == GameInputFeedbackAxisLinearX && motor->location == GameInputLocationChassis && + motor->maxSimultaneousEffects == 8, + "force feedback: motorInfo carries the axis, the location and the effect limit"); + check(motor->isConstantEffectSupported && motor->isRampEffectSupported && motor->isSpringEffectSupported && + motor->isSawtoothDownWaveEffectSupported, + "force feedback: the effect kinds come from SDL's feature bits"); + check(info->hapticFeedbackMotorCount == 0, "force feedback: a wheel without trigger motors reports no haptics"); + check(dev->IsForceFeedbackMotorPoweredOn(0) && !dev->IsForceFeedbackMotorPoweredOn(1), + "force feedback: motor 0 is powered on, and motor 1 does not exist"); + + IGameInputForceFeedbackEffect* effect = nullptr; + GameInputForceFeedbackParams params{}; + params.kind = GameInputForceFeedbackConstant; + params.data.constant.magnitude.linearX = 0.5f; + check(dev->CreateForceFeedbackEffect(1, ¶ms, reinterpret_cast(&effect)) == E_INVALIDARG && !effect, + "force feedback: motor 1 does not exist"); + check(dev->CreateForceFeedbackEffect(0, ¶ms, nullptr) == E_POINTER, "force feedback: a null effect is E_POINTER"); + check(dev->CreateForceFeedbackEffect(0, nullptr, reinterpret_cast(&effect)) == E_INVALIDARG, + "force feedback: null parameters are E_INVALIDARG"); + GameInputForceFeedbackParams unknown{}; + unknown.kind = static_cast(11); + check(dev->CreateForceFeedbackEffect(0, &unknown, reinterpret_cast(&effect)) == E_INVALIDARG, + "force feedback: an effect kind outside the enum is E_INVALIDARG"); + check(dev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast(&effect)) == S_OK && effect, + "force feedback: CreateForceFeedbackEffect"); + if (!effect) { // leave nothing behind for the next test + detach(wheel); + dev->Release(); + rt::gameinput::poll(); + rt::gameinput::set_haptic_source(nullptr); + return; + } + check(effect->GetMotorIndex() == 0 && approx(effect->GetGain(), 1.0f) && effect->GetState() == GameInputFeedbackRunning, + "force feedback: a new effect is on motor 0, at full gain, running"); + IGameInputDevice* owner = nullptr; + effect->GetDevice(&owner); + check(owner == dev, "force feedback: the effect names its device"); + if (owner) owner->Release(); + + // Nothing is asked of SDL before the poll thread runs: the title only wrote the parameters. + 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"); + 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, + "force feedback: no duration in GameInput is SDL's 60 s hold, run until stopped"); + rt::gameinput::poll(); + check(g_fake.runs == runs0 + 1, "force feedback: an unchanged effect is not re-sent every poll"); + + params.data.constant.magnitude.linearX = -1.0f; + check(effect->SetParams(¶ms), "force feedback: SetParams"); + rt::gameinput::poll(); + check(g_fake.updates == 1 && g_fake.runs == runs0 + 2 && g_fake.last.constant.level == -32767, + "force feedback: changed parameters update the SDL effect once"); + GameInputForceFeedbackParams read{}; + effect->GetParams(&read); + check(read.kind == GameInputForceFeedbackConstant && read.data.constant.magnitude.linearX == -1.0f, + "force feedback: GetParams returns what was set"); + + effect->SetGain(0.5f); + rt::gameinput::poll(); + check(approx(effect->GetGain(), 0.5f) && g_fake.last.constant.level == -16384, + "force feedback: the effect's gain scales the SDL level"); + // The device gain is SDL's, sent once when the pad is opened (100) and once per change. + check(g_fake.last_gain == 100 && g_fake.gain_calls == 1, "force feedback: the device gain starts at SDL's full 100"); + dev->SetForceFeedbackMotorGain(0, 0.5f); + rt::gameinput::poll(); + check(g_fake.last_gain == 50 && g_fake.gain_calls == 2, + "force feedback: SetForceFeedbackMotorGain sets SDL's device gain once"); + dev->SetForceFeedbackMotorGain(0, 1.0f); + effect->SetGain(1.0f); + rt::gameinput::poll(); + check(g_fake.last_gain == 100, "force feedback: the master gain follows the request"); + + // A wave with GameInput's envelope: 50 Hz for 90 degrees over 100 ms, played once. + GameInputForceFeedbackParams wave{}; + wave.kind = GameInputForceFeedbackSineWave; + wave.data.sineWave.envelope.sustainDuration = 100'000; // microseconds + wave.data.sineWave.envelope.sustainGain = 1.0f; + wave.data.sineWave.envelope.playCount = 1; + wave.data.sineWave.magnitude.linearX = 1.0f; + wave.data.sineWave.frequency = 50.0f; + wave.data.sineWave.phase = 90.0f; + check(effect->SetParams(&wave), "force feedback: SetParams for a sine wave"); + rt::gameinput::poll(); + check(g_fake.last.type == SDL_HAPTIC_SINE && g_fake.last.periodic.period == 20 && + g_fake.last.periodic.magnitude == 32767 && g_fake.last.periodic.phase == 9000, + "force feedback: a 50 Hz sine with a 90 degree phase becomes SDL's period and hundredths of a degree"); + check(g_fake.last.periodic.length == 100 && g_fake.last_iterations == 1, + "force feedback: the envelope's 100 ms is SDL's length and its playCount the iteration count"); + Sleep(130); + rt::gameinput::poll(); + check(effect->GetState() == GameInputFeedbackStopped && g_fake.stops >= 1, + "force feedback: an effect that ran its duration reports stopped"); + + // A condition effect: coefficients, saturations, dead zone and centre. + GameInputForceFeedbackParams spring{}; + spring.kind = GameInputForceFeedbackSpring; + spring.data.spring.magnitude.linearX = 0.5f; + spring.data.spring.positiveCoefficient = 0.5f; + spring.data.spring.negativeCoefficient = 0.25f; + spring.data.spring.maxPositiveMagnitude = 1.0f; + spring.data.spring.maxNegativeMagnitude = 0.5f; + spring.data.spring.deadZone = 0.1f; + check(effect->SetParams(&spring), "force feedback: SetParams for a spring"); + effect->SetState(GameInputFeedbackRunning); + rt::gameinput::poll(); + check(g_fake.last.type == SDL_HAPTIC_SPRING && g_fake.last.condition.right_coeff[0] == 16384 && + g_fake.last.condition.left_coeff[0] == -8192 && g_fake.last.condition.right_sat[0] == 65535 && + g_fake.last.condition.left_sat[0] == 32768 && g_fake.last.condition.deadband[0] == 6554 && + g_fake.last.condition.center[0] == 16384, + "force feedback: a spring maps its coefficients, saturations, dead zone and centre"); + + effect->SetState(GameInputFeedbackPaused); + const int stops0 = g_fake.stops; + rt::gameinput::poll(); + check(g_fake.stops == stops0 + 1 && effect->GetState() == GameInputFeedbackPaused, + "force feedback: a paused effect is stopped in SDL"); + + // LEONIDA_RUMBLE=0 silences this path too: a running effect is stopped, and it runs again when the mute lifts. + effect->SetState(GameInputFeedbackRunning); + rt::gameinput::poll(); + const int runs1 = g_fake.runs, stops1 = g_fake.stops; + check(g_fake.runs == runs1, "force feedback: the effect is running before the mute"); + set_env("LEONIDA_RUMBLE", "0"); + rt::gameinput::reload_config(); + rt::gameinput::poll(); + check(g_fake.runs == runs1 && g_fake.stops == stops1 + 1, "force feedback: LEONIDA_RUMBLE=0 stops the effect"); + set_env("LEONIDA_RUMBLE", nullptr); + rt::gameinput::reload_config(); + rt::gameinput::poll(); + check(g_fake.runs == runs1 + 1, "force feedback: and it runs again once the mute is lifted"); + + check(effect->Release() == 0, "force feedback: the effect is freed once the title releases it"); + rt::gameinput::poll(); + check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); + + detach(wheel); + rt::gameinput::poll(); + check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0), + "force feedback: unplugging closes the haptic device"); + dev->Release(); + + // A pad SDL opens no haptic device for keeps the old answer: no motor, and no effect. + g_fake_open = false; + Pad plain; + attach(plain, "Leonida virtual plain pad", 0x045E, 0x02FF, false); + const size_t plain_before = g_device_events.size(); + rt::gameinput::poll(); + IGameInputDevice* plaindev = g_device_events.size() > plain_before ? g_device_events.back().device : nullptr; + check(plaindev && plaindev->GetDeviceInfo()->forceFeedbackMotorCount == 0 && + plaindev->GetDeviceInfo()->forceFeedbackMotorInfo == nullptr, + "force feedback: a pad without SDL haptics reports no motor"); + if (plaindev) { + effect = nullptr; + check(plaindev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast(&effect)) == E_INVALIDARG && !effect, + "force feedback: so CreateForceFeedbackEffect is E_INVALIDARG"); + check(!plaindev->IsForceFeedbackMotorPoweredOn(0), "force feedback: and no motor is powered on"); + } + detach(plain); + rt::gameinput::poll(); + rt::gameinput::set_haptic_source(nullptr); +} + +// The trigger haptic motors of a pad that has them. Runs with no other pad. +void test_haptic_motors(IGameInput* input) { + set_env("LEONIDA_DS_TRIGGERS", nullptr); + rt::gameinput::reload_config(); + Pad xbox; + attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); + const size_t before = g_device_events.size(); + rt::gameinput::poll(); + IGameInputDevice* dev = g_device_events.size() > before ? g_device_events.back().device : nullptr; + check(dev && rt::gameinput::connected_count() == 1, "haptics: the pad with trigger motors is connected"); + if (!dev) return; + dev->AddRef(); + const GameInputDeviceInfo* info = dev->GetDeviceInfo(); + check(info->hapticFeedbackMotorCount == 2 && info->hapticFeedbackMotorInfo != nullptr, + "haptics: the pad reports two haptic motors"); + const GameInputHapticFeedbackMotorInfo* motors = + static_cast(info->hapticFeedbackMotorInfo); + check(motors[0].mappedRumbleMotors == GameInputRumbleLeftTrigger && + motors[1].mappedRumbleMotors == GameInputRumbleRightTrigger && motors[0].waveformCount == 1 && + motors[0].waveformInfo != nullptr && motors[0].waveformInfo->isIntensitySupported, + "haptics: they are the left and right trigger, one waveform each, an intensity"); + check(sticks_at_rest(pad_state(input, dev)), "haptics: the pad still reads normally"); + + GameInputHapticFeedbackParams p{}; + p.intensity = 0.5f; + p.duration = 100'000; // microseconds + p.playCount = 1; + check(dev->SetHapticMotorState(2, &p) == E_INVALIDARG, "haptics: motor 2 does not exist"); + GameInputHapticFeedbackParams other_wave = p; + other_wave.waveformIndex = 1; + check(dev->SetHapticMotorState(0, &other_wave) == E_INVALIDARG, "haptics: waveform 1 does not exist"); + check(dev->SetHapticMotorState(0, &p) == S_OK, "haptics: SetHapticMotorState on the left trigger"); + rt::gameinput::poll(); + check(xbox.left == 32768 && xbox.right == 0, "haptics: the intensity reaches the trigger motor"); + Sleep(130); + rt::gameinput::poll(); + check(xbox.left == 0, "haptics: the request ends with its duration"); + + p.intensity = 1.0f; + p.playCount = 0; // until changed + check(dev->SetHapticMotorState(1, &p) == S_OK, "haptics: playCount 0 plays until changed"); + rt::gameinput::poll(); + check(xbox.right == 65535 && xbox.left == 0, "haptics: the right trigger takes its own motor"); + Sleep(130); + rt::gameinput::poll(); + check(xbox.right == 65535, "haptics: and keeps playing past the duration"); + check(dev->SetHapticMotorState(1, nullptr) == S_OK, "haptics: a null state stops the motor"); + rt::gameinput::poll(); + check(xbox.right == 0 && xbox.left == 0, "haptics: both triggers are silent again"); + + // SetRumbleState still works on the same pad, and the two share the trigger output. + GameInputRumbleParams rumble{0, 0, 0.25f, 0}; + dev->SetRumbleState(&rumble); + rt::gameinput::poll(); + check(xbox.left == 16384, "haptics: SetRumbleState still drives the trigger motor"); + p.intensity = 1.0f; + p.playCount = 1; + p.duration = 0; // no deadline at playCount 1 either: the level holds until it is set again + dev->SetHapticMotorState(0, &p); + rt::gameinput::poll(); + check(xbox.left == 65535, "haptics: the stronger of the two wins on one trigger"); + dev->SetRumbleState(nullptr); + dev->SetHapticMotorState(0, nullptr); + rt::gameinput::poll(); + check(xbox.left == 0, "haptics: releasing both leaves the trigger silent"); + + detach(xbox); + rt::gameinput::poll(); + check(rt::gameinput::connected_count() == 0, "haptics: detached"); + dev->Release(); +} + } // namespace int main() { @@ -1170,6 +1491,8 @@ int main() { test_keyboard_pad(input); test_motion(input); test_aggregate_device(input); + test_force_feedback(input); + test_haptic_motors(input); input->Release(); std::printf("%d checks, %d failures\n", g_checks, g_failures); -- cgit v1.2.3 From 2aed56c6e50c4fa577c50e5b10976ba9f9a7379f Mon Sep 17 00:00:00 2001 From: marquezini Date: Sun, 22 Nov 2026 14:31:09 -0300 Subject: RT-176: IGameInputDispatcher::Dispatch honours its quota Dispatch ran the whole queue whatever quota it was given. It now runs queued events oldest first, at least one per call (a quota of 0 runs exactly one, as the GDK documents), takes no further event once the quota has passed, and returns whether events remain. While events remain the wait handle is signalled again, so a title that waits on it before each call comes back for the rest. The automatic path (no dispatcher, poll thread) still delivers the whole queue. gameinput_test checks Dispatch(0), a 1 ms quota against a 5 ms callback, a large quota draining the queue, and an empty queue. --- docs/gameinput.md | 8 +++-- runtime/shims/gameinput.cpp | 74 ++++++++++++++++++++++++++++++----------- runtime/test/gameinput_test.cpp | 29 ++++++++++++++++ 3 files changed, 88 insertions(+), 23 deletions(-) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/docs/gameinput.md b/docs/gameinput.md index f848ab0d2..383b16558 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -181,7 +181,9 @@ pads, closes removed ones, takes a reading of each, and applies rumble requests. the latest reading or leave a rumble request, so a call costs a lock and a copy. Callbacks (device, reading, guide button) run on that thread, as GameInput runs them on its own threads. Once the title creates a dispatcher (`IGameInput::CreateDispatcher`), callbacks are queued instead, the dispatcher's wait handle is signalled, and -`IGameInputDispatcher::Dispatch` runs them on the title's thread. +`IGameInputDispatcher::Dispatch` runs them on the title's thread, oldest first, within its quota: at least one queued +event per call (a quota of 0 runs exactly one), no further event once the quota has passed, and the wait handle is +signalled again while events remain. The API surface: @@ -190,7 +192,7 @@ The API surface: | `IGameInput` | `GetCurrentTimestamp` (µs), `GetCurrentReading`, `GetNextReading`, `GetPreviousReading`, `RegisterReadingCallback`, `RegisterDeviceCallback` (blocking and async enumeration), `RegisterGuideButtonCallback`, `RegisterKeyboardLayoutCallback` (never fires), `StopCallback`, `UnregisterCallback`, `CreateDispatcher`, `FindDeviceFromId`, `EnableOemDeviceSupport` (no-op), `SetFocusPolicy` (no-op) | `GetTemporalReading`, `CreateAggregateDevice`, `FindDeviceFromObject/PlatformHandle/PlatformString` | | `IGameInputReading` | kind, sequence number, timestamp, device, gamepad state, UI navigation state, controller axes and buttons, motion state | raw report, keyboard, mouse, touch, arcade/flight stick, racing wheel (all return nothing) | | `IGameInputDevice` | device info, status, battery, `SetRumbleState` | force feedback and haptic motors (none reported), raw device I/O, power off, input synchronization (no-ops) | -| `IGameInputDispatcher` | `Dispatch`, `OpenWaitHandle` | the quota (the queue is short) | +| `IGameInputDispatcher` | `Dispatch` (with its quota), `OpenWaitHandle` | | A device object stays alive for the whole process. When a pad is unplugged its status drops to `GameInputDeviceNoStatus` and its device callback runs; when the same pad is plugged in again (matched by SDL GUID and @@ -256,7 +258,7 @@ shim fills (`GameInputDeviceInfo` 320 bytes with `deviceFamily` at 96 and the in the fact, both directions, two pads interleaved and filtered by device, ageing out, held readings, unplug and replug), the guide button, motor and trigger rumble as SDL receives them, the DualSense effect bytes in each `LEONIDA_DS_TRIGGERS` mode, the lightbar, a - dispatcher, async and blocking enumeration, unregistering, unplugging and replugging, and three identical pads + dispatcher and its quota, async and blocking enumeration, unregistering, unplugging and replugging, and three identical pads (an id each, `FindDeviceFromId` and device-filtered readings per pad, replugging one, two and all three, and the 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 diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 35eac4c4e..61999a3ad 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -423,6 +424,28 @@ void call(const Callback& c, const Event& e) { } } +// Runs one queued event's callbacks on the calling thread, without holding the state lock. +void deliver_event(const Event& e) { + State& s = g(); + std::vector targets; + { + std::lock_guard lock(s.mu); + for (const Callback& c : s.callbacks) + if (matches(c, e)) targets.push_back(c); + } + for (const Callback& c : targets) { + bool still = false; + { + std::lock_guard lock(s.mu); + for (const Callback& now : s.callbacks) + if (now.token == c.token) still = now.active; + } + if (still) call(c, e); + } + if (e.device) e.device->Release(); + if (e.reading) e.reading->Release(); +} + // Runs queued callbacks on the calling thread, without holding the state lock. Returns whether more events arrived // meanwhile. bool deliver() { @@ -433,30 +456,41 @@ bool deliver() { std::lock_guard lock(s.mu); events.swap(s.queue); } - const DWORD self = GetCurrentThreadId(); - s.delivering_thread = self; - for (const Event& e : events) { - std::vector targets; + s.delivering_thread = GetCurrentThreadId(); + for (const Event& e : events) deliver_event(e); + s.delivering_thread = 0; + std::lock_guard lock(s.mu); + return !s.queue.empty(); +} + +// IGameInputDispatcher::Dispatch: runs queued events oldest first, at least one even with a quota of 0, and takes no +// further event once quota_us microseconds have passed (an event already started runs to the end, so the quota is a +// target, as on Windows). While events remain the wait handle is signalled again, so a title that waits on it before +// each call comes back for the rest. Returns whether events remain. +bool deliver_within(uint64_t quota_us) { + State& s = g(); + std::lock_guard dl(s.deliver_mu); + const auto start = std::chrono::steady_clock::now(); + s.delivering_thread = GetCurrentThreadId(); + for (bool first = true;; first = false) { + if (!first) { + const auto spent = std::chrono::duration_cast(std::chrono::steady_clock::now() - start); + if (static_cast(spent.count()) >= quota_us) break; + } + Event e; { std::lock_guard lock(s.mu); - for (const Callback& c : s.callbacks) - if (matches(c, e)) targets.push_back(c); + if (s.queue.empty()) break; + e = s.queue.front(); + s.queue.erase(s.queue.begin()); } - for (const Callback& c : targets) { - bool still = false; - { - std::lock_guard lock(s.mu); - for (const Callback& now : s.callbacks) - if (now.token == c.token) still = now.active; - } - if (still) call(c, e); - } - if (e.device) e.device->Release(); - if (e.reading) e.reading->Release(); + deliver_event(e); } s.delivering_thread = 0; std::lock_guard lock(s.mu); - return !s.queue.empty(); + const bool remaining = !s.queue.empty(); + if (remaining && s.wait_event) SetEvent(s.wait_event); + return remaining; } // ---- SDL backend (poll thread only) ----------------------------------------------------------------------------- @@ -1153,8 +1187,8 @@ public: } return r; } - // Runs the queued callbacks on the caller's thread. The quota is not enforced: the queue is short. - bool STDMETHODCALLTYPE Dispatch(uint64_t) override { return deliver(); } + // Runs the queued callbacks on the caller's thread within the quota (see deliver_within). + bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { return deliver_within(quotaInMicroseconds); } HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override { if (!handle) return E_POINTER; *handle = nullptr; diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index f525d1cb9..d9b5d57e2 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -130,8 +130,11 @@ void CALLBACK on_device(GameInputCallbackToken token, void*, IGameInputDevice* d uint32_t previous) { g_device_events.push_back({device, current, previous, token}); } +DWORD g_reading_sleep_ms = 0; // makes on_reading slow, for the Dispatch quota checks + void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { if (reading) ++g_reading_events; + if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); } void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_t, bool pressed) { g_guide_events.push_back(pressed); @@ -979,6 +982,32 @@ int main() { check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled"); if (dispatcher) dispatcher->Dispatch(1000); check(g_reading_events > before_dispatch, "Dispatch runs them"); + + // The quota: Dispatch runs at least one queued event, takes no more once the quota has passed, and returns whether + // events remain (re-signalling the wait handle). Four button changes queue four reading events. + auto queue_changes = [&](int n) { + for (int i = 0; i < n; ++i) { + SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_NORTH, i % 2 == 1); // NORTH is down now: up, down, up, down + rt::gameinput::poll(); + } + }; + if (dispatcher) { + while (dispatcher->Dispatch(UINT64_MAX)) {} + queue_changes(4); + int before_quota = g_reading_events; + if (wait) WaitForSingleObject(wait, 0); // consume the signal from queueing + check(dispatcher->Dispatch(0) && g_reading_events == before_quota + 1, "Dispatch(0) runs exactly one event and reports more"); + check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled again while events remain"); + g_reading_sleep_ms = 5; + before_quota = g_reading_events; + check(dispatcher->Dispatch(1000) && g_reading_events == before_quota + 1, + "a 1 ms quota stops after a 5 ms callback and reports more"); + g_reading_sleep_ms = 0; + before_quota = g_reading_events; + check(!dispatcher->Dispatch(1'000'000) && g_reading_events >= before_quota + 2, + "a large quota drains the queue and returns false"); + check(!dispatcher->Dispatch(0), "Dispatch on an empty queue returns false"); + } if (wait) CloseHandle(wait); if (dispatcher) dispatcher->Release(); -- cgit v1.2.3 From af330fcce33843c94c2f446986b82f66cef00538 Mon Sep 17 00:00:00 2001 From: Leonida local test Date: Sun, 22 Nov 2026 18:37:28 +0100 Subject: gameinput: keep only the haptic calls the shim makes in the test seam (RT-160) HapticApi carried SDL_GetNumHapticAxes and SDL_HapticEffectSupported, which nothing calls: the motor's axes are the shim's choice and the effect kinds are gated on SDL_GetHapticFeatures. They are gone from the table, its SDL implementation and the test's fake, so the seam is exactly the calls a pad's force feedback makes. AI-assisted. --- runtime/shims/gameinput.cpp | 12 ++++-------- runtime/shims/gameinput.h | 2 -- runtime/test/gameinput_test.cpp | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) (limited to 'runtime/test/gameinput_test.cpp') diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 1b9a55db2..07ac20b26 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -103,11 +103,7 @@ bool same_guid(const GUID& a, const GUID& b) { return std::memcmp(&a, &b, sizeof void* sdl_open_haptic(void* js) { return SDL_OpenHapticFromJoystick(static_cast(js)); } void sdl_close_haptic(void* h) { SDL_CloseHaptic(static_cast(h)); } uint32_t sdl_haptic_features(void* h) { return SDL_GetHapticFeatures(static_cast(h)); } -int sdl_haptic_axes(void* h) { return SDL_GetNumHapticAxes(static_cast(h)); } int sdl_haptic_max_effects(void* h) { return SDL_GetMaxHapticEffects(static_cast(h)); } -bool sdl_haptic_supported(void* h, const void* e) { - return SDL_HapticEffectSupported(static_cast(h), static_cast(e)); -} int sdl_haptic_create(void* h, const void* e) { return SDL_CreateHapticEffect(static_cast(h), static_cast(e)); } @@ -121,10 +117,10 @@ bool sdl_haptic_stop(void* h, int id) { return SDL_StopHapticEffect(static_cast< void sdl_haptic_destroy(void* h, int id) { SDL_DestroyHapticEffect(static_cast(h), id); } bool sdl_haptic_gain(void* h, int gain) { return SDL_SetHapticGain(static_cast(h), gain); } -const rt::gameinput::HapticApi kSdlHaptic{sdl_open_haptic, sdl_close_haptic, sdl_haptic_features, sdl_haptic_axes, - sdl_haptic_max_effects, sdl_haptic_supported, sdl_haptic_create, - sdl_haptic_update, sdl_haptic_run, sdl_haptic_stop, - sdl_haptic_destroy, sdl_haptic_gain}; +const rt::gameinput::HapticApi kSdlHaptic{sdl_open_haptic, sdl_close_haptic, sdl_haptic_features, + sdl_haptic_max_effects, sdl_haptic_create, sdl_haptic_update, + sdl_haptic_run, sdl_haptic_stop, sdl_haptic_destroy, + sdl_haptic_gain}; std::atomic g_haptic_api{&kSdlHaptic}; const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); } diff --git a/runtime/shims/gameinput.h b/runtime/shims/gameinput.h index 829f7e436..63097c3ae 100644 --- a/runtime/shims/gameinput.h +++ b/runtime/shims/gameinput.h @@ -45,9 +45,7 @@ struct HapticApi { void* (*open_joystick)(void* joystick); // SDL_OpenHapticFromJoystick void (*close)(void* haptic); // SDL_CloseHaptic uint32_t (*features)(void* haptic); // SDL_GetHapticFeatures: the SDL_HAPTIC_* bits the device supports - int (*num_axes)(void* haptic); // SDL_GetNumHapticAxes int (*max_effects)(void* haptic); // SDL_GetMaxHapticEffects - bool (*effect_supported)(void* haptic, const void* effect); // SDL_HapticEffectSupported int (*create_effect)(void* haptic, const void* effect); // SDL_CreateHapticEffect, -1 on failure bool (*update_effect)(void* haptic, int effect, const void* data); // SDL_UpdateHapticEffect bool (*run_effect)(void* haptic, int effect, uint32_t iterations); // SDL_RunHapticEffect diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 106f68fe4..a74c2616d 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -741,9 +741,7 @@ void* fake_open(void*) { } void fake_close(void*) { ++g_fake.closes; } uint32_t fake_features(void* h) { return static_cast(h)->features; } -int fake_axes(void*) { return 1; } int fake_max_effects(void* h) { return static_cast(h)->max_effects; } -bool fake_supported(void*, const void*) { return true; } int fake_create(void* h, const void* e) { FakeHaptic* f = static_cast(h); ++f->creates; @@ -773,9 +771,8 @@ bool fake_gain(void* h, int gain) { f->last_gain = gain; return true; } -const rt::gameinput::HapticApi kFakeHaptic{fake_open, fake_close, fake_features, fake_axes, fake_max_effects, - fake_supported, fake_create, fake_update, fake_run, fake_stop, - fake_destroy, fake_gain}; +const rt::gameinput::HapticApi kFakeHaptic{fake_open, fake_close, fake_features, fake_max_effects, fake_create, + fake_update, fake_run, fake_stop, fake_destroy, fake_gain}; // The motors of one device, and the effect object its force-feedback motor hands out. Runs with no other pad. void test_force_feedback(IGameInput* input) { -- cgit v1.2.3