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:
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