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.
|
|
|
|
// Reads and writes the es_input.cfg configuration file.
|
|
|
|
//
|
|
|
|
|
2012-07-20 01:08:29 +00:00
|
|
|
#include "InputManager.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2020-07-10 16:32:23 +00:00
|
|
|
#include "utils/StringUtil.h"
|
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"
|
2020-06-26 16:03:55 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
2017-12-04 23:30:25 +00:00
|
|
|
#include <assert.h>
|
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
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// There are four distinct IDs used for joysticks:
|
|
|
|
// 1. Device index - this is the "lowest level" identifier, and is just the Nth joystick plugged
|
|
|
|
// in to the system (like /dev/js#). It can change even if the device is the same, and is
|
|
|
|
// only used to open joysticks (required to receive SDL events).
|
|
|
|
// 2. SDL_JoystickID - this is an ID for each joystick that is supposed to remain consistent
|
|
|
|
// between plugging and unplugging. ES doesn't care if it does, though.
|
2019-08-25 15:23:02 +00:00
|
|
|
// 3. "Device ID" - this is something I made up and is what InputConfig's getDeviceID() returns.
|
2020-06-21 12:25:28 +00:00
|
|
|
// This is actually just an SDL_JoystickID (also called instance ID), but -1 means "keyboard"
|
|
|
|
// instead of "error."
|
|
|
|
// 4. Joystick GUID - this is some squashed version of joystick vendor, version, and a bunch of
|
|
|
|
// other device-specific things. It should remain the same across runs of the program/system
|
|
|
|
// restarts/device reordering and is what I use to identify which joystick to load.
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Hack for CEC support.
|
2017-11-08 22:22:15 +00:00
|
|
|
int SDL_USER_CECBUTTONDOWN = -1;
|
|
|
|
int SDL_USER_CECBUTTONUP = -1;
|
|
|
|
|
2021-03-19 17:25:37 +00:00
|
|
|
InputManager* InputManager::sInstance = nullptr;
|
2014-04-18 18:07:32 +00:00
|
|
|
|
2020-06-21 12:25:28 +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
|
|
|
{
|
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();
|
|
|
|
|
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
|
|
|
|
Settings::getInstance()->getBool("BackgroundJoystickInput") ? "1" : "0");
|
|
|
|
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
SDL_JoystickEventState(SDL_ENABLE);
|
|
|
|
|
|
|
|
// First, open all currently present joysticks.
|
|
|
|
int numJoysticks = SDL_NumJoysticks();
|
|
|
|
for (int i = 0; i < numJoysticks; i++)
|
|
|
|
addJoystickByDeviceIndex(i);
|
|
|
|
|
|
|
|
mKeyboardInputConfig = new InputConfig(DEVICE_KEYBOARD, "Keyboard", KEYBOARD_GUID_STRING);
|
|
|
|
loadInputConfig(mKeyboardInputConfig);
|
|
|
|
|
|
|
|
SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2);
|
2020-07-10 16:32:23 +00:00
|
|
|
SDL_USER_CECBUTTONUP = SDL_USER_CECBUTTONDOWN + 1;
|
2020-06-21 12:25:28 +00:00
|
|
|
CECInput::init();
|
|
|
|
mCECInputConfig = new InputConfig(DEVICE_CEC, "CEC", CEC_GUID_STRING);
|
|
|
|
loadInputConfig(mCECInputConfig);
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
void InputManager::addJoystickByDeviceIndex(int id)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
assert(id >= 0 && id < SDL_NumJoysticks());
|
|
|
|
|
|
|
|
// Open joystick & add to our list.
|
|
|
|
SDL_Joystick* joy = SDL_JoystickOpen(id);
|
|
|
|
assert(joy);
|
|
|
|
|
|
|
|
// Add it to our list so we can close it again later.
|
|
|
|
SDL_JoystickID joyId = SDL_JoystickInstanceID(joy);
|
|
|
|
mJoysticks[joyId] = joy;
|
|
|
|
|
|
|
|
char guid[65];
|
|
|
|
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65);
|
|
|
|
|
|
|
|
// Create the InputConfig.
|
|
|
|
mInputConfigs[joyId] = new InputConfig(joyId, SDL_JoystickName(joy), guid);
|
|
|
|
if (!loadInputConfig(mInputConfigs[joyId]))
|
|
|
|
LOG(LogInfo) << "Added unconfigured joystick " << SDL_JoystickName(joy) << " (GUID: " <<
|
|
|
|
guid << ", instance ID: " << joyId << ", device index: " << id << ").";
|
|
|
|
else
|
|
|
|
LOG(LogInfo) << "Added known joystick " << SDL_JoystickName(joy) << " (instance ID: " <<
|
|
|
|
joyId << ", device index: " << id << ")";
|
|
|
|
|
|
|
|
// Set up the prevAxisValues.
|
|
|
|
int numAxes = SDL_JoystickNumAxes(joy);
|
|
|
|
mPrevAxisValues[joyId] = new int[numAxes];
|
|
|
|
// Initialize array to 0.
|
|
|
|
std::fill(mPrevAxisValues[joyId], mPrevAxisValues[joyId] + numAxes, 0);
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::removeJoystickByJoystickID(SDL_JoystickID joyId)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
assert(joyId != -1);
|
|
|
|
|
|
|
|
// Delete old prevAxisValues.
|
|
|
|
auto axisIt = mPrevAxisValues.find(joyId);
|
|
|
|
delete[] axisIt->second;
|
|
|
|
mPrevAxisValues.erase(axisIt);
|
|
|
|
|
|
|
|
// Delete old InputConfig.
|
|
|
|
auto it = mInputConfigs.find(joyId);
|
|
|
|
delete it->second;
|
|
|
|
mInputConfigs.erase(it);
|
|
|
|
|
|
|
|
// Close the joystick.
|
|
|
|
auto joyIt = mJoysticks.find(joyId);
|
|
|
|
if (joyIt != mJoysticks.cend()) {
|
|
|
|
SDL_JoystickClose(joyIt->second);
|
|
|
|
mJoysticks.erase(joyIt);
|
|
|
|
}
|
|
|
|
else {
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "Could not find joystick to close (instance ID: " << joyId << ")";
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2013-06-30 01:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::deinit()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!initialized())
|
|
|
|
return;
|
|
|
|
|
2020-07-24 16:24:04 +00:00
|
|
|
for (auto it = mJoysticks.cbegin(); it != mJoysticks.cend(); it++)
|
|
|
|
SDL_JoystickClose(it->second);
|
2020-06-21 12:25:28 +00:00
|
|
|
mJoysticks.clear();
|
|
|
|
|
2020-07-24 16:24:04 +00:00
|
|
|
for (auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); it++)
|
|
|
|
delete it->second;
|
2020-06-21 12:25:28 +00:00
|
|
|
mInputConfigs.clear();
|
|
|
|
|
2020-07-24 16:24:04 +00:00
|
|
|
for (auto it = mPrevAxisValues.cbegin(); it != mPrevAxisValues.cend(); it++)
|
|
|
|
delete[] it->second;
|
2020-06-21 12:25:28 +00:00
|
|
|
mPrevAxisValues.clear();
|
|
|
|
|
|
|
|
if (mKeyboardInputConfig != nullptr) {
|
|
|
|
delete mKeyboardInputConfig;
|
|
|
|
mKeyboardInputConfig = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCECInputConfig != nullptr) {
|
|
|
|
delete mCECInputConfig;
|
|
|
|
mCECInputConfig = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CECInput::deinit();
|
|
|
|
|
|
|
|
SDL_JoystickEventState(SDL_DISABLE);
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2020-08-03 10:13:28 +00:00
|
|
|
int InputManager::getNumJoysticks()
|
|
|
|
{
|
|
|
|
int numJoysticks = 0;
|
|
|
|
|
|
|
|
// This is a workaround to exclude the keyboard (ID -1) from the total joystick count.
|
|
|
|
// It's incorrectly added when configuring the keyboard in GuiInputConfig, but I've
|
|
|
|
// been unable to find a proper fix to not having it added to mJoysticks.
|
|
|
|
for (auto it = mJoysticks.cbegin(); it != mJoysticks.cend(); it++) {
|
|
|
|
if ((*it).first >= 0)
|
|
|
|
numJoysticks += 1;
|
|
|
|
}
|
|
|
|
return numJoysticks;
|
|
|
|
}
|
2019-04-06 09:02:15 +00:00
|
|
|
|
|
|
|
int InputManager::getAxisCountByDevice(SDL_JoystickID id)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return SDL_JoystickNumAxes(mJoysticks[id]);
|
2019-04-06 09:02:15 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
int InputManager::getButtonCountByDevice(SDL_JoystickID id)
|
2013-04-13 23:10:23 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (id == DEVICE_KEYBOARD)
|
|
|
|
return 120; // It's a lot, okay.
|
|
|
|
else if (id == DEVICE_CEC)
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(HAVE_CECLIB)
|
2020-06-21 12:25:28 +00:00
|
|
|
return CEC::CEC_USER_CONTROL_CODE_MAX;
|
2020-07-24 16:24:04 +00:00
|
|
|
#else // HAVE_LIBCEF
|
2020-06-21 12:25:28 +00:00
|
|
|
return 0;
|
2020-07-24 16:24:04 +00:00
|
|
|
#endif // HAVE_CECLIB
|
2020-06-21 12:25:28 +00:00
|
|
|
else
|
|
|
|
return SDL_JoystickNumButtons(mJoysticks[id]);
|
2013-04-13 23:10:23 +00:00
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
InputConfig* InputManager::getInputConfigByDevice(int device)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (device == DEVICE_KEYBOARD)
|
|
|
|
return mKeyboardInputConfig;
|
|
|
|
else if (device == DEVICE_CEC)
|
|
|
|
return mCECInputConfig;
|
|
|
|
else
|
|
|
|
return mInputConfigs[device];
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2014-04-18 18:07:32 +00:00
|
|
|
bool InputManager::parseEvent(const SDL_Event& ev, Window* window)
|
2013-01-08 02:24:59 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
bool causedEvent = false;
|
2020-07-24 16:24:04 +00:00
|
|
|
int32_t axisValue;
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
switch (ev.type) {
|
|
|
|
case SDL_JOYAXISMOTION:
|
2020-08-24 16:59:04 +00:00
|
|
|
// This should hopefully prevent a potential crash if the controller was unplugged
|
|
|
|
// while a game was running.
|
|
|
|
if (!mInputConfigs[ev.jaxis.which])
|
|
|
|
return false;
|
|
|
|
|
2020-07-24 16:24:04 +00:00
|
|
|
axisValue = ev.jaxis.value;
|
|
|
|
// For the analog trigger buttons, convert the negative<->positive axis values to only
|
|
|
|
// positive values in order to avoid registering double inputs. This is only a
|
|
|
|
// temporary solution until ES has been updated to use the SDL GameController API.
|
|
|
|
if (ev.jaxis.axis == mInputConfigs[ev.jaxis.which]->getInputIDByName("lefttrigger") ||
|
|
|
|
ev.jaxis.axis == mInputConfigs[ev.jaxis.which]->getInputIDByName("righttrigger")) {
|
|
|
|
axisValue += 32768;
|
|
|
|
axisValue /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the input value switched boundaries.
|
|
|
|
if ((abs(axisValue) > DEADZONE) !=
|
2020-06-21 12:25:28 +00:00
|
|
|
(abs(mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis]) > DEADZONE)) {
|
|
|
|
int normValue;
|
2020-07-24 16:24:04 +00:00
|
|
|
if (abs(axisValue) <= DEADZONE) {
|
2020-06-21 12:25:28 +00:00
|
|
|
normValue = 0;
|
2020-07-24 16:24:04 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (axisValue > 0)
|
2020-06-21 12:25:28 +00:00
|
|
|
normValue = 1;
|
|
|
|
else
|
|
|
|
normValue = -1;
|
2020-07-24 16:24:04 +00:00
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
window->input(getInputConfigByDevice(ev.jaxis.which), Input(ev.jaxis.which,
|
|
|
|
TYPE_AXIS, ev.jaxis.axis, normValue, false));
|
|
|
|
causedEvent = true;
|
|
|
|
}
|
|
|
|
|
2020-07-24 16:24:04 +00:00
|
|
|
mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis] = axisValue;
|
2020-06-21 12:25:28 +00:00
|
|
|
return causedEvent;
|
|
|
|
|
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
|
|
|
|
case SDL_JOYBUTTONUP:
|
2020-08-24 16:59:04 +00:00
|
|
|
if (!mInputConfigs[ev.jaxis.which])
|
|
|
|
return false;
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
window->input(getInputConfigByDevice(ev.jbutton.which), Input(ev.jbutton.which,
|
|
|
|
TYPE_BUTTON, ev.jbutton.button, ev.jbutton.state == SDL_PRESSED, false));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_JOYHATMOTION:
|
2020-08-24 16:59:04 +00:00
|
|
|
if (!mInputConfigs[ev.jaxis.which])
|
|
|
|
return false;
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
window->input(getInputConfigByDevice(ev.jhat.which), Input(ev.jhat.which,
|
|
|
|
TYPE_HAT, ev.jhat.hat, ev.jhat.value, false));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if (ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive())
|
|
|
|
window->textInput("\b");
|
|
|
|
|
|
|
|
if (ev.key.repeat)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ev.key.keysym.sym == SDLK_F4) {
|
2020-10-17 12:32:08 +00:00
|
|
|
SDL_Event quit;
|
|
|
|
quit.type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(&quit);
|
2020-06-21 12:25:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD,
|
|
|
|
TYPE_KEY, ev.key.keysym.sym, 1, false));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD,
|
|
|
|
TYPE_KEY, ev.key.keysym.sym, 0, false));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_TEXTINPUT:
|
|
|
|
window->textInput(ev.text.text);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYDEVICEADDED:
|
|
|
|
// ev.jdevice.which is a device index.
|
|
|
|
addJoystickByDeviceIndex(ev.jdevice.which);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_JOYDEVICEREMOVED:
|
|
|
|
// ev.jdevice.which is an SDL_JoystickID (instance ID).
|
|
|
|
removeJoystickByJoystickID(ev.jdevice.which);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-17 22:06:54 +00:00
|
|
|
if ((ev.type == static_cast<unsigned int>(SDL_USER_CECBUTTONDOWN)) ||
|
|
|
|
(ev.type == static_cast<unsigned int>(SDL_USER_CECBUTTONUP))) {
|
2020-06-21 12:25:28 +00:00
|
|
|
window->input(getInputConfigByDevice(DEVICE_CEC), Input(DEVICE_CEC,
|
|
|
|
TYPE_CEC_BUTTON, ev.user.code, ev.type ==
|
2020-11-17 22:06:54 +00:00
|
|
|
static_cast<unsigned int>(SDL_USER_CECBUTTONDOWN), false));
|
2020-06-21 12:25:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-04-08 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
bool InputManager::loadInputConfig(InputConfig* config)
|
2013-04-08 16:52:40 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string path = getConfigPath();
|
2020-07-14 17:16:21 +00:00
|
|
|
if (!Utils::FileSystem::exists(path)) {
|
|
|
|
if (config->getDeviceName() == "Keyboard") {
|
2021-01-06 23:14:45 +00:00
|
|
|
LOG(LogDebug) << "InputManager::loadInputConfig(): Will assign default keyboard "
|
2020-12-23 16:56:21 +00:00
|
|
|
"mappings as there is no es_input.cfg configuration file";
|
2020-07-14 17:16:21 +00:00
|
|
|
loadDefaultKBConfig();
|
|
|
|
config->setDefaultConfigFlag();
|
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
return false;
|
2020-07-14 17:16:21 +00:00
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
pugi::xml_document doc;
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
pugi::xml_parse_result res = doc.load_file(Utils::String::stringToWideString(path).c_str());
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
pugi::xml_parse_result res = doc.load_file(path.c_str());
|
2020-07-10 16:32:23 +00:00
|
|
|
#endif
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
LOG(LogError) << "Error parsing input config: " << res.description();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
if (!root)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pugi::xml_node configNode = root.find_child_by_attribute("inputConfig",
|
|
|
|
"deviceGUID", config->getDeviceGUIDString().c_str());
|
|
|
|
if (!configNode)
|
|
|
|
configNode = root.find_child_by_attribute("inputConfig",
|
|
|
|
"deviceName", config->getDeviceName().c_str());
|
2020-07-14 17:16:21 +00:00
|
|
|
if (!configNode) {
|
|
|
|
if (config->getDeviceName() == "Keyboard") {
|
|
|
|
LOG(LogDebug) << "InputManager::loadInputConfig(): Assigning default keyboard "
|
|
|
|
"mappings as there is no keyboard configuration in es_input.cfg.";
|
|
|
|
loadDefaultKBConfig();
|
|
|
|
config->setDefaultConfigFlag();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
config->loadFromXML(configNode);
|
|
|
|
return true;
|
2013-04-13 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:16:21 +00:00
|
|
|
// If there is no es_input.cfg file or if the user has not yet configured the keyboard
|
|
|
|
// mappings, then load these defaults.
|
2014-03-22 01:12:57 +00:00
|
|
|
void InputManager::loadDefaultKBConfig()
|
2013-04-13 18:19:06 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
InputConfig* cfg = getInputConfigByDevice(DEVICE_KEYBOARD);
|
|
|
|
|
|
|
|
cfg->clear();
|
|
|
|
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));
|
2020-08-18 15:48:21 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
cfg->mapInput("y", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PRINTSCREEN, 1, true));
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
cfg->mapInput("y", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_INSERT, 1, true));
|
2020-08-18 15:48:21 +00:00
|
|
|
#endif
|
2020-06-21 12:25:28 +00:00
|
|
|
cfg->mapInput("start", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_ESCAPE, 1, true));
|
|
|
|
cfg->mapInput("select", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 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("lefttrigger", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_HOME, 1, true));
|
|
|
|
cfg->mapInput("righttrigger", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_END, 1, true));
|
2013-04-11 22:27:27 +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(): "
|
|
|
|
"Saving input configuration file to " << path;
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
pugi::xml_document doc;
|
|
|
|
|
|
|
|
if (Utils::FileSystem::exists(path)) {
|
|
|
|
// Merge files.
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
pugi::xml_parse_result result =
|
|
|
|
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());
|
2020-07-10 16:32:23 +00:00
|
|
|
#endif
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!result) {
|
|
|
|
LOG(LogError) << "Error parsing input config: " << result.description();
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
pugi::xml_node actionnode = root.find_child_by_attribute("inputAction",
|
|
|
|
"type", "onfinish");
|
|
|
|
if (actionnode) {
|
|
|
|
path = getTemporaryConfigPath();
|
|
|
|
doc.reset();
|
|
|
|
root = doc.append_child("inputList");
|
|
|
|
root.append_copy(actionnode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pugi::xml_node oldEntry = root.find_child_by_attribute("inputConfig",
|
|
|
|
"deviceGUID", config->getDeviceGUIDString().c_str());
|
|
|
|
if (oldEntry)
|
|
|
|
root.remove_child(oldEntry);
|
|
|
|
oldEntry = root.find_child_by_attribute("inputConfig", "deviceName",
|
|
|
|
config->getDeviceName().c_str());
|
|
|
|
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
|
|
|
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
doc.save_file(Utils::String::stringToWideString(path).c_str());
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
doc.save_file(path.c_str());
|
2020-07-10 16:32:23 +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();
|
|
|
|
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)) {
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-10 16:32:23 +00:00
|
|
|
pugi::xml_parse_result result =
|
|
|
|
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());
|
2020-07-10 16:32:23 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!result) {
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "Couldn't parse input config: " << 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;
|
|
|
|
command = command.next_sibling("command")) {
|
|
|
|
std::string tocall = command.text().get();
|
|
|
|
|
|
|
|
LOG(LogInfo) << " " << tocall;
|
|
|
|
std::cout << "==============================================\n"
|
|
|
|
"input config finish command:\n";
|
|
|
|
int exitCode = runSystemCommand(tocall);
|
|
|
|
std::cout << "==============================================\n";
|
|
|
|
|
2020-06-25 17:52:38 +00:00
|
|
|
if (exitCode != 0) {
|
2020-06-21 12:25:28 +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();
|
|
|
|
path += "/.emulationstation/es_input.cfg";
|
|
|
|
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();
|
|
|
|
path += "/.emulationstation/es_temporaryinput.cfg";
|
|
|
|
return path;
|
2015-04-19 09:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
bool InputManager::initialized() const
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
return mKeyboardInputConfig != nullptr;
|
2013-08-19 14:05:30 +00:00
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
int InputManager::getNumConfiguredDevices()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
int num = 0;
|
|
|
|
for (auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); it++) {
|
|
|
|
if (it->second->isConfigured())
|
|
|
|
num++;
|
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mKeyboardInputConfig->isConfigured())
|
|
|
|
num++;
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mCECInputConfig->isConfigured())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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!";
|
2020-06-21 12:25:28 +00:00
|
|
|
return "something went horribly wrong";
|
|
|
|
}
|
|
|
|
|
|
|
|
char guid[65];
|
|
|
|
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(it->second), guid, 65);
|
|
|
|
return std::string(guid);
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|