2022-02-19 14:34:12 +00:00
|
|
|
#include "input.hpp"
|
2022-03-20 07:45:45 +00:00
|
|
|
#include "aurora/pad.hpp"
|
2022-05-08 04:20:52 +00:00
|
|
|
#include "Runtime/ConsoleVariables/FileStoreManager.hpp"
|
|
|
|
|
|
|
|
#include "magic_enum.hpp"
|
2022-03-20 07:45:45 +00:00
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
#include <SDL_haptic.h>
|
2022-02-19 14:34:12 +00:00
|
|
|
|
2022-02-23 07:59:52 +00:00
|
|
|
#include <absl/container/btree_map.h>
|
2022-03-15 06:18:45 +00:00
|
|
|
#include <absl/container/flat_hash_map.h>
|
2022-02-23 07:59:52 +00:00
|
|
|
#include <absl/strings/str_split.h>
|
2022-03-21 00:30:47 +00:00
|
|
|
#include <cmath>
|
2022-02-23 07:59:52 +00:00
|
|
|
|
2022-02-19 14:34:12 +00:00
|
|
|
namespace aurora::input {
|
2022-02-23 07:59:52 +00:00
|
|
|
static logvisor::Module Log("aurora::input");
|
|
|
|
|
2022-02-19 14:34:12 +00:00
|
|
|
struct GameController {
|
2022-02-22 09:12:15 +00:00
|
|
|
SDL_GameController* m_controller = nullptr;
|
2022-02-19 14:34:12 +00:00
|
|
|
bool m_isGameCube = false;
|
2022-02-22 09:12:15 +00:00
|
|
|
Sint32 m_index = -1;
|
|
|
|
bool m_hasRumble = false;
|
2022-05-08 04:20:52 +00:00
|
|
|
PADDeadZones m_deadZones;
|
|
|
|
u16 m_vid = 0;
|
|
|
|
u16 m_pid = 0;
|
2022-05-08 08:50:21 +00:00
|
|
|
std::array<PADButtonMapping, 12> m_mapping{};
|
2022-05-08 04:20:52 +00:00
|
|
|
bool m_mappingLoaded = false;
|
|
|
|
constexpr bool operator==(const GameController& other) const {
|
|
|
|
return m_controller == other.m_controller && m_index == other.m_index;
|
|
|
|
}
|
2022-02-19 14:34:12 +00:00
|
|
|
};
|
2022-03-15 06:18:45 +00:00
|
|
|
absl::flat_hash_map<Uint32, GameController> g_GameControllers;
|
2022-02-19 14:34:12 +00:00
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
GameController* get_controller_for_player(u32 player) noexcept {
|
|
|
|
for (auto& [which, controller] : g_GameControllers) {
|
2022-03-20 07:45:45 +00:00
|
|
|
if (player_index(which) == player) {
|
2022-05-08 04:20:52 +00:00
|
|
|
return &controller;
|
2022-03-20 07:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 08:50:21 +00:00
|
|
|
#if 0
|
2022-05-08 04:20:52 +00:00
|
|
|
/* If we don't have a controller assigned to this port use the first unassigned controller */
|
|
|
|
if (!g_GameControllers.empty()) {
|
|
|
|
int32_t availIndex = -1;
|
2022-05-08 08:50:21 +00:00
|
|
|
GameController* ct = nullptr;
|
|
|
|
for (auto& controller : g_GameControllers) {
|
|
|
|
if (player_index(controller.first) == -1) {
|
|
|
|
availIndex = controller.first;
|
|
|
|
ct = &controller.second;
|
2022-05-08 04:20:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (availIndex != -1) {
|
2022-05-08 08:50:21 +00:00
|
|
|
set_player_index(availIndex, player);
|
|
|
|
return ct;
|
2022-05-08 04:20:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-08 08:50:21 +00:00
|
|
|
#endif
|
2022-05-08 04:20:52 +00:00
|
|
|
return nullptr;
|
2022-03-20 07:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 19:44:35 +00:00
|
|
|
Sint32 get_instance_for_player(u32 player) noexcept {
|
2022-03-20 07:45:45 +00:00
|
|
|
for (const auto& [which, controller] : g_GameControllers) {
|
|
|
|
if (player_index(which) == player) {
|
|
|
|
return which;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-23 08:10:16 +00:00
|
|
|
static std::optional<std::string> remap_controller_layout(std::string_view mapping) {
|
2022-02-23 07:59:52 +00:00
|
|
|
std::string newMapping;
|
|
|
|
newMapping.reserve(mapping.size());
|
2022-02-23 08:10:16 +00:00
|
|
|
absl::btree_map<std::string_view, std::string_view> entries;
|
2022-02-23 07:59:52 +00:00
|
|
|
for (size_t idx = 0; const auto value : absl::StrSplit(mapping, ',')) {
|
|
|
|
if (idx < 2) {
|
|
|
|
if (idx > 0) {
|
|
|
|
newMapping.push_back(',');
|
|
|
|
}
|
|
|
|
newMapping.append(value);
|
|
|
|
} else {
|
|
|
|
const auto split = absl::StrSplit(value, absl::MaxSplits(':', 2));
|
|
|
|
auto iter = split.begin();
|
|
|
|
entries.emplace(*iter++, *iter);
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
if (entries.contains("rightshoulder"sv) && !entries.contains("leftshoulder"sv)) {
|
|
|
|
Log.report(logvisor::Info, FMT_STRING("Remapping GameCube controller layout"));
|
2022-02-23 08:10:16 +00:00
|
|
|
entries.insert_or_assign("back"sv, entries["rightshoulder"sv]);
|
2022-02-23 07:59:52 +00:00
|
|
|
// TODO trigger buttons may differ per platform
|
|
|
|
entries.insert_or_assign("leftshoulder"sv, "b11"sv);
|
|
|
|
entries.insert_or_assign("rightshoulder"sv, "b10"sv);
|
|
|
|
} else if (entries.contains("leftshoulder"sv) && entries.contains("rightshoulder"sv) && entries.contains("back"sv)) {
|
|
|
|
Log.report(logvisor::Info, FMT_STRING("Controller has standard layout"));
|
|
|
|
auto a = entries["a"sv];
|
|
|
|
entries.insert_or_assign("a"sv, entries["b"sv]);
|
|
|
|
entries.insert_or_assign("b"sv, a);
|
|
|
|
auto x = entries["x"sv];
|
|
|
|
entries.insert_or_assign("x"sv, entries["y"sv]);
|
|
|
|
entries.insert_or_assign("y"sv, x);
|
|
|
|
} else {
|
|
|
|
Log.report(logvisor::Error, FMT_STRING("Controller has unsupported layout: {}"), mapping);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
for (const auto [k, v] : entries) {
|
|
|
|
newMapping.push_back(',');
|
|
|
|
newMapping.append(k);
|
|
|
|
newMapping.push_back(':');
|
|
|
|
newMapping.append(v);
|
|
|
|
}
|
|
|
|
return newMapping;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
Sint32 add_controller(Sint32 which) noexcept {
|
2022-02-19 14:34:12 +00:00
|
|
|
auto* ctrl = SDL_GameControllerOpen(which);
|
|
|
|
if (ctrl != nullptr) {
|
2022-02-23 07:59:52 +00:00
|
|
|
{
|
2022-02-23 08:19:19 +00:00
|
|
|
char* mapping = SDL_GameControllerMapping(ctrl);
|
2022-02-23 07:59:52 +00:00
|
|
|
if (mapping != nullptr) {
|
|
|
|
auto newMapping = remap_controller_layout(mapping);
|
2022-02-23 08:19:19 +00:00
|
|
|
SDL_free(mapping);
|
2022-02-23 07:59:52 +00:00
|
|
|
if (newMapping) {
|
|
|
|
if (SDL_GameControllerAddMapping(newMapping->c_str()) == -1) {
|
|
|
|
Log.report(logvisor::Error, FMT_STRING("Failed to update controller mapping: {}"), SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.report(logvisor::Error, FMT_STRING("Failed to retrieve mapping for controller"));
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 14:34:12 +00:00
|
|
|
GameController controller;
|
|
|
|
controller.m_controller = ctrl;
|
|
|
|
controller.m_index = which;
|
2022-05-08 04:20:52 +00:00
|
|
|
controller.m_vid = SDL_GameControllerGetVendor(ctrl);
|
|
|
|
controller.m_pid = SDL_GameControllerGetProduct(ctrl);
|
|
|
|
controller.m_isGameCube = controller.m_vid == 0x057E && controller.m_pid == 0x0337;
|
2022-02-22 09:12:15 +00:00
|
|
|
controller.m_hasRumble = (SDL_GameControllerHasRumble(ctrl) != 0u);
|
2022-02-19 14:34:12 +00:00
|
|
|
Sint32 instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ctrl));
|
|
|
|
g_GameControllers[instance] = controller;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
void remove_controller(Uint32 instance) noexcept {
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
SDL_GameControllerClose(g_GameControllers[instance].m_controller);
|
|
|
|
g_GameControllers.erase(instance);
|
2022-02-19 14:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
bool is_gamecube(Uint32 instance) noexcept {
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
return g_GameControllers[instance].m_isGameCube;
|
2022-02-19 14:34:12 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
int32_t player_index(Uint32 instance) noexcept {
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
return SDL_GameControllerGetPlayerIndex(g_GameControllers[instance].m_controller);
|
2022-02-19 14:34:12 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
void set_player_index(Uint32 instance, Sint32 index) noexcept {
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
SDL_GameControllerSetPlayerIndex(g_GameControllers[instance].m_controller, index);
|
2022-02-19 14:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
std::string controller_name(Uint32 instance) noexcept {
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
const auto* name = SDL_GameControllerName(g_GameControllers[instance].m_controller);
|
|
|
|
if (name != nullptr) {
|
|
|
|
return {name};
|
2022-02-19 14:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
bool controller_has_rumble(Uint32 instance) noexcept {
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
return g_GameControllers[instance].m_hasRumble;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_rumble(uint32_t instance, uint16_t low_freq_intensity, uint16_t high_freq_intensity,
|
|
|
|
uint16_t duration_ms) noexcept {
|
|
|
|
|
|
|
|
if (g_GameControllers.find(instance) != g_GameControllers.end()) {
|
|
|
|
SDL_GameControllerRumble(g_GameControllers[instance].m_controller, low_freq_intensity, high_freq_intensity,
|
|
|
|
duration_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ControllerButton translate_controller_button(SDL_GameControllerButton btn) noexcept {
|
2022-02-19 14:34:12 +00:00
|
|
|
switch (btn) {
|
|
|
|
case SDL_CONTROLLER_BUTTON_A:
|
|
|
|
return ControllerButton::A;
|
|
|
|
case SDL_CONTROLLER_BUTTON_B:
|
|
|
|
return ControllerButton::B;
|
|
|
|
case SDL_CONTROLLER_BUTTON_X:
|
|
|
|
return ControllerButton::X;
|
|
|
|
case SDL_CONTROLLER_BUTTON_Y:
|
|
|
|
return ControllerButton::Y;
|
|
|
|
case SDL_CONTROLLER_BUTTON_BACK:
|
|
|
|
return ControllerButton::Back;
|
|
|
|
case SDL_CONTROLLER_BUTTON_GUIDE:
|
|
|
|
return ControllerButton::Guide;
|
2022-02-20 00:27:11 +00:00
|
|
|
case SDL_CONTROLLER_BUTTON_START:
|
|
|
|
return ControllerButton::Start;
|
2022-02-19 14:34:12 +00:00
|
|
|
case SDL_CONTROLLER_BUTTON_LEFTSTICK:
|
|
|
|
return ControllerButton::LeftStick;
|
|
|
|
case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
|
|
|
|
return ControllerButton::RightStick;
|
|
|
|
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
|
|
|
|
return ControllerButton::LeftShoulder;
|
2022-02-20 00:27:11 +00:00
|
|
|
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
|
|
|
|
return ControllerButton::RightShoulder;
|
2022-02-19 14:34:12 +00:00
|
|
|
case SDL_CONTROLLER_BUTTON_DPAD_UP:
|
|
|
|
return ControllerButton::DPadUp;
|
|
|
|
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
|
|
|
|
return ControllerButton::DPadDown;
|
|
|
|
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
|
|
|
|
return ControllerButton::DPadRight;
|
2022-02-20 00:27:11 +00:00
|
|
|
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
|
|
|
|
return ControllerButton::DPadLeft;
|
2022-02-19 14:34:12 +00:00
|
|
|
default:
|
|
|
|
return ControllerButton::Other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
ControllerAxis translate_controller_axis(SDL_GameControllerAxis axis) noexcept {
|
2022-02-19 14:34:12 +00:00
|
|
|
switch (axis) {
|
|
|
|
case SDL_CONTROLLER_AXIS_LEFTX:
|
|
|
|
return ControllerAxis::LeftX;
|
|
|
|
case SDL_CONTROLLER_AXIS_LEFTY:
|
|
|
|
return ControllerAxis::LeftY;
|
|
|
|
case SDL_CONTROLLER_AXIS_RIGHTX:
|
|
|
|
return ControllerAxis::RightX;
|
|
|
|
case SDL_CONTROLLER_AXIS_RIGHTY:
|
|
|
|
return ControllerAxis::RightY;
|
|
|
|
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
|
|
|
|
return ControllerAxis::TriggerLeft;
|
|
|
|
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
|
|
|
|
return ControllerAxis::TriggerRight;
|
|
|
|
default:
|
|
|
|
return ControllerAxis::MAX;
|
|
|
|
}
|
|
|
|
}
|
2022-02-20 03:22:03 +00:00
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
char translate_key(SDL_Keysym sym, SpecialKey& specialSym, ModifierKey& modifierSym) noexcept {
|
2022-02-20 03:22:03 +00:00
|
|
|
specialSym = SpecialKey::None;
|
|
|
|
modifierSym = ModifierKey::None;
|
|
|
|
if (sym.sym >= SDLK_F1 && sym.sym <= SDLK_F12) {
|
|
|
|
specialSym = SpecialKey(int(SpecialKey::F1) + sym.sym - SDLK_F1);
|
|
|
|
} else if (sym.sym == SDLK_ESCAPE) {
|
|
|
|
specialSym = SpecialKey::Esc;
|
|
|
|
} else if (sym.sym == SDLK_RETURN) {
|
|
|
|
specialSym = SpecialKey::Enter;
|
|
|
|
} else if (sym.sym == SDLK_KP_ENTER) {
|
|
|
|
specialSym = SpecialKey::KpEnter;
|
|
|
|
} else if (sym.sym == SDLK_BACKSPACE) {
|
|
|
|
specialSym = SpecialKey::Backspace;
|
|
|
|
} else if (sym.sym == SDLK_INSERT) {
|
|
|
|
specialSym = SpecialKey::Insert;
|
|
|
|
} else if (sym.sym == SDLK_DELETE) {
|
|
|
|
specialSym = SpecialKey::Delete;
|
|
|
|
} else if (sym.sym == SDLK_HOME) {
|
|
|
|
specialSym = SpecialKey::Home;
|
|
|
|
} else if (sym.sym == SDLK_END) {
|
|
|
|
specialSym = SpecialKey::End;
|
|
|
|
} else if (sym.sym == SDLK_PAGEUP) {
|
|
|
|
specialSym = SpecialKey::PgUp;
|
|
|
|
} else if (sym.sym == SDLK_PAGEDOWN) {
|
|
|
|
specialSym = SpecialKey::PgDown;
|
|
|
|
} else if (sym.sym == SDLK_LEFT) {
|
|
|
|
specialSym = SpecialKey::Left;
|
|
|
|
} else if (sym.sym == SDLK_RIGHT) {
|
|
|
|
specialSym = SpecialKey::Right;
|
|
|
|
} else if (sym.sym == SDLK_UP) {
|
|
|
|
specialSym = SpecialKey::Up;
|
|
|
|
} else if (sym.sym == SDLK_DOWN) {
|
|
|
|
specialSym = SpecialKey::Down;
|
|
|
|
} else if (sym.sym == SDLK_TAB) {
|
|
|
|
specialSym = SpecialKey::Tab;
|
|
|
|
} else if (sym.sym == SDLK_LSHIFT) {
|
|
|
|
modifierSym = ModifierKey::LeftShift;
|
|
|
|
} else if (sym.sym == SDLK_RSHIFT) {
|
|
|
|
modifierSym = ModifierKey::RightShift;
|
|
|
|
} else if (sym.sym == SDLK_LCTRL) {
|
|
|
|
modifierSym = ModifierKey::LeftControl;
|
|
|
|
} else if (sym.sym == SDLK_RCTRL) {
|
|
|
|
modifierSym = ModifierKey::RightControl;
|
|
|
|
} else if (sym.sym == SDLK_LALT) {
|
|
|
|
modifierSym = ModifierKey::LeftAlt;
|
|
|
|
} else if (sym.sym == SDLK_RALT) {
|
|
|
|
modifierSym = ModifierKey::RightAlt;
|
|
|
|
} else if (sym.sym >= ' ' && sym.sym <= 'z') {
|
|
|
|
return static_cast<char>(sym.sym);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
ModifierKey translate_modifiers(Uint16 mods) noexcept {
|
2022-02-20 03:22:03 +00:00
|
|
|
ModifierKey ret = ModifierKey::None;
|
|
|
|
if ((mods & SDLK_LSHIFT) != 0) {
|
|
|
|
ret |= ModifierKey::LeftShift;
|
|
|
|
}
|
|
|
|
if ((mods & SDLK_RSHIFT) != 0) {
|
|
|
|
ret |= ModifierKey::RightShift;
|
|
|
|
}
|
|
|
|
if ((mods & SDLK_LCTRL) != 0) {
|
|
|
|
ret |= ModifierKey::LeftControl;
|
|
|
|
}
|
|
|
|
if ((mods & SDLK_RCTRL) != 0) {
|
|
|
|
ret |= ModifierKey::RightControl;
|
|
|
|
}
|
|
|
|
if ((mods & SDLK_LALT) != 0) {
|
|
|
|
ret |= ModifierKey::LeftAlt;
|
|
|
|
}
|
|
|
|
if ((mods & SDLK_RALT) != 0) {
|
|
|
|
ret |= ModifierKey::RightAlt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
MouseButton translate_mouse_button(Uint8 button) noexcept {
|
2022-02-20 03:22:03 +00:00
|
|
|
if (button == 1) {
|
|
|
|
return MouseButton::Primary;
|
|
|
|
}
|
|
|
|
if (button == 2) {
|
|
|
|
return MouseButton::Middle;
|
|
|
|
}
|
|
|
|
if (button == 3) {
|
|
|
|
return MouseButton::Secondary;
|
|
|
|
}
|
|
|
|
if (button == 4) {
|
|
|
|
return MouseButton::Aux1;
|
|
|
|
}
|
|
|
|
if (button == 5) {
|
|
|
|
return MouseButton::Aux2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MouseButton::None;
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:12:15 +00:00
|
|
|
MouseButton translate_mouse_button_state(Uint8 state) noexcept {
|
2022-02-20 03:22:03 +00:00
|
|
|
auto ret = MouseButton::None;
|
|
|
|
if ((state & 0x01) != 0) {
|
|
|
|
ret |= MouseButton::Primary;
|
|
|
|
}
|
|
|
|
if ((state & 0x02) != 0) {
|
|
|
|
ret |= MouseButton::Middle;
|
|
|
|
}
|
|
|
|
if ((state & 0x04) != 0) {
|
|
|
|
ret |= MouseButton::Secondary;
|
|
|
|
}
|
|
|
|
if ((state & 0x08) != 0) {
|
|
|
|
ret |= MouseButton::Aux1;
|
|
|
|
}
|
|
|
|
if ((state & 0x10) != 0) {
|
|
|
|
ret |= MouseButton::Aux2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
uint32_t controller_count() noexcept { return g_GameControllers.size(); }
|
2022-03-20 07:45:45 +00:00
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
} // namespace aurora::input
|
2022-03-20 07:45:45 +00:00
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
static const std::array<PADButtonMapping, 12> mDefaultButtons{{
|
2022-03-20 07:45:45 +00:00
|
|
|
{SDL_CONTROLLER_BUTTON_A, PAD::BUTTON_A},
|
|
|
|
{SDL_CONTROLLER_BUTTON_B, PAD::BUTTON_B},
|
|
|
|
{SDL_CONTROLLER_BUTTON_X, PAD::BUTTON_X},
|
|
|
|
{SDL_CONTROLLER_BUTTON_Y, PAD::BUTTON_Y},
|
|
|
|
{SDL_CONTROLLER_BUTTON_START, PAD::BUTTON_START},
|
|
|
|
{SDL_CONTROLLER_BUTTON_BACK, PAD::TRIGGER_Z},
|
|
|
|
{SDL_CONTROLLER_BUTTON_LEFTSHOULDER, PAD::TRIGGER_L},
|
|
|
|
{SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, PAD::TRIGGER_R},
|
|
|
|
{SDL_CONTROLLER_BUTTON_DPAD_UP, PAD::BUTTON_UP},
|
|
|
|
{SDL_CONTROLLER_BUTTON_DPAD_DOWN, PAD::BUTTON_DOWN},
|
|
|
|
{SDL_CONTROLLER_BUTTON_DPAD_LEFT, PAD::BUTTON_LEFT},
|
|
|
|
{SDL_CONTROLLER_BUTTON_DPAD_RIGHT, PAD::BUTTON_RIGHT},
|
|
|
|
}};
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
void PADSetSpec(s32 spec) {}
|
|
|
|
void PADInit() {}
|
|
|
|
|
2022-05-08 08:50:21 +00:00
|
|
|
aurora::input::GameController* __PADGetControllerForIndex(u32 idx) {
|
|
|
|
if (idx >= aurora::input::g_GameControllers.size()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 tmp = 0;
|
|
|
|
auto iter = aurora::input::g_GameControllers.begin();
|
|
|
|
while (tmp < idx) {
|
|
|
|
++iter;
|
|
|
|
++tmp;
|
|
|
|
}
|
|
|
|
if (iter == aurora::input::g_GameControllers.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 PADCount() { return aurora::input::g_GameControllers.size(); }
|
|
|
|
|
|
|
|
const char* PADGetNameForControllerIndex(u32 idx) {
|
|
|
|
auto* ctrl = __PADGetControllerForIndex(idx);
|
|
|
|
if (ctrl == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_GameControllerName(ctrl->m_controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADSetPortForIndex(u32 idx, s32 port) {
|
|
|
|
auto* ctrl = __PADGetControllerForIndex(idx);
|
|
|
|
auto* dest = aurora::input::get_controller_for_player(port);
|
|
|
|
if (ctrl == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dest != nullptr) {
|
|
|
|
SDL_GameControllerSetPlayerIndex(dest->m_controller, -1);
|
|
|
|
}
|
|
|
|
SDL_GameControllerSetPlayerIndex(ctrl->m_controller, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 PADGetIndexForPort(u32 port) {
|
|
|
|
auto* ctrl = aurora::input::get_controller_for_player(port);
|
|
|
|
if (ctrl == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s32 index = 0;
|
|
|
|
for (auto iter = aurora::input::g_GameControllers.begin(); iter != aurora::input::g_GameControllers.end();
|
|
|
|
++iter, ++index) {
|
|
|
|
if (&iter->second == ctrl) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADClearPort(u32 port) {
|
|
|
|
auto* ctrl = aurora::input::get_controller_for_player(port);
|
|
|
|
if (ctrl == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SDL_GameControllerSetPlayerIndex(ctrl->m_controller, -1);
|
|
|
|
}
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
void __PADLoadMapping(aurora::input::GameController* controller) {
|
|
|
|
s32 playerIndex = SDL_GameControllerGetPlayerIndex(controller->m_controller);
|
|
|
|
if (playerIndex == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string basePath = std::string(metaforce::FileStoreManager::instance()->getStoreRoot());
|
|
|
|
if (!controller->m_mappingLoaded) {
|
|
|
|
controller->m_mapping = mDefaultButtons;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->m_mappingLoaded = true;
|
|
|
|
|
2022-05-08 08:50:21 +00:00
|
|
|
auto path = fmt::format(FMT_STRING("{}/{}_{:04X}_{:04X}.controller"), basePath, PADGetName(playerIndex),
|
|
|
|
controller->m_vid, controller->m_pid);
|
2022-05-13 19:48:20 +00:00
|
|
|
FILE* file = fopen(path.c_str(), "rb");
|
2022-05-08 04:20:52 +00:00
|
|
|
if (file == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 magic = 0;
|
|
|
|
fread(&magic, 1, sizeof(u32), file);
|
|
|
|
if (magic != SBIG('CTRL')) {
|
|
|
|
fmt::print(FMT_STRING("Invalid controller mapping magic!\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 version = 0;
|
|
|
|
fread(&version, 1, sizeof(u32), file);
|
|
|
|
if (version != 1) {
|
|
|
|
fmt::print(FMT_STRING("Invalid controller mapping version!\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isGameCube = false;
|
|
|
|
fread(&isGameCube, 1, 1, file);
|
|
|
|
fseek(file, (ftell(file) + 31) & ~31, SEEK_SET);
|
|
|
|
u32 dataStart = ftell(file);
|
|
|
|
if (isGameCube) {
|
|
|
|
fseek(file, dataStart + ((sizeof(PADDeadZones) + sizeof(PADButtonMapping)) * playerIndex), SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
fread(&controller->m_deadZones, 1, sizeof(PADDeadZones), file);
|
2022-05-08 08:50:21 +00:00
|
|
|
fread(&controller->m_mapping, 1, sizeof(PADButtonMapping) * controller->m_mapping.size(), file);
|
2022-05-08 04:20:52 +00:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool gBlockPAD = false;
|
2022-03-20 07:45:45 +00:00
|
|
|
u32 PADRead(PAD::Status* status) {
|
2022-05-08 04:20:52 +00:00
|
|
|
if (gBlockPAD) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:45:45 +00:00
|
|
|
u32 rumbleSupport = 0;
|
|
|
|
for (u32 i = 0; i < 4; ++i) {
|
|
|
|
memset(&status[i], 0, sizeof(PAD::Status));
|
|
|
|
auto controller = aurora::input::get_controller_for_player(i);
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller == nullptr) {
|
2022-03-20 07:45:45 +00:00
|
|
|
status[i].xa_err = PAD::ERR_NO_CONTROLLER;
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-08 04:20:52 +00:00
|
|
|
|
|
|
|
if (!controller->m_mappingLoaded) {
|
|
|
|
__PADLoadMapping(controller);
|
|
|
|
}
|
2022-03-20 07:45:45 +00:00
|
|
|
status[i].xa_err = PAD::ERR_NONE;
|
2022-05-08 04:20:52 +00:00
|
|
|
std::for_each(controller->m_mapping.begin(), controller->m_mapping.end(),
|
|
|
|
[&controller, &i, &status](const auto& mapping) {
|
|
|
|
if (SDL_GameControllerGetButton(controller->m_controller,
|
|
|
|
static_cast<SDL_GameControllerButton>(mapping.nativeButton))) {
|
|
|
|
status[i].x0_buttons |= mapping.padButton;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Sint16 x = SDL_GameControllerGetAxis(controller->m_controller, SDL_CONTROLLER_AXIS_LEFTX);
|
|
|
|
Sint16 y = SDL_GameControllerGetAxis(controller->m_controller, SDL_CONTROLLER_AXIS_LEFTY);
|
|
|
|
if (controller->m_deadZones.useDeadzones) {
|
|
|
|
if (std::abs(x) > controller->m_deadZones.stickDeadZone) {
|
|
|
|
x /= 256;
|
|
|
|
} else {
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
if (std::abs(y) > controller->m_deadZones.stickDeadZone) {
|
|
|
|
y = (-(y + 1u)) / 256u;
|
|
|
|
} else {
|
|
|
|
y = 0;
|
2022-03-20 07:45:45 +00:00
|
|
|
}
|
2022-03-22 15:01:33 +00:00
|
|
|
} else {
|
2022-05-08 04:20:52 +00:00
|
|
|
x /= 256;
|
2022-03-22 15:01:33 +00:00
|
|
|
y = (-(y + 1u)) / 256u;
|
|
|
|
}
|
2022-03-20 07:45:45 +00:00
|
|
|
|
|
|
|
status[i].x2_stickX = static_cast<s8>(x);
|
|
|
|
status[i].x3_stickY = static_cast<s8>(y);
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
x = SDL_GameControllerGetAxis(controller->m_controller, SDL_CONTROLLER_AXIS_RIGHTX);
|
|
|
|
y = SDL_GameControllerGetAxis(controller->m_controller, SDL_CONTROLLER_AXIS_RIGHTY);
|
|
|
|
if (controller->m_deadZones.useDeadzones) {
|
|
|
|
if (std::abs(x) > controller->m_deadZones.substickDeadZone) {
|
|
|
|
x /= 256;
|
|
|
|
} else {
|
|
|
|
x = 0;
|
|
|
|
}
|
2022-03-22 15:01:33 +00:00
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
if (std::abs(y) > controller->m_deadZones.substickDeadZone) {
|
|
|
|
y = (-(y + 1u)) / 256u;
|
|
|
|
} else {
|
|
|
|
y = 0;
|
|
|
|
}
|
2022-03-22 15:01:33 +00:00
|
|
|
} else {
|
2022-05-08 04:20:52 +00:00
|
|
|
x /= 256;
|
|
|
|
y = (-(y + 1u)) / 256u;
|
2022-03-22 15:01:33 +00:00
|
|
|
}
|
2022-03-20 07:45:45 +00:00
|
|
|
|
|
|
|
status[i].x4_substickX = static_cast<s8>(x);
|
|
|
|
status[i].x5_substickY = static_cast<s8>(y);
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
x = SDL_GameControllerGetAxis(controller->m_controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
|
|
|
|
y = SDL_GameControllerGetAxis(controller->m_controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
|
|
|
|
if (/*!controller->m_isGameCube && */ controller->m_deadZones.emulateTriggers) {
|
|
|
|
if (x > controller->m_deadZones.leftTriggerActivationZone) {
|
2022-03-24 08:43:18 +00:00
|
|
|
status[i].x0_buttons |= PAD::TRIGGER_L;
|
|
|
|
}
|
2022-05-08 04:20:52 +00:00
|
|
|
if (y > controller->m_deadZones.rightTriggerActivationZone) {
|
2022-03-24 08:43:18 +00:00
|
|
|
status[i].x0_buttons |= PAD::TRIGGER_R;
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 07:45:45 +00:00
|
|
|
x /= 128;
|
2022-03-20 07:48:09 +00:00
|
|
|
y /= 128;
|
2022-03-20 07:45:45 +00:00
|
|
|
|
|
|
|
status[i].x6_triggerL = static_cast<s8>(x);
|
|
|
|
status[i].x7_triggerR = static_cast<s8>(y);
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller->m_hasRumble) {
|
2022-03-20 07:50:39 +00:00
|
|
|
rumbleSupport |= PAD::CHAN0_BIT >> i;
|
2022-03-20 07:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rumbleSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADControlAllMotors(const u32* commands) {
|
|
|
|
for (u32 i = 0; i < 4; ++i) {
|
|
|
|
auto controller = aurora::input::get_controller_for_player(i);
|
|
|
|
auto instance = aurora::input::get_instance_for_player(i);
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller == nullptr) {
|
2022-03-20 07:45:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller->m_isGameCube) {
|
2022-03-20 07:45:45 +00:00
|
|
|
if (commands[i] == PAD::MOTOR_STOP) {
|
|
|
|
aurora::input::controller_rumble(instance, 0, 1, 0);
|
|
|
|
} else if (commands[i] == PAD::MOTOR_RUMBLE) {
|
|
|
|
aurora::input::controller_rumble(instance, 1, 1, 0);
|
|
|
|
} else if (commands[i] == PAD::MOTOR_STOP_HARD) {
|
|
|
|
aurora::input::controller_rumble(instance, 0, 0, 0);
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-04 01:13:17 +00:00
|
|
|
if (commands[i] == PAD::MOTOR_STOP) {
|
|
|
|
aurora::input::controller_rumble(instance, 0, 0, 1);
|
|
|
|
} else if (commands[i] == PAD::MOTOR_RUMBLE) {
|
|
|
|
aurora::input::controller_rumble(instance, 32767, 32767, 0);
|
|
|
|
} else if (commands[i] == PAD::MOTOR_STOP_HARD) {
|
|
|
|
aurora::input::controller_rumble(instance, 0, 0, 0);
|
|
|
|
}
|
2022-03-20 07:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 SIProbe(s32 chan) {
|
2022-05-08 08:50:21 +00:00
|
|
|
auto* const controller = aurora::input::get_controller_for_player(chan);
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller == nullptr) {
|
2022-03-20 07:45:45 +00:00
|
|
|
return SI::ERROR_NO_RESPONSE;
|
|
|
|
}
|
|
|
|
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller->m_isGameCube) {
|
|
|
|
auto level = SDL_JoystickCurrentPowerLevel(SDL_GameControllerGetJoystick(controller->m_controller));
|
2022-03-20 07:45:45 +00:00
|
|
|
if (level == SDL_JOYSTICK_POWER_UNKNOWN) {
|
|
|
|
return SI::GC_WAVEBIRD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SI::GC_CONTROLLER;
|
2022-03-21 00:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PADCLampRegion {
|
|
|
|
u8 minTrigger;
|
|
|
|
u8 maxTrigger;
|
|
|
|
s8 minStick;
|
|
|
|
s8 maxStick;
|
|
|
|
s8 xyStick;
|
|
|
|
s8 minSubstick;
|
|
|
|
s8 maxSubstick;
|
|
|
|
s8 xySubstick;
|
|
|
|
s8 radStick;
|
|
|
|
s8 radSubstick;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr PADCLampRegion ClampRegion{
|
|
|
|
// Triggers
|
|
|
|
30,
|
|
|
|
180,
|
|
|
|
|
|
|
|
// Left stick
|
|
|
|
15,
|
|
|
|
72,
|
|
|
|
40,
|
|
|
|
|
|
|
|
// Right stick
|
|
|
|
15,
|
|
|
|
59,
|
|
|
|
31,
|
|
|
|
|
|
|
|
// Stick radii
|
|
|
|
56,
|
|
|
|
44,
|
|
|
|
};
|
|
|
|
|
|
|
|
void ClampTrigger(u8* trigger, u8 min, u8 max) {
|
|
|
|
if (*trigger <= min) {
|
|
|
|
*trigger = 0;
|
|
|
|
} else {
|
|
|
|
if (*trigger > max) {
|
|
|
|
*trigger = max;
|
|
|
|
}
|
|
|
|
*trigger -= min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClampCircle(s8* px, s8* py, s8 radius, s8 min) {
|
|
|
|
int x = *px;
|
|
|
|
int y = *py;
|
|
|
|
|
|
|
|
if (-min < x && x < min) {
|
|
|
|
x = 0;
|
|
|
|
} else if (0 < x) {
|
|
|
|
x -= min;
|
|
|
|
} else {
|
|
|
|
x += min;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-min < y && y < min) {
|
|
|
|
y = 0;
|
|
|
|
} else if (0 < y) {
|
|
|
|
y -= min;
|
|
|
|
} else {
|
|
|
|
y += min;
|
|
|
|
}
|
|
|
|
|
|
|
|
int squared = x * x + y * y;
|
|
|
|
if (radius * radius < squared) {
|
|
|
|
s32 length = static_cast<s32>(std::sqrt(squared));
|
|
|
|
x = (x * radius) / length;
|
|
|
|
y = (y * radius) / length;
|
|
|
|
}
|
|
|
|
|
|
|
|
*px = static_cast<s8>(x);
|
|
|
|
*py = static_cast<s8>(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClampStick(s8* px, s8* py, s8 max, s8 xy, s8 min) {
|
2022-03-22 19:04:57 +00:00
|
|
|
s32 x = *px;
|
|
|
|
s32 y = *py;
|
2022-03-21 00:30:47 +00:00
|
|
|
|
2022-03-22 19:04:57 +00:00
|
|
|
s32 signX = 0;
|
2022-03-21 00:30:47 +00:00
|
|
|
if (0 <= x) {
|
|
|
|
signX = 1;
|
|
|
|
} else {
|
|
|
|
signX = -1;
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
|
|
|
|
s8 signY = 0;
|
|
|
|
if (0 <= y) {
|
|
|
|
signY = 1;
|
|
|
|
} else {
|
|
|
|
signY = -1;
|
|
|
|
y = -y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x <= min) {
|
|
|
|
x = 0;
|
|
|
|
} else {
|
|
|
|
x -= min;
|
|
|
|
}
|
|
|
|
if (y <= min) {
|
|
|
|
y = 0;
|
|
|
|
} else {
|
|
|
|
y -= min;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == 0 && y == 0) {
|
|
|
|
*px = *py = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xy * y <= xy * x) {
|
2022-03-22 19:04:57 +00:00
|
|
|
s32 d = xy * x + (max - xy) * y;
|
2022-03-21 00:30:47 +00:00
|
|
|
if (xy * max < d) {
|
|
|
|
x = (xy * max * x / d);
|
|
|
|
y = (xy * max * y / d);
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-22 19:04:57 +00:00
|
|
|
s32 d = xy * y + (max - xy) * x;
|
2022-03-21 00:30:47 +00:00
|
|
|
if (xy * max < d) {
|
|
|
|
x = (xy * max * x / d);
|
|
|
|
y = (xy * max * y / d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*px = (signX * x);
|
|
|
|
*py = (signY * y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADClamp(PAD::Status* status) {
|
|
|
|
for (u32 i = 0; i < 4; ++i) {
|
|
|
|
if (status[i].xa_err != PAD::ERR_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClampStick(&status[i].x2_stickX, &status[i].x3_stickY, ClampRegion.maxStick, ClampRegion.xyStick,
|
|
|
|
ClampRegion.minStick);
|
|
|
|
ClampStick(&status[i].x4_substickX, &status[i].x5_substickY, ClampRegion.maxSubstick, ClampRegion.xySubstick,
|
|
|
|
ClampRegion.minSubstick);
|
|
|
|
ClampTrigger(&status[i].x6_triggerL, ClampRegion.minTrigger, ClampRegion.maxTrigger);
|
|
|
|
ClampTrigger(&status[i].x7_triggerR, ClampRegion.minTrigger, ClampRegion.maxTrigger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADClampCircle(PAD::Status* status) {
|
|
|
|
for (u32 i = 0; i < 4; ++i) {
|
|
|
|
if (status[i].xa_err != PAD::ERR_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClampCircle(&status[i].x2_stickX, &status[i].x3_stickY, ClampRegion.radStick, ClampRegion.minStick);
|
|
|
|
ClampCircle(&status[i].x4_substickX, &status[i].x5_substickY, ClampRegion.radSubstick, ClampRegion.minSubstick);
|
|
|
|
ClampTrigger(&status[i].x6_triggerL, ClampRegion.minTrigger, ClampRegion.maxTrigger);
|
|
|
|
ClampTrigger(&status[i].x7_triggerR, ClampRegion.minTrigger, ClampRegion.maxTrigger);
|
|
|
|
}
|
2022-05-08 04:20:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 08:50:21 +00:00
|
|
|
void PADGetVidPid(u32 port, u32* vid, u32* pid) {
|
2022-05-08 04:20:52 +00:00
|
|
|
*vid = 0;
|
|
|
|
*pid = 0;
|
2022-05-08 08:50:21 +00:00
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vid = controller->m_vid;
|
|
|
|
*pid = controller->m_pid;
|
|
|
|
}
|
|
|
|
|
2022-05-08 08:50:21 +00:00
|
|
|
const char* PADGetName(u32 port) {
|
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
2022-05-08 04:20:52 +00:00
|
|
|
if (controller == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_GameControllerName(controller->m_controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADSetButtonMapping(u32 port, PADButtonMapping mapping) {
|
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
|
|
|
if (controller == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-12 15:13:04 +00:00
|
|
|
auto iter = std::find_if(controller->m_mapping.begin(), controller->m_mapping.end(),
|
|
|
|
[mapping](const auto& pair) { return mapping.padButton == pair.padButton; });
|
2022-05-08 04:20:52 +00:00
|
|
|
if (iter == controller->m_mapping.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*iter = mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADSetAllButtonMappings(u32 port, PADButtonMapping buttons[12]) {
|
|
|
|
for (u32 i = 0; i < 12; ++i) {
|
|
|
|
PADSetButtonMapping(port, buttons[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PADButtonMapping* PADGetButtonMappings(u32 port, u32* buttonCount) {
|
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
|
|
|
if (controller == nullptr) {
|
|
|
|
*buttonCount = 0;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buttonCount = controller->m_mapping.size();
|
|
|
|
return controller->m_mapping.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __PADWriteDeadZones(FILE* file, aurora::input::GameController& controller) {
|
|
|
|
fwrite(&controller.m_deadZones, 1, sizeof(PADDeadZones), file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADSerializeMappings() {
|
|
|
|
std::string basePath = std::string(metaforce::FileStoreManager::instance()->getStoreRoot());
|
|
|
|
|
|
|
|
bool wroteGameCubeAlready = false;
|
|
|
|
for (auto& controller : aurora::input::g_GameControllers) {
|
|
|
|
if (!controller.second.m_mappingLoaded) {
|
|
|
|
__PADLoadMapping(&controller.second);
|
|
|
|
}
|
|
|
|
FILE* file = fopen(fmt::format(FMT_STRING("{}/{}_{:04X}_{:04X}.controller"), basePath,
|
|
|
|
aurora::input::controller_name(controller.second.m_index), controller.second.m_vid,
|
|
|
|
controller.second.m_pid)
|
|
|
|
.c_str(),
|
|
|
|
"wbe");
|
|
|
|
if (file == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t magic = SBIG('CTRL');
|
|
|
|
uint32_t version = 1;
|
|
|
|
fwrite(&magic, 1, sizeof(magic), file);
|
|
|
|
fwrite(&version, 1, sizeof(magic), file);
|
|
|
|
fwrite(&controller.second.m_isGameCube, 1, 1, file);
|
|
|
|
fseek(file, (ftell(file) + 31) & ~31, SEEK_SET);
|
|
|
|
int32_t dataStart = ftell(file);
|
|
|
|
if (!controller.second.m_isGameCube) {
|
|
|
|
__PADWriteDeadZones(file, controller.second);
|
|
|
|
fwrite(controller.second.m_mapping.data(), 1, sizeof(PADButtonMapping) * controller.second.m_mapping.size(),
|
|
|
|
file);
|
|
|
|
} else {
|
|
|
|
if (!wroteGameCubeAlready) {
|
|
|
|
for (u32 i = 0; i < 4; ++i) {
|
|
|
|
/* Just use the current controller's configs for this */
|
|
|
|
__PADWriteDeadZones(file, controller.second);
|
|
|
|
fwrite(mDefaultButtons.data(), 1, sizeof(PADButtonMapping) * mDefaultButtons.size(), file);
|
|
|
|
}
|
|
|
|
fflush(file);
|
|
|
|
wroteGameCubeAlready = true;
|
|
|
|
}
|
|
|
|
uint32_t port = aurora::input::player_index(controller.second.m_index);
|
|
|
|
fseek(file, dataStart + ((sizeof(PADDeadZones) + sizeof(PADButtonMapping)) * port), SEEK_SET);
|
|
|
|
__PADWriteDeadZones(file, controller.second);
|
|
|
|
fwrite(controller.second.m_mapping.data(), 1, sizeof(PADButtonMapping) * controller.second.m_mapping.size(),
|
|
|
|
file);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PADDeadZones* PADGetDeadZones(u32 port) {
|
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
|
|
|
if (controller == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &controller->m_deadZones;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr std::array<std::pair<PAD::BUTTON, std::string_view>, 12> skButtonNames = {{
|
|
|
|
{PAD::BUTTON_LEFT, "Left"sv},
|
|
|
|
{PAD::BUTTON_RIGHT, "Right"sv},
|
|
|
|
{PAD::BUTTON_DOWN, "Down"sv},
|
|
|
|
{PAD::BUTTON_UP, "Up"sv},
|
|
|
|
{PAD::TRIGGER_Z, "Z"sv},
|
|
|
|
{PAD::TRIGGER_R, "R"sv},
|
|
|
|
{PAD::TRIGGER_L, "L"sv},
|
|
|
|
{PAD::BUTTON_A, "A"sv},
|
|
|
|
{PAD::BUTTON_B, "B"sv},
|
|
|
|
{PAD::BUTTON_X, "X"sv},
|
|
|
|
{PAD::BUTTON_Y, "Y"sv},
|
|
|
|
{PAD::BUTTON_START, "Start"sv},
|
|
|
|
}};
|
|
|
|
|
|
|
|
const char* PADGetButtonName(PAD::BUTTON button) {
|
|
|
|
auto it = std::find_if(skButtonNames.begin(), skButtonNames.end(),
|
|
|
|
[&button](const auto& pair) { return button == pair.first; });
|
|
|
|
|
|
|
|
if (it != skButtonNames.end()) {
|
|
|
|
return it->second.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* PADGetNativeButtonName(u32 button) {
|
|
|
|
return SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button));
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 PADGetNativeButtonPressed(u32 port) {
|
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
|
|
|
if (controller == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
|
|
|
|
if (SDL_GameControllerGetButton(controller->m_controller, static_cast<SDL_GameControllerButton>(i)) != 0u) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PADRestoreDefaultMapping(u32 port) {
|
|
|
|
auto* controller = aurora::input::get_controller_for_player(port);
|
|
|
|
if (controller == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
controller->m_mapping = mDefaultButtons;
|
|
|
|
}
|
|
|
|
|
2022-05-12 15:13:04 +00:00
|
|
|
void PADBlockInput(bool block) { gBlockPAD = block; }
|