Cleaned up some code in InputeManager.

This commit is contained in:
Leon Styhre 2023-01-21 11:29:43 +01:00
parent f7a050b1b9
commit ff5273c265

View file

@ -70,7 +70,7 @@ void InputManager::init()
mKeyboardInputConfig = mKeyboardInputConfig =
std::make_unique<InputConfig>(DEVICE_KEYBOARD, "Keyboard", KEYBOARD_GUID_STRING); std::make_unique<InputConfig>(DEVICE_KEYBOARD, "Keyboard", KEYBOARD_GUID_STRING);
bool customConfig = loadInputConfig(mKeyboardInputConfig.get()); bool customConfig {loadInputConfig(mKeyboardInputConfig.get())};
if (customConfig) { if (customConfig) {
LOG(LogInfo) << "Added keyboard with custom configuration"; LOG(LogInfo) << "Added keyboard with custom configuration";
@ -85,28 +85,28 @@ void InputManager::init()
// the bundled mapping is incorrect, or the SDL version is a bit older, it makes sense to be // 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 // 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. // already present inside SDL, the custom mapping will overwrite the bundled one.
std::string mappingsFile = std::string mappingsFile {Utils::FileSystem::getHomePath() + "/.emulationstation/" +
Utils::FileSystem::getHomePath() + "/.emulationstation/" + "es_controller_mappings.cfg"; "es_controller_mappings.cfg"};
if (!Utils::FileSystem::exists(mappingsFile)) if (!Utils::FileSystem::exists(mappingsFile))
mappingsFile = ResourceManager::getInstance().getResourcePath( mappingsFile = ResourceManager::getInstance().getResourcePath(
":/controllers/es_controller_mappings.cfg"); ":/controllers/es_controller_mappings.cfg");
int controllerMappings = SDL_GameControllerAddMappingsFromFile(mappingsFile.c_str()); int controllerMappings {SDL_GameControllerAddMappingsFromFile(mappingsFile.c_str())};
if (controllerMappings != -1 && controllerMappings != 0) { if (controllerMappings != -1 && controllerMappings != 0) {
LOG(LogInfo) << "Loaded " << controllerMappings << " controller " LOG(LogInfo) << "Loaded " << controllerMappings << " controller "
<< (controllerMappings == 1 ? "mapping" : "mappings"); << (controllerMappings == 1 ? "mapping" : "mappings");
} }
int numJoysticks = SDL_NumJoysticks(); int numJoysticks {SDL_NumJoysticks()};
// Make sure that every joystick is actually supported by the GameController API. // Make sure that every joystick is actually supported by the GameController API.
for (int i = 0; i < numJoysticks; ++i) for (int i {0}; i < numJoysticks; ++i)
if (!SDL_IsGameController(i)) if (!SDL_IsGameController(i))
--numJoysticks; --numJoysticks;
for (int i = 0; i < numJoysticks; ++i) for (int i {0}; i < numJoysticks; ++i)
addControllerByDeviceIndex(nullptr, i); addControllerByDeviceIndex(nullptr, i);
SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2); SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2);
@ -140,7 +140,7 @@ void InputManager::writeDeviceConfig(InputConfig* config)
{ {
assert(initialized()); assert(initialized());
std::string path = getConfigPath(); std::string path {getConfigPath()};
LOG(LogDebug) << "InputManager::writeDeviceConfig(): " LOG(LogDebug) << "InputManager::writeDeviceConfig(): "
"Saving input configuration file to \"" "Saving input configuration file to \""
@ -152,23 +152,23 @@ void InputManager::writeDeviceConfig(InputConfig* config)
// Merge files. // Merge files.
#if defined(_WIN64) #if defined(_WIN64)
pugi::xml_parse_result result = pugi::xml_parse_result result {
doc.load_file(Utils::String::stringToWideString(path).c_str()); doc.load_file(Utils::String::stringToWideString(path).c_str())};
#else #else
pugi::xml_parse_result result = doc.load_file(path.c_str()); pugi::xml_parse_result result {doc.load_file(path.c_str())};
#endif #endif
if (!result) { if (!result) {
LOG(LogError) << "Couldn't parse input configuration file: " << result.description(); LOG(LogError) << "Couldn't parse input configuration file: " << result.description();
} }
else { else {
// Successfully loaded, delete the old entry if it exists. // Successfully loaded, delete the old entry if it exists.
pugi::xml_node root = doc.child("inputList"); pugi::xml_node root {doc.child("inputList")};
if (root) { if (root) {
// If inputAction @type=onfinish is set, let doOnFinish command take care of // If inputAction @type=onfinish is set, let doOnFinish command take care of
// creating input configuration. We just put the input configuration into a // creating input configuration. We just put the input configuration into a
// temporary input config file. // temporary input config file.
pugi::xml_node actionnode = pugi::xml_node actionnode {
root.find_child_by_attribute("inputAction", "type", "onfinish"); root.find_child_by_attribute("inputAction", "type", "onfinish")};
if (actionnode) { if (actionnode) {
path = getTemporaryConfigPath(); path = getTemporaryConfigPath();
doc.reset(); doc.reset();
@ -176,8 +176,8 @@ void InputManager::writeDeviceConfig(InputConfig* config)
root.append_copy(actionnode); root.append_copy(actionnode);
} }
else { else {
pugi::xml_node oldEntry = root.find_child_by_attribute( pugi::xml_node oldEntry {root.find_child_by_attribute(
"inputConfig", "deviceGUID", config->getDeviceGUIDString().c_str()); "inputConfig", "deviceGUID", config->getDeviceGUIDString().c_str())};
if (oldEntry) if (oldEntry)
root.remove_child(oldEntry); root.remove_child(oldEntry);
oldEntry = root.find_child_by_attribute("inputConfig", "deviceName", oldEntry = root.find_child_by_attribute("inputConfig", "deviceName",
@ -189,7 +189,7 @@ void InputManager::writeDeviceConfig(InputConfig* config)
} }
} }
pugi::xml_node root = doc.child("inputList"); pugi::xml_node root {doc.child("inputList")};
if (!root) if (!root)
root = doc.append_child("inputList"); root = doc.append_child("inputList");
@ -213,28 +213,28 @@ void InputManager::writeDeviceConfig(InputConfig* config)
void InputManager::doOnFinish() void InputManager::doOnFinish()
{ {
assert(initialized()); assert(initialized());
std::string path = getConfigPath(); std::string path {getConfigPath()};
pugi::xml_document doc; pugi::xml_document doc;
if (Utils::FileSystem::exists(path)) { if (Utils::FileSystem::exists(path)) {
#if defined(_WIN64) #if defined(_WIN64)
pugi::xml_parse_result result = pugi::xml_parse_result result {
doc.load_file(Utils::String::stringToWideString(path).c_str()); doc.load_file(Utils::String::stringToWideString(path).c_str())};
#else #else
pugi::xml_parse_result result = doc.load_file(path.c_str()); pugi::xml_parse_result result {doc.load_file(path.c_str())};
#endif #endif
if (!result) { if (!result) {
LOG(LogError) << "Couldn't parse input configuration file: " << result.description(); LOG(LogError) << "Couldn't parse input configuration file: " << result.description();
} }
else { else {
pugi::xml_node root = doc.child("inputList"); pugi::xml_node root {doc.child("inputList")};
if (root) { if (root) {
root = root.find_child_by_attribute("inputAction", "type", "onfinish"); root = root.find_child_by_attribute("inputAction", "type", "onfinish");
if (root) { if (root) {
for (pugi::xml_node command = root.child("command"); command; for (pugi::xml_node command {root.child("command")}; command;
command = command.next_sibling("command")) { command = command.next_sibling("command")) {
std::string tocall = command.text().get(); std::string tocall {command.text().get()};
LOG(LogInfo) << " " << tocall; LOG(LogInfo) << " " << tocall;
std::cout << "==============================================\n" std::cout << "==============================================\n"
@ -255,21 +255,21 @@ void InputManager::doOnFinish()
std::string InputManager::getConfigPath() std::string InputManager::getConfigPath()
{ {
std::string path = Utils::FileSystem::getHomePath(); std::string path {Utils::FileSystem::getHomePath()};
path += "/.emulationstation/es_input.xml"; path.append("/.emulationstation/es_input.xml");
return path; return path;
} }
std::string InputManager::getTemporaryConfigPath() std::string InputManager::getTemporaryConfigPath()
{ {
std::string path = Utils::FileSystem::getHomePath(); std::string path {Utils::FileSystem::getHomePath()};
path += "/.emulationstation/es_temporaryinput.xml"; path.append("/.emulationstation/es_temporaryinput.xml");
return path; return path;
} }
int InputManager::getNumConfiguredDevices() int InputManager::getNumConfiguredDevices()
{ {
int num = 0; int num {0};
for (auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); ++it) for (auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); ++it)
if (it->second->isConfigured()) if (it->second->isConfigured())
++num; ++num;
@ -355,7 +355,7 @@ bool InputManager::parseEvent(const SDL_Event& event)
} }
axisValue = event.caxis.value; axisValue = event.caxis.value;
int deadzone = 0; int deadzone {0};
if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT ||
event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) {
@ -369,7 +369,7 @@ bool InputManager::parseEvent(const SDL_Event& event)
if ((abs(axisValue) > deadzone) != if ((abs(axisValue) > deadzone) !=
(abs(mPrevAxisValues[std::make_pair(event.caxis.which, event.caxis.axis)]) > (abs(mPrevAxisValues[std::make_pair(event.caxis.which, event.caxis.axis)]) >
deadzone)) { deadzone)) {
int normValue; int normValue {0};
if (abs(axisValue) <= deadzone) { if (abs(axisValue) <= deadzone) {
normValue = 0; normValue = 0;
} }
@ -401,8 +401,8 @@ bool InputManager::parseEvent(const SDL_Event& event)
// starting with the state 0 when using the D-pad. I consider this invalid behavior // starting with the state 0 when using the D-pad. I consider this invalid behavior
// and the more popular controllers such as those from Microsoft and Sony do not show // and the more popular controllers such as those from Microsoft and Sony do not show
// this strange behavior. // this strange behavior.
int buttonState = int buttonState {
mPrevButtonValues[std::make_pair(event.cbutton.which, event.cbutton.button)]; mPrevButtonValues[std::make_pair(event.cbutton.which, event.cbutton.button)]};
if ((buttonState == -1 || buttonState == 0) && event.cbutton.state == 0) if ((buttonState == -1 || buttonState == 0) && event.cbutton.state == 0)
return false; return false;
@ -496,13 +496,13 @@ bool InputManager::loadInputConfig(InputConfig* config)
if (!mConfigFileExists) if (!mConfigFileExists)
return false; return false;
std::string path = getConfigPath(); std::string path {getConfigPath()};
pugi::xml_document doc; pugi::xml_document doc;
#if defined(_WIN64) #if defined(_WIN64)
pugi::xml_parse_result res = doc.load_file(Utils::String::stringToWideString(path).c_str()); pugi::xml_parse_result res {doc.load_file(Utils::String::stringToWideString(path).c_str())};
#else #else
pugi::xml_parse_result res = doc.load_file(path.c_str()); pugi::xml_parse_result res {doc.load_file(path.c_str())};
#endif #endif
if (!res) { if (!res) {
@ -510,12 +510,12 @@ bool InputManager::loadInputConfig(InputConfig* config)
return false; return false;
} }
pugi::xml_node root = doc.child("inputList"); pugi::xml_node root {doc.child("inputList")};
if (!root) if (!root)
return false; return false;
pugi::xml_node configNode = root.find_child_by_attribute("inputConfig", "deviceGUID", pugi::xml_node configNode {root.find_child_by_attribute("inputConfig", "deviceGUID",
config->getDeviceGUIDString().c_str()); config->getDeviceGUIDString().c_str())};
// Enabling this will match an entry in es_input.xml based on the device name if there // Enabling this will match an entry in es_input.xml based on the device name if there
// was no GUID match. This is probably not a good idea as many controllers share the same // was no GUID match. This is probably not a good idea as many controllers share the same
@ -543,7 +543,7 @@ bool InputManager::loadInputConfig(InputConfig* config)
void InputManager::loadDefaultKBConfig() void InputManager::loadDefaultKBConfig()
{ {
InputConfig* cfg = getInputConfigByDevice(DEVICE_KEYBOARD); InputConfig* cfg {getInputConfigByDevice(DEVICE_KEYBOARD)};
if (cfg->isConfigured()) if (cfg->isConfigured())
return; return;
@ -562,8 +562,8 @@ void InputManager::loadDefaultKBConfig()
#else #else
cfg->mapInput("Y", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_INSERT, 1, true)); cfg->mapInput("Y", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_INSERT, 1, true));
#endif #endif
cfg->mapInput("Start", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_ESCAPE, 1, true));
cfg->mapInput("Back", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 1, true)); cfg->mapInput("Back", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 1, true));
cfg->mapInput("Start", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_ESCAPE, 1, true));
cfg->mapInput("LeftShoulder", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PAGEUP, 1, true)); 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("RightShoulder", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PAGEDOWN, 1, true));
@ -576,7 +576,7 @@ void InputManager::loadDefaultKBConfig()
void InputManager::loadDefaultControllerConfig(SDL_JoystickID deviceIndex) void InputManager::loadDefaultControllerConfig(SDL_JoystickID deviceIndex)
{ {
InputConfig* cfg = getInputConfigByDevice(deviceIndex); InputConfig* cfg {getInputConfigByDevice(deviceIndex)};
if (cfg->isConfigured()) if (cfg->isConfigured())
return; return;
@ -612,17 +612,17 @@ void InputManager::loadDefaultControllerConfig(SDL_JoystickID deviceIndex)
void InputManager::addControllerByDeviceIndex(Window* window, int deviceIndex) void InputManager::addControllerByDeviceIndex(Window* window, int deviceIndex)
{ {
// Open joystick and add it to our list. // Open joystick and add it to our list.
SDL_GameController* controller = SDL_GameControllerOpen(deviceIndex); SDL_GameController* controller {SDL_GameControllerOpen(deviceIndex)};
if (controller == nullptr) { if (controller == nullptr) {
LOG(LogError) << "Couldn't add controller with device index " << deviceIndex; LOG(LogError) << "Couldn't add controller with device index " << deviceIndex;
return; return;
} }
SDL_Joystick* joy = SDL_GameControllerGetJoystick(controller); SDL_Joystick* joy {SDL_GameControllerGetJoystick(controller)};
// Add it to our list so we can close it again later. // Add it to our list so we can close it again later.
SDL_JoystickID joyID = SDL_JoystickInstanceID(joy); SDL_JoystickID joyID {SDL_JoystickInstanceID(joy)};
mJoysticks[joyID] = joy; mJoysticks[joyID] = joy;
mControllers[joyID] = controller; mControllers[joyID] = controller;
@ -654,13 +654,13 @@ void InputManager::addControllerByDeviceIndex(Window* window, int deviceIndex)
4000); 4000);
} }
int numAxes = SDL_JoystickNumAxes(joy); int numAxes {SDL_JoystickNumAxes(joy)};
int numButtons = SDL_JoystickNumButtons(joy); int numButtons {SDL_JoystickNumButtons(joy)};
for (int axis = 0; axis < numAxes; ++axis) for (int axis {0}; axis < numAxes; ++axis)
mPrevAxisValues[std::make_pair(joyID, axis)] = 0; mPrevAxisValues[std::make_pair(joyID, axis)] = 0;
for (int button = 0; button < numButtons; ++button) for (int button {0}; button < numButtons; ++button)
mPrevButtonValues[std::make_pair(joyID, button)] = -1; mPrevButtonValues[std::make_pair(joyID, button)] = -1;
} }
@ -669,7 +669,7 @@ void InputManager::removeControllerByJoystickID(Window* window, SDL_JoystickID j
assert(joyID != -1); assert(joyID != -1);
char guid[65]; char guid[65];
SDL_Joystick* joy = SDL_JoystickFromInstanceID(joyID); SDL_Joystick* joy {SDL_JoystickFromInstanceID(joyID)};
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65); SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65);
LOG(LogInfo) << "Removed controller \"" << SDL_GameControllerName(mControllers[joyID]) LOG(LogInfo) << "Removed controller \"" << SDL_GameControllerName(mControllers[joyID])
@ -684,8 +684,8 @@ void InputManager::removeControllerByJoystickID(Window* window, SDL_JoystickID j
} }
// Delete mPrevAxisValues for the device. // Delete mPrevAxisValues for the device.
int axisEntries = static_cast<int>(mPrevAxisValues.size()); int axisEntries {static_cast<int>(mPrevAxisValues.size())};
for (int i = 0; i < axisEntries; ++i) { for (int i {0}; i < axisEntries; ++i) {
auto entry = mPrevAxisValues.find(std::make_pair(joyID, i)); auto entry = mPrevAxisValues.find(std::make_pair(joyID, i));
if (entry != mPrevAxisValues.end()) { if (entry != mPrevAxisValues.end()) {
mPrevAxisValues.erase(entry); mPrevAxisValues.erase(entry);
@ -693,8 +693,8 @@ void InputManager::removeControllerByJoystickID(Window* window, SDL_JoystickID j
} }
// Delete mPrevButtonValues for the device. // Delete mPrevButtonValues for the device.
int buttonEntries = static_cast<int>(mPrevButtonValues.size()); int buttonEntries {static_cast<int>(mPrevButtonValues.size())};
for (int i = 0; i < buttonEntries; ++i) { for (int i {0}; i < buttonEntries; ++i) {
auto entry = mPrevButtonValues.find(std::make_pair(joyID, i)); auto entry = mPrevButtonValues.find(std::make_pair(joyID, i));
if (entry != mPrevButtonValues.end()) { if (entry != mPrevButtonValues.end()) {
mPrevButtonValues.erase(entry); mPrevButtonValues.erase(entry);