2020-03-21 14:49:46 +00:00
|
|
|
#include "controller_interface.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
2020-08-22 06:44:06 +00:00
|
|
|
#include "common/string_util.h"
|
2020-03-21 14:49:46 +00:00
|
|
|
#include "core/controller.h"
|
|
|
|
#include "core/system.h"
|
|
|
|
#include <cmath>
|
|
|
|
Log_SetChannel(ControllerInterface);
|
|
|
|
|
|
|
|
ControllerInterface::ControllerInterface() = default;
|
|
|
|
|
|
|
|
ControllerInterface::~ControllerInterface() = default;
|
|
|
|
|
|
|
|
bool ControllerInterface::Initialize(CommonHostInterface* host_interface)
|
|
|
|
{
|
|
|
|
m_host_interface = host_interface;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerInterface::Shutdown()
|
|
|
|
{
|
|
|
|
m_host_interface = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:18:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:49:46 +00:00
|
|
|
void ControllerInterface::SetHook(Hook::Callback callback)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_event_intercept_mutex);
|
|
|
|
Assert(!m_event_intercept_callback);
|
|
|
|
m_event_intercept_callback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerInterface::ClearHook()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_event_intercept_mutex);
|
|
|
|
if (m_event_intercept_callback)
|
|
|
|
m_event_intercept_callback = {};
|
|
|
|
}
|
|
|
|
|
2021-02-24 16:05:33 +00:00
|
|
|
bool ControllerInterface::HasHook()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_event_intercept_mutex);
|
|
|
|
return (bool)m_event_intercept_callback;
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:56:52 +00:00
|
|
|
bool ControllerInterface::DoEventHook(Hook::Type type, int controller_index, int button_or_axis_number,
|
|
|
|
std::variant<float, std::string_view> value, bool track_history)
|
2020-03-21 14:49:46 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_event_intercept_mutex);
|
|
|
|
if (!m_event_intercept_callback)
|
|
|
|
return false;
|
|
|
|
|
2020-11-15 13:56:52 +00:00
|
|
|
const Hook ei{type, controller_index, button_or_axis_number, std::move(value), track_history};
|
2020-03-21 14:49:46 +00:00
|
|
|
const Hook::CallbackResult action = m_event_intercept_callback(ei);
|
|
|
|
if (action == Hook::CallbackResult::StopMonitoring)
|
|
|
|
m_event_intercept_callback = {};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerInterface::OnControllerConnected(int host_id)
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("Host controller %d connected, updating input map", host_id);
|
|
|
|
m_host_interface->UpdateInputMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerInterface::OnControllerDisconnected(int host_id)
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("Host controller %d disconnected, updating input map", host_id);
|
|
|
|
m_host_interface->UpdateInputMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerInterface::ClearBindings() {}
|
|
|
|
|
2020-11-15 13:56:52 +00:00
|
|
|
bool ControllerInterface::BindControllerAxis(int controller_index, int axis_number, AxisSide axis_side,
|
|
|
|
AxisCallback callback)
|
2020-03-21 14:49:46 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControllerInterface::BindControllerButton(int controller_index, int button_number, ButtonCallback callback)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControllerInterface::BindControllerAxisToButton(int controller_index, int axis_number, bool direction,
|
|
|
|
ButtonCallback callback)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-22 06:44:06 +00:00
|
|
|
static constexpr std::array<const char*, static_cast<u32>(ControllerInterface::Backend::Count)> s_backend_names = {{
|
|
|
|
TRANSLATABLE("ControllerInterface", "None"),
|
|
|
|
#ifdef WITH_SDL2
|
|
|
|
TRANSLATABLE("ControllerInterface", "SDL"),
|
|
|
|
#endif
|
2021-06-28 10:16:48 +00:00
|
|
|
#ifdef _WIN32
|
2020-08-22 06:44:06 +00:00
|
|
|
TRANSLATABLE("ControllerInterface", "XInput"),
|
2020-12-29 06:29:30 +00:00
|
|
|
TRANSLATABLE("ControllerInterface", "DInput"),
|
2020-08-22 06:44:06 +00:00
|
|
|
#endif
|
2020-12-27 07:50:29 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
// Deliberately not translated as it's not exposed to users.
|
|
|
|
"Android",
|
|
|
|
#endif
|
2021-02-07 11:32:59 +00:00
|
|
|
#ifdef WITH_EVDEV
|
|
|
|
TRANSLATABLE("ControllerInterface", "Evdev"),
|
|
|
|
#endif
|
2020-08-22 06:44:06 +00:00
|
|
|
}};
|
|
|
|
|
|
|
|
std::optional<ControllerInterface::Backend> ControllerInterface::ParseBackendName(const char* name)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < static_cast<u32>(s_backend_names.size()); i++)
|
|
|
|
{
|
|
|
|
if (StringUtil::Strcasecmp(name, s_backend_names[i]) == 0)
|
|
|
|
return static_cast<Backend>(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* ControllerInterface::GetBackendName(Backend type)
|
|
|
|
{
|
|
|
|
return s_backend_names[static_cast<u32>(type)];
|
|
|
|
}
|
|
|
|
|
|
|
|
ControllerInterface::Backend ControllerInterface::GetDefaultBackend()
|
|
|
|
{
|
|
|
|
#ifdef WITH_SDL2
|
|
|
|
return Backend::SDL;
|
2021-06-26 05:05:21 +00:00
|
|
|
#else
|
2021-06-28 10:16:48 +00:00
|
|
|
#ifdef _WIN32
|
2020-08-22 06:44:06 +00:00
|
|
|
return Backend::XInput;
|
|
|
|
#else
|
|
|
|
return Backend::None;
|
|
|
|
#endif
|
2021-06-26 05:05:21 +00:00
|
|
|
#endif
|
2020-08-22 06:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_SDL2
|
|
|
|
#include "sdl_controller_interface.h"
|
|
|
|
#endif
|
2021-06-28 10:16:48 +00:00
|
|
|
#ifdef _WIN32
|
2020-12-29 06:29:30 +00:00
|
|
|
#include "dinput_controller_interface.h"
|
2020-08-22 06:44:06 +00:00
|
|
|
#include "xinput_controller_interface.h"
|
|
|
|
#endif
|
2021-02-07 11:32:59 +00:00
|
|
|
#ifdef WITH_EVDEV
|
|
|
|
#include "evdev_controller_interface.h"
|
|
|
|
#endif
|
2020-08-22 06:44:06 +00:00
|
|
|
|
|
|
|
std::unique_ptr<ControllerInterface> ControllerInterface::Create(Backend type)
|
|
|
|
{
|
|
|
|
#ifdef WITH_SDL2
|
|
|
|
if (type == Backend::SDL)
|
|
|
|
return std::make_unique<SDLControllerInterface>();
|
|
|
|
#endif
|
2021-06-28 10:16:48 +00:00
|
|
|
#ifdef _WIN32
|
2020-08-22 06:44:06 +00:00
|
|
|
if (type == Backend::XInput)
|
|
|
|
return std::make_unique<XInputControllerInterface>();
|
2020-12-29 06:29:30 +00:00
|
|
|
if (type == Backend::DInput)
|
|
|
|
return std::make_unique<DInputControllerInterface>();
|
2020-08-22 06:44:06 +00:00
|
|
|
#endif
|
2021-02-07 11:32:59 +00:00
|
|
|
#ifdef WITH_EVDEV
|
|
|
|
if (type == Backend::Evdev)
|
|
|
|
return std::make_unique<EvdevControllerInterface>();
|
|
|
|
#endif
|
2020-08-22 06:44:06 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|