1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-05 06:04:06 +02:00

Allow to map actions to keyboard keys and joystick keys at the same time

This commit is contained in:
Paul Cercueil 2014-04-20 16:08:17 +02:00
parent 92a6453ff8
commit 5fa8919f2b
2 changed files with 18 additions and 19 deletions

View File

@ -33,7 +33,8 @@ void InputManager::init(const string &conffile, Menu *menu) {
this->menu = menu; this->menu = menu;
for (int i = 0; i < BUTTON_TYPE_SIZE; i++) { for (int i = 0; i < BUTTON_TYPE_SIZE; i++) {
buttonMap[i].source = UNMAPPED; buttonMap[i].js_mapped = false;
buttonMap[i].kb_mapped = false;
} }
readConfFile(conffile); readConfFile(conffile);
} }
@ -102,20 +103,19 @@ void InputManager::readConfFile(const string &conffile) {
string sourceStr = trim(line.substr(0,pos)); string sourceStr = trim(line.substr(0,pos));
line = trim(line.substr(pos+1, line.length())); line = trim(line.substr(pos+1, line.length()));
ButtonSource source;
if (sourceStr == "keyboard") { if (sourceStr == "keyboard") {
source = KEYBOARD; buttonMap[button].kb_mapped = true;
buttonMap[button].kb_code = atoi(line.c_str());
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED
} else if (sourceStr == "joystick") { } else if (sourceStr == "joystick") {
source = JOYSTICK; buttonMap[button].js_mapped = true;
buttonMap[button].js_code = atoi(line.c_str());
#endif #endif
} else { } else {
WARNING("InputManager: Ignoring unknown button source \"%s\"\n", WARNING("InputManager: Ignoring unknown button source \"%s\"\n",
sourceStr.c_str()); sourceStr.c_str());
continue; continue;
} }
buttonMap[button].source = source;
buttonMap[button].code = atoi(line.c_str());
} }
inf.close(); inf.close();
@ -146,10 +146,10 @@ bool InputManager::getButton(Button *button, bool wait) {
else if (!SDL_PollEvent(&event)) else if (!SDL_PollEvent(&event))
return false; return false;
ButtonSource source; bool is_kb = false, is_js = false;
switch(event.type) { switch(event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
source = KEYBOARD; is_kb = true;
break; break;
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED
case SDL_JOYHATMOTION: { case SDL_JOYHATMOTION: {
@ -176,10 +176,10 @@ bool InputManager::getButton(Button *button, bool wait) {
startTimer(joystick); startTimer(joystick);
} }
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
source = JOYSTICK; is_js = true;
break; break;
case SDL_JOYAXISMOTION: { case SDL_JOYAXISMOTION: {
source = JOYSTICK; is_js = true;
unsigned int axis = event.jaxis.axis; unsigned int axis = event.jaxis.axis;
/* We only handle the first joystick */ /* We only handle the first joystick */
@ -246,19 +246,19 @@ bool InputManager::getButton(Button *button, bool wait) {
} }
int i = 0; int i = 0;
if (source == KEYBOARD) { if (is_kb) {
for (i = 0; i < BUTTON_TYPE_SIZE; i++) { for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
if (buttonMap[i].source == KEYBOARD if (buttonMap[i].kb_mapped
&& (unsigned int)event.key.keysym.sym == buttonMap[i].code) { && (unsigned int)event.key.keysym.sym == buttonMap[i].kb_code) {
*button = static_cast<Button>(i); *button = static_cast<Button>(i);
break; break;
} }
} }
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED
} else if (source == JOYSTICK && event.type == SDL_JOYBUTTONDOWN) { } else if (is_js && event.type == SDL_JOYBUTTONDOWN) {
for (i = 0; i < BUTTON_TYPE_SIZE; i++) { for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
if (buttonMap[i].source == JOYSTICK if (buttonMap[i].js_mapped
&& (unsigned int)event.jbutton.button == buttonMap[i].code) { && (unsigned int)event.jbutton.button == buttonMap[i].js_code) {
*button = static_cast<Button>(i); *button = static_cast<Button>(i);
break; break;
} }

View File

@ -70,10 +70,9 @@ public:
private: private:
void readConfFile(const std::string &conffile); void readConfFile(const std::string &conffile);
enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK };
struct ButtonMapEntry { struct ButtonMapEntry {
ButtonSource source; bool kb_mapped, js_mapped;
unsigned int code; unsigned int kb_code, js_code;
}; };
Menu *menu; Menu *menu;