diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index c989daff4..63f3e3637 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -270,13 +270,13 @@ class Reading final : public IGameInputReading { uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { const uint32_t n = out ? std::min(count, kAxisCount) : 0; - std::memcpy(out, s_.axes, n * sizeof(float)); + if (n) std::memcpy(out, s_.axes, n * sizeof(float)); // never with a null buffer, even for 0 bytes return n; } uint32_t STDMETHODCALLTYPE GetControllerButtonCount() override { return kButtonCount; } uint32_t STDMETHODCALLTYPE GetControllerButtonState(uint32_t count, bool* out) override { const uint32_t n = out ? std::min(count, kButtonCount) : 0; - std::memcpy(out, s_.buttons, n * sizeof(bool)); + if (n) std::memcpy(out, s_.buttons, n * sizeof(bool)); return n; } uint32_t STDMETHODCALLTYPE GetControllerSwitchCount() override { return 0; } @@ -832,6 +832,7 @@ class ForceFeedbackEffect final : public IGameInputForceFeedbackEffect { // Poll thread only. uint64_t applied_ = 0; // the version SDL has int sdl_effect_ = -1; + uint16_t sdl_type_ = 0; // the SDL_HAPTIC_* type sdl_effect_ was created with bool sdl_running_ = false; bool endless_ = true; // GameInput gave no duration: re-run before the SDL length runs out uint64_t run_at_ = 0, run_for_ = 0; @@ -2157,16 +2158,22 @@ void Device::SetRumbleState(const GameInputRumbleParams* params) { HRESULT Device::CreateForceFeedbackEffect(uint32_t motorIndex, const void* params, void** effect) { if (!effect) return E_POINTER; *effect = nullptr; - if (!params || motorIndex >= info.forceFeedbackMotorCount) return E_INVALIDARG; + if (!params) return E_INVALIDARG; const auto* p = static_cast(params); - // Only what the motor reported. SDL is not asked here: this is a title call, so the cached feature mask decides, - // and the poll thread is what finds out if SDL refuses the effect anyway. - if (!valid_kind(p->kind) || !(ff_features & kind_feature(p->kind))) return E_INVALIDARG; + { + // The motor count and features are the poll thread's (open_pad, close_pad), written under g().mu. + std::lock_guard lock(g().mu); + if (motorIndex >= info.forceFeedbackMotorCount) return E_INVALIDARG; + // Only what the motor reported. SDL is not asked here: this is a title call, so the cached feature mask + // decides, and the poll thread is what finds out if SDL refuses the effect anyway. + if (!valid_kind(p->kind) || !(ff_features & kind_feature(p->kind))) return E_INVALIDARG; + } *effect = new ForceFeedbackEffect(this, motorIndex, *p); return S_OK; } bool Device::IsForceFeedbackMotorPoweredOn(uint32_t motorIndex) { + std::lock_guard lock(g().mu); // haptic and the motor count change on the poll thread return motorIndex < info.forceFeedbackMotorCount && haptic != nullptr && status.load() != GameInputDeviceNoStatus; } @@ -2269,6 +2276,9 @@ void STDMETHODCALLTYPE ForceFeedbackEffect::GetParams(GameInputForceFeedbackPara bool STDMETHODCALLTYPE ForceFeedbackEffect::SetParams(const GameInputForceFeedbackParams* params) { if (!params || !valid_kind(params->kind)) return false; std::lock_guard lock(g().mu); + // A kind the motor does not report is refused here as CreateForceFeedbackEffect refuses it. While the pad is away + // (ff_features 0) the effect keeps its parameters, as it was created with a kind the motor had. + if (device_->ff_features && !(device_->ff_features & kind_feature(params->kind))) return false; params_ = *params; ++version_; return true; @@ -2347,8 +2357,17 @@ void ForceFeedbackEffect::sync(Device* d, uint64_t now, bool muted) { sdl_running_ = false; return; } + if (sdl_effect_ >= 0 && fx.type != sdl_type_) { + // SetParams changed the kind. SDL_UpdateHapticEffect refuses a new type ("Updating effect type is + // illegal"), so the old effect goes and one of the new type is made below. + if (sdl_running_) haptics().stop_effect(d->haptic, sdl_effect_); + sdl_running_ = false; + haptics().destroy_effect(d->haptic, sdl_effect_); + sdl_effect_ = -1; + } if (sdl_effect_ < 0) { sdl_effect_ = haptics().create_effect(d->haptic, &fx); // SDL refused: nothing to drive this effect + sdl_type_ = fx.type; if (sdl_effect_ < 0) { note_effect(d, -1, false); return; diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 09d959f34..55c0d2123 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -1272,17 +1272,19 @@ void test_twins(IGameInput* input) { // ---- 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. +// the shim asked SDL to do and reports the capabilities a force-feedback wheel has: every effect kind but inertia, +// so that a kind the motor lacks can be refused. 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; + SDL_HAPTIC_FRICTION | SDL_HAPTIC_DAMPER; 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; SDL_HapticEffect last{}; + uint16_t created_type = 0; // the type of the effect create made; update refuses another, as SDL does }; FakeHaptic g_fake; bool g_fake_open = false; // pads attached while this is set are opened as haptic devices @@ -1303,12 +1305,18 @@ int fake_create(void* h, const void* e) { return -1; } f->last = *static_cast(e); + f->created_type = f->last.type; return f->create_result; } bool fake_update(void* h, int, const void* e) { FakeHaptic* f = static_cast(h); ++f->updates; - f->last = *static_cast(e); + const auto* effect = static_cast(e); + if (effect->type != f->created_type) { // SDL_UpdateHapticEffect: "Haptic: Updating effect type is illegal" + SDL_SetError("fake: updating effect type is illegal"); + return false; + } + f->last = *effect; return true; } bool fake_run(void* h, int, uint32_t iterations) { @@ -1379,6 +1387,10 @@ void test_force_feedback(IGameInput* input) { 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"); + GameInputForceFeedbackParams inertia{}; + inertia.kind = GameInputForceFeedbackInertia; + check(dev->CreateForceFeedbackEffect(0, &inertia, reinterpret_cast(&effect)) == E_INVALIDARG && !effect, + "force feedback: a kind the motor does not report 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 @@ -1459,8 +1471,19 @@ void test_force_feedback(IGameInput* input) { wave.data.sineWave.magnitude.linearX = 1.0f; wave.data.sineWave.frequency = 50.0f; wave.data.sineWave.phase = 90.0f; + // A kind the motor lacks is refused by SetParams too, and the effect keeps what it had. + check(!effect->SetParams(&inertia), "force feedback: SetParams refuses a kind the motor does not report"); + effect->GetParams(&read); + check(read.kind == GameInputForceFeedbackConstant, "force feedback: a refused SetParams changes nothing"); + // Another kind: SDL cannot update an effect to another type, so the shim makes a new one (the fake refuses a type + // change in an update, as SDL_UpdateHapticEffect does). + const int creates_before_wave = g_fake.creates, destroys_before_wave = g_fake.destroys; check(effect->SetParams(&wave), "force feedback: SetParams for a sine wave"); rt::gameinput::poll(); + rt::gameinput::OutputState wave_out{}; + check(g_fake.creates == creates_before_wave + 1 && g_fake.destroys == destroys_before_wave + 1 && + rt::gameinput::output_state(0, &wave_out) && wave_out.effect_ran && !wave_out.effect_error[0], + "force feedback: a new kind replaces the SDL effect instead of updating its type"); 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"); @@ -1509,9 +1532,10 @@ void test_force_feedback(IGameInput* input) { rt::gameinput::poll(); check(g_fake.runs == runs1 + 1, "force feedback: and it runs again once the mute is lifted"); + const int destroys_before_release = g_fake.destroys; 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"); + check(g_fake.destroys == destroys_before_release + 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 @@ -1914,6 +1938,8 @@ int main(int argc, char** argv) { check(reading->GetControllerButtonState(64, buttons) == SDL_GAMEPAD_BUTTON_COUNT && buttons[SDL_GAMEPAD_BUTTON_SOUTH] && !buttons[SDL_GAMEPAD_BUTTON_EAST], "controller buttons"); + check(reading->GetControllerAxisState(8, nullptr) == 0 && reading->GetControllerButtonState(64, nullptr) == 0, + "controller axes and buttons with no buffer: none written"); check(!reading->GetMotionState(nullptr) && !reading->GetMouseState(nullptr), "no motion or mouse state"); IGameInputReading* none = nullptr; check(input->GetNextReading(reading, GameInputKindGamepad, nullptr, &none) == GAMEINPUT_E_READING_NOT_FOUND && !none,