diff options
| author | Tyrannys <80750971+Tyrannys@users.noreply.github.com> | 2026-11-27 13:37:20 -0600 |
|---|---|---|
| committer | Tyrannys <80750971+Tyrannys@users.noreply.github.com> | 2026-11-27 13:37:20 -0600 |
| commit | aeb24285be97e415c9f46e262791473d44a66f0c (patch) | |
| tree | 6a9512f56fb6e5d282531d163c732123da42376e /runtime/shims/gameinput.cpp | |
| parent | 43a9d562d355e7c5c428e486071b456353de8b05 (diff) | |
gameinput: force-feedback kind changes, SetParams features, locked motor reads, no null memcpy
From a review of the shim by kevincommits (#1608):
- ForceFeedbackEffect::SetParams accepted any kind, also one the motor
does not report, which CreateForceFeedbackEffect refuses. It now
refuses it the same way.
- A kind change through SetParams was sent to SDL as an update of the
existing effect, which SDL_UpdateHapticEffect refuses ("Updating
effect type is illegal"), so the effect stopped. The poll thread now
destroys the old SDL effect and creates one of the new type. The
test's fake haptics refuses a type change in an update, as SDL does.
- CreateForceFeedbackEffect and IsForceFeedbackMotorPoweredOn read the
motor count, features and haptic handle that open_pad and close_pad
write on the poll thread under g().mu, without the lock. They take
it now.
- GetControllerAxisState and GetControllerButtonState called memcpy
with a null buffer for 0 bytes. They copy only when there is
something to copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 31 |
1 files changed, 25 insertions, 6 deletions
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 @@ public: | |||
| 270 | uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } | 270 | uint32_t STDMETHODCALLTYPE GetControllerAxisCount() override { return kAxisCount; } |
| 271 | uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { | 271 | uint32_t STDMETHODCALLTYPE GetControllerAxisState(uint32_t count, float* out) override { |
| 272 | const uint32_t n = out ? std::min(count, kAxisCount) : 0; | 272 | const uint32_t n = out ? std::min(count, kAxisCount) : 0; |
| 273 | std::memcpy(out, s_.axes, n * sizeof(float)); | 273 | if (n) std::memcpy(out, s_.axes, n * sizeof(float)); // never with a null buffer, even for 0 bytes |
| 274 | return n; | 274 | return n; |
| 275 | } | 275 | } |
| 276 | uint32_t STDMETHODCALLTYPE GetControllerButtonCount() override { return kButtonCount; } | 276 | uint32_t STDMETHODCALLTYPE GetControllerButtonCount() override { return kButtonCount; } |
| 277 | uint32_t STDMETHODCALLTYPE GetControllerButtonState(uint32_t count, bool* out) override { | 277 | uint32_t STDMETHODCALLTYPE GetControllerButtonState(uint32_t count, bool* out) override { |
| 278 | const uint32_t n = out ? std::min(count, kButtonCount) : 0; | 278 | const uint32_t n = out ? std::min(count, kButtonCount) : 0; |
| 279 | std::memcpy(out, s_.buttons, n * sizeof(bool)); | 279 | if (n) std::memcpy(out, s_.buttons, n * sizeof(bool)); |
| 280 | return n; | 280 | return n; |
| 281 | } | 281 | } |
| 282 | uint32_t STDMETHODCALLTYPE GetControllerSwitchCount() override { return 0; } | 282 | uint32_t STDMETHODCALLTYPE GetControllerSwitchCount() override { return 0; } |
| @@ -832,6 +832,7 @@ private: | |||
| 832 | // Poll thread only. | 832 | // Poll thread only. |
| 833 | uint64_t applied_ = 0; // the version SDL has | 833 | uint64_t applied_ = 0; // the version SDL has |
| 834 | int sdl_effect_ = -1; | 834 | int sdl_effect_ = -1; |
| 835 | uint16_t sdl_type_ = 0; // the SDL_HAPTIC_* type sdl_effect_ was created with | ||
| 835 | bool sdl_running_ = false; | 836 | bool sdl_running_ = false; |
| 836 | bool endless_ = true; // GameInput gave no duration: re-run before the SDL length runs out | 837 | bool endless_ = true; // GameInput gave no duration: re-run before the SDL length runs out |
| 837 | uint64_t run_at_ = 0, run_for_ = 0; | 838 | uint64_t run_at_ = 0, run_for_ = 0; |
| @@ -2157,16 +2158,22 @@ void Device::SetRumbleState(const GameInputRumbleParams* params) { | |||
| 2157 | HRESULT Device::CreateForceFeedbackEffect(uint32_t motorIndex, const void* params, void** effect) { | 2158 | HRESULT Device::CreateForceFeedbackEffect(uint32_t motorIndex, const void* params, void** effect) { |
| 2158 | if (!effect) return E_POINTER; | 2159 | if (!effect) return E_POINTER; |
| 2159 | *effect = nullptr; | 2160 | *effect = nullptr; |
| 2160 | if (!params || motorIndex >= info.forceFeedbackMotorCount) return E_INVALIDARG; | 2161 | if (!params) return E_INVALIDARG; |
| 2161 | const auto* p = static_cast<const GameInputForceFeedbackParams*>(params); | 2162 | const auto* p = static_cast<const GameInputForceFeedbackParams*>(params); |
| 2162 | // Only what the motor reported. SDL is not asked here: this is a title call, so the cached feature mask decides, | 2163 | { |
| 2163 | // and the poll thread is what finds out if SDL refuses the effect anyway. | 2164 | // The motor count and features are the poll thread's (open_pad, close_pad), written under g().mu. |
| 2164 | if (!valid_kind(p->kind) || !(ff_features & kind_feature(p->kind))) return E_INVALIDARG; | 2165 | std::lock_guard lock(g().mu); |
| 2166 | if (motorIndex >= info.forceFeedbackMotorCount) return E_INVALIDARG; | ||
| 2167 | // Only what the motor reported. SDL is not asked here: this is a title call, so the cached feature mask | ||
| 2168 | // decides, and the poll thread is what finds out if SDL refuses the effect anyway. | ||
| 2169 | if (!valid_kind(p->kind) || !(ff_features & kind_feature(p->kind))) return E_INVALIDARG; | ||
| 2170 | } | ||
| 2165 | *effect = new ForceFeedbackEffect(this, motorIndex, *p); | 2171 | *effect = new ForceFeedbackEffect(this, motorIndex, *p); |
| 2166 | return S_OK; | 2172 | return S_OK; |
| 2167 | } | 2173 | } |
| 2168 | 2174 | ||
| 2169 | bool Device::IsForceFeedbackMotorPoweredOn(uint32_t motorIndex) { | 2175 | bool Device::IsForceFeedbackMotorPoweredOn(uint32_t motorIndex) { |
| 2176 | std::lock_guard lock(g().mu); // haptic and the motor count change on the poll thread | ||
| 2170 | return motorIndex < info.forceFeedbackMotorCount && haptic != nullptr && status.load() != GameInputDeviceNoStatus; | 2177 | return motorIndex < info.forceFeedbackMotorCount && haptic != nullptr && status.load() != GameInputDeviceNoStatus; |
| 2171 | } | 2178 | } |
| 2172 | 2179 | ||
| @@ -2269,6 +2276,9 @@ void STDMETHODCALLTYPE ForceFeedbackEffect::GetParams(GameInputForceFeedbackPara | |||
| 2269 | bool STDMETHODCALLTYPE ForceFeedbackEffect::SetParams(const GameInputForceFeedbackParams* params) { | 2276 | bool STDMETHODCALLTYPE ForceFeedbackEffect::SetParams(const GameInputForceFeedbackParams* params) { |
| 2270 | if (!params || !valid_kind(params->kind)) return false; | 2277 | if (!params || !valid_kind(params->kind)) return false; |
| 2271 | std::lock_guard lock(g().mu); | 2278 | std::lock_guard lock(g().mu); |
| 2279 | // A kind the motor does not report is refused here as CreateForceFeedbackEffect refuses it. While the pad is away | ||
| 2280 | // (ff_features 0) the effect keeps its parameters, as it was created with a kind the motor had. | ||
| 2281 | if (device_->ff_features && !(device_->ff_features & kind_feature(params->kind))) return false; | ||
| 2272 | params_ = *params; | 2282 | params_ = *params; |
| 2273 | ++version_; | 2283 | ++version_; |
| 2274 | return true; | 2284 | return true; |
| @@ -2347,8 +2357,17 @@ void ForceFeedbackEffect::sync(Device* d, uint64_t now, bool muted) { | |||
| 2347 | sdl_running_ = false; | 2357 | sdl_running_ = false; |
| 2348 | return; | 2358 | return; |
| 2349 | } | 2359 | } |
| 2360 | if (sdl_effect_ >= 0 && fx.type != sdl_type_) { | ||
| 2361 | // SetParams changed the kind. SDL_UpdateHapticEffect refuses a new type ("Updating effect type is | ||
| 2362 | // illegal"), so the old effect goes and one of the new type is made below. | ||
| 2363 | if (sdl_running_) haptics().stop_effect(d->haptic, sdl_effect_); | ||
| 2364 | sdl_running_ = false; | ||
| 2365 | haptics().destroy_effect(d->haptic, sdl_effect_); | ||
| 2366 | sdl_effect_ = -1; | ||
| 2367 | } | ||
| 2350 | if (sdl_effect_ < 0) { | 2368 | if (sdl_effect_ < 0) { |
| 2351 | sdl_effect_ = haptics().create_effect(d->haptic, &fx); // SDL refused: nothing to drive this effect | 2369 | sdl_effect_ = haptics().create_effect(d->haptic, &fx); // SDL refused: nothing to drive this effect |
| 2370 | sdl_type_ = fx.type; | ||
| 2352 | if (sdl_effect_ < 0) { | 2371 | if (sdl_effect_ < 0) { |
| 2353 | note_effect(d, -1, false); | 2372 | note_effect(d, -1, false); |
| 2354 | return; | 2373 | return; |