2012-07-20 01:08:29 +00:00
|
|
|
#include "InputManager.h"
|
2013-04-08 14:41:25 +00:00
|
|
|
#include "InputConfig.h"
|
|
|
|
#include "Window.h"
|
2013-04-11 22:27:27 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "pugiXML/pugixml.hpp"
|
|
|
|
#include <boost/filesystem.hpp>
|
2013-05-13 19:53:28 +00:00
|
|
|
#include "platform.h"
|
2013-04-11 22:27:27 +00:00
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
2012-12-20 18:29:05 +00:00
|
|
|
|
2013-05-14 19:45:56 +00:00
|
|
|
InputManager::InputManager(Window* window) : mWindow(window),
|
2013-08-19 14:05:30 +00:00
|
|
|
mKeyboardInputConfig(NULL),
|
|
|
|
mNumJoysticks(0), mNumPlayers(0)
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
mNumJoysticks = SDL_NumJoysticks();
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
for(int i = 0; i < mNumJoysticks; i++)
|
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_Joystick* joy = SDL_JoystickOpen(i);
|
|
|
|
SDL_JoystickID joyId = SDL_JoystickInstanceID(joy);
|
|
|
|
mJoysticks.push_back(joy);
|
|
|
|
mInputConfigs[joyId] = new InputConfig(i, SDL_JoystickName(joy));
|
|
|
|
|
|
|
|
int numAxes = SDL_JoystickNumAxes(joy);
|
|
|
|
mPrevAxisValues[joyId] = new int[numAxes];
|
|
|
|
std::fill(mPrevAxisValues[joyId], mPrevAxisValues[joyId] + numAxes, 0); //initialize array to 0
|
2012-07-22 21:15:55 +00:00
|
|
|
}
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2013-08-18 17:17:24 +00:00
|
|
|
mKeyboardInputConfig = new InputConfig(DEVICE_KEYBOARD, "Keyboard");
|
2013-03-25 13:16:54 +00:00
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
loadConfig();
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_JoystickEventState(SDL_ENABLE);
|
2013-06-30 01:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::deinit()
|
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
SDL_JoystickEventState(SDL_DISABLE);
|
2012-07-22 21:15:55 +00:00
|
|
|
|
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
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
for(auto iter = mJoysticks.begin(); iter != mJoysticks.end(); iter++)
|
2012-07-22 21:15:55 +00:00
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_JoystickClose(*iter);
|
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
mJoysticks.clear();
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
for(auto iter = mInputConfigs.begin(); iter != mInputConfigs.end(); iter++)
|
|
|
|
{
|
|
|
|
delete iter->second;
|
|
|
|
}
|
2013-05-14 19:45:56 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
mInputConfigs.clear();
|
2013-05-14 19:45:56 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
for(auto iter = mPrevAxisValues.begin(); iter != mPrevAxisValues.end(); iter++)
|
|
|
|
{
|
|
|
|
delete[] iter->second;
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-07-22 21:15:55 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
int InputManager::getNumJoysticks() { return mNumJoysticks; }
|
2013-04-13 23:10:23 +00:00
|
|
|
int InputManager::getButtonCountByDevice(int id)
|
|
|
|
{
|
|
|
|
if(id == DEVICE_KEYBOARD)
|
|
|
|
return 120; //it's a lot, okay.
|
|
|
|
else
|
|
|
|
return SDL_JoystickNumButtons(mJoysticks[id]);
|
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
int InputManager::getNumPlayers() { return mNumPlayers; }
|
|
|
|
void InputManager::setNumPlayers(int num) { mNumPlayers = num; }
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
InputConfig* InputManager::getInputConfigByDevice(SDL_JoystickID device)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
|
|
|
if(device == DEVICE_KEYBOARD)
|
|
|
|
return mKeyboardInputConfig;
|
|
|
|
else
|
|
|
|
return mInputConfigs[device];
|
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
InputConfig* InputManager::getInputConfigByPlayer(int player)
|
|
|
|
{
|
|
|
|
if(mKeyboardInputConfig->getPlayerNum() == player)
|
|
|
|
return mKeyboardInputConfig;
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
for(auto iter = mInputConfigs.begin(); iter != mInputConfigs.end(); iter++)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
if(iter->second->getPlayerNum() == player)
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 18:19:06 +00:00
|
|
|
LOG(LogError) << "Could not find input config for player number " << player << "!";
|
2013-04-08 14:41:25 +00:00
|
|
|
return NULL;
|
2013-01-08 02:24:59 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 16:52:40 +00:00
|
|
|
bool InputManager::parseEvent(const SDL_Event& ev)
|
2013-01-08 02:24:59 +00:00
|
|
|
{
|
2013-04-08 16:52:40 +00:00
|
|
|
bool causedEvent = false;
|
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:
|
|
|
|
//if it switched boundaries
|
|
|
|
if((abs(ev.jaxis.value) > 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;
|
|
|
|
if(abs(ev.jaxis.value) <= DEADZONE)
|
|
|
|
normValue = 0;
|
|
|
|
else
|
|
|
|
if(ev.jaxis.value > 0)
|
|
|
|
normValue = 1;
|
|
|
|
else
|
|
|
|
normValue = -1;
|
|
|
|
|
|
|
|
mWindow->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:
|
|
|
|
mWindow->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:
|
|
|
|
mWindow->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())
|
|
|
|
{
|
|
|
|
if(mWindow->peekGui() != NULL)
|
|
|
|
mWindow->peekGui()->textInput("\b");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
mWindow->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:
|
|
|
|
mWindow->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:
|
|
|
|
if(mWindow->peekGui() != NULL)
|
|
|
|
mWindow->peekGui()->textInput(ev.text.text);
|
|
|
|
break;
|
|
|
|
|
2013-08-19 14:05:30 +00:00
|
|
|
case SDL_JOYDEVICEADDED:
|
|
|
|
deinit();
|
|
|
|
init();
|
|
|
|
return true;
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
2013-04-08 16:52:40 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::loadConfig()
|
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
if(!initialized())
|
2013-04-11 22:27:27 +00:00
|
|
|
{
|
2013-04-13 18:19:06 +00:00
|
|
|
LOG(LogError) << "ERROR - cannot load InputManager config without being initialized!";
|
2013-04-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
if(!fs::exists(path))
|
|
|
|
return;
|
2013-04-08 16:52:40 +00:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error loading input config: " << res.description();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mNumPlayers = 0;
|
|
|
|
|
2013-05-13 19:53:28 +00:00
|
|
|
bool* configuredDevice = new bool[mNumJoysticks];
|
2013-08-19 14:05:30 +00:00
|
|
|
std::fill(configuredDevice, configuredDevice + mNumJoysticks, false);
|
|
|
|
|
|
|
|
for(auto iter = mInputConfigs.begin(); iter != mInputConfigs.end(); iter++)
|
2013-04-13 18:19:06 +00:00
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
iter->second->setPlayerNum(-1);
|
2013-04-13 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
|
2014-01-25 00:29:53 +00:00
|
|
|
bool loadedKeyboard = false;
|
2013-04-11 22:27:27 +00:00
|
|
|
for(pugi::xml_node node = root.child("inputConfig"); node; node = node.next_sibling("inputConfig"))
|
|
|
|
{
|
|
|
|
std::string type = node.attribute("type").as_string();
|
|
|
|
|
|
|
|
if(type == "keyboard")
|
|
|
|
{
|
|
|
|
getInputConfigByDevice(DEVICE_KEYBOARD)->loadFromXML(node, mNumPlayers);
|
2014-01-25 00:29:53 +00:00
|
|
|
loadedKeyboard = true;
|
2013-04-11 22:27:27 +00:00
|
|
|
mNumPlayers++;
|
|
|
|
}else if(type == "joystick")
|
|
|
|
{
|
|
|
|
bool found = false;
|
2013-04-12 02:59:19 +00:00
|
|
|
std::string devName = node.attribute("deviceName").as_string();
|
2013-04-11 22:27:27 +00:00
|
|
|
for(int i = 0; i < mNumJoysticks; i++)
|
|
|
|
{
|
2013-08-18 17:17:24 +00:00
|
|
|
if(!configuredDevice[i] && SDL_JoystickName(mJoysticks[i]) == devName)
|
2013-04-11 22:27:27 +00:00
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
SDL_JoystickID joyId = SDL_JoystickInstanceID(mJoysticks[i]);
|
|
|
|
mInputConfigs[joyId]->loadFromXML(node, mNumPlayers);
|
2013-04-11 22:27:27 +00:00
|
|
|
mNumPlayers++;
|
|
|
|
found = true;
|
2013-04-13 18:19:06 +00:00
|
|
|
configuredDevice[i] = true;
|
2013-04-11 22:27:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found)
|
|
|
|
{
|
2013-04-13 18:19:06 +00:00
|
|
|
LOG(LogWarning) << "Could not find unconfigured joystick named \"" << devName << "\"! Skipping it.\n";
|
2013-04-11 22:27:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
LOG(LogWarning) << "Device type \"" << type << "\" unknown!\n";
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 18:19:06 +00:00
|
|
|
|
2013-05-13 19:53:28 +00:00
|
|
|
delete[] configuredDevice;
|
|
|
|
|
2014-01-25 00:29:53 +00:00
|
|
|
if(!loadedKeyboard)
|
2013-04-13 18:19:06 +00:00
|
|
|
{
|
2014-01-25 00:29:53 +00:00
|
|
|
LOG(LogInfo) << "No keyboard input found. Loading default keyboard config.";
|
2013-04-13 18:19:06 +00:00
|
|
|
loadDefaultConfig();
|
|
|
|
}
|
2013-06-30 01:37:18 +00:00
|
|
|
|
|
|
|
LOG(LogInfo) << "Loaded InputConfig data for " << getNumPlayers() << " devices.";
|
2013-04-13 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//used in an "emergency" where no configs could be loaded from the inputmanager config file
|
|
|
|
//allows the user to select to reconfigure in menus if this happens without having to delete es_input.cfg manually
|
|
|
|
void InputManager::loadDefaultConfig()
|
|
|
|
{
|
|
|
|
InputConfig* cfg = getInputConfigByDevice(DEVICE_KEYBOARD);
|
|
|
|
|
|
|
|
mNumPlayers++;
|
|
|
|
cfg->setPlayerNum(0);
|
|
|
|
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));
|
|
|
|
cfg->mapInput("menu", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F1, 1, true));
|
|
|
|
cfg->mapInput("select", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_F2, 1, true));
|
2013-04-13 23:10:23 +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-05-23 09:43:50 +00:00
|
|
|
|
|
|
|
cfg->mapInput("mastervolup", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_PLUS, 1, true));
|
|
|
|
cfg->mapInput("mastervoldown", Input(DEVICE_KEYBOARD, TYPE_KEY, SDLK_MINUS, 1, true));
|
2013-04-11 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::writeConfig()
|
|
|
|
{
|
2013-08-19 14:05:30 +00:00
|
|
|
if(!initialized())
|
2013-04-11 22:27:27 +00:00
|
|
|
{
|
2013-04-13 18:19:06 +00:00
|
|
|
LOG(LogError) << "ERROR - cannot write config without being initialized!";
|
2013-04-11 22:27:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path = getConfigPath();
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
|
|
|
pugi::xml_node root = doc.append_child("inputList");
|
|
|
|
|
|
|
|
mKeyboardInputConfig->writeToXML(root);
|
|
|
|
for(int i = 0; i < mNumJoysticks; i++)
|
|
|
|
{
|
|
|
|
mInputConfigs[i]->writeToXML(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.save_file(path.c_str());
|
2012-07-23 23:53:33 +00:00
|
|
|
}
|
2013-04-08 16:52:40 +00:00
|
|
|
|
|
|
|
std::string InputManager::getConfigPath()
|
|
|
|
{
|
2013-05-13 19:53:28 +00:00
|
|
|
std::string path = getHomePath();
|
2013-04-08 16:52:40 +00:00
|
|
|
path += "/.emulationstation/es_input.cfg";
|
|
|
|
return path;
|
|
|
|
}
|
2013-08-19 14:05:30 +00:00
|
|
|
|
|
|
|
bool InputManager::initialized() const
|
|
|
|
{
|
|
|
|
return mKeyboardInputConfig != NULL;
|
|
|
|
}
|