2018-09-20 02:28:05 -04:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
2018-09-22 16:11:15 -04:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-08-28 21:21:14 -04:00
|
|
|
#include <array>
|
2018-09-22 16:11:15 -04:00
|
|
|
#include <atomic>
|
2020-07-10 22:20:50 -04:00
|
|
|
#include <chrono>
|
2018-09-22 16:11:15 -04:00
|
|
|
#include <cmath>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
2020-07-22 10:39:53 -04:00
|
|
|
#include <optional>
|
|
|
|
#include <sstream>
|
2018-09-22 16:11:15 -04:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <tuple>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/param_package.h"
|
|
|
|
#include "common/threadsafe_queue.h"
|
2018-09-20 02:28:05 -04:00
|
|
|
#include "core/frontend/input.h"
|
2020-09-17 21:26:34 -04:00
|
|
|
#include "input_common/motion_input.h"
|
2018-09-20 02:28:05 -04:00
|
|
|
#include "input_common/sdl/sdl_impl.h"
|
2020-08-28 19:15:06 -04:00
|
|
|
#include "input_common/settings.h"
|
2018-09-22 16:11:15 -04:00
|
|
|
|
2019-06-03 15:49:04 -04:00
|
|
|
namespace InputCommon::SDL {
|
2018-09-22 16:11:15 -04:00
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
namespace {
|
|
|
|
std::string GetGUID(SDL_Joystick* joystick) {
|
2019-06-03 16:30:33 -04:00
|
|
|
const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
|
2018-09-22 16:11:15 -04:00
|
|
|
char guid_str[33];
|
|
|
|
SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
|
|
|
|
return guid_str;
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
/// Creates a ParamPackage from an SDL_Event that can directly be used to create a ButtonDevice
|
2020-07-22 10:39:53 -04:00
|
|
|
Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event);
|
|
|
|
} // Anonymous namespace
|
2018-09-20 02:28:05 -04:00
|
|
|
|
2019-06-03 16:19:22 -04:00
|
|
|
static int SDLEventWatcher(void* user_data, SDL_Event* event) {
|
|
|
|
auto* const sdl_state = static_cast<SDLState*>(user_data);
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
// Don't handle the event if we are configuring
|
|
|
|
if (sdl_state->polling) {
|
|
|
|
sdl_state->event_queue.Push(*event);
|
|
|
|
} else {
|
|
|
|
sdl_state->HandleGameControllerEvent(*event);
|
|
|
|
}
|
2019-06-03 16:19:22 -04:00
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
class SDLJoystick {
|
|
|
|
public:
|
2020-07-22 10:39:53 -04:00
|
|
|
SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick,
|
2020-10-14 02:51:14 -04:00
|
|
|
SDL_GameController* game_controller)
|
2020-07-22 10:39:53 -04:00
|
|
|
: guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose},
|
2020-10-14 02:51:14 -04:00
|
|
|
sdl_controller{game_controller, &SDL_GameControllerClose} {}
|
2018-09-22 16:11:15 -04:00
|
|
|
|
|
|
|
void SetButton(int button, bool value) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{mutex};
|
2019-06-03 16:15:30 -04:00
|
|
|
state.buttons.insert_or_assign(button, value);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetButton(int button) const {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{mutex};
|
2018-09-22 16:11:15 -04:00
|
|
|
return state.buttons.at(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAxis(int axis, Sint16 value) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{mutex};
|
2019-06-03 16:15:30 -04:00
|
|
|
state.axes.insert_or_assign(axis, value);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2020-07-13 11:58:40 -04:00
|
|
|
float GetAxis(int axis, float range) const {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{mutex};
|
2020-10-14 02:51:14 -04:00
|
|
|
return static_cast<float>(state.axes.at(axis)) / (32767.0f * range);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:51:14 -04:00
|
|
|
bool RumblePlay(f32 amp_low, f32 amp_high, u32 time) {
|
2020-07-10 22:20:50 -04:00
|
|
|
const u16 raw_amp_low = static_cast<u16>(amp_low * 0xFFFF);
|
|
|
|
const u16 raw_amp_high = static_cast<u16>(amp_high * 0xFFFF);
|
|
|
|
// Lower drastically the number of state changes
|
|
|
|
if (raw_amp_low >> 11 == last_state_rumble_low >> 11 &&
|
|
|
|
raw_amp_high >> 11 == last_state_rumble_high >> 11) {
|
|
|
|
if (raw_amp_low + raw_amp_high != 0 ||
|
|
|
|
last_state_rumble_low + last_state_rumble_high == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Don't change state if last vibration was < 20ms
|
|
|
|
const auto now = std::chrono::system_clock::now();
|
|
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_vibration) <
|
|
|
|
std::chrono::milliseconds(20)) {
|
|
|
|
return raw_amp_low + raw_amp_high == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_vibration = now;
|
|
|
|
last_state_rumble_low = raw_amp_low;
|
|
|
|
last_state_rumble_high = raw_amp_high;
|
|
|
|
if (sdl_joystick) {
|
|
|
|
SDL_JoystickRumble(sdl_joystick.get(), raw_amp_low, raw_amp_high, time);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-13 11:58:40 -04:00
|
|
|
std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const {
|
|
|
|
float x = GetAxis(axis_x, range);
|
|
|
|
float y = GetAxis(axis_y, range);
|
2018-09-22 16:11:15 -04: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);
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:51:14 -04:00
|
|
|
const MotionInput& GetMotion() const {
|
2020-09-17 21:26:34 -04:00
|
|
|
return motion;
|
|
|
|
}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
void SetHat(int hat, Uint8 direction) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{mutex};
|
2019-06-03 16:15:30 -04:00
|
|
|
state.hats.insert_or_assign(hat, direction);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetHatDirection(int hat, Uint8 direction) const {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{mutex};
|
2018-09-22 16:11:15 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
void SetSDLJoystick(SDL_Joystick* joystick, SDL_GameController* controller) {
|
|
|
|
sdl_controller.reset(controller);
|
2019-06-03 16:10:08 -04:00
|
|
|
sdl_joystick.reset(joystick);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
SDL_GameController* GetSDLGameController() const {
|
|
|
|
return sdl_controller.get();
|
|
|
|
}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
private:
|
|
|
|
struct State {
|
|
|
|
std::unordered_map<int, bool> buttons;
|
|
|
|
std::unordered_map<int, Sint16> axes;
|
|
|
|
std::unordered_map<int, Uint8> hats;
|
|
|
|
} state;
|
|
|
|
std::string guid;
|
|
|
|
int port;
|
2020-10-14 02:51:14 -04:00
|
|
|
u16 last_state_rumble_high = 0;
|
|
|
|
u16 last_state_rumble_low = 0;
|
2020-07-10 22:20:50 -04:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> last_vibration;
|
2018-09-22 16:11:15 -04:00
|
|
|
std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
|
2020-07-22 10:39:53 -04:00
|
|
|
std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller;
|
2018-09-22 16:11:15 -04:00
|
|
|
mutable std::mutex mutex;
|
2020-09-17 21:26:34 -04:00
|
|
|
|
2020-10-14 02:51:14 -04:00
|
|
|
// Motion is initialized without PID values as motion input is not aviable for SDL2
|
|
|
|
MotionInput motion{0.0f, 0.0f, 0.0f};
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{joystick_map_mutex};
|
2018-09-22 16:11:15 -04:00
|
|
|
const auto it = joystick_map.find(guid);
|
|
|
|
if (it != joystick_map.end()) {
|
2019-05-29 01:33:00 -04:00
|
|
|
while (it->second.size() <= static_cast<std::size_t>(port)) {
|
2020-07-22 10:39:53 -04:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()),
|
|
|
|
nullptr, nullptr);
|
2018-09-22 16:11:15 -04:00
|
|
|
it->second.emplace_back(std::move(joystick));
|
|
|
|
}
|
2020-10-14 02:51:14 -04:00
|
|
|
return it->second[static_cast<std::size_t>(port)];
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
2020-07-22 10:39:53 -04:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, nullptr);
|
2018-09-22 16:11:15 -04:00
|
|
|
return joystick_map[guid].emplace_back(std::move(joystick));
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) {
|
2018-09-22 16:11:15 -04:00
|
|
|
auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id);
|
2020-07-22 10:39:53 -04:00
|
|
|
auto sdl_controller = SDL_GameControllerFromInstanceID(sdl_id);
|
2018-09-22 16:11:15 -04:00
|
|
|
const std::string guid = GetGUID(sdl_joystick);
|
2019-04-01 12:29:59 -04:00
|
|
|
|
|
|
|
std::lock_guard lock{joystick_map_mutex};
|
2019-06-03 16:30:33 -04:00
|
|
|
const auto map_it = joystick_map.find(guid);
|
2018-09-22 16:11:15 -04:00
|
|
|
if (map_it != joystick_map.end()) {
|
2019-06-03 16:04:56 -04:00
|
|
|
const 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();
|
|
|
|
});
|
2018-09-22 16:11:15 -04:00
|
|
|
if (vec_it != map_it->second.end()) {
|
2020-10-14 02:51:14 -04:00
|
|
|
// This is the common case: There is already an existing SDL_Joystick mapped to a
|
2018-09-22 16:11:15 -04:00
|
|
|
// SDLJoystick. return the SDLJoystick
|
|
|
|
return *vec_it;
|
|
|
|
}
|
2019-06-03 16:04:56 -04:00
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
// Search for a SDLJoystick without a mapped SDL_Joystick...
|
2019-06-03 16:04:56 -04:00
|
|
|
const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(),
|
|
|
|
[](const std::shared_ptr<SDLJoystick>& joystick) {
|
2020-10-14 02:51:14 -04:00
|
|
|
return joystick->GetSDLJoystick() == nullptr;
|
2019-06-03 16:04:56 -04:00
|
|
|
});
|
2018-09-22 16:11:15 -04:00
|
|
|
if (nullptr_it != map_it->second.end()) {
|
|
|
|
// ... and map it
|
2020-07-22 10:39:53 -04:00
|
|
|
(*nullptr_it)->SetSDLJoystick(sdl_joystick, sdl_controller);
|
2018-09-22 16:11:15 -04:00
|
|
|
return *nullptr_it;
|
|
|
|
}
|
2019-06-03 16:04:56 -04:00
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
// There is no SDLJoystick without a mapped SDL_Joystick
|
|
|
|
// Create a new SDLJoystick
|
2019-06-03 16:04:56 -04:00
|
|
|
const int port = static_cast<int>(map_it->second.size());
|
2020-07-22 10:39:53 -04:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_controller);
|
2018-09-22 16:11:15 -04:00
|
|
|
return map_it->second.emplace_back(std::move(joystick));
|
|
|
|
}
|
2019-06-03 16:04:56 -04:00
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_controller);
|
2018-09-22 16:11:15 -04:00
|
|
|
return joystick_map[guid].emplace_back(std::move(joystick));
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
void SDLState::InitJoystick(int joystick_index) {
|
2018-09-22 16:11:15 -04:00
|
|
|
SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index);
|
2020-07-22 10:39:53 -04:00
|
|
|
SDL_GameController* sdl_gamecontroller = nullptr;
|
|
|
|
if (SDL_IsGameController(joystick_index)) {
|
|
|
|
sdl_gamecontroller = SDL_GameControllerOpen(joystick_index);
|
|
|
|
}
|
2018-09-22 16:11:15 -04:00
|
|
|
if (!sdl_joystick) {
|
2020-07-10 22:20:50 -04:00
|
|
|
LOG_ERROR(Input, "Failed to open joystick {}", joystick_index);
|
2018-09-22 16:11:15 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-04-01 12:29:59 -04:00
|
|
|
const std::string guid = GetGUID(sdl_joystick);
|
|
|
|
|
|
|
|
std::lock_guard lock{joystick_map_mutex};
|
2018-09-22 16:11:15 -04:00
|
|
|
if (joystick_map.find(guid) == joystick_map.end()) {
|
2020-07-22 10:39:53 -04:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller);
|
2018-09-22 16:11:15 -04:00
|
|
|
joystick_map[guid].emplace_back(std::move(joystick));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto& joystick_guid_list = joystick_map[guid];
|
|
|
|
const auto it = std::find_if(
|
|
|
|
joystick_guid_list.begin(), joystick_guid_list.end(),
|
|
|
|
[](const std::shared_ptr<SDLJoystick>& joystick) { return !joystick->GetSDLJoystick(); });
|
|
|
|
if (it != joystick_guid_list.end()) {
|
2020-07-22 10:39:53 -04:00
|
|
|
(*it)->SetSDLJoystick(sdl_joystick, sdl_gamecontroller);
|
2018-09-22 16:11:15 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-06-03 16:04:56 -04:00
|
|
|
const int port = static_cast<int>(joystick_guid_list.size());
|
2020-07-22 10:39:53 -04:00
|
|
|
auto joystick = std::make_shared<SDLJoystick>(guid, port, sdl_joystick, sdl_gamecontroller);
|
2018-09-22 16:11:15 -04:00
|
|
|
joystick_guid_list.emplace_back(std::move(joystick));
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
|
2019-06-03 16:10:08 -04:00
|
|
|
const std::string guid = GetGUID(sdl_joystick);
|
|
|
|
|
2020-09-03 11:07:57 -04:00
|
|
|
std::lock_guard lock{joystick_map_mutex};
|
|
|
|
auto& joystick_guid_list = joystick_map[guid];
|
|
|
|
auto joystick_it = std::find_if(
|
|
|
|
joystick_guid_list.begin(), joystick_guid_list.end(),
|
|
|
|
[&sdl_joystick](auto& joystick) { return joystick->GetSDLJoystick() == sdl_joystick; });
|
2019-06-03 16:10:08 -04:00
|
|
|
|
2020-09-03 11:07:57 -04:00
|
|
|
if (joystick_it != joystick_guid_list.end()) {
|
|
|
|
(*joystick_it)->SetSDLJoystick(nullptr, nullptr);
|
|
|
|
joystick_guid_list.erase(joystick_it);
|
|
|
|
if (joystick_guid_list.empty()) {
|
|
|
|
joystick_map.erase(guid);
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
|
2018-09-22 16:11:15 -04:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYBUTTONUP: {
|
2018-09-20 02:28:05 -04:00
|
|
|
if (auto joystick = GetSDLJoystickBySDLID(event.jbutton.which)) {
|
2018-09-22 16:11:15 -04:00
|
|
|
joystick->SetButton(event.jbutton.button, false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYBUTTONDOWN: {
|
2018-09-20 02:28:05 -04:00
|
|
|
if (auto joystick = GetSDLJoystickBySDLID(event.jbutton.which)) {
|
2018-09-22 16:11:15 -04:00
|
|
|
joystick->SetButton(event.jbutton.button, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYHATMOTION: {
|
2018-09-20 02:28:05 -04:00
|
|
|
if (auto joystick = GetSDLJoystickBySDLID(event.jhat.which)) {
|
2018-09-22 16:11:15 -04:00
|
|
|
joystick->SetHat(event.jhat.hat, event.jhat.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_JOYAXISMOTION: {
|
2018-09-20 02:28:05 -04:00
|
|
|
if (auto joystick = GetSDLJoystickBySDLID(event.jaxis.which)) {
|
2018-09-22 16:11:15 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
void SDLState::CloseJoysticks() {
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{joystick_map_mutex};
|
2018-09-22 16:11:15 -04:00
|
|
|
joystick_map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SDLButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_)
|
|
|
|
: joystick(std::move(joystick_)), button(button_) {}
|
|
|
|
|
|
|
|
bool GetStatus() const override {
|
|
|
|
return joystick->GetButton(button);
|
|
|
|
}
|
|
|
|
|
2020-07-10 22:20:50 -04:00
|
|
|
bool SetRumblePlay(f32 amp_high, f32 amp_low, f32 freq_high, f32 freq_low) const override {
|
|
|
|
const f32 new_amp_low = pow(amp_low, 0.5f) * (3.0f - 2.0f * pow(amp_low, 0.15f));
|
|
|
|
const f32 new_amp_high = pow(amp_high, 0.5f) * (3.0f - 2.0f * pow(amp_high, 0.15f));
|
|
|
|
return joystick->RumblePlay(new_amp_low, new_amp_high, 250);
|
|
|
|
}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
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_)
|
|
|
|
: joystick(std::move(joystick_)), hat(hat_), direction(direction_) {}
|
|
|
|
|
|
|
|
bool GetStatus() const override {
|
|
|
|
return joystick->GetHatDirection(hat, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int hat;
|
|
|
|
Uint8 direction;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLAxisButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLAxisButton(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_,
|
|
|
|
bool trigger_if_greater_)
|
|
|
|
: joystick(std::move(joystick_)), axis(axis_), threshold(threshold_),
|
|
|
|
trigger_if_greater(trigger_if_greater_) {}
|
|
|
|
|
|
|
|
bool GetStatus() const override {
|
2020-07-13 11:58:40 -04:00
|
|
|
const float axis_value = joystick->GetAxis(axis, 1.0f);
|
2019-06-03 16:30:33 -04:00
|
|
|
if (trigger_if_greater) {
|
2018-09-22 16:11:15 -04:00
|
|
|
return axis_value > threshold;
|
2019-06-03 16:30:33 -04:00
|
|
|
}
|
2018-09-22 16:11:15 -04:00
|
|
|
return axis_value < threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int axis;
|
|
|
|
float threshold;
|
|
|
|
bool trigger_if_greater;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLAnalog final : public Input::AnalogDevice {
|
|
|
|
public:
|
2020-10-14 02:51:14 -04:00
|
|
|
explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_,
|
|
|
|
float deadzone_, float range_)
|
2020-07-13 11:58:40 -04:00
|
|
|
: joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_),
|
|
|
|
range(range_) {}
|
2018-09-22 16:11:15 -04:00
|
|
|
|
|
|
|
std::tuple<float, float> GetStatus() const override {
|
2020-07-13 11:58:40 -04:00
|
|
|
const auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range);
|
2018-10-01 09:10:37 -04:00
|
|
|
const float r = std::sqrt((x * x) + (y * y));
|
|
|
|
if (r > deadzone) {
|
|
|
|
return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone),
|
|
|
|
y / r * (r - deadzone) / (1 - deadzone));
|
|
|
|
}
|
2020-08-28 21:04:14 -04:00
|
|
|
return {};
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 05:25:15 -05:00
|
|
|
bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
|
|
|
|
const auto [x, y] = GetStatus();
|
2020-08-19 04:17:38 -04:00
|
|
|
const float directional_deadzone = 0.5f;
|
2020-01-15 05:25:15 -05:00
|
|
|
switch (direction) {
|
|
|
|
case Input::AnalogDirection::RIGHT:
|
|
|
|
return x > directional_deadzone;
|
|
|
|
case Input::AnalogDirection::LEFT:
|
|
|
|
return x < -directional_deadzone;
|
|
|
|
case Input::AnalogDirection::UP:
|
|
|
|
return y > directional_deadzone;
|
|
|
|
case Input::AnalogDirection::DOWN:
|
|
|
|
return y < -directional_deadzone;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
2018-10-01 09:10:37 -04:00
|
|
|
const int axis_x;
|
|
|
|
const int axis_y;
|
|
|
|
const float deadzone;
|
2020-07-13 11:58:40 -04:00
|
|
|
const float range;
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
|
|
|
|
2020-09-17 21:26:34 -04:00
|
|
|
class SDLDirectionMotion final : public Input::MotionDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLDirectionMotion(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_)
|
|
|
|
: joystick(std::move(joystick_)), hat(hat_), direction(direction_) {}
|
|
|
|
|
|
|
|
Input::MotionStatus GetStatus() const override {
|
|
|
|
if (joystick->GetHatDirection(hat, direction)) {
|
|
|
|
return joystick->GetMotion().GetRandomMotion(2, 6);
|
|
|
|
}
|
|
|
|
return joystick->GetMotion().GetRandomMotion(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int hat;
|
|
|
|
Uint8 direction;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLAxisMotion final : public Input::MotionDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLAxisMotion(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_,
|
|
|
|
bool trigger_if_greater_)
|
|
|
|
: joystick(std::move(joystick_)), axis(axis_), threshold(threshold_),
|
|
|
|
trigger_if_greater(trigger_if_greater_) {}
|
|
|
|
|
|
|
|
Input::MotionStatus GetStatus() const override {
|
|
|
|
const float axis_value = joystick->GetAxis(axis, 1.0f);
|
|
|
|
bool trigger = axis_value < threshold;
|
|
|
|
if (trigger_if_greater) {
|
|
|
|
trigger = axis_value > threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trigger) {
|
|
|
|
return joystick->GetMotion().GetRandomMotion(2, 6);
|
|
|
|
}
|
|
|
|
return joystick->GetMotion().GetRandomMotion(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int axis;
|
|
|
|
float threshold;
|
|
|
|
bool trigger_if_greater;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SDLButtonMotion final : public Input::MotionDevice {
|
|
|
|
public:
|
|
|
|
explicit SDLButtonMotion(std::shared_ptr<SDLJoystick> joystick_, int button_)
|
|
|
|
: joystick(std::move(joystick_)), button(button_) {}
|
|
|
|
|
|
|
|
Input::MotionStatus GetStatus() const override {
|
|
|
|
if (joystick->GetButton(button)) {
|
|
|
|
return joystick->GetMotion().GetRandomMotion(2, 6);
|
|
|
|
}
|
|
|
|
return joystick->GetMotion().GetRandomMotion(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SDLJoystick> joystick;
|
|
|
|
int button;
|
|
|
|
};
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
/// A button device factory that creates button devices from SDL joystick
|
|
|
|
class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> {
|
|
|
|
public:
|
2018-09-20 02:28:05 -04:00
|
|
|
explicit SDLButtonFactory(SDLState& state_) : state(state_) {}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
/**
|
|
|
|
* Creates a button device from a joystick button
|
|
|
|
* @param params contains parameters for creating the device:
|
|
|
|
* - "guid": the guid of the joystick to bind
|
|
|
|
* - "port": the nth joystick of the same type to bind
|
|
|
|
* - "button"(optional): the index of the button to bind
|
|
|
|
* - "hat"(optional): the index of the hat to bind as direction buttons
|
|
|
|
* - "axis"(optional): the index of the axis to bind
|
|
|
|
* - "direction"(only used for hat): the direction name of the hat to bind. Can be "up",
|
|
|
|
* "down", "left" or "right"
|
|
|
|
* - "threshold"(only used for axis): a float value in (-1.0, 1.0) which the button is
|
|
|
|
* triggered if the axis value crosses
|
|
|
|
* - "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
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override {
|
|
|
|
const std::string guid = params.Get("guid", "0");
|
|
|
|
const int port = params.Get("port", 0);
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
auto joystick = state.GetSDLJoystickByGUID(guid, port);
|
2018-09-22 16:11:15 -04: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;
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
LOG_ERROR(Input, "Unknown direction {}", direction_name);
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int button = params.Get("button", 0);
|
|
|
|
// This is necessary so accessing GetButton with button won't crash
|
|
|
|
joystick->SetButton(button, false);
|
|
|
|
return std::make_unique<SDLButton>(joystick, button);
|
|
|
|
}
|
2018-09-20 02:28:05 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
SDLState& state;
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An analog device factory that creates analog devices from SDL joystick
|
|
|
|
class SDLAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
|
|
|
|
public:
|
2018-09-20 02:28:05 -04:00
|
|
|
explicit SDLAnalogFactory(SDLState& state_) : state(state_) {}
|
2018-09-22 16:11:15 -04:00
|
|
|
/**
|
|
|
|
* Creates analog device from joystick axes
|
|
|
|
* @param params contains parameters for creating the device:
|
|
|
|
* - "guid": the guid of the joystick to bind
|
|
|
|
* - "port": the nth joystick of the same type
|
|
|
|
* - "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 {
|
|
|
|
const std::string guid = params.Get("guid", "0");
|
|
|
|
const int port = params.Get("port", 0);
|
|
|
|
const int axis_x = params.Get("axis_x", 0);
|
|
|
|
const int axis_y = params.Get("axis_y", 1);
|
2020-07-22 10:39:53 -04:00
|
|
|
const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
|
2020-08-10 12:39:42 -04:00
|
|
|
const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f);
|
2018-09-20 02:28:05 -04:00
|
|
|
auto joystick = state.GetSDLJoystickByGUID(guid, port);
|
2018-09-22 16:11:15 -04:00
|
|
|
|
|
|
|
// 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);
|
2020-07-13 11:58:40 -04:00
|
|
|
return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone, range);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
2018-09-20 02:28:05 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
SDLState& state;
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
|
|
|
|
2020-09-17 21:26:34 -04:00
|
|
|
/// A motion device factory that creates motion devices from SDL joystick
|
|
|
|
class SDLMotionFactory final : public Input::Factory<Input::MotionDevice> {
|
|
|
|
public:
|
|
|
|
explicit SDLMotionFactory(SDLState& state_) : state(state_) {}
|
|
|
|
/**
|
|
|
|
* Creates motion device from joystick axes
|
|
|
|
* @param params contains parameters for creating the device:
|
|
|
|
* - "guid": the guid of the joystick to bind
|
|
|
|
* - "port": the nth joystick of the same type
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override {
|
|
|
|
const std::string guid = params.Get("guid", "0");
|
|
|
|
const int port = params.Get("port", 0);
|
|
|
|
|
|
|
|
auto joystick = state.GetSDLJoystickByGUID(guid, port);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// This is necessary so accessing GetHat with hat won't crash
|
|
|
|
joystick->SetHat(hat, SDL_HAT_CENTERED);
|
|
|
|
return std::make_unique<SDLDirectionMotion>(joystick, hat, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
LOG_ERROR(Input, "Unknown direction {}", direction_name);
|
|
|
|
}
|
|
|
|
// This is necessary so accessing GetAxis with axis won't crash
|
|
|
|
joystick->SetAxis(axis, 0);
|
|
|
|
return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int button = params.Get("button", 0);
|
|
|
|
// This is necessary so accessing GetButton with button won't crash
|
|
|
|
joystick->SetButton(button, false);
|
|
|
|
return std::make_unique<SDLButtonMotion>(joystick, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SDLState& state;
|
|
|
|
};
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
SDLState::SDLState() {
|
2018-09-22 16:11:15 -04:00
|
|
|
using namespace Input;
|
2020-07-22 10:39:53 -04:00
|
|
|
analog_factory = std::make_shared<SDLAnalogFactory>(*this);
|
|
|
|
button_factory = std::make_shared<SDLButtonFactory>(*this);
|
2020-09-17 21:26:34 -04:00
|
|
|
motion_factory = std::make_shared<SDLMotionFactory>(*this);
|
2020-07-22 10:39:53 -04:00
|
|
|
RegisterFactory<AnalogDevice>("sdl", analog_factory);
|
|
|
|
RegisterFactory<ButtonDevice>("sdl", button_factory);
|
2020-09-17 21:26:34 -04:00
|
|
|
RegisterFactory<MotionDevice>("sdl", motion_factory);
|
2018-09-20 02:28:05 -04:00
|
|
|
|
2020-10-14 02:51:14 -04:00
|
|
|
// If the frontend is going to manage the event loop, then we don't start one here
|
|
|
|
start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0;
|
2018-09-20 02:28:05 -04:00
|
|
|
if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) {
|
|
|
|
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
2020-10-14 02:51:14 -04:00
|
|
|
has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0;
|
2018-10-01 09:10:37 -04:00
|
|
|
if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) {
|
2019-06-03 16:44:54 -04:00
|
|
|
LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError());
|
2018-10-01 09:10:37 -04:00
|
|
|
}
|
2018-09-20 02:28:05 -04:00
|
|
|
|
|
|
|
SDL_AddEventWatch(&SDLEventWatcher, this);
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
initialized = true;
|
2018-09-20 02:28:05 -04:00
|
|
|
if (start_thread) {
|
2019-03-17 03:20:53 -04:00
|
|
|
poll_thread = std::thread([this] {
|
2018-09-20 02:28:05 -04:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
while (initialized) {
|
|
|
|
SDL_PumpEvents();
|
2020-07-22 10:39:53 -04:00
|
|
|
std::this_thread::sleep_for(5ms);
|
2018-09-20 02:28:05 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Because the events for joystick connection happens before we have our event watcher added, we
|
|
|
|
// can just open all the joysticks right here
|
|
|
|
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
|
|
|
|
InitJoystick(i);
|
|
|
|
}
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 02:28:05 -04:00
|
|
|
SDLState::~SDLState() {
|
|
|
|
using namespace Input;
|
|
|
|
UnregisterFactory<ButtonDevice>("sdl");
|
|
|
|
UnregisterFactory<AnalogDevice>("sdl");
|
2020-09-17 21:26:34 -04:00
|
|
|
UnregisterFactory<MotionDevice>("sdl");
|
2018-09-20 02:28:05 -04:00
|
|
|
|
|
|
|
CloseJoysticks();
|
|
|
|
SDL_DelEventWatch(&SDLEventWatcher, this);
|
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
if (start_thread) {
|
|
|
|
poll_thread.join();
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
std::vector<Common::ParamPackage> SDLState::GetInputDevices() {
|
|
|
|
std::scoped_lock lock(joystick_map_mutex);
|
2020-08-15 15:26:29 -04:00
|
|
|
std::vector<Common::ParamPackage> devices;
|
2020-07-22 10:39:53 -04:00
|
|
|
for (const auto& [key, value] : joystick_map) {
|
|
|
|
for (const auto& joystick : value) {
|
2020-10-14 02:51:14 -04:00
|
|
|
auto* joy = joystick->GetSDLJoystick();
|
|
|
|
if (auto* controller = joystick->GetSDLGameController()) {
|
2020-07-22 10:39:53 -04:00
|
|
|
std::string name =
|
|
|
|
fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort());
|
|
|
|
devices.emplace_back(Common::ParamPackage{
|
|
|
|
{"class", "sdl"},
|
2020-08-15 15:26:29 -04:00
|
|
|
{"display", std::move(name)},
|
2020-07-22 10:39:53 -04:00
|
|
|
{"guid", joystick->GetGUID()},
|
|
|
|
{"port", std::to_string(joystick->GetPort())},
|
|
|
|
});
|
|
|
|
} else if (joy) {
|
|
|
|
std::string name = fmt::format("{} {}", SDL_JoystickName(joy), joystick->GetPort());
|
|
|
|
devices.emplace_back(Common::ParamPackage{
|
|
|
|
{"class", "sdl"},
|
2020-08-15 15:26:29 -04:00
|
|
|
{"display", std::move(name)},
|
2020-07-22 10:39:53 -04:00
|
|
|
{"guid", joystick->GetGUID()},
|
|
|
|
{"port", std::to_string(joystick->GetPort())},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2020-10-14 02:51:14 -04:00
|
|
|
Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis,
|
2020-08-28 21:08:06 -04:00
|
|
|
float value = 0.1f) {
|
2020-07-22 10:39:53 -04:00
|
|
|
Common::ParamPackage params({{"engine", "sdl"}});
|
|
|
|
params.Set("port", port);
|
2020-08-28 21:14:51 -04:00
|
|
|
params.Set("guid", std::move(guid));
|
2020-07-22 10:39:53 -04:00
|
|
|
params.Set("axis", axis);
|
|
|
|
if (value > 0) {
|
|
|
|
params.Set("direction", "+");
|
|
|
|
params.Set("threshold", "0.5");
|
|
|
|
} else {
|
|
|
|
params.Set("direction", "-");
|
|
|
|
params.Set("threshold", "-0.5");
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:51:14 -04:00
|
|
|
Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) {
|
2020-07-22 10:39:53 -04:00
|
|
|
Common::ParamPackage params({{"engine", "sdl"}});
|
|
|
|
params.Set("port", port);
|
2020-08-28 21:14:51 -04:00
|
|
|
params.Set("guid", std::move(guid));
|
2020-07-22 10:39:53 -04:00
|
|
|
params.Set("button", button);
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:51:14 -04:00
|
|
|
Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) {
|
2018-09-22 16:11:15 -04:00
|
|
|
Common::ParamPackage params({{"engine", "sdl"}});
|
2018-09-20 02:28:05 -04:00
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
params.Set("port", port);
|
2020-08-28 21:14:51 -04:00
|
|
|
params.Set("guid", std::move(guid));
|
2020-07-22 10:39:53 -04:00
|
|
|
params.Set("hat", hat);
|
|
|
|
switch (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 {};
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Event& event) {
|
2018-09-22 16:11:15 -04:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYAXISMOTION: {
|
2019-06-03 16:30:33 -04:00
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
|
2020-08-15 15:26:29 -04:00
|
|
|
return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
|
2020-10-14 02:51:14 -04:00
|
|
|
static_cast<s32>(event.jaxis.axis),
|
|
|
|
event.jaxis.value);
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
case SDL_JOYBUTTONUP: {
|
2019-06-03 16:30:33 -04:00
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
|
2020-08-15 15:26:29 -04:00
|
|
|
return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
|
2020-10-14 02:51:14 -04:00
|
|
|
static_cast<s32>(event.jbutton.button));
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
case SDL_JOYHATMOTION: {
|
2019-06-03 16:30:33 -04:00
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
|
2020-08-15 15:26:29 -04:00
|
|
|
return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
|
2020-10-14 02:51:14 -04:00
|
|
|
static_cast<s32>(event.jhat.hat),
|
|
|
|
static_cast<s32>(event.jhat.value));
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 15:26:29 -04:00
|
|
|
return {};
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
2020-09-17 21:26:34 -04:00
|
|
|
Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Event& event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYAXISMOTION: {
|
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
|
|
|
|
return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
|
2020-10-14 02:51:14 -04:00
|
|
|
static_cast<s32>(event.jaxis.axis),
|
|
|
|
event.jaxis.value);
|
2020-09-17 21:26:34 -04:00
|
|
|
}
|
|
|
|
case SDL_JOYBUTTONUP: {
|
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
|
|
|
|
return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
|
2020-10-14 02:51:14 -04:00
|
|
|
static_cast<s32>(event.jbutton.button));
|
2020-09-17 21:26:34 -04:00
|
|
|
}
|
|
|
|
case SDL_JOYHATMOTION: {
|
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
|
|
|
|
return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
|
2020-10-14 02:51:14 -04:00
|
|
|
static_cast<s32>(event.jhat.hat),
|
|
|
|
static_cast<s32>(event.jhat.value));
|
2020-09-17 21:26:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid,
|
|
|
|
const SDL_GameControllerButtonBind& binding) {
|
|
|
|
switch (binding.bindType) {
|
|
|
|
case SDL_CONTROLLER_BINDTYPE_AXIS:
|
2020-08-15 15:26:29 -04:00
|
|
|
return BuildAnalogParamPackageForButton(port, guid, binding.value.axis);
|
2020-07-22 10:39:53 -04:00
|
|
|
case SDL_CONTROLLER_BINDTYPE_BUTTON:
|
2020-08-15 15:26:29 -04:00
|
|
|
return BuildButtonParamPackageForButton(port, guid, binding.value.button);
|
2020-07-22 10:39:53 -04:00
|
|
|
case SDL_CONTROLLER_BINDTYPE_HAT:
|
2020-08-15 15:26:29 -04:00
|
|
|
return BuildHatParamPackageForButton(port, guid, binding.value.hat.hat,
|
|
|
|
binding.value.hat.hat_mask);
|
2020-07-22 10:39:53 -04:00
|
|
|
}
|
2020-08-15 15:26:29 -04:00
|
|
|
return {};
|
|
|
|
}
|
2020-07-22 10:39:53 -04:00
|
|
|
|
|
|
|
Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& guid, int axis_x,
|
|
|
|
int axis_y) {
|
2020-08-15 15:26:29 -04:00
|
|
|
Common::ParamPackage params;
|
2020-07-22 10:39:53 -04:00
|
|
|
params.Set("engine", "sdl");
|
|
|
|
params.Set("port", port);
|
|
|
|
params.Set("guid", guid);
|
|
|
|
params.Set("axis_x", axis_x);
|
|
|
|
params.Set("axis_y", axis_y);
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
ButtonMapping SDLState::GetButtonMappingForDevice(const Common::ParamPackage& params) {
|
|
|
|
if (!params.Has("guid") || !params.Has("port")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
|
2020-08-28 21:13:23 -04:00
|
|
|
auto* controller = joystick->GetSDLGameController();
|
|
|
|
if (controller == nullptr) {
|
2020-07-22 10:39:53 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-08-28 21:21:14 -04:00
|
|
|
// This list is missing ZL/ZR since those are not considered buttons in SDL GameController.
|
|
|
|
// We will add those afterwards
|
|
|
|
// This list also excludes Screenshot since theres not really a mapping for that
|
|
|
|
using ButtonBindings =
|
|
|
|
std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>;
|
|
|
|
static constexpr ButtonBindings switch_to_sdl_button{{
|
|
|
|
{Settings::NativeButton::A, SDL_CONTROLLER_BUTTON_B},
|
|
|
|
{Settings::NativeButton::B, SDL_CONTROLLER_BUTTON_A},
|
|
|
|
{Settings::NativeButton::X, SDL_CONTROLLER_BUTTON_Y},
|
|
|
|
{Settings::NativeButton::Y, SDL_CONTROLLER_BUTTON_X},
|
|
|
|
{Settings::NativeButton::LStick, SDL_CONTROLLER_BUTTON_LEFTSTICK},
|
|
|
|
{Settings::NativeButton::RStick, SDL_CONTROLLER_BUTTON_RIGHTSTICK},
|
|
|
|
{Settings::NativeButton::L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
|
|
|
|
{Settings::NativeButton::R, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
|
|
|
|
{Settings::NativeButton::Plus, SDL_CONTROLLER_BUTTON_START},
|
|
|
|
{Settings::NativeButton::Minus, SDL_CONTROLLER_BUTTON_BACK},
|
|
|
|
{Settings::NativeButton::DLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT},
|
|
|
|
{Settings::NativeButton::DUp, SDL_CONTROLLER_BUTTON_DPAD_UP},
|
|
|
|
{Settings::NativeButton::DRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT},
|
|
|
|
{Settings::NativeButton::DDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN},
|
|
|
|
{Settings::NativeButton::SL, SDL_CONTROLLER_BUTTON_LEFTSHOULDER},
|
|
|
|
{Settings::NativeButton::SR, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER},
|
|
|
|
{Settings::NativeButton::Home, SDL_CONTROLLER_BUTTON_GUIDE},
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Add the missing bindings for ZL/ZR
|
|
|
|
using ZBindings =
|
|
|
|
std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>;
|
|
|
|
static constexpr ZBindings switch_to_sdl_axis{{
|
|
|
|
{Settings::NativeButton::ZL, SDL_CONTROLLER_AXIS_TRIGGERLEFT},
|
|
|
|
{Settings::NativeButton::ZR, SDL_CONTROLLER_AXIS_TRIGGERRIGHT},
|
|
|
|
}};
|
|
|
|
|
|
|
|
ButtonMapping mapping;
|
|
|
|
mapping.reserve(switch_to_sdl_button.size() + switch_to_sdl_axis.size());
|
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
for (const auto& [switch_button, sdl_button] : switch_to_sdl_button) {
|
|
|
|
const auto& binding = SDL_GameControllerGetBindForButton(controller, sdl_button);
|
2020-08-28 21:13:23 -04:00
|
|
|
mapping.insert_or_assign(
|
|
|
|
switch_button,
|
|
|
|
BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
|
2020-07-22 10:39:53 -04:00
|
|
|
}
|
|
|
|
for (const auto& [switch_button, sdl_axis] : switch_to_sdl_axis) {
|
|
|
|
const auto& binding = SDL_GameControllerGetBindForAxis(controller, sdl_axis);
|
2020-08-28 21:13:23 -04:00
|
|
|
mapping.insert_or_assign(
|
|
|
|
switch_button,
|
|
|
|
BuildParamPackageForBinding(joystick->GetPort(), joystick->GetGUID(), binding));
|
2020-07-22 10:39:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& params) {
|
|
|
|
if (!params.Has("guid") || !params.Has("port")) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto joystick = GetSDLJoystickByGUID(params.Get("guid", ""), params.Get("port", 0));
|
2020-08-28 21:13:23 -04:00
|
|
|
auto* controller = joystick->GetSDLGameController();
|
|
|
|
if (controller == nullptr) {
|
2020-07-22 10:39:53 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalogMapping mapping = {};
|
|
|
|
const auto& binding_left_x =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
|
|
|
|
const auto& binding_left_y =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
|
2020-08-28 21:13:23 -04:00
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::LStick,
|
|
|
|
BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
|
|
|
|
binding_left_x.value.axis,
|
|
|
|
binding_left_y.value.axis));
|
2020-07-22 10:39:53 -04:00
|
|
|
const auto& binding_right_x =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX);
|
|
|
|
const auto& binding_right_y =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY);
|
2020-08-28 21:13:23 -04:00
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::RStick,
|
|
|
|
BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
|
|
|
|
binding_right_x.value.axis,
|
|
|
|
binding_right_y.value.axis));
|
2020-07-22 10:39:53 -04:00
|
|
|
return mapping;
|
|
|
|
}
|
2018-09-22 16:11:15 -04:00
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
namespace Polling {
|
2018-09-22 16:11:15 -04:00
|
|
|
class SDLPoller : public InputCommon::Polling::DevicePoller {
|
|
|
|
public:
|
2018-09-20 02:28:05 -04:00
|
|
|
explicit SDLPoller(SDLState& state_) : state(state_) {}
|
|
|
|
|
2020-08-15 15:26:29 -04:00
|
|
|
void Start(const std::string& device_id) override {
|
2018-09-20 02:28:05 -04:00
|
|
|
state.event_queue.Clear();
|
|
|
|
state.polling = true;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() override {
|
2018-09-20 02:28:05 -04:00
|
|
|
state.polling = false;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
2018-09-20 02:28:05 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
SDLState& state;
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class SDLButtonPoller final : public SDLPoller {
|
|
|
|
public:
|
2018-09-20 02:28:05 -04:00
|
|
|
explicit SDLButtonPoller(SDLState& state_) : SDLPoller(state_) {}
|
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
Common::ParamPackage GetNextInput() override {
|
|
|
|
SDL_Event event;
|
2018-09-20 02:28:05 -04:00
|
|
|
while (state.event_queue.Pop(event)) {
|
2020-07-22 10:39:53 -04:00
|
|
|
const auto package = FromEvent(event);
|
|
|
|
if (package) {
|
|
|
|
return *package;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2020-08-28 20:58:29 -04:00
|
|
|
[[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const {
|
2020-07-22 10:39:53 -04:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYAXISMOTION:
|
|
|
|
if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case SDL_JOYBUTTONUP:
|
|
|
|
case SDL_JOYHATMOTION:
|
|
|
|
return {SDLEventToButtonParamPackage(state, event)};
|
|
|
|
}
|
2020-08-28 20:58:29 -04:00
|
|
|
return std::nullopt;
|
2020-07-22 10:39:53 -04:00
|
|
|
}
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
|
|
|
|
2020-09-17 21:26:34 -04:00
|
|
|
class SDLMotionPoller final : public SDLPoller {
|
|
|
|
public:
|
|
|
|
explicit SDLMotionPoller(SDLState& state_) : SDLPoller(state_) {}
|
|
|
|
|
|
|
|
Common::ParamPackage GetNextInput() override {
|
|
|
|
SDL_Event event;
|
|
|
|
while (state.event_queue.Pop(event)) {
|
|
|
|
const auto package = FromEvent(event);
|
|
|
|
if (package) {
|
|
|
|
return *package;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
[[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_JOYAXISMOTION:
|
|
|
|
if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
case SDL_JOYBUTTONUP:
|
|
|
|
case SDL_JOYHATMOTION:
|
|
|
|
return {SDLEventToMotionParamPackage(state, event)};
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-22 10:39:53 -04:00
|
|
|
/**
|
|
|
|
* Attempts to match the press to a controller joy axis (left/right stick) and if a match
|
|
|
|
* isn't found, checks if the event matches anything from SDLButtonPoller and uses that
|
|
|
|
* instead
|
|
|
|
*/
|
|
|
|
class SDLAnalogPreferredPoller final : public SDLPoller {
|
2018-09-22 16:11:15 -04:00
|
|
|
public:
|
2020-07-22 10:39:53 -04:00
|
|
|
explicit SDLAnalogPreferredPoller(SDLState& state_)
|
|
|
|
: SDLPoller(state_), button_poller(state_) {}
|
2018-09-22 16:11:15 -04:00
|
|
|
|
2020-08-15 15:26:29 -04:00
|
|
|
void Start(const std::string& device_id) override {
|
2020-07-22 10:39:53 -04:00
|
|
|
SDLPoller::Start(device_id);
|
|
|
|
// Load the game controller
|
2018-09-22 16:11:15 -04:00
|
|
|
// Reset stored axes
|
2019-06-03 16:32:57 -04:00
|
|
|
analog_x_axis = -1;
|
|
|
|
analog_y_axis = -1;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::ParamPackage GetNextInput() override {
|
|
|
|
SDL_Event event;
|
2018-09-20 02:28:05 -04:00
|
|
|
while (state.event_queue.Pop(event)) {
|
2020-07-22 10:39:53 -04:00
|
|
|
// Filter out axis events that are below a threshold
|
|
|
|
if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) {
|
2018-09-22 16:11:15 -04:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-22 10:39:53 -04:00
|
|
|
// Simplify controller config by testing if game controller support is enabled.
|
|
|
|
if (event.type == SDL_JOYAXISMOTION) {
|
|
|
|
const auto axis = event.jaxis.axis;
|
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
|
2020-10-14 02:51:14 -04:00
|
|
|
auto* const controller = joystick->GetSDLGameController();
|
2020-07-22 10:39:53 -04:00
|
|
|
if (controller) {
|
|
|
|
const auto axis_left_x =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX)
|
|
|
|
.value.axis;
|
|
|
|
const auto axis_left_y =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY)
|
|
|
|
.value.axis;
|
|
|
|
const auto axis_right_x =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX)
|
|
|
|
.value.axis;
|
|
|
|
const auto axis_right_y =
|
|
|
|
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY)
|
|
|
|
.value.axis;
|
|
|
|
|
|
|
|
if (axis == axis_left_x || axis == axis_left_y) {
|
|
|
|
analog_x_axis = axis_left_x;
|
|
|
|
analog_y_axis = axis_left_y;
|
|
|
|
break;
|
|
|
|
} else if (axis == axis_right_x || axis == axis_right_y) {
|
|
|
|
analog_x_axis = axis_right_x;
|
|
|
|
analog_y_axis = axis_right_y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the press wasn't accepted as a joy axis, check for a button press
|
|
|
|
auto button_press = button_poller.FromEvent(event);
|
|
|
|
if (button_press) {
|
|
|
|
return *button_press;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 10:39:53 -04:00
|
|
|
|
2019-06-03 16:32:57 -04:00
|
|
|
if (analog_x_axis != -1 && analog_y_axis != -1) {
|
2019-06-03 16:30:33 -04:00
|
|
|
const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
|
2020-07-22 10:39:53 -04:00
|
|
|
auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
|
|
|
|
analog_x_axis, analog_y_axis);
|
2019-06-03 16:32:57 -04:00
|
|
|
analog_x_axis = -1;
|
|
|
|
analog_y_axis = -1;
|
2018-09-22 16:11:15 -04:00
|
|
|
return params;
|
|
|
|
}
|
2020-07-22 10:39:53 -04:00
|
|
|
return {};
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-03 16:32:57 -04:00
|
|
|
int analog_x_axis = -1;
|
|
|
|
int analog_y_axis = -1;
|
2020-07-22 10:39:53 -04:00
|
|
|
SDLButtonPoller button_poller;
|
2018-09-22 16:11:15 -04:00
|
|
|
};
|
2018-09-20 02:28:05 -04:00
|
|
|
} // namespace Polling
|
2018-09-22 16:11:15 -04:00
|
|
|
|
2019-03-18 11:26:13 -04:00
|
|
|
SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) {
|
|
|
|
Pollers pollers;
|
2019-03-18 11:32:49 -04:00
|
|
|
|
2018-09-22 16:11:15 -04:00
|
|
|
switch (type) {
|
2020-07-22 10:39:53 -04:00
|
|
|
case InputCommon::Polling::DeviceType::AnalogPreferred:
|
|
|
|
pollers.emplace_back(std::make_unique<Polling::SDLAnalogPreferredPoller>(*this));
|
2018-09-22 16:11:15 -04:00
|
|
|
break;
|
|
|
|
case InputCommon::Polling::DeviceType::Button:
|
2018-09-20 02:28:05 -04:00
|
|
|
pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this));
|
2018-09-22 16:11:15 -04:00
|
|
|
break;
|
2020-09-17 21:26:34 -04:00
|
|
|
case InputCommon::Polling::DeviceType::Motion:
|
|
|
|
pollers.emplace_back(std::make_unique<Polling::SDLMotionPoller>(*this));
|
|
|
|
break;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
2019-03-18 11:32:49 -04:00
|
|
|
|
|
|
|
return pollers;
|
2018-09-22 16:11:15 -04:00
|
|
|
}
|
2018-09-20 02:28:05 -04:00
|
|
|
|
2019-06-03 15:49:04 -04:00
|
|
|
} // namespace InputCommon::SDL
|