upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authorNoname0202 <marta.indra2002@gmail.com>2026-11-23 19:26:21 +0100
committerNoname0202 <marta.indra2002@gmail.com>2026-11-23 19:26:21 +0100
commit3bbf352b7a9f71ba6bd1c69f8774c4f320233cc4 (patch)
tree87d311c968a67fdcd64731f3b40ce9aa1aaba3f5 /runtime/shims/gameinput.cpp
parent34702237c00e08f0b9e6af7a12aa979095938d25 (diff)
gameinput: SetHapticMotorState saturates a request's end time instead of wrapping
IGameInputDevice::SetHapticMotorState computed the end of a request as duration * playCount + repeatDelay * (playCount - 1), then now + total, all in uint64 microseconds with no overflow check. A very long request wrapped: duration = UINT64_MAX with playCount 1 put the deadline just before now, so the motor never played, and duration = 2^63 + 50 ms with playCount 2 wrapped to a 100 ms request that stopped early. The products and sums now saturate at UINT64_MAX, a deadline the poll thread never reaches, so such a request plays until the state is set again. Requests that fit are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 4a42afbcf..9f5b78dd9 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -2030,10 +2030,15 @@ HRESULT Device::SetHapticMotorState(uint32_t motorIndex, const void* params) {
2030 if (p->waveformIndex >= haptic_motor_info[motorIndex].waveformCount) return E_INVALIDARG; 2030 if (p->waveformIndex >= haptic_motor_info[motorIndex].waveformCount) return E_INVALIDARG;
2031 haptic_intensity[motorIndex] = std::clamp(p->intensity, 0.0f, 1.0f); 2031 haptic_intensity[motorIndex] = std::clamp(p->intensity, 0.0f, 1.0f);
2032 // duration and repeatDelay are microseconds; playCount 0 plays until the state is set again. The poll thread sends 2032 // duration and repeatDelay are microseconds; playCount 0 plays until the state is set again. The poll thread sends
2033 // the level and drops it when the deadline passes. 2033 // the level and drops it when the deadline passes. The sums saturate: a request longer than a uint64 of
2034 // microseconds (a duration of UINT64_MAX, say) ends at UINT64_MAX, which the poll thread never reaches, instead of
2035 // wrapping to a deadline already past or only a moment away.
2036 constexpr uint64_t kMax = UINT64_MAX;
2037 const auto mul = [](uint64_t a, uint64_t b) { return b && a > kMax / b ? kMax : a * b; };
2038 const auto add = [](uint64_t a, uint64_t b) { return a > kMax - b ? kMax : a + b; };
2034 uint64_t total = p->playCount == 0 ? 0 : p->duration; 2039 uint64_t total = p->playCount == 0 ? 0 : p->duration;
2035 if (p->playCount > 1) total = p->duration * p->playCount + p->repeatDelay * (p->playCount - 1); 2040 if (p->playCount > 1) total = add(mul(p->duration, p->playCount), mul(p->repeatDelay, p->playCount - 1));
2036 haptic_until[motorIndex] = total ? now_us() + total : 0; 2041 haptic_until[motorIndex] = total ? add(now_us(), total) : 0;
2037 return S_OK; 2042 return S_OK;
2038} 2043}
2039 2044