2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-26 15:17:35 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// InputManager.cpp
|
|
|
|
//
|
|
|
|
// Low-level input handling.
|
|
|
|
// Initiates and maps the keyboard and controllers.
|
2021-06-16 17:20:53 +00:00
|
|
|
// Reads and writes the es_input.xml configuration file.
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
|
|
|
|
2012-07-20 01:08:29 +00:00
|
|
|
#include "InputManager.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
#include "CECInput.h"
|
2013-04-11 22:27:27 +00:00
|
|
|
#include "Log.h"
|
2020-06-21 10:26:21 +00:00
|
|
|
#include "Platform.h"
|
2018-01-30 00:49:08 +00:00
|
|
|
#include "Scripting.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Window.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "resources/ResourceManager.h"
|
|
|
|
#include "utils/FileSystemUtil.h"
|
|
|
|
#include "utils/StringUtil.h"
|
2020-06-26 16:03:55 +00:00
|
|
|
|
2020-06-24 15:38:41 +00:00
|
|
|
#include <iostream>
|
2020-09-21 17:17:34 +00:00
|
|
|
#include <pugixml.hpp>
|
2013-04-11 22:27:27 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
#define KEYBOARD_GUID_STRING "-1"
|
2020-11-17 22:06:54 +00:00
|
|
|
#define CEC_GUID_STRING "-2"
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
int SDL_USER_CECBUTTONDOWN = -1;
|
2021-05-22 20:18:00 +00:00
|
|
|
int SDL_USER_CECBUTTONUP = -1;
|
2017-11-08 22:22:15 +00:00
|
|
|
|
2021-07-07 12:31:32 +00:00
|
|
|
// Save button states for combo-button exit support and predefine exit option-function map.
|
2021-08-11 10:09:58 +00:00
|
|
|
static bool sAltDown = false;
|
|
|
|
static bool sLguiDown = false;
|
2021-07-01 15:46:03 +00:00
|
|
|
|
2021-03-19 17:25:37 +00:00
|
|
|
InputManager* InputManager::sInstance = nullptr;
|
2014-04-18 18:07:32 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
InputManager::InputManager()
|
|
|
|
: mKeyboardInputConfig(nullptr)
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
InputManager::~InputManager()
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Deinit when destroyed.
|
2020-06-21 12:25:28 +00:00
|
|
|
deinit();
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:07:32 +00:00
|
|
|
InputManager* InputManager::getInstance()
|
|
|
|
{
|
2021-03-19 17:25:37 +00:00
|
|
|
if (!sInstance)
|
|
|
|
sInstance = new InputManager();
|
2014-04-18 18:07:32 +00:00
|
|
|
|
2021-03-19 17:25:37 +00:00
|
|
|
return sInstance;
|
2014-04-18 18:07:32 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
void InputManager::init()
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (initialized())
|
|
|
|
deinit();
|
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
mConfigFileExists = false;
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
LOG(LogInfo) << "Setting up InputManager...";
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
|
|
|
SDL_GameControllerEventState(SDL_ENABLE);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
if (!Utils::FileSystem::exists(getConfigPath())) {
|
2021-06-16 18:20:21 +00:00
|
|
|
LOG(LogInfo) << "No input configuration file found, default mappings will be applied";
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mConfigFileExists = true;
|
|
|
|
}
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
mKeyboardInputConfig =
|
|
|
|
std::make_unique<InputConfig>(DEVICE_KEYBOARD, "Keyboard", KEYBOARD_GUID_STRING);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
bool customConfig = loadInputConfig(mKeyboardInputConfig.get());
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
if (customConfig) {
|
|
|
|
LOG(LogInfo) << "Added keyboard with custom configuration";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
loadDefaultKBConfig();
|
|
|
|
LOG(LogInfo) << "Added keyboard with default configuration";
|
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
// Load optional controller mappings. Normally the supported controllers should be compiled
|
|
|
|
// into SDL as a header file, but if a user has a very rare controller that is not supported,
|
|
|
|
// the bundled mapping is incorrect, or the SDL version is a bit older, it makes sense to be
|
|
|
|
// able to customize this. If a controller GUID is present in the mappings file that is
|
|
|
|
// already present inside SDL, the custom mapping will overwrite the bundled one.
|
2021-07-07 18:31:46 +00:00
|
|
|
std::string mappingsFile =
|
|
|
|
Utils::FileSystem::getHomePath() + "/.emulationstation/" + "es_controller_mappings.cfg";
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
if (!Utils::FileSystem::exists(mappingsFile))
|
2021-07-07 18:31:46 +00:00
|
|
|
mappingsFile = ResourceManager::getInstance()->getResourcePath(
|
|
|
|
":/controllers/es_controller_mappings.cfg");
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
int controllerMappings = SDL_GameControllerAddMappingsFromFile(mappingsFile.c_str());
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
if (controllerMappings != -1 && controllerMappings != 0) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogInfo) << "Loaded " << controllerMappings << " controller "
|
|
|
|
<< (controllerMappings == 1 ? "mapping" : "mappings");
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
int numJoysticks = SDL_NumJoysticks();
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
// Make sure that every joystick is actually supported by the GameController API.
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int i = 0; i < numJoysticks; ++i)
|
2021-05-22 20:18:00 +00:00
|
|
|
if (!SDL_IsGameController(i))
|
2021-11-17 16:48:49 +00:00
|
|
|
--numJoysticks;
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int i = 0; i < numJoysticks; ++i)
|
2021-10-07 16:33:13 +00:00
|
|
|
addControllerByDeviceIndex(nullptr, i);
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2);
|
|
|
|
SDL_USER_CECBUTTONUP = SDL_USER_CECBUTTONDOWN + 1;
|
|
|
|
CECInput::init();
|
|
|
|
mCECInputConfig = std::make_unique<InputConfig>(DEVICE_CEC, "CEC", CEC_GUID_STRING);
|
|
|
|
loadInputConfig(mCECInputConfig.get());
|
2013-06-30 01:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::deinit()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!initialized())
|
|
|
|
return;
|
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = mControllers.cbegin(); it != mControllers.cend(); ++it)
|
2021-05-22 20:18:00 +00:00
|
|
|
SDL_GameControllerClose(it->second);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
mControllers.clear();
|
|
|
|
mJoysticks.clear();
|
2020-06-21 12:25:28 +00:00
|
|
|
mPrevAxisValues.clear();
|
2021-05-22 20:18:00 +00:00
|
|
|
mPrevButtonValues.clear();
|
|
|
|
mInputConfigs.clear();
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
mKeyboardInputConfig.reset();
|
|
|
|
mCECInputConfig.reset();
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
CECInput::deinit();
|
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
SDL_GameControllerEventState(SDL_DISABLE);
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
|
2021-03-19 17:34:10 +00:00
|
|
|
|
|
|
|
if (sInstance) {
|
|
|
|
delete sInstance;
|
|
|
|
sInstance = nullptr;
|
|
|
|
}
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
void InputManager::writeDeviceConfig(InputConfig* config)
|
2013-04-11 22:27:27 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
assert(initialized());
|
|
|
|
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
|
2020-07-14 17:16:21 +00:00
|
|
|
LOG(LogDebug) << "InputManager::writeDeviceConfig(): "
|
2021-07-07 18:31:46 +00:00
|
|
|
"Saving input configuration file to \""
|
|
|
|
<< path << "\"";
|
2020-07-14 17:16:21 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
pugi::xml_document doc;
|
|
|
|
|
|
|
|
if (Utils::FileSystem::exists(path)) {
|
|
|
|
// Merge files.
|
2021-07-07 18:31:46 +00:00
|
|
|
|
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
pugi::xml_parse_result result =
|
2021-07-07 18:31:46 +00:00
|
|
|
doc.load_file(Utils::String::stringToWideString(path).c_str());
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
pugi::xml_parse_result result = doc.load_file(path.c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!result) {
|
2021-05-22 20:18:00 +00:00
|
|
|
LOG(LogError) << "Couldn't parse input configuration file: " << result.description();
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Successfully loaded, delete the old entry if it exists.
|
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
if (root) {
|
|
|
|
// If inputAction @type=onfinish is set, let doOnFinish command take care of
|
|
|
|
// creating input configuration. We just put the input configuration into a
|
|
|
|
// temporary input config file.
|
2021-07-07 18:31:46 +00:00
|
|
|
pugi::xml_node actionnode =
|
|
|
|
root.find_child_by_attribute("inputAction", "type", "onfinish");
|
2020-06-21 12:25:28 +00:00
|
|
|
if (actionnode) {
|
|
|
|
path = getTemporaryConfigPath();
|
|
|
|
doc.reset();
|
|
|
|
root = doc.append_child("inputList");
|
|
|
|
root.append_copy(actionnode);
|
|
|
|
}
|
|
|
|
else {
|
2021-07-07 18:31:46 +00:00
|
|
|
pugi::xml_node oldEntry = root.find_child_by_attribute(
|
|
|
|
"inputConfig", "deviceGUID", config->getDeviceGUIDString().c_str());
|
2020-06-21 12:25:28 +00:00
|
|
|
if (oldEntry)
|
|
|
|
root.remove_child(oldEntry);
|
|
|
|
oldEntry = root.find_child_by_attribute("inputConfig", "deviceName",
|
2021-07-07 18:31:46 +00:00
|
|
|
config->getDeviceName().c_str());
|
2020-06-21 12:25:28 +00:00
|
|
|
if (oldEntry)
|
|
|
|
root.remove_child(oldEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
if (!root)
|
|
|
|
root = doc.append_child("inputList");
|
|
|
|
|
|
|
|
config->writeToXML(root);
|
2020-07-10 16:32:23 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
doc.save_file(Utils::String::stringToWideString(path).c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
doc.save_file(path.c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
Scripting::fireEvent("config-changed");
|
|
|
|
Scripting::fireEvent("controls-changed");
|
|
|
|
|
2020-07-14 17:16:21 +00:00
|
|
|
// Execute any doOnFinish commands and reload the config for changes.
|
2020-06-21 12:25:28 +00:00
|
|
|
doOnFinish();
|
2021-05-22 20:18:00 +00:00
|
|
|
mConfigFileExists = true;
|
2020-06-21 12:25:28 +00:00
|
|
|
loadInputConfig(config);
|
2012-07-23 23:53:33 +00:00
|
|
|
}
|
2013-04-08 16:52:40 +00:00
|
|
|
|
2015-04-19 09:57:10 +00:00
|
|
|
void InputManager::doOnFinish()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
assert(initialized());
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
|
|
|
if (Utils::FileSystem::exists(path)) {
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
pugi::xml_parse_result result =
|
2021-07-07 18:31:46 +00:00
|
|
|
doc.load_file(Utils::String::stringToWideString(path).c_str());
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
pugi::xml_parse_result result = doc.load_file(path.c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-07-10 16:32:23 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!result) {
|
2021-05-22 20:18:00 +00:00
|
|
|
LOG(LogError) << "Couldn't parse input configuration file: " << result.description();
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
if (root) {
|
|
|
|
root = root.find_child_by_attribute("inputAction", "type", "onfinish");
|
|
|
|
if (root) {
|
|
|
|
for (pugi::xml_node command = root.child("command"); command;
|
2021-07-07 18:31:46 +00:00
|
|
|
command = command.next_sibling("command")) {
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string tocall = command.text().get();
|
|
|
|
|
|
|
|
LOG(LogInfo) << " " << tocall;
|
|
|
|
std::cout << "==============================================\n"
|
2021-07-07 18:31:46 +00:00
|
|
|
"input config finish command:\n";
|
2020-06-21 12:25:28 +00:00
|
|
|
int exitCode = runSystemCommand(tocall);
|
|
|
|
std::cout << "==============================================\n";
|
|
|
|
|
2020-06-25 17:52:38 +00:00
|
|
|
if (exitCode != 0) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogWarning) << "...launch terminated with nonzero exit code "
|
|
|
|
<< exitCode << "!";
|
2020-06-25 17:52:38 +00:00
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 09:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 16:52:40 +00:00
|
|
|
std::string InputManager::getConfigPath()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string path = Utils::FileSystem::getHomePath();
|
2021-06-16 17:20:53 +00:00
|
|
|
path += "/.emulationstation/es_input.xml";
|
2020-06-21 12:25:28 +00:00
|
|
|
return path;
|
2013-04-08 16:52:40 +00:00
|
|
|
}
|
2013-08-19 14:05:30 +00:00
|
|
|
|
2015-04-19 09:57:10 +00:00
|
|
|
std::string InputManager::getTemporaryConfigPath()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string path = Utils::FileSystem::getHomePath();
|
2021-06-16 17:20:53 +00:00
|
|
|
path += "/.emulationstation/es_temporaryinput.xml";
|
2020-06-21 12:25:28 +00:00
|
|
|
return path;
|
2015-04-19 09:57:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
int InputManager::getNumConfiguredDevices()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
int num = 0;
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); ++it)
|
2020-06-21 12:25:28 +00:00
|
|
|
if (it->second->isConfigured())
|
2021-11-17 16:48:49 +00:00
|
|
|
++num;
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mKeyboardInputConfig->isConfigured())
|
2021-11-17 16:48:49 +00:00
|
|
|
++num;
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mCECInputConfig->isConfigured())
|
2021-11-17 16:48:49 +00:00
|
|
|
++num;
|
2017-11-08 22:22:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return num;
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
int InputManager::getAxisCountByDevice(SDL_JoystickID id)
|
|
|
|
{
|
|
|
|
return SDL_JoystickNumAxes(mJoysticks[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int InputManager::getButtonCountByDevice(SDL_JoystickID id)
|
|
|
|
{
|
|
|
|
if (id == DEVICE_KEYBOARD)
|
|
|
|
return -1;
|
|
|
|
else if (id == DEVICE_CEC)
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(HAVE_CECLIB)
|
2021-05-22 20:18:00 +00:00
|
|
|
return CEC::CEC_USER_CONTROL_CODE_MAX;
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2021-05-22 20:18:00 +00:00
|
|
|
return 0;
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2021-05-22 20:18:00 +00:00
|
|
|
else
|
|
|
|
return SDL_JoystickNumButtons(mJoysticks[id]);
|
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
std::string InputManager::getDeviceGUIDString(int deviceId)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (deviceId == DEVICE_KEYBOARD)
|
|
|
|
return KEYBOARD_GUID_STRING;
|
|
|
|
|
|
|
|
if (deviceId == DEVICE_CEC)
|
|
|
|
return CEC_GUID_STRING;
|
|
|
|
|
|
|
|
auto it = mJoysticks.find(deviceId);
|
|
|
|
if (it == mJoysticks.cend()) {
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "getDeviceGUIDString - deviceId " << deviceId << " not found!";
|
2021-05-22 20:18:00 +00:00
|
|
|
return "Something went horribly wrong";
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char guid[65];
|
|
|
|
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(it->second), guid, 65);
|
|
|
|
return std::string(guid);
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
InputConfig* InputManager::getInputConfigByDevice(int device)
|
|
|
|
{
|
|
|
|
if (device == DEVICE_KEYBOARD)
|
|
|
|
return mKeyboardInputConfig.get();
|
|
|
|
else if (device == DEVICE_CEC)
|
|
|
|
return mCECInputConfig.get();
|
|
|
|
else
|
|
|
|
return mInputConfigs[device].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputManager::parseEvent(const SDL_Event& event, Window* window)
|
|
|
|
{
|
|
|
|
bool causedEvent = false;
|
|
|
|
int32_t axisValue;
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_CONTROLLERAXISMOTION: {
|
2021-07-07 18:31:46 +00:00
|
|
|
// Whether to only accept input from the first controller.
|
|
|
|
if (Settings::getInstance()->getBool("InputOnlyFirstController"))
|
|
|
|
if (mInputConfigs.begin()->first != event.cdevice.which)
|
|
|
|
return false;
|
2021-05-23 09:45:45 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
// This is needed for a situation which sometimes occur when a game is launched,
|
|
|
|
// some axis input is generated and then the controller is disconnected before
|
|
|
|
// leaving the game. In this case, events for the old device index could be received
|
|
|
|
// when returning from the game. If this happens we simply delete the configuration
|
|
|
|
// map entry.
|
|
|
|
if (!mInputConfigs[event.caxis.which]) {
|
|
|
|
auto it = mInputConfigs.find(event.cdevice.which);
|
|
|
|
mInputConfigs.erase(it);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
axisValue = event.caxis.value;
|
2021-07-01 15:39:08 +00:00
|
|
|
int deadzone = 0;
|
|
|
|
|
|
|
|
if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT ||
|
2021-07-07 18:31:46 +00:00
|
|
|
event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) {
|
2021-07-01 15:39:08 +00:00
|
|
|
deadzone = DEADZONE_TRIGGERS;
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-07-01 15:39:08 +00:00
|
|
|
deadzone = DEADZONE_THUMBSTICKS;
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
// Check if the input value switched boundaries.
|
2021-07-07 18:31:46 +00:00
|
|
|
if ((abs(axisValue) > deadzone) !=
|
|
|
|
(abs(mPrevAxisValues[std::make_pair(event.caxis.which, event.caxis.axis)]) >
|
|
|
|
deadzone)) {
|
2021-05-22 20:18:00 +00:00
|
|
|
int normValue;
|
2021-07-01 15:39:08 +00:00
|
|
|
if (abs(axisValue) <= deadzone) {
|
2021-05-22 20:18:00 +00:00
|
|
|
normValue = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (axisValue > 0)
|
|
|
|
normValue = 1;
|
|
|
|
else
|
|
|
|
normValue = -1;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
window->input(
|
|
|
|
getInputConfigByDevice(event.caxis.which),
|
|
|
|
Input(event.caxis.which, TYPE_AXIS, event.caxis.axis, normValue, false));
|
2021-05-22 20:18:00 +00:00
|
|
|
causedEvent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPrevAxisValues[std::make_pair(event.caxis.which, event.caxis.axis)] = axisValue;
|
|
|
|
return causedEvent;
|
|
|
|
}
|
|
|
|
case SDL_CONTROLLERBUTTONDOWN: {
|
|
|
|
}
|
|
|
|
case SDL_CONTROLLERBUTTONUP: {
|
2021-07-07 18:31:46 +00:00
|
|
|
// Whether to only accept input from the first controller.
|
|
|
|
if (Settings::getInstance()->getBool("InputOnlyFirstController"))
|
|
|
|
if (mInputConfigs.begin()->first != event.cdevice.which)
|
|
|
|
return false;
|
2021-05-23 09:45:45 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
// The event filtering below is required as some controllers send button presses
|
2021-09-21 20:53:08 +00:00
|
|
|
// starting with the state 0 when using the D-pad. I consider this invalid behavior
|
2021-05-22 20:18:00 +00:00
|
|
|
// and the more popular controllers such as those from Microsoft and Sony do not show
|
|
|
|
// this strange behavior.
|
2021-07-07 18:31:46 +00:00
|
|
|
int buttonState =
|
|
|
|
mPrevButtonValues[std::make_pair(event.cbutton.which, event.cbutton.button)];
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
if ((buttonState == -1 || buttonState == 0) && event.cbutton.state == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mPrevButtonValues[std::make_pair(event.cbutton.which, event.cbutton.button)] =
|
2021-07-07 18:31:46 +00:00
|
|
|
event.cbutton.state;
|
2021-05-22 20:18:00 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
window->input(getInputConfigByDevice(event.cbutton.which),
|
|
|
|
Input(event.cbutton.which, TYPE_BUTTON, event.cbutton.button,
|
|
|
|
event.cbutton.state == SDL_PRESSED, false));
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case SDL_KEYDOWN: {
|
2021-07-01 15:46:03 +00:00
|
|
|
|
2021-07-07 12:31:32 +00:00
|
|
|
// Save button states for alt and command.
|
2021-07-01 15:46:03 +00:00
|
|
|
if (event.key.keysym.sym == SDLK_LALT)
|
2021-08-11 10:09:58 +00:00
|
|
|
sAltDown = true;
|
2021-07-01 15:46:03 +00:00
|
|
|
if (event.key.keysym.sym == SDLK_LGUI)
|
2021-08-11 10:09:58 +00:00
|
|
|
sLguiDown = true;
|
2021-07-01 15:46:03 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
if (event.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive())
|
|
|
|
window->textInput("\b");
|
|
|
|
|
|
|
|
if (event.key.repeat)
|
|
|
|
return false;
|
|
|
|
|
2021-07-07 12:31:32 +00:00
|
|
|
// Handle application exit.
|
2021-07-07 12:13:46 +00:00
|
|
|
bool exitState;
|
2021-07-01 15:46:03 +00:00
|
|
|
std::string exitOption = Settings::getInstance()->getString("ExitButtonCombo");
|
2021-07-07 12:31:32 +00:00
|
|
|
if (exitOption == "AltF4")
|
2021-08-11 10:09:58 +00:00
|
|
|
exitState = event.key.keysym.sym == SDLK_F4 && sAltDown;
|
2021-07-07 12:31:32 +00:00
|
|
|
else if (exitOption == "CmdQ")
|
2021-08-11 10:09:58 +00:00
|
|
|
exitState = event.key.keysym.sym == SDLK_q && sLguiDown;
|
2021-07-07 12:31:32 +00:00
|
|
|
else if (exitOption == "AltQ")
|
2021-08-11 10:09:58 +00:00
|
|
|
exitState = event.key.keysym.sym == SDLK_q && sAltDown;
|
2021-07-07 12:31:32 +00:00
|
|
|
else
|
2021-07-07 12:13:46 +00:00
|
|
|
exitState = event.key.keysym.sym == SDLK_F4;
|
2021-07-01 15:46:03 +00:00
|
|
|
if (exitState) {
|
2021-05-22 20:18:00 +00:00
|
|
|
SDL_Event quit;
|
|
|
|
quit.type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(&quit);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
window->input(getInputConfigByDevice(DEVICE_KEYBOARD),
|
|
|
|
Input(DEVICE_KEYBOARD, TYPE_KEY, event.key.keysym.sym, 1, false));
|
2021-05-22 20:18:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case SDL_KEYUP: {
|
2021-07-01 15:46:03 +00:00
|
|
|
|
2021-07-07 12:31:32 +00:00
|
|
|
// Release button states.
|
2021-07-01 15:46:03 +00:00
|
|
|
if (event.key.keysym.sym == SDLK_LALT)
|
2021-08-11 10:09:58 +00:00
|
|
|
sAltDown = false;
|
2021-07-01 15:46:03 +00:00
|
|
|
if (event.key.keysym.sym == SDLK_LGUI)
|
2021-08-11 10:09:58 +00:00
|
|
|
sLguiDown = false;
|
2021-07-01 15:46:03 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
window->input(getInputConfigByDevice(DEVICE_KEYBOARD),
|
|
|
|
Input(DEVICE_KEYBOARD, TYPE_KEY, event.key.keysym.sym, 0, false));
|
2021-05-22 20:18:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case SDL_TEXTINPUT: {
|
|
|
|
window->textInput(event.text.text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_CONTROLLERDEVICEADDED: {
|
2021-10-07 16:33:13 +00:00
|
|
|
addControllerByDeviceIndex(window, event.cdevice.which);
|
2021-05-22 20:18:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case SDL_CONTROLLERDEVICEREMOVED: {
|
2021-10-07 16:33:13 +00:00
|
|
|
removeControllerByJoystickID(window, event.cdevice.which);
|
2021-05-22 20:18:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event.type == static_cast<unsigned int>(SDL_USER_CECBUTTONDOWN)) ||
|
2021-07-07 18:31:46 +00:00
|
|
|
(event.type == static_cast<unsigned int>(SDL_USER_CECBUTTONUP))) {
|
|
|
|
window->input(getInputConfigByDevice(DEVICE_CEC),
|
|
|
|
Input(DEVICE_CEC, TYPE_CEC_BUTTON, event.user.code,
|
|
|
|
event.type == static_cast<unsigned int>(SDL_USER_CECBUTTONDOWN),
|
|
|
|
false));
|
2021-05-22 20:18:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputManager::loadInputConfig(InputConfig* config)
|
|
|
|
{
|
|
|
|
if (!mConfigFileExists)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2021-05-22 20:18:00 +00:00
|
|
|
pugi::xml_parse_result res = doc.load_file(Utils::String::stringToWideString(path).c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2021-05-22 20:18:00 +00:00
|
|
|
pugi::xml_parse_result res = doc.load_file(path.c_str());
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
LOG(LogError) << "Couldn't parse the input configuration file: " << res.description();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
if (!root)
|
|
|
|
return false;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
pugi::xml_node configNode = root.find_child_by_attribute("inputConfig", "deviceGUID",
|
|
|
|
config->getDeviceGUIDString().c_str());
|
2021-05-22 20:18:00 +00:00
|
|
|
|
2021-06-16 17:20:53 +00:00
|
|
|
// Enabling this will match an entry in es_input.xml based on the device name if there
|
2021-05-22 20:18:00 +00:00
|
|
|
// was no GUID match. This is probably not a good idea as many controllers share the same
|
|
|
|
// name even though the GUID differ and potentially the button configuration could be
|
|
|
|
// different between them. Keeping the code for now though.
|
2021-07-07 18:31:46 +00:00
|
|
|
// if (!configNode)
|
|
|
|
// configNode = root.find_child_by_attribute("inputConfig",
|
|
|
|
// "deviceName", config->getDeviceName().c_str());
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
// With the move to the SDL GameController API the button layout changed quite a lot, so
|
2021-06-16 17:20:53 +00:00
|
|
|
// es_input.xml files generated using the old API will end up with a completely unusable
|
2021-05-22 20:18:00 +00:00
|
|
|
// controller configuration. These older files had the configuration entry type set to
|
|
|
|
// "joystick", so it's easy to ignore such entries by only accepting entries with the
|
2021-06-16 17:20:53 +00:00
|
|
|
// type set to "controller" (which is now applied when saving the es_input.xml file).
|
2021-05-22 20:18:00 +00:00
|
|
|
if (configNode && config->getDeviceName() != "Keyboard")
|
|
|
|
if (!root.find_child_by_attribute("inputConfig", "type", "controller"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!configNode)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
config->loadFromXML(configNode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::loadDefaultKBConfig()
|
|
|
|
{
|
|
|
|
InputConfig* cfg = getInputConfigByDevice(DEVICE_KEYBOARD);
|
|
|
|
|
|
|
|
if (cfg->isConfigured())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cfg->clear();
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("Up", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_UP, 1, true));
|
|
|
|
cfg->mapInput("Down", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_DOWN, 1, true));
|
|
|
|
cfg->mapInput("Left", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_LEFT, 1, true));
|
|
|
|
cfg->mapInput("Right", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_RIGHT, 1, true));
|
|
|
|
|
|
|
|
cfg->mapInput("A", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_RETURN, 1, true));
|
|
|
|
cfg->mapInput("B", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_BACKSPACE, 1, true));
|
|
|
|
cfg->mapInput("X", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_DELETE, 1, true));
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(__APPLE__)
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("Y", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PRINTSCREEN, 1, true));
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("Y", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_INSERT, 1, true));
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("Start", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_ESCAPE, 1, true));
|
|
|
|
cfg->mapInput("Back", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 1, true));
|
2021-05-22 20:18:00 +00:00
|
|
|
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("LeftShoulder", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PAGEUP, 1, true));
|
|
|
|
cfg->mapInput("RightShoulder", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PAGEDOWN, 1, true));
|
|
|
|
cfg->mapInput("LeftTrigger", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_HOME, 1, true));
|
|
|
|
cfg->mapInput("RightTrigger", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_END, 1, true));
|
2021-05-22 20:18:00 +00:00
|
|
|
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("LeftThumbstickClick", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F2, 1, true));
|
|
|
|
cfg->mapInput("RightThumbstickClick", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F3, 1, true));
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::loadDefaultControllerConfig(SDL_JoystickID deviceIndex)
|
|
|
|
{
|
|
|
|
InputConfig* cfg = getInputConfigByDevice(deviceIndex);
|
|
|
|
|
|
|
|
if (cfg->isConfigured())
|
|
|
|
return;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format off
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("Up", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_UP, 1, true));
|
|
|
|
cfg->mapInput("Down", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_DOWN, 1, true));
|
|
|
|
cfg->mapInput("Left", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_LEFT, 1, true));
|
|
|
|
cfg->mapInput("Right", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, 1, true));
|
2021-05-22 20:18:00 +00:00
|
|
|
cfg->mapInput("Start", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_START, 1, true));
|
2021-05-23 18:31:15 +00:00
|
|
|
cfg->mapInput("Back", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_BACK, 1, true));
|
2021-05-22 20:18:00 +00:00
|
|
|
cfg->mapInput("A", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_A, 1, true));
|
|
|
|
cfg->mapInput("B", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_B, 1, true));
|
|
|
|
cfg->mapInput("X", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_X, 1, true));
|
|
|
|
cfg->mapInput("Y", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_Y, 1, true));
|
|
|
|
cfg->mapInput("LeftShoulder", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, 1, true));
|
|
|
|
cfg->mapInput("RightShoulder", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, 1, true));
|
|
|
|
cfg->mapInput("LeftTrigger", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 1, true));
|
|
|
|
cfg->mapInput("RightTrigger", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 1, true));
|
|
|
|
cfg->mapInput("LeftThumbstickUp", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_LEFTY, -1, true));
|
|
|
|
cfg->mapInput("LeftThumbstickDown", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_LEFTY, 1, true));
|
|
|
|
cfg->mapInput("LeftThumbstickLeft", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_LEFTX, -1, true));
|
|
|
|
cfg->mapInput("LeftThumbstickRight", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_LEFTX, 1, true));
|
|
|
|
cfg->mapInput("LeftThumbstickClick", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_LEFTSTICK, 1, true));
|
|
|
|
cfg->mapInput("RightThumbstickUp", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_RIGHTY, -1, true));
|
|
|
|
cfg->mapInput("RightThumbstickDown", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_RIGHTY, 1, true));
|
|
|
|
cfg->mapInput("RightThumbstickLeft", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_RIGHTX, -1, true));
|
|
|
|
cfg->mapInput("RightThumbstickRight", Input(deviceIndex, TYPE_AXIS, SDL_CONTROLLER_AXIS_RIGHTX, 1, true));
|
|
|
|
cfg->mapInput("RightThumbstickClick", Input(deviceIndex, TYPE_BUTTON, SDL_CONTROLLER_BUTTON_RIGHTSTICK, 1, true));
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format on
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 16:33:13 +00:00
|
|
|
void InputManager::addControllerByDeviceIndex(Window* window, int deviceIndex)
|
2021-05-22 20:18:00 +00:00
|
|
|
{
|
|
|
|
// Open joystick and add it to our list.
|
|
|
|
SDL_GameController* controller = SDL_GameControllerOpen(deviceIndex);
|
2021-12-10 15:51:41 +00:00
|
|
|
|
|
|
|
if (controller == nullptr) {
|
|
|
|
LOG(LogError) << "Couldn't add controller with device index " << deviceIndex;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
SDL_Joystick* joy = SDL_GameControllerGetJoystick(controller);
|
|
|
|
|
|
|
|
// Add it to our list so we can close it again later.
|
|
|
|
SDL_JoystickID joyID = SDL_JoystickInstanceID(joy);
|
|
|
|
mJoysticks[joyID] = joy;
|
|
|
|
mControllers[joyID] = controller;
|
|
|
|
|
|
|
|
char guid[65];
|
|
|
|
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65);
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
mInputConfigs[joyID] =
|
|
|
|
std::make_unique<InputConfig>(joyID, SDL_GameControllerName(mControllers[joyID]), guid);
|
2021-05-22 20:18:00 +00:00
|
|
|
|
|
|
|
bool customConfig = loadInputConfig(mInputConfigs[joyID].get());
|
|
|
|
|
|
|
|
if (customConfig) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogInfo) << "Added controller with custom configuration: \""
|
|
|
|
<< SDL_GameControllerName(mControllers[joyID]) << "\" (GUID: " << guid
|
|
|
|
<< ", instance ID: " << joyID << ", device index: " << deviceIndex << ")";
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
loadDefaultControllerConfig(joyID);
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogInfo) << "Added controller with default configuration: \""
|
|
|
|
<< SDL_GameControllerName(mControllers[joyID]) << "\" (GUID: " << guid
|
|
|
|
<< ", instance ID: " << joyID << ", device index: " << deviceIndex << ")";
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 16:33:13 +00:00
|
|
|
if (window != nullptr) {
|
|
|
|
window->queueInfoPopup(
|
|
|
|
"ADDED INPUT DEVICE '" +
|
|
|
|
Utils::String::toUpper(std::string(SDL_GameControllerName(mControllers[joyID]))) +
|
|
|
|
"'",
|
|
|
|
4000);
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
int numAxes = SDL_JoystickNumAxes(joy);
|
|
|
|
int numButtons = SDL_JoystickNumButtons(joy);
|
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int axis = 0; axis < numAxes; ++axis)
|
2021-05-22 20:18:00 +00:00
|
|
|
mPrevAxisValues[std::make_pair(joyID, axis)] = 0;
|
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int button = 0; button < numButtons; ++button)
|
2021-05-22 20:18:00 +00:00
|
|
|
mPrevButtonValues[std::make_pair(joyID, button)] = -1;
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:33:13 +00:00
|
|
|
void InputManager::removeControllerByJoystickID(Window* window, SDL_JoystickID joyID)
|
2021-05-22 20:18:00 +00:00
|
|
|
{
|
2021-06-26 20:54:00 +00:00
|
|
|
assert(joyID != -1);
|
|
|
|
|
|
|
|
char guid[65];
|
|
|
|
SDL_Joystick* joy = SDL_JoystickFromInstanceID(joyID);
|
|
|
|
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65);
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogInfo) << "Removed controller \"" << SDL_GameControllerName(mControllers[joyID])
|
|
|
|
<< "\" (GUID: " << guid << ", instance ID: " << joyID << ")";
|
2021-05-22 20:18:00 +00:00
|
|
|
|
2021-10-07 16:33:13 +00:00
|
|
|
if (window != nullptr) {
|
|
|
|
window->queueInfoPopup(
|
|
|
|
"REMOVED INPUT DEVICE '" +
|
|
|
|
Utils::String::toUpper(std::string(SDL_GameControllerName(mControllers[joyID]))) +
|
|
|
|
"'",
|
|
|
|
4000);
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
// Delete mPrevAxisValues for the device.
|
2021-05-22 20:31:03 +00:00
|
|
|
int axisEntries = static_cast<int>(mPrevAxisValues.size());
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int i = 0; i < axisEntries; ++i) {
|
2021-06-26 20:54:00 +00:00
|
|
|
auto entry = mPrevAxisValues.find(std::make_pair(joyID, i));
|
2021-05-22 20:18:00 +00:00
|
|
|
if (entry != mPrevAxisValues.end()) {
|
|
|
|
mPrevAxisValues.erase(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete mPrevButtonValues for the device.
|
2021-05-22 20:31:03 +00:00
|
|
|
int buttonEntries = static_cast<int>(mPrevButtonValues.size());
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int i = 0; i < buttonEntries; ++i) {
|
2021-06-26 20:54:00 +00:00
|
|
|
auto entry = mPrevButtonValues.find(std::make_pair(joyID, i));
|
2021-05-22 20:18:00 +00:00
|
|
|
if (entry != mPrevButtonValues.end()) {
|
|
|
|
mPrevButtonValues.erase(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 20:54:00 +00:00
|
|
|
auto it = mInputConfigs.find(joyID);
|
2021-05-22 20:18:00 +00:00
|
|
|
mInputConfigs.erase(it);
|
|
|
|
|
2021-07-03 10:24:23 +00:00
|
|
|
// Close the controller and remove its entry.
|
2021-06-26 20:54:00 +00:00
|
|
|
auto controllerIt = mControllers.find(joyID);
|
2021-05-22 20:18:00 +00:00
|
|
|
if (controllerIt != mControllers.cend()) {
|
|
|
|
SDL_GameControllerClose(controllerIt->second);
|
|
|
|
mControllers.erase(controllerIt);
|
|
|
|
}
|
|
|
|
else {
|
2021-06-26 20:54:00 +00:00
|
|
|
LOG(LogError) << "Couldn't find controller to close (instance ID: " << joyID << ")";
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|
2021-07-03 10:24:23 +00:00
|
|
|
|
|
|
|
// Remove the joystick entry.
|
|
|
|
auto joystickIt = mJoysticks.find(joyID);
|
|
|
|
if (joystickIt != mJoysticks.cend()) {
|
|
|
|
mJoysticks.erase(joystickIt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogError) << "Couldn't find joystick entry to remove (instance ID: " << joyID << ")";
|
|
|
|
}
|
2021-05-22 20:18:00 +00:00
|
|
|
}
|