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"
|
2017-11-08 22:22:15 +00:00
|
|
|
#include "CECInput.h"
|
2013-04-11 22:27:27 +00:00
|
|
|
#include "Log.h"
|
2013-05-13 19:53:28 +00:00
|
|
|
#include "platform.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Window.h"
|
2017-11-10 19:16:42 +00:00
|
|
|
#include <pugixml/src/pugixml.hpp>
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <iostream>
|
2017-12-04 23:30:25 +00:00
|
|
|
#include <assert.h>
|
2013-04-11 22:27:27 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
#define KEYBOARD_GUID_STRING "-1"
|
2017-11-08 22:22:15 +00:00
|
|
|
#define CEC_GUID_STRING "-2"
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
// SO HEY POTENTIAL POOR SAP WHO IS TRYING TO MAKE SENSE OF ALL THIS (by which I mean my future self)
|
|
|
|
// There are like four distinct IDs used for joysticks (crazy, right?)
|
|
|
|
// 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.
|
|
|
|
// 3. "Device ID" - this is something I made up and is what InputConfig's getDeviceID() returns.
|
|
|
|
// 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.
|
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
// hack for cec support
|
|
|
|
int SDL_USER_CECBUTTONDOWN = -1;
|
|
|
|
int SDL_USER_CECBUTTONUP = -1;
|
|
|
|
|
2014-04-18 18:07:32 +00:00
|
|
|
InputManager* InputManager::mInstance = NULL;
|
|
|
|
|
|
|
|
InputManager::InputManager() : mKeyboardInputConfig(NULL)
|
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
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
deinit();
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:07:32 +00:00
|
|
|
InputManager* InputManager::getInstance()
|
|
|
|
{
|
|
|
|
if(!mInstance)
|
|
|
|
mInstance = new InputManager();
|
|
|
|
|
|
|
|
return mInstance;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
void InputManager::init()
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
if(initialized())
|
2013-04-08 14:41:25 +00:00
|
|
|
deinit();
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2015-01-15 23:47:31 +00:00
|
|
|
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
|
|
|
|
Settings::getInstance()->getBool("BackgroundJoystickInput") ? "1" : "0");
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
2014-03-22 01:12:57 +00:00
|
|
|
SDL_JoystickEventState(SDL_ENABLE);
|
|
|
|
|
|
|
|
// first, open all currently present joysticks
|
|
|
|
int numJoysticks = SDL_NumJoysticks();
|
|
|
|
for(int i = 0; i < numJoysticks; i++)
|
|
|
|
{
|
|
|
|
addJoystickByDeviceIndex(i);
|
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
mKeyboardInputConfig = new InputConfig(DEVICE_KEYBOARD, "Keyboard", KEYBOARD_GUID_STRING);
|
|
|
|
loadInputConfig(mKeyboardInputConfig);
|
2017-11-08 22:22:15 +00:00
|
|
|
|
|
|
|
SDL_USER_CECBUTTONDOWN = SDL_RegisterEvents(2);
|
|
|
|
SDL_USER_CECBUTTONUP = SDL_USER_CECBUTTONDOWN + 1;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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]))
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
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 << ")";
|
2012-07-22 21:15:55 +00:00
|
|
|
}
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
// set up the prevAxisValues
|
|
|
|
int numAxes = SDL_JoystickNumAxes(joy);
|
|
|
|
mPrevAxisValues[joyId] = new int[numAxes];
|
2018-06-08 04:40:02 +00:00
|
|
|
mInitAxisValues[joyId] = new int[numAxes];
|
|
|
|
|
|
|
|
int axis;
|
|
|
|
for (int i = 0; i< numAxes; i++) {
|
|
|
|
axis = SDL_JoystickGetAxis(joy, i);
|
|
|
|
mInitAxisValues[joyId][i] = axis;
|
|
|
|
mPrevAxisValues[joyId][i] = axis;
|
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::removeJoystickByJoystickID(SDL_JoystickID joyId)
|
|
|
|
{
|
|
|
|
assert(joyId != -1);
|
2013-03-25 13:16:54 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
// delete old prevAxisValues
|
|
|
|
auto axisIt = mPrevAxisValues.find(joyId);
|
|
|
|
delete[] axisIt->second;
|
|
|
|
mPrevAxisValues.erase(axisIt);
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
// delete old InputConfig
|
|
|
|
auto it = mInputConfigs.find(joyId);
|
|
|
|
delete it->second;
|
|
|
|
mInputConfigs.erase(it);
|
|
|
|
|
|
|
|
// close the joystick
|
|
|
|
auto joyIt = mJoysticks.find(joyId);
|
2017-11-11 14:56:22 +00:00
|
|
|
if(joyIt != mJoysticks.cend())
|
2014-03-22 01:12:57 +00:00
|
|
|
{
|
|
|
|
SDL_JoystickClose(joyIt->second);
|
|
|
|
mJoysticks.erase(joyIt);
|
|
|
|
}else{
|
|
|
|
LOG(LogError) << "Could not find joystick to close (instance ID: " << joyId << ")";
|
|
|
|
}
|
2013-06-30 01:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::deinit()
|
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
if(!initialized())
|
2013-04-08 14:41:25 +00:00
|
|
|
return;
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto iter = mJoysticks.cbegin(); iter != mJoysticks.cend(); iter++)
|
2012-07-22 21:15:55 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
SDL_JoystickClose(iter->second);
|
2013-08-19 14:05:30 +00:00
|
|
|
}
|
|
|
|
mJoysticks.clear();
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto iter = mInputConfigs.cbegin(); iter != mInputConfigs.cend(); iter++)
|
2013-08-19 14:05:30 +00:00
|
|
|
{
|
|
|
|
delete iter->second;
|
|
|
|
}
|
|
|
|
mInputConfigs.clear();
|
2013-05-14 19:45:56 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto iter = mPrevAxisValues.cbegin(); iter != mPrevAxisValues.cend(); iter++)
|
2013-08-19 14:05:30 +00:00
|
|
|
{
|
|
|
|
delete[] iter->second;
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2013-08-19 14:05:30 +00:00
|
|
|
mPrevAxisValues.clear();
|
2013-05-24 11:44:40 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
if(mKeyboardInputConfig != NULL)
|
|
|
|
{
|
|
|
|
delete mKeyboardInputConfig;
|
|
|
|
mKeyboardInputConfig = NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
if(mCECInputConfig != NULL)
|
|
|
|
{
|
|
|
|
delete mCECInputConfig;
|
|
|
|
mCECInputConfig = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CECInput::deinit();
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
SDL_JoystickEventState(SDL_DISABLE);
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
int InputManager::getNumJoysticks() { return (int)mJoysticks.size(); }
|
2014-03-22 01:12:57 +00:00
|
|
|
int InputManager::getButtonCountByDevice(SDL_JoystickID id)
|
2013-04-13 23:10:23 +00:00
|
|
|
{
|
|
|
|
if(id == DEVICE_KEYBOARD)
|
|
|
|
return 120; //it's a lot, okay.
|
2017-11-08 22:22:15 +00:00
|
|
|
else if(id == DEVICE_CEC)
|
|
|
|
#ifdef HAVE_CECLIB
|
|
|
|
return CEC::CEC_USER_CONTROL_CODE_MAX;
|
|
|
|
#else // HAVE_LIBCEF
|
|
|
|
return 0;
|
|
|
|
#endif // HAVE_CECLIB
|
2013-04-13 23:10:23 +00:00
|
|
|
else
|
|
|
|
return SDL_JoystickNumButtons(mJoysticks[id]);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
if(device == DEVICE_KEYBOARD)
|
|
|
|
return mKeyboardInputConfig;
|
2017-11-08 22:22:15 +00:00
|
|
|
else if(device == DEVICE_CEC)
|
|
|
|
return mCECInputConfig;
|
2013-04-08 14:41:25 +00:00
|
|
|
else
|
|
|
|
return mInputConfigs[device];
|
|
|
|
}
|
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
|
|
|
{
|
2013-04-08 16:52:40 +00:00
|
|
|
bool causedEvent = false;
|
2018-06-08 04:40:02 +00:00
|
|
|
int axis;
|
2013-04-08 14:41:25 +00:00
|
|
|
switch(ev.type)
|
2012-07-22 21:15:55 +00:00
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
case SDL_JOYAXISMOTION:
|
2018-06-08 04:40:02 +00:00
|
|
|
axis = ev.jaxis.value;
|
|
|
|
// Check for ABS_Z/ABS_RZ trigger axes which rest at -32768
|
|
|
|
if ((ev.jaxis.axis == 2 || ev.jaxis.axis == 5) && mInitAxisValues[ev.jaxis.which][ev.jaxis.axis] == -32768)
|
|
|
|
{
|
|
|
|
// shift to 0 - 32767.
|
|
|
|
axis = axis / 2 + 16384;
|
|
|
|
}
|
2013-04-08 14:41:25 +00:00
|
|
|
//if it switched boundaries
|
2018-06-08 04:40:02 +00:00
|
|
|
if((abs(axis) > DEADZONE) != (abs(mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis]) > DEADZONE))
|
2012-09-14 18:22:01 +00:00
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
int normValue;
|
2018-06-08 04:40:02 +00:00
|
|
|
if(abs(axis) <= DEADZONE)
|
2013-04-08 14:41:25 +00:00
|
|
|
normValue = 0;
|
|
|
|
else
|
2018-06-08 04:40:02 +00:00
|
|
|
if(axis > 0)
|
2013-04-08 14:41:25 +00:00
|
|
|
normValue = 1;
|
|
|
|
else
|
|
|
|
normValue = -1;
|
2014-04-18 18:07:32 +00:00
|
|
|
window->input(getInputConfigByDevice(ev.jaxis.which), Input(ev.jaxis.which, TYPE_AXIS, ev.jaxis.axis, normValue, false));
|
2013-04-08 16:52:40 +00:00
|
|
|
causedEvent = true;
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-12-20 18:29:05 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis] = ev.jaxis.value;
|
2013-04-08 16:52:40 +00:00
|
|
|
return causedEvent;
|
2012-12-20 18:29:05 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
case SDL_JOYBUTTONUP:
|
2014-04-18 18:07:32 +00:00
|
|
|
window->input(getInputConfigByDevice(ev.jbutton.which), Input(ev.jbutton.which, TYPE_BUTTON, ev.jbutton.button, ev.jbutton.state == SDL_PRESSED, false));
|
2013-04-08 16:52:40 +00:00
|
|
|
return true;
|
2012-12-20 18:29:05 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
case SDL_JOYHATMOTION:
|
2014-04-18 18:07:32 +00:00
|
|
|
window->input(getInputConfigByDevice(ev.jhat.which), Input(ev.jhat.which, TYPE_HAT, ev.jhat.hat, ev.jhat.value, false));
|
2013-04-08 16:52:40 +00:00
|
|
|
return true;
|
2012-12-20 18:29:05 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
case SDL_KEYDOWN:
|
2013-08-19 15:36:48 +00:00
|
|
|
if(ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive())
|
|
|
|
{
|
2014-04-18 18:07:32 +00:00
|
|
|
window->textInput("\b");
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 17:32:21 +00:00
|
|
|
if(ev.key.repeat)
|
2013-08-19 15:36:48 +00:00
|
|
|
return false;
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
if(ev.key.keysym.sym == SDLK_F4)
|
|
|
|
{
|
|
|
|
SDL_Event* quit = new SDL_Event();
|
|
|
|
quit->type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(quit);
|
2013-04-08 16:52:40 +00:00
|
|
|
return false;
|
2012-09-14 18:22:01 +00:00
|
|
|
}
|
2013-03-29 02:55:29 +00:00
|
|
|
|
2014-04-18 18:07:32 +00:00
|
|
|
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 1, false));
|
2013-04-08 16:52:40 +00:00
|
|
|
return true;
|
2012-07-23 23:53:33 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
case SDL_KEYUP:
|
2014-04-18 18:07:32 +00:00
|
|
|
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 0, false));
|
2013-04-08 16:52:40 +00:00
|
|
|
return true;
|
2013-05-24 11:44:40 +00:00
|
|
|
|
2013-08-19 15:36:48 +00:00
|
|
|
case SDL_TEXTINPUT:
|
2014-04-18 18:07:32 +00:00
|
|
|
window->textInput(ev.text.text);
|
2013-08-19 15:36:48 +00:00
|
|
|
break;
|
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
case SDL_JOYDEVICEADDED:
|
2014-03-22 01:12:57 +00:00
|
|
|
addJoystickByDeviceIndex(ev.jdevice.which); // ev.jdevice.which is a device index
|
2013-08-19 14:05:30 +00:00
|
|
|
return true;
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
case SDL_JOYDEVICEREMOVED:
|
|
|
|
removeJoystickByJoystickID(ev.jdevice.which); // ev.jdevice.which is an SDL_JoystickID (instance ID)
|
|
|
|
return false;
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2013-04-08 16:52:40 +00:00
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
if((ev.type == (unsigned int)SDL_USER_CECBUTTONDOWN) || (ev.type == (unsigned int)SDL_USER_CECBUTTONUP))
|
|
|
|
{
|
|
|
|
window->input(getInputConfigByDevice(DEVICE_CEC), Input(DEVICE_CEC, TYPE_CEC_BUTTON, ev.user.code, ev.type == (unsigned int)SDL_USER_CECBUTTONDOWN, false));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-08 16:52:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
bool InputManager::loadInputConfig(InputConfig* config)
|
2013-04-08 16:52:40 +00:00
|
|
|
{
|
2013-04-11 22:27:27 +00:00
|
|
|
std::string path = getConfigPath();
|
2018-01-09 22:55:09 +00:00
|
|
|
if(!Utils::FileSystem::exists(path))
|
2014-03-22 01:12:57 +00:00
|
|
|
return false;
|
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
pugi::xml_document doc;
|
|
|
|
pugi::xml_parse_result res = doc.load_file(path.c_str());
|
|
|
|
|
|
|
|
if(!res)
|
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
LOG(LogError) << "Error parsing input config: " << res.description();
|
|
|
|
return false;
|
2013-04-13 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
pugi::xml_node root = doc.child("inputList");
|
2014-03-22 01:12:57 +00:00
|
|
|
if(!root)
|
|
|
|
return false;
|
2013-04-11 22:27:27 +00:00
|
|
|
|
2014-03-22 16:43:33 +00:00
|
|
|
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());
|
2014-03-22 01:12:57 +00:00
|
|
|
if(!configNode)
|
|
|
|
return false;
|
2013-04-11 22:27:27 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
config->loadFromXML(configNode);
|
|
|
|
return true;
|
2013-04-13 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
//used in an "emergency" where no keyboard config could be loaded from the inputmanager config file
|
2013-04-13 18:19:06 +00:00
|
|
|
//allows the user to select to reconfigure in menus if this happens without having to delete es_input.cfg manually
|
2014-03-22 01:12:57 +00:00
|
|
|
void InputManager::loadDefaultKBConfig()
|
2013-04-13 18:19:06 +00:00
|
|
|
{
|
|
|
|
InputConfig* cfg = getInputConfigByDevice(DEVICE_KEYBOARD);
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
cfg->clear();
|
2013-04-13 18:19:06 +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_ESCAPE, 1, true));
|
2014-03-22 01:12:57 +00:00
|
|
|
cfg->mapInput("start", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 1, true));
|
2013-04-13 18:19:06 +00:00
|
|
|
cfg->mapInput("select", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F2, 1, true));
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2017-04-04 02:06:07 +00:00
|
|
|
cfg->mapInput("pageup", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_RIGHTBRACKET, 1, true));
|
|
|
|
cfg->mapInput("pagedown", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_LEFTBRACKET, 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
|
|
|
{
|
2016-08-10 01:48:04 +00:00
|
|
|
assert(initialized());
|
|
|
|
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
if(Utils::FileSystem::exists(path))
|
2016-08-10 01:48:04 +00:00
|
|
|
{
|
|
|
|
// merge files
|
|
|
|
pugi::xml_parse_result result = doc.load_file(path.c_str());
|
|
|
|
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 onfinish command take care for 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-11 22:27:27 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
if(!root)
|
|
|
|
root = doc.append_child("inputList");
|
|
|
|
|
|
|
|
config->writeToXML(root);
|
2013-04-11 22:27:27 +00:00
|
|
|
doc.save_file(path.c_str());
|
2016-07-02 05:14:41 +00:00
|
|
|
|
|
|
|
// execute any onFinish commands and re-load the config for changes
|
|
|
|
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()
|
|
|
|
{
|
2016-08-10 01:48:04 +00:00
|
|
|
assert(initialized());
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
if(Utils::FileSystem::exists(path))
|
2016-08-10 01:48:04 +00:00
|
|
|
{
|
|
|
|
pugi::xml_parse_result result = doc.load_file(path.c_str());
|
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error parsing input config: " << result.description();
|
|
|
|
}
|
|
|
|
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 << "==============================================\ninput config finish command:\n";
|
|
|
|
int exitCode = runSystemCommand(tocall);
|
|
|
|
std::cout << "==============================================\n";
|
|
|
|
|
|
|
|
if(exitCode != 0)
|
|
|
|
{
|
|
|
|
LOG(LogWarning) << "...launch terminated with nonzero exit code " << exitCode << "!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 09:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 16:52:40 +00:00
|
|
|
std::string InputManager::getConfigPath()
|
|
|
|
{
|
2018-01-27 20:04:08 +00:00
|
|
|
std::string path = Utils::FileSystem::getHomePath();
|
2013-04-08 16:52:40 +00:00
|
|
|
path += "/.emulationstation/es_input.cfg";
|
|
|
|
return path;
|
|
|
|
}
|
2013-08-19 14:05:30 +00:00
|
|
|
|
2015-04-19 09:57:10 +00:00
|
|
|
std::string InputManager::getTemporaryConfigPath()
|
|
|
|
{
|
2018-01-27 20:04:08 +00:00
|
|
|
std::string path = Utils::FileSystem::getHomePath();
|
2015-04-19 09:57:10 +00:00
|
|
|
path += "/.emulationstation/es_temporaryinput.cfg";
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
bool InputManager::initialized() const
|
|
|
|
{
|
|
|
|
return mKeyboardInputConfig != NULL;
|
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
int InputManager::getNumConfiguredDevices()
|
|
|
|
{
|
|
|
|
int num = 0;
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mInputConfigs.cbegin(); it != mInputConfigs.cend(); it++)
|
2014-03-22 01:12:57 +00:00
|
|
|
{
|
|
|
|
if(it->second->isConfigured())
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mKeyboardInputConfig->isConfigured())
|
|
|
|
num++;
|
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
if(mCECInputConfig->isConfigured())
|
|
|
|
num++;
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InputManager::getDeviceGUIDString(int deviceId)
|
|
|
|
{
|
|
|
|
if(deviceId == DEVICE_KEYBOARD)
|
|
|
|
return KEYBOARD_GUID_STRING;
|
|
|
|
|
2017-11-08 22:22:15 +00:00
|
|
|
if(deviceId == DEVICE_CEC)
|
|
|
|
return CEC_GUID_STRING;
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
auto it = mJoysticks.find(deviceId);
|
2017-11-11 14:56:22 +00:00
|
|
|
if(it == mJoysticks.cend())
|
2014-03-22 01:12:57 +00:00
|
|
|
{
|
|
|
|
LOG(LogError) << "getDeviceGUIDString - deviceId " << deviceId << " not found!";
|
|
|
|
return "something went horribly wrong";
|
|
|
|
}
|
|
|
|
|
|
|
|
char guid[65];
|
|
|
|
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(it->second), guid, 65);
|
|
|
|
return std::string(guid);
|
|
|
|
}
|