From 03663d5552f99705e6b265051e3bce32d95b0139 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 29 Dec 2023 16:06:53 -0600 Subject: [PATCH] Fix integer promotion issue in gamepad state merging --- app/streaming/input/gamepad.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/streaming/input/gamepad.cpp b/app/streaming/input/gamepad.cpp index 53cc6b78..458666a7 100644 --- a/app/streaming/input/gamepad.cpp +++ b/app/streaming/input/gamepad.cpp @@ -86,11 +86,14 @@ void SdlInputHandler::sendGamepadState(GamepadState* state) if (rt < m_GamepadState[i].rt) { rt = m_GamepadState[i].rt; } - if (qAbs(lsX) < qAbs(m_GamepadState[i].lsX) || qAbs(lsY) < qAbs(m_GamepadState[i].lsY)) { + + // We use abs() here instead of qAbs() for get proper integer promotion to + // correctly handle abs(-32768), which is not representable in a short. + if (abs(lsX) < abs(m_GamepadState[i].lsX) || abs(lsY) < abs(m_GamepadState[i].lsY)) { lsX = m_GamepadState[i].lsX; lsY = m_GamepadState[i].lsY; } - if (qAbs(rsX) < qAbs(m_GamepadState[i].rsX) || qAbs(rsY) < qAbs(m_GamepadState[i].rsY)) { + if (abs(rsX) < abs(m_GamepadState[i].rsX) || abs(rsY) < abs(m_GamepadState[i].rsY)) { rsX = m_GamepadState[i].rsX; rsY = m_GamepadState[i].rsY; } @@ -154,11 +157,11 @@ Uint32 SdlInputHandler::mouseEmulationTimerCallback(Uint32 interval, void *param { auto gamepad = reinterpret_cast(param); - short rawX; - short rawY; + int rawX; + int rawY; // Determine which analog stick is currently receiving the strongest input - if ((uint32_t)qAbs(gamepad->lsX) + qAbs(gamepad->lsY) > (uint32_t)qAbs(gamepad->rsX) + qAbs(gamepad->rsY)) { + if (abs(gamepad->lsX) + abs(gamepad->lsY) > abs(gamepad->rsX) + abs(gamepad->rsY)) { rawX = gamepad->lsX; rawY = -gamepad->lsY; }