1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-29 21:22:28 +02:00

InputManager: Don't break if joystick support is disabled in SDL.

Early Dingux versions had joystick support disabled, but many programs
broke on that, so booboo enabled joystick support later.
This commit is contained in:
Maarten ter Huurne 2011-10-23 10:12:11 +02:00
parent 433be5daf7
commit 9548aa0a41
2 changed files with 29 additions and 9 deletions

View File

@ -23,8 +23,6 @@
#include "utilities.h" #include "utilities.h"
#include "powersaver.h" #include "powersaver.h"
#include <SDL.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -37,17 +35,22 @@ void InputManager::init(const string &conffile) {
} }
InputManager::InputManager() InputManager::InputManager()
: joystick(NULL)
{ {
#ifndef SDL_JOYSTICK_DISABLED
if (SDL_NumJoysticks() > 0) { if (SDL_NumJoysticks() > 0) {
joystick = SDL_JoystickOpen(0); joystick = SDL_JoystickOpen(0);
} else {
joystick = NULL;
} }
#endif
} }
InputManager::~InputManager() { InputManager::~InputManager() {
#ifndef SDL_JOYSTICK_DISABLED
if (joystick) { if (joystick) {
SDL_JoystickClose(joystick); SDL_JoystickClose(joystick);
} }
#endif
} }
bool InputManager::readConfFile(const string &conffile) { bool InputManager::readConfFile(const string &conffile) {
@ -91,9 +94,19 @@ bool InputManager::readConfFile(const string &conffile) {
source = trim(line.substr(0,pos)); source = trim(line.substr(0,pos));
line = trim(line.substr(pos+1, line.length())); line = trim(line.substr(pos+1, line.length()));
if (source == "keyboard") buttonMap[button].source = KEYBOARD; if (source == "keyboard") {
else if (source == "joystick") buttonMap[button].source = JOYSTICK; buttonMap[button].source = KEYBOARD;
else return false; } else if (source == "joystick") {
#ifdef SDL_JOYSTICK_DISABLED
WARNING("Ignoring joystick button mapping "
"because SDL was compiled without joystick support\n");
continue;
#else
buttonMap[button].source = JOYSTICK;
#endif
} else {
return false;
}
buttonMap[button].code = atoi(line.c_str()); buttonMap[button].code = atoi(line.c_str());
} }
@ -130,9 +143,11 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
//TODO: when an event is processed, program a new event //TODO: when an event is processed, program a new event
//in some time, and when it occurs, do a key repeat //in some time, and when it occurs, do a key repeat
#ifndef SDL_JOYSTICK_DISABLED
if (joystick) { if (joystick) {
SDL_JoystickUpdate(); SDL_JoystickUpdate();
} }
#endif
SDL_Event event; SDL_Event event;
if (wait) { if (wait) {
SDL_WaitEvent(&event); SDL_WaitEvent(&event);
@ -153,6 +168,7 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
bevent->state = RELEASED; bevent->state = RELEASED;
source = KEYBOARD; source = KEYBOARD;
break; break;
#ifndef SDL_JOYSTICK_DISABLED
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
bevent->state = PRESSED; bevent->state = PRESSED;
source = JOYSTICK; source = JOYSTICK;
@ -161,6 +177,7 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
bevent->state = RELEASED; bevent->state = RELEASED;
source = JOYSTICK; source = JOYSTICK;
break; break;
#endif
default: default:
return false; return false;
} }
@ -173,7 +190,8 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
break; break;
} }
} }
} else { #ifndef SDL_JOYSTICK_DISABLED
} else if (source == JOYSTICK) {
for (int i = 0; i < BUTTON_TYPE_SIZE; i++) { for (int i = 0; i < BUTTON_TYPE_SIZE; i++) {
if (buttonMap[i].source == JOYSTICK if (buttonMap[i].source == JOYSTICK
&& (unsigned int)event.jbutton.button == buttonMap[i].code) { && (unsigned int)event.jbutton.button == buttonMap[i].code) {
@ -181,6 +199,7 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
break; break;
} }
} }
#endif
} }
if (wait && PowerSaver::isRunning()) { if (wait && PowerSaver::isRunning()) {
PowerSaver::getInstance()->resetScreenTimer(); PowerSaver::getInstance()->resetScreenTimer();

View File

@ -21,10 +21,9 @@
#ifndef INPUTMANAGER_H #ifndef INPUTMANAGER_H
#define INPUTMANAGER_H #define INPUTMANAGER_H
#include <SDL.h>
#include <string> #include <string>
typedef struct _SDL_Joystick SDL_Joystick;
class InputManager { class InputManager {
public: public:
enum Button { enum Button {
@ -65,7 +64,9 @@ private:
Button waitForButton(ButtonState state); Button waitForButton(ButtonState state);
ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE];
#ifndef SDL_JOYSTICK_DISABLED
SDL_Joystick *joystick; SDL_Joystick *joystick;
#endif
}; };
#endif #endif