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"
|
2012-07-20 01:08:29 +00:00
|
|
|
#include <iostream>
|
2013-04-11 22:27:27 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "pugiXML/pugixml.hpp"
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
2012-12-20 18:29:05 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
InputManager::InputManager(Window* window) : mWindow(window)
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
mJoysticks = NULL;
|
|
|
|
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-04-08 14:41:25 +00:00
|
|
|
if(mJoysticks != NULL)
|
|
|
|
deinit();
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +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();
|
|
|
|
mJoysticks = new SDL_Joystick*[mNumJoysticks];
|
|
|
|
mInputConfigs = new InputConfig*[mNumJoysticks];
|
|
|
|
mPrevAxisValues = new std::map<int, int>[mNumJoysticks];
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
for(int i = 0; i < mNumJoysticks; i++)
|
|
|
|
{
|
|
|
|
mJoysticks[i] = SDL_JoystickOpen(i);
|
|
|
|
mInputConfigs[i] = new InputConfig(i);
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
for(int k = 0; k < SDL_JoystickNumAxes(mJoysticks[i]); k++)
|
2012-07-22 21:15:55 +00:00
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
mPrevAxisValues[i][k] = 0;
|
2012-07-22 21:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
mKeyboardInputConfig = new InputConfig(DEVICE_KEYBOARD);
|
2013-03-25 13:16:54 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
SDL_JoystickEventState(SDL_ENABLE);
|
2013-04-10 17:29:07 +00:00
|
|
|
|
2013-04-11 22:27:27 +00:00
|
|
|
loadConfig();
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
void InputManager::deinit()
|
2012-07-22 21:15:55 +00:00
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
SDL_JoystickEventState(SDL_DISABLE);
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
if(!SDL_WasInit(SDL_INIT_JOYSTICK))
|
|
|
|
return;
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
if(mJoysticks != NULL)
|
2012-07-22 21:15:55 +00:00
|
|
|
{
|
2013-04-08 14:41:25 +00:00
|
|
|
for(int i = 0; i < mNumJoysticks; i++)
|
|
|
|
{
|
|
|
|
SDL_JoystickClose(mJoysticks[i]);
|
|
|
|
delete mInputConfigs[i];
|
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
delete mKeyboardInputConfig;
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
delete[] mJoysticks;
|
|
|
|
delete[] mInputConfigs;
|
|
|
|
delete[] mPrevAxisValues;
|
|
|
|
mJoysticks = NULL;
|
|
|
|
mKeyboardInputConfig = NULL;
|
|
|
|
mInputConfigs = NULL;
|
|
|
|
}
|
2012-07-22 21:15:55 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
|
|
|
}
|
2012-09-14 18:22:01 +00:00
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
int InputManager::getNumJoysticks() { return mNumJoysticks; }
|
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-04-08 14:41:25 +00:00
|
|
|
InputConfig* InputManager::getInputConfigByDevice(int device)
|
|
|
|
{
|
|
|
|
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-04-08 14:41:25 +00:00
|
|
|
for(int i = 0; i < mNumJoysticks; i++)
|
|
|
|
{
|
|
|
|
if(mInputConfigs[i]->getPlayerNum() == player)
|
|
|
|
return mInputConfigs[i];
|
2012-07-22 21:15:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
std::cout << "Could not find input config for player number " << player << "!\n";
|
|
|
|
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:
|
|
|
|
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-04-08 14:41:25 +00:00
|
|
|
}
|
2013-04-08 16:52:40 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::loadConfig()
|
|
|
|
{
|
2013-04-11 22:27:27 +00:00
|
|
|
if(!mJoysticks)
|
|
|
|
{
|
|
|
|
std::cout << "ERROR - cannot load config without being initialized!\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
pugi::xml_node root = doc.child("inputList");
|
|
|
|
|
|
|
|
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);
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if(SDL_JoystickName(i) == devName)
|
|
|
|
{
|
|
|
|
mInputConfigs[i]->loadFromXML(node, mNumPlayers);
|
|
|
|
mNumPlayers++;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
LOG(LogWarning) << "Could not find joystick named \"" << devName << "\"! Skipping it.\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
LOG(LogWarning) << "Device type \"" << type << "\" unknown!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputManager::writeConfig()
|
|
|
|
{
|
|
|
|
if(!mJoysticks)
|
|
|
|
{
|
|
|
|
std::cout << "ERROR - cannot write config without being initialized!\n";
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
std::string path = getenv("HOME");
|
|
|
|
path += "/.emulationstation/es_input.cfg";
|
|
|
|
return path;
|
|
|
|
}
|