diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index dd3db4507..fce0a44ef 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -1033,6 +1033,9 @@ void CommonHostInterface::UpdateControllerInputMap(SettingsInterface& si) const float axis_scale = si.GetFloatValue(category, "AxisScale", 1.00f); m_controller_interface->SetControllerAxisScale(controller_index, axis_scale); + + const float deadzone_size = si.GetFloatValue(category, "Deadzone", 0.25f); + m_controller_interface->SetControllerDeadzone(controller_index, deadzone_size); } } diff --git a/src/frontend-common/controller_interface.h b/src/frontend-common/controller_interface.h index c8f3f14de..906a27d56 100644 --- a/src/frontend-common/controller_interface.h +++ b/src/frontend-common/controller_interface.h @@ -46,6 +46,9 @@ public: // Set scaling that will be applied on axis-to-axis mappings virtual bool SetControllerAxisScale(int controller_index, float scale) = 0; + // Set deadzone that will be applied on axis-to-button mappings + virtual bool SetControllerDeadzone(int controller_index, float size) = 0; + // Input monitoring for external access. struct Hook { diff --git a/src/frontend-common/sdl_controller_interface.cpp b/src/frontend-common/sdl_controller_interface.cpp index 098f74cf1..a2c54ed52 100644 --- a/src/frontend-common/sdl_controller_interface.cpp +++ b/src/frontend-common/sdl_controller_interface.cpp @@ -273,9 +273,6 @@ bool SDLControllerInterface::BindControllerAxisToButton(int controller_index, in bool SDLControllerInterface::HandleControllerAxisEvent(const SDL_Event* ev) { - // TODO: Make deadzone customizable. - static constexpr float deadzone = 8192.0f / 32768.0f; - const float value = static_cast(ev->caxis.value) / (ev->caxis.value < 0 ? 32768.0f : 32767.0f); Log_DebugPrintf("controller %d axis %d %d %f", ev->caxis.which, ev->caxis.axis, ev->caxis.value, value); @@ -295,7 +292,7 @@ bool SDLControllerInterface::HandleControllerAxisEvent(const SDL_Event* ev) } // set the other direction to false so large movements don't leave the opposite on - const bool outside_deadzone = (std::abs(value) >= deadzone); + const bool outside_deadzone = (std::abs(value) >= it->deadzone); const bool positive = (value >= 0.0f); const ButtonCallback& other_button_cb = it->axis_button_mapping[ev->caxis.axis][BoolToUInt8(!positive)]; const ButtonCallback& button_cb = it->axis_button_mapping[ev->caxis.axis][BoolToUInt8(positive)]; @@ -398,3 +395,14 @@ bool SDLControllerInterface::SetControllerAxisScale(int controller_index, float Log_InfoPrintf("Controller %d axis scale set to %f", controller_index, it->axis_scale); return true; } + +bool SDLControllerInterface::SetControllerDeadzone(int controller_index, float size /* = 0.25f */) +{ + auto it = GetControllerDataForPlayerId(controller_index); + if (it == m_controllers.end()) + return false; + + it->deadzone = std::clamp(std::abs(size), 0.01f, 0.99f); + Log_InfoPrintf("Controller %d deadzone size set to %f", controller_index, it->deadzone); + return true; +} diff --git a/src/frontend-common/sdl_controller_interface.h b/src/frontend-common/sdl_controller_interface.h index 69e8270c5..1aacab523 100644 --- a/src/frontend-common/sdl_controller_interface.h +++ b/src/frontend-common/sdl_controller_interface.h @@ -32,6 +32,9 @@ public: // Set scaling that will be applied on axis-to-axis mappings bool SetControllerAxisScale(int controller_index, float scale = 1.00f) override; + // Set deadzone that will be applied on axis-to-button mappings + bool SetControllerDeadzone(int controller_index, float size = 0.25f) override; + void PollEvents() override; bool ProcessSDLEvent(const SDL_Event* event); @@ -47,6 +50,7 @@ private: // Scaling value of 1.30f to 1.40f recommended when using recent controllers float axis_scale = 1.00f; + float deadzone = 0.25f; std::array axis_mapping; std::array button_mapping;