2017-01-21 15:33:48 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-09 08:11:47 +00:00
|
|
|
#include <algorithm>
|
2018-09-08 17:01:30 +00:00
|
|
|
#include <atomic>
|
2017-01-21 15:33:48 +00:00
|
|
|
#include <cmath>
|
2018-09-08 17:01:30 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
#include <mutex>
|
2017-01-21 15:33:48 +00:00
|
|
|
#include <string>
|
2018-09-08 17:01:30 +00:00
|
|
|
#include <thread>
|
2017-01-21 15:33:48 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include <unordered_map>
|
2017-12-06 04:26:29 +00:00
|
|
|
#include <utility>
|
2018-09-08 17:01:30 +00:00
|
|
|
#include <vector>
|
2017-01-21 15:33:48 +00:00
|
|
|
#include <SDL.h>
|
2018-09-08 17:01:30 +00:00
|
|
|
#include "common/assert.h"
|
2017-04-17 08:49:04 +00:00
|
|
|
#include "common/logging/log.h"
|
2017-01-21 15:33:48 +00:00
|
|
|
#include "common/math_util.h"
|
2017-12-06 04:26:29 +00:00
|
|
|
#include "common/param_package.h"
|
2018-09-08 17:01:30 +00:00
|
|
|
#include "common/threadsafe_queue.h"
|
2017-12-06 04:26:29 +00:00
|
|
|
#include "input_common/main.h"
|
2017-01-21 15:33:48 +00:00
|
|
|
#include "input_common/sdl/sdl.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
namespace SDL {
|
|
|
|
|
|
|
|
class SDLJoystick;
|
|
|
|
class SDLButtonFactory;
|
|
|
|
class SDLAnalogFactory;
|
2018-09-08 17:01:30 +00:00
|
|
|
|
2018-09-09 08:11:47 +00:00
|
|
|
/// Map of GUID of a list of corresponding virtual Joysticks
|
2018-09-08 17:01:30 +00:00
|
|
|
static std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
|
|
|
|
static std::mutex joystick_map_mutex;
|
|
|
|
|
2017-01-21 15:33:48 +00:00
|
|
|
static std::shared_ptr<SDLButtonFactory> button_factory;
|
|
|
|
static std::shared_ptr<SDLAnalogFactory> analog_factory;
|
|
|
|
|
2018-09-08 17:01:30 +00:00
|
|
|
/// Used by the Pollers during config
|
|
|
|
static std::atomic<bool> polling;
|
|
|
|
static Common::SPSCQueue<SDL_Event> event_queue;
|
|
|
|
|
|
|
|
static std::atomic<bool> initialized = false;
|
|
|
|
|
|
|
|
static std::string GetGUID(SDL_Joystick* joystick) {
|
|
|
|
SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
|
|
|
|
char guid_str[33];
|
|
|
|
SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
|
|
|
|
return guid_str;
|
|
|
|
}
|
2017-01-21 15:33:48 +00:00
|
|
|
|
|
|
|
class SDLJoystick {
|
|
|
|
public:
|
2018-09-09 08:11:47 +00:00
|
|
|
SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick,
|
2018-09-08 17:01:30 +00:00
|
|
|
decltype(&SDL_JoystickClose) deleter = &SDL_JoystickClose)
|
2018-09-09 08:11:47 +00:00
|
|
|
: guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, deleter} {}
|
2018-09-08 17:01:30 +00:00
|
|
|
|
|
|
|
void SetButton(int button, bool value) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
state.buttons[button] = value;
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetButton(int button) const {
|
2018-09-08 17:01:30 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
return state.buttons.at(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAxis(int axis, Sint16 value) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
state.axes[axis] = value;
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 08:49:04 +00:00
|
|
|
float GetAxis(int axis) const {
|
2018-09-08 17:01:30 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
return state.axes.at(axis) / 32767.0f;
|
2017-04-17 08:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const {
|
|
|
|
float x = GetAxis(axis_x);
|
|
|
|
float y = GetAxis(axis_y);
|
2017-01-21 15:33:48 +00:00
|
|
|
y = -y; // 3DS uses an y-axis inverse from SDL
|
|
|
|
|
|
|
|
// Make sure the coordinates are in the unit circle,
|
|
|
|
// otherwise normalize it.
|
|
|
|
float r = x * x + y * y;
|
|
|
|
if (r > 1.0f) {
|
|
|
|
r = std::sqrt(r);
|
|
|
|
x /= r;
|
|
|
|
y /= r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_tuple(x, y);
|
|
|
|
}
|
|
|
|
|
2018-09-08 17:01:30 +00:00
|
|
|
void SetHat(int hat, Uint8 direction) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
state.hats[hat] = direction;
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:33:48 +00:00
|
|
|
bool GetHatDirection(int hat, Uint8 direction) const {
|
2018-09-08 17:01:30 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
return (state.hats.at(hat) & direction) != 0;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The guid of the joystick
|
|
|
|
*/
|
|
|
|
const std::string& GetGUID() const {
|
|
|
|
return guid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of joystick from the same type that were connected before this joystick
|
|
|
|
*/
|
|
|
|
int GetPort() const {
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Joystick* GetSDLJoystick() const {
|
|
|
|
return sdl_joystick.get();
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 17:01:30 +00:00
|
|
|
void SetSDLJoystick(SDL_Joystick* joystick,
|
|
|
|
decltype(&SDL_JoystickClose) deleter = &SDL_JoystickClose) {
|
|
|
|
sdl_joystick =
|
|
|
|
std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)>(joystick, deleter);
|
2017-12-06 04:26:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 15:33:48 +00:00
|
|
|
private:
|
2018-09-08 17:01:30 +00:00
|
|
|
struct State {
|
|
|
|
std::unordered_map<int, bool> buttons;
|
|
|
|
std::unordered_map<int, Sint16> axes;
|
|
|
|
std::unordered_map<int, Uint8> hats;
|
|
|
|
} state;
|
2018-09-09 08:11:47 +00:00
|
|
|
std::string guid;
|
|
|
|
int port;
|
2018-09-08 17:01:30 +00:00
|
|
|
std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
|
|
|
|
mutable std::mutex mutex;
|
2017-01-21 15:33:48 +00:00
|
|
|
};
|
|
|
|
|
2018-09-08 17:01:30 +00:00
|
|
|
/**
|
|
|
|
* Get the nth joystick with the corresponding GUID
|
|
|
|
*/
|
|
|
|
static std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port) {
|
|
|
|
std::lock_guard<std::mutex> lock(joystick_map_mutex);
|
2018-09-09 08:11:47 +00:00
|
|
|
const auto it = joystick_map.find(guid);
|
2018-09-08 17:01:30 +00:00
|
|
|
if (it != joystick_map.end()) {
|
|
|
|
while (it->second.size() <= port) {
|
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, it->second.size(), nullptr,
|
|
|
|
[](SDL_Joystick*) {});
|
2018-09-09 08:11:47 +00:00
|
|
|
it->second.emplace_back(std::move(joystick));
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
return it->second[port];
|
|
|
|
}
|
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, [](SDL_Joystick*) {});
|
2018-09-09 08:11:47 +00:00
|
|
|
return joystick_map[guid].emplace_back(std::move(joystick));
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check how many identical joysticks (by guid) were connected before the one with sdl_id and so tie
|
|
|
|
* it to a SDLJoystick with the same guid and that port
|
|
|
|
*/
|
|
|
|
static std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) {
|
|
|
|
std::lock_guard<std::mutex> lock(joystick_map_mutex);
|
|
|
|
auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id);
|
|
|
|
const std::string guid = GetGUID(sdl_joystick);
|
|
|
|
auto map_it = joystick_map.find(guid);
|
|
|
|
if (map_it != joystick_map.end()) {
|
|
|
|
auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(),
|
|
|
|
[&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) {
|
|
|
|
return sdl_joystick == joystick->GetSDLJoystick();
|
|
|
|
});
|
|
|
|
if (vec_it != map_it->second.end()) {
|
2018-09-09 08:11:47 +00:00
|
|
|
// This is the common case: There is already an existing SDL_Joystick maped to a
|
2018-09-08 17:01:30 +00:00
|
|
|
// SDLJoystick. return the SDLJoystick
|
|
|
|
return *vec_it;
|
|
|
|
}
|
2018-09-09 08:11:47 +00:00
|
|
|
// Search for a SDLJoystick without a mapped SDL_Joystick...
|
2018-09-08 17:01:30 +00:00
|
|
|
auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(),
|
|
|
|
[](const std::shared_ptr<SDLJoystick>& joystick) {
|
|
|
|
return !joystick->GetSDLJoystick();
|
|
|
|
});
|
|
|
|
if (nullptr_it != map_it->second.end()) {
|
|
|
|
// ... and map it
|
|
|
|
(*nullptr_it)->SetSDLJoystick(sdl_joystick);
|
|
|
|
return *nullptr_it;
|
|
|
|
}
|
2018-09-09 08:11:47 +00:00
|
|
|
// There is no SDLJoystick without a mapped SDL_Joystick
|
|
|
|
// Create a new SDLJoystick
|
2018-09-08 17:01:30 +00:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, map_it->second.size(), sdl_joystick);
|
2018-09-09 08:11:47 +00:00
|
|
|
return map_it->second.emplace_back(std::move(joystick));
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick);
|
2018-09-09 08:11:47 +00:00
|
|
|
return joystick_map[guid].emplace_back(std::move(joystick));
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitJoystick(int joystick_index) {
|
|
|
|
std::lock_guard<std::mutex> lock(joystick_map_mutex);
|
|
|
|
SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index);
|
|
|
|
if (!sdl_joystick) {
|
|
|
|
LOG_ERROR(Input, "failed to open joystick {}", joystick_index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string guid = GetGUID(sdl_joystick);
|
|
|
|
if (joystick_map.find(guid) == joystick_map.end()) {
|
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick);
|
2018-09-09 08:11:47 +00:00
|
|
|
joystick_map[guid].emplace_back(std::move(joystick));
|
2018-09-08 17:01:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto& joystick_guid_list = joystick_map[guid];
|
2018-09-09 08:11:47 +00:00
|
|
|
const auto it = std::find_if(
|
2018-09-08 17:01:30 +00:00
|
|
|
joystick_guid_list.begin(), joystick_guid_list.end(),
|
|
|
|
[](const std::shared_ptr<SDLJoystick>& joystick) { return !joystick->GetSDLJoystick(); });
|
|
|
|
if (it != joystick_guid_list.end()) {
|
|
|
|
(*it)->SetSDLJoystick(sdl_joystick);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, joystick_guid_list.size(), sdl_joystick);
|
2018-09-09 08:11:47 +00:00
|
|
|
joystick_guid_list.emplace_back(std::move(joystick));
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseJoystick(SDL_Joystick* sdl_joystick) {
|
|
|
|
std::lock_guard<std::mutex> lock(joystick_map_mutex);
|
|
|
|
std::string guid = GetGUID(sdl_joystick);
|
|
|
|
// This call to guid is save since the joystick is guranteed to be in that map
|
|
|
|
auto& joystick_guid_list = joystick_map[guid];
|
2018-09-09 08:11:47 +00:00
|
|
|
const auto joystick_it =
|
2018-09-08 17:01:30 +00:00
|
|
|
std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
|
|
|
|
[&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) {
|
|
|
|
return joystick->GetSDLJoystick() == sdl_joystick;
|
|
|
|
});
|
|
|
|
(*joystick_it)->SetSDLJoystick(nullptr, [](SDL_Joystick*) {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleGameControllerEvent(const SDL_Event& event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYBUTTONUP: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jbutton.which);
|
|
|
|
if (joystick) {
|
|
|
|
joystick->SetButton(event.jbutton.button, false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYBUTTONDOWN: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jbutton.which);
|
|
|
|
if (joystick) {
|
|
|
|
joystick->SetButton(event.jbutton.button, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYHATMOTION: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jhat.which);
|
|
|
|
if (joystick) {
|
|
|
|
joystick->SetHat(event.jhat.hat, event.jhat.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYAXISMOTION: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jaxis.which);
|
|
|
|
if (joystick) {
|
|
|
|
joystick->SetAxis(event.jaxis.axis, event.jaxis.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYDEVICEREMOVED:
|
|
|
|
LOG_DEBUG(Input, "Controller removed with Instance_ID {}", event.jdevice.which);
|
|
|
|
CloseJoystick(SDL_JoystickFromInstanceID(event.jdevice.which));
|
|
|
|
break;
|
|
|
|
case SDL_JOYDEVICEADDED:
|
|
|
|
LOG_DEBUG(Input, "Controller connected with device index {}", event.jdevice.which);
|
|
|
|
InitJoystick(event.jdevice.which);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseSDLJoysticks() {
|
|
|
|
std::lock_guard<std::mutex> lock(joystick_map_mutex);
|
|
|
|
joystick_map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PollLoop() {
|
|
|
|
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
|
|
|
|
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
while (initialized) {
|
|
|
|
// Wait for 10 ms or until an event happens
|
|
|
|
if (SDL_WaitEventTimeout(&event, 10)) {
|
|
|
|
// Don't handle the event if we are configuring
|
2018-09-09 08:11:47 +00:00
|
|
|
if (polling) {
|
2018-09-08 17:01:30 +00:00
|
|
|
event_queue.Push(event);
|
2018-09-09 08:11:47 +00:00
|
|
|
} else {
|
|
|
|
HandleGameControllerEvent(event);
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseSDLJoysticks();
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:33:48 +00:00
|
|
|
class SDLButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_)
|
2018-08-03 01:51:08 +00:00
|
|
|
: joystick(std::move(joystick_)), button(button_) {}
|
2017-01-21 15:33:48 +00:00
|
|
|
|
|
|
|
bool GetStatus() const override {
|
|
|
|
return joystick->GetButton(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int button;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLDirectionButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLDirectionButton(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_)
|
2018-08-03 01:51:08 +00:00
|
|
|
: joystick(std::move(joystick_)), hat(hat_), direction(direction_) {}
|
2017-01-21 15:33:48 +00:00
|
|
|
|
|
|
|
bool GetStatus() const override {
|
|
|
|
return joystick->GetHatDirection(hat, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int hat;
|
|
|
|
Uint8 direction;
|
|
|
|
};
|
|
|
|
|
2017-04-17 08:49:04 +00:00
|
|
|
class SDLAxisButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLAxisButton(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_,
|
|
|
|
bool trigger_if_greater_)
|
2018-08-03 01:51:08 +00:00
|
|
|
: joystick(std::move(joystick_)), axis(axis_), threshold(threshold_),
|
2017-04-17 08:49:04 +00:00
|
|
|
trigger_if_greater(trigger_if_greater_) {}
|
|
|
|
|
|
|
|
bool GetStatus() const override {
|
|
|
|
float axis_value = joystick->GetAxis(axis);
|
|
|
|
if (trigger_if_greater)
|
|
|
|
return axis_value > threshold;
|
|
|
|
return axis_value < threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int axis;
|
|
|
|
float threshold;
|
|
|
|
bool trigger_if_greater;
|
|
|
|
};
|
|
|
|
|
2017-01-21 15:33:48 +00:00
|
|
|
class SDLAnalog final : public Input::AnalogDevice {
|
|
|
|
public:
|
|
|
|
SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_)
|
2018-08-03 01:51:08 +00:00
|
|
|
: joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_) {}
|
2017-01-21 15:33:48 +00:00
|
|
|
|
|
|
|
std::tuple<float, float> GetStatus() const override {
|
|
|
|
return joystick->GetAnalog(axis_x, axis_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int axis_x;
|
|
|
|
int axis_y;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A button device factory that creates button devices from SDL joystick
|
|
|
|
class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates a button device from a joystick button
|
|
|
|
* @param params contains parameters for creating the device:
|
2018-09-08 17:01:30 +00:00
|
|
|
* - "guid": the guid of the joystick to bind
|
|
|
|
* - "port": the nth joystick of the same type to bind
|
2017-01-21 15:33:48 +00:00
|
|
|
* - "button"(optional): the index of the button to bind
|
|
|
|
* - "hat"(optional): the index of the hat to bind as direction buttons
|
2017-04-17 08:49:04 +00:00
|
|
|
* - "axis"(optional): the index of the axis to bind
|
2017-01-21 15:33:48 +00:00
|
|
|
* - "direction"(only used for hat): the direction name of the hat to bind. Can be "up",
|
2017-04-17 08:49:04 +00:00
|
|
|
* "down", "left" or "right"
|
2017-08-11 07:51:16 +00:00
|
|
|
* - "threshold"(only used for axis): a float value in (-1.0, 1.0) which the button is
|
2017-04-17 08:49:04 +00:00
|
|
|
* triggered if the axis value crosses
|
2018-09-08 17:01:30 +00:00
|
|
|
* - "direction"(only used for axis): "+" means the button is triggered when the axis
|
|
|
|
* value is greater than the threshold; "-" means the button is triggered when the axis
|
|
|
|
* value is smaller than the threshold
|
2017-01-21 15:33:48 +00:00
|
|
|
*/
|
|
|
|
std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override {
|
2018-09-08 17:01:30 +00:00
|
|
|
const std::string guid = params.Get("guid", "0");
|
|
|
|
const int port = params.Get("port", 0);
|
|
|
|
|
|
|
|
auto joystick = GetSDLJoystickByGUID(guid, port);
|
2017-01-21 15:33:48 +00:00
|
|
|
|
|
|
|
if (params.Has("hat")) {
|
|
|
|
const int hat = params.Get("hat", 0);
|
|
|
|
const std::string direction_name = params.Get("direction", "");
|
|
|
|
Uint8 direction;
|
|
|
|
if (direction_name == "up") {
|
|
|
|
direction = SDL_HAT_UP;
|
|
|
|
} else if (direction_name == "down") {
|
|
|
|
direction = SDL_HAT_DOWN;
|
|
|
|
} else if (direction_name == "left") {
|
|
|
|
direction = SDL_HAT_LEFT;
|
|
|
|
} else if (direction_name == "right") {
|
|
|
|
direction = SDL_HAT_RIGHT;
|
|
|
|
} else {
|
|
|
|
direction = 0;
|
|
|
|
}
|
2018-09-08 17:01:30 +00:00
|
|
|
// This is necessary so accessing GetHat with hat won't crash
|
|
|
|
joystick->SetHat(hat, SDL_HAT_CENTERED);
|
|
|
|
return std::make_unique<SDLDirectionButton>(joystick, hat, direction);
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 08:49:04 +00:00
|
|
|
if (params.Has("axis")) {
|
|
|
|
const int axis = params.Get("axis", 0);
|
|
|
|
const float threshold = params.Get("threshold", 0.5f);
|
|
|
|
const std::string direction_name = params.Get("direction", "");
|
|
|
|
bool trigger_if_greater;
|
|
|
|
if (direction_name == "+") {
|
|
|
|
trigger_if_greater = true;
|
|
|
|
} else if (direction_name == "-") {
|
|
|
|
trigger_if_greater = false;
|
|
|
|
} else {
|
|
|
|
trigger_if_greater = true;
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Input, "Unknown direction {}", direction_name);
|
2017-04-17 08:49:04 +00:00
|
|
|
}
|
2018-09-08 17:01:30 +00:00
|
|
|
// This is necessary so accessing GetAxis with axis won't crash
|
|
|
|
joystick->SetAxis(axis, 0);
|
|
|
|
return std::make_unique<SDLAxisButton>(joystick, axis, threshold, trigger_if_greater);
|
2017-04-17 08:49:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 15:33:48 +00:00
|
|
|
const int button = params.Get("button", 0);
|
2018-09-08 17:01:30 +00:00
|
|
|
// This is necessary so accessing GetButton with button won't crash
|
|
|
|
joystick->SetButton(button, false);
|
|
|
|
return std::make_unique<SDLButton>(joystick, button);
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An analog device factory that creates analog devices from SDL joystick
|
|
|
|
class SDLAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates analog device from joystick axes
|
|
|
|
* @param params contains parameters for creating the device:
|
2018-09-08 17:01:30 +00:00
|
|
|
* - "guid": the guid of the joystick to bind
|
|
|
|
* - "port": the nth joystick of the same type
|
2017-01-21 15:33:48 +00:00
|
|
|
* - "axis_x": the index of the axis to be bind as x-axis
|
|
|
|
* - "axis_y": the index of the axis to be bind as y-axis
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override {
|
2018-09-08 17:01:30 +00:00
|
|
|
const std::string guid = params.Get("guid", "0");
|
|
|
|
const int port = params.Get("port", 0);
|
2017-01-21 15:33:48 +00:00
|
|
|
const int axis_x = params.Get("axis_x", 0);
|
|
|
|
const int axis_y = params.Get("axis_y", 1);
|
2018-09-08 17:01:30 +00:00
|
|
|
|
|
|
|
auto joystick = GetSDLJoystickByGUID(guid, port);
|
|
|
|
|
|
|
|
// This is necessary so accessing GetAxis with axis_x and axis_y won't crash
|
|
|
|
joystick->SetAxis(axis_x, 0);
|
|
|
|
joystick->SetAxis(axis_y, 0);
|
|
|
|
return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y);
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void Init() {
|
2018-09-08 17:01:30 +00:00
|
|
|
using namespace Input;
|
|
|
|
RegisterFactory<ButtonDevice>("sdl", std::make_shared<SDLButtonFactory>());
|
|
|
|
RegisterFactory<AnalogDevice>("sdl", std::make_shared<SDLAnalogFactory>());
|
|
|
|
polling = false;
|
|
|
|
initialized = true;
|
2017-01-21 15:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
|
|
|
if (initialized) {
|
|
|
|
using namespace Input;
|
|
|
|
UnregisterFactory<ButtonDevice>("sdl");
|
|
|
|
UnregisterFactory<AnalogDevice>("sdl");
|
2018-09-08 17:01:30 +00:00
|
|
|
initialized = false;
|
2017-12-06 04:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::ParamPackage SDLEventToButtonParamPackage(const SDL_Event& event) {
|
|
|
|
Common::ParamPackage params({{"engine", "sdl"}});
|
|
|
|
switch (event.type) {
|
2018-09-08 17:01:30 +00:00
|
|
|
case SDL_JOYAXISMOTION: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jaxis.which);
|
|
|
|
params.Set("port", joystick->GetPort());
|
|
|
|
params.Set("guid", joystick->GetGUID());
|
2017-12-06 04:26:29 +00:00
|
|
|
params.Set("axis", event.jaxis.axis);
|
|
|
|
if (event.jaxis.value > 0) {
|
|
|
|
params.Set("direction", "+");
|
|
|
|
params.Set("threshold", "0.5");
|
|
|
|
} else {
|
|
|
|
params.Set("direction", "-");
|
|
|
|
params.Set("threshold", "-0.5");
|
|
|
|
}
|
|
|
|
break;
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
case SDL_JOYBUTTONUP: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jbutton.which);
|
|
|
|
params.Set("port", joystick->GetPort());
|
|
|
|
params.Set("guid", joystick->GetGUID());
|
2017-12-06 04:26:29 +00:00
|
|
|
params.Set("button", event.jbutton.button);
|
|
|
|
break;
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
|
|
|
case SDL_JOYHATMOTION: {
|
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jhat.which);
|
|
|
|
params.Set("port", joystick->GetPort());
|
|
|
|
params.Set("guid", joystick->GetGUID());
|
2017-12-06 04:26:29 +00:00
|
|
|
params.Set("hat", event.jhat.hat);
|
|
|
|
switch (event.jhat.value) {
|
|
|
|
case SDL_HAT_UP:
|
|
|
|
params.Set("direction", "up");
|
|
|
|
break;
|
|
|
|
case SDL_HAT_DOWN:
|
|
|
|
params.Set("direction", "down");
|
|
|
|
break;
|
|
|
|
case SDL_HAT_LEFT:
|
|
|
|
params.Set("direction", "left");
|
|
|
|
break;
|
|
|
|
case SDL_HAT_RIGHT:
|
|
|
|
params.Set("direction", "right");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-08 17:01:30 +00:00
|
|
|
}
|
2017-12-06 04:26:29 +00:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Polling {
|
|
|
|
|
|
|
|
class SDLPoller : public InputCommon::Polling::DevicePoller {
|
|
|
|
public:
|
|
|
|
void Start() override {
|
2018-09-08 17:01:30 +00:00
|
|
|
event_queue.Clear();
|
|
|
|
polling = true;
|
2017-12-06 04:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() override {
|
2018-09-08 17:01:30 +00:00
|
|
|
polling = false;
|
2017-12-06 04:26:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLButtonPoller final : public SDLPoller {
|
|
|
|
public:
|
|
|
|
Common::ParamPackage GetNextInput() override {
|
|
|
|
SDL_Event event;
|
2018-09-08 17:01:30 +00:00
|
|
|
while (event_queue.Pop(event)) {
|
2017-12-06 04:26:29 +00:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYAXISMOTION:
|
|
|
|
if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYBUTTONUP:
|
|
|
|
case SDL_JOYHATMOTION:
|
|
|
|
return SDLEventToButtonParamPackage(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLAnalogPoller final : public SDLPoller {
|
|
|
|
public:
|
|
|
|
void Start() override {
|
|
|
|
SDLPoller::Start();
|
|
|
|
|
|
|
|
// Reset stored axes
|
|
|
|
analog_xaxis = -1;
|
|
|
|
analog_yaxis = -1;
|
|
|
|
analog_axes_joystick = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::ParamPackage GetNextInput() override {
|
|
|
|
SDL_Event event;
|
2018-09-08 17:01:30 +00:00
|
|
|
while (event_queue.Pop(event)) {
|
2017-12-06 04:26:29 +00:00
|
|
|
if (event.type != SDL_JOYAXISMOTION || std::abs(event.jaxis.value / 32767.0) < 0.5) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// An analog device needs two axes, so we need to store the axis for later and wait for
|
|
|
|
// a second SDL event. The axes also must be from the same joystick.
|
|
|
|
int axis = event.jaxis.axis;
|
|
|
|
if (analog_xaxis == -1) {
|
|
|
|
analog_xaxis = axis;
|
|
|
|
analog_axes_joystick = event.jaxis.which;
|
|
|
|
} else if (analog_yaxis == -1 && analog_xaxis != axis &&
|
|
|
|
analog_axes_joystick == event.jaxis.which) {
|
|
|
|
analog_yaxis = axis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Common::ParamPackage params;
|
|
|
|
if (analog_xaxis != -1 && analog_yaxis != -1) {
|
2018-09-08 17:01:30 +00:00
|
|
|
auto joystick = GetSDLJoystickBySDLID(event.jaxis.which);
|
2017-12-06 04:26:29 +00:00
|
|
|
params.Set("engine", "sdl");
|
2018-09-08 17:01:30 +00:00
|
|
|
params.Set("port", joystick->GetPort());
|
|
|
|
params.Set("guid", joystick->GetGUID());
|
2017-12-06 04:26:29 +00:00
|
|
|
params.Set("axis_x", analog_xaxis);
|
|
|
|
params.Set("axis_y", analog_yaxis);
|
|
|
|
analog_xaxis = -1;
|
|
|
|
analog_yaxis = -1;
|
|
|
|
analog_axes_joystick = -1;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int analog_xaxis = -1;
|
|
|
|
int analog_yaxis = -1;
|
|
|
|
SDL_JoystickID analog_axes_joystick = -1;
|
|
|
|
};
|
|
|
|
|
2018-01-18 15:43:17 +00:00
|
|
|
void GetPollers(InputCommon::Polling::DeviceType type,
|
|
|
|
std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>>& pollers) {
|
2017-12-06 04:26:29 +00:00
|
|
|
switch (type) {
|
|
|
|
case InputCommon::Polling::DeviceType::Analog:
|
2018-01-18 15:43:17 +00:00
|
|
|
pollers.emplace_back(std::make_unique<SDLAnalogPoller>());
|
2017-12-06 04:26:29 +00:00
|
|
|
break;
|
|
|
|
case InputCommon::Polling::DeviceType::Button:
|
2018-01-18 15:43:17 +00:00
|
|
|
pollers.emplace_back(std::make_unique<SDLButtonPoller>());
|
2017-12-06 04:26:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace Polling
|
2017-01-21 15:33:48 +00:00
|
|
|
} // namespace SDL
|
|
|
|
} // namespace InputCommon
|