From c7c5c42ae820f7232460b200a54452f50182969d Mon Sep 17 00:00:00 2001 From: cathery Date: Fri, 24 Jan 2020 01:38:10 +0300 Subject: [PATCH] Fix joystick values going out of bounds Joystick value of 32768 (0x8000) causes games like Xenoblade 2 to crash under failed assertions. Even though it should be within range of acceptable joystick values. --- .vscode/settings.json | 5 ++++- source/ControllerSwitch/SwitchHDLHandler.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 90a2852..adbb2e0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -46,7 +46,10 @@ "random": "cpp", "ratio": "cpp", "string": "cpp", - "thread": "cpp" + "thread": "cpp", + "bit": "cpp", + "cinttypes": "cpp", + "variant": "cpp" }, "C_Cpp.dimInactiveRegions": true } \ No newline at end of file diff --git a/source/ControllerSwitch/SwitchHDLHandler.cpp b/source/ControllerSwitch/SwitchHDLHandler.cpp index 6449a13..7e97c39 100644 --- a/source/ControllerSwitch/SwitchHDLHandler.cpp +++ b/source/ControllerSwitch/SwitchHDLHandler.cpp @@ -1,6 +1,7 @@ #include "SwitchHDLHandler.h" #include "ControllerHelpers.h" #include +#include SwitchHDLHandler::SwitchHDLHandler(std::unique_ptr &&controller) : SwitchVirtualGamepadHandler(std::move(controller)) @@ -149,6 +150,12 @@ void SwitchHDLHandler::FillHdlState(const NormalizedButtonData &data) m_controllerHandler.ConvertAxisToSwitchAxis(data.sticks[1].axis_x, data.sticks[1].axis_y, 0, &m_hdlState.joysticks[JOYSTICK_RIGHT].dx, &m_hdlState.joysticks[JOYSTICK_RIGHT].dy); + //Actual joystick values never use the full range of the s32 type and exceeding them has caused crashes with multiple games, so we clamp them to s16 + m_hdlState.joysticks[JOYSTICK_LEFT].dx = std::clamp(m_hdlState.joysticks[JOYSTICK_LEFT].dx, -32768, 32768 -1); + m_hdlState.joysticks[JOYSTICK_LEFT].dy = std::clamp(m_hdlState.joysticks[JOYSTICK_LEFT].dy, -32768, 32768 -1); + m_hdlState.joysticks[JOYSTICK_RIGHT].dx = std::clamp(m_hdlState.joysticks[JOYSTICK_RIGHT].dx, -32768, 32768 -1); + m_hdlState.joysticks[JOYSTICK_RIGHT].dy = std::clamp(m_hdlState.joysticks[JOYSTICK_RIGHT].dy, -32768, 32768 -1); + m_hdlState.buttons |= (data.buttons[16] ? KEY_CAPTURE : 0); m_hdlState.buttons |= (data.buttons[17] ? KEY_HOME : 0); m_hdlState.buttons |= (data.buttons[18] ? KEY_TOUCH : 0);