CommonHostInterface: Move controller ID parsing to interface

This commit is contained in:
Connor McLaughlin 2021-03-14 16:18:56 +10:00
parent e87ce48601
commit ee171465ea
4 changed files with 25 additions and 43 deletions

View file

@ -1538,21 +1538,9 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const
return false; return false;
} }
if (StringUtil::StartsWith(device, "Controller")) std::optional<int> controller_index;
if (m_controller_interface && (controller_index = m_controller_interface->GetControllerIndex(device)))
{ {
if (!m_controller_interface)
{
Log_ErrorPrintf("No controller interface set, cannot bind '%s'", binding.c_str());
return false;
}
const std::optional<int> controller_index = StringUtil::FromChars<int>(device.substr(10));
if (!controller_index || *controller_index < 0)
{
Log_WarningPrintf("Invalid controller index in button binding '%s'", binding.c_str());
return false;
}
if (StringUtil::StartsWith(button, "Button")) if (StringUtil::StartsWith(button, "Button"))
{ {
const std::optional<int> button_index = StringUtil::FromChars<int>(button.substr(6)); const std::optional<int> button_index = StringUtil::FromChars<int>(button.substr(6));
@ -1652,21 +1640,9 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st
} }
} }
if (StringUtil::StartsWith(device, "Controller")) std::optional<int> controller_index;
if (m_controller_interface && (controller_index = m_controller_interface->GetControllerIndex(device)))
{ {
if (!m_controller_interface)
{
Log_ErrorPrintf("No controller interface set, cannot bind '%s'", binding.c_str());
return false;
}
const std::optional<int> controller_index = StringUtil::FromChars<int>(device.substr(10));
if (!controller_index || *controller_index < 0)
{
Log_WarningPrintf("Invalid controller index in axis binding '%s'", binding.c_str());
return false;
}
if (StringUtil::StartsWith(axis, "Axis") || StringUtil::StartsWith(axis, "+Axis") || if (StringUtil::StartsWith(axis, "Axis") || StringUtil::StartsWith(axis, "+Axis") ||
StringUtil::StartsWith(axis, "-Axis")) StringUtil::StartsWith(axis, "-Axis"))
{ {
@ -1723,21 +1699,9 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st
bool CommonHostInterface::AddRumbleToInputMap(const std::string& binding, u32 controller_index, u32 num_motors) bool CommonHostInterface::AddRumbleToInputMap(const std::string& binding, u32 controller_index, u32 num_motors)
{ {
if (StringUtil::StartsWith(binding, "Controller")) std::optional<int> host_controller_index;
if (m_controller_interface && (host_controller_index = m_controller_interface->GetControllerIndex(binding)))
{ {
if (!m_controller_interface)
{
Log_ErrorPrintf("No controller interface set, cannot bind '%s'", binding.c_str());
return false;
}
const std::optional<int> host_controller_index = StringUtil::FromChars<int>(binding.substr(10));
if (!host_controller_index || *host_controller_index < 0)
{
Log_WarningPrintf("Invalid controller index in rumble binding '%s'", binding.c_str());
return false;
}
AddControllerRumble(controller_index, num_motors, AddControllerRumble(controller_index, num_motors,
std::bind(&ControllerInterface::SetControllerRumbleStrength, m_controller_interface.get(), std::bind(&ControllerInterface::SetControllerRumbleStrength, m_controller_interface.get(),
host_controller_index.value(), std::placeholders::_1, std::placeholders::_2)); host_controller_index.value(), std::placeholders::_1, std::placeholders::_2));

View file

@ -356,7 +356,7 @@ protected:
void RegisterHotkey(String category, String name, String display_name, InputButtonHandler handler); void RegisterHotkey(String category, String name, String display_name, InputButtonHandler handler);
bool HandleHostKeyEvent(HostKeyCode code, HostKeyCode modifiers, bool pressed); bool HandleHostKeyEvent(HostKeyCode code, HostKeyCode modifiers, bool pressed);
bool HandleHostMouseEvent(HostMouseButton button, bool pressed); bool HandleHostMouseEvent(HostMouseButton button, bool pressed);
void UpdateInputMap(SettingsInterface& si); virtual void UpdateInputMap(SettingsInterface& si);
void ClearInputMap(); void ClearInputMap();
void AddControllerRumble(u32 controller_index, u32 num_motors, ControllerRumbleCallback callback); void AddControllerRumble(u32 controller_index, u32 num_motors, ControllerRumbleCallback callback);

View file

@ -22,6 +22,22 @@ void ControllerInterface::Shutdown()
m_host_interface = nullptr; m_host_interface = nullptr;
} }
std::optional<int> ControllerInterface::GetControllerIndex(const std::string_view& device)
{
if (!StringUtil::StartsWith(device, "Controller"))
return std::nullopt;
const std::optional<int> controller_index = StringUtil::FromChars<int>(device.substr(10));
if (!controller_index || *controller_index < 0)
{
Log_WarningPrintf("Invalid controller index in button binding '%*s'", static_cast<int>(device.length()),
device.data());
return std::nullopt;
}
return controller_index;
}
void ControllerInterface::SetHook(Hook::Callback callback) void ControllerInterface::SetHook(Hook::Callback callback)
{ {
std::unique_lock<std::mutex> lock(m_event_intercept_mutex); std::unique_lock<std::mutex> lock(m_event_intercept_mutex);

View file

@ -6,6 +6,7 @@
#include <map> #include <map>
#include <mutex> #include <mutex>
#include <optional> #include <optional>
#include <string_view>
#include <variant> #include <variant>
class HostInterface; class HostInterface;
@ -70,6 +71,7 @@ public:
virtual void ClearBindings() = 0; virtual void ClearBindings() = 0;
// Binding to events. If a binding for this axis/button already exists, returns false. // Binding to events. If a binding for this axis/button already exists, returns false.
virtual std::optional<int> GetControllerIndex(const std::string_view& device);
virtual bool BindControllerAxis(int controller_index, int axis_number, AxisSide axis_side, AxisCallback callback) = 0; virtual bool BindControllerAxis(int controller_index, int axis_number, AxisSide axis_side, AxisCallback callback) = 0;
virtual bool BindControllerButton(int controller_index, int button_number, ButtonCallback callback) = 0; virtual bool BindControllerButton(int controller_index, int button_number, ButtonCallback callback) = 0;
virtual bool BindControllerAxisToButton(int controller_index, int axis_number, bool direction, virtual bool BindControllerAxisToButton(int controller_index, int axis_number, bool direction,