1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-10-05 22:25:26 +03:00

InputManager: re-layouted code.

I don't really care for tabs vs spaces, but it's impractical to use tabs
in one source file and spaces in another.
This commit is contained in:
Maarten ter Huurne 2011-10-23 07:49:43 +02:00
parent 0cb1645c03
commit f9696971ad
2 changed files with 147 additions and 147 deletions

View File

@ -29,36 +29,36 @@
static SDL_Joystick *joystick; static SDL_Joystick *joystick;
void InputManager::init(const string &conffile) { void InputManager::init(const string &conffile) {
if (!readConfFile(conffile)) if (!readConfFile(conffile)) {
ERROR("InputManager initialization from config file failed.\n"); ERROR("InputManager initialization from config file failed.\n");
}
} }
InputManager::InputManager() { InputManager::InputManager() {
initJoystick(); initJoystick();
} }
InputManager::~InputManager() { InputManager::~InputManager() {
if (SDL_NumJoysticks() > 0) { if (SDL_NumJoysticks() > 0) {
SDL_JoystickClose(joystick); SDL_JoystickClose(joystick);
} }
} }
void InputManager::initJoystick() { void InputManager::initJoystick() {
if (SDL_NumJoysticks() > 0) { if (SDL_NumJoysticks() > 0) {
joystick = SDL_JoystickOpen(0); joystick = SDL_JoystickOpen(0);
} }
} }
bool InputManager::readConfFile(const string &conffile) { bool InputManager::readConfFile(const string &conffile) {
if (!fileExists(conffile)) return false; if (!fileExists(conffile)) {
return false;
}
ifstream inf(conffile.c_str(), ios_base::in); ifstream inf(conffile.c_str(), ios_base::in);
if (!(inf.is_open())) if (!(inf.is_open())) {
return false; return false;
}
string line, name, source; string line, name, source;
string::size_type pos; string::size_type pos;
@ -102,12 +102,10 @@ bool InputManager::readConfFile(const string &conffile) {
return true; return true;
} }
buttontype_t InputManager::waitForPressedButton() { buttontype_t InputManager::waitForPressedButton() {
return waitForButton(PRESSED); return waitForButton(PRESSED);
} }
buttontype_t InputManager::waitForReleasedButton() { buttontype_t InputManager::waitForReleasedButton() {
return waitForButton(RELEASED); return waitForButton(RELEASED);
} }
@ -116,37 +114,34 @@ buttontype_t InputManager::waitForButton(enum state_e state) {
bevent_t event; bevent_t event;
do { do {
waitForEvent(&event); waitForEvent(&event);
} while(event.state != state); } while (event.state != state);
return event.button; return event.button;
} }
void InputManager::waitForEvent(bevent_t *event) { void InputManager::waitForEvent(bevent_t *event) {
getEvent(event, true); getEvent(event, true);
} }
bool InputManager::pollEvent(bevent_t *event) { bool InputManager::pollEvent(bevent_t *event) {
return getEvent(event, false); return getEvent(event, false);
} }
bool InputManager::getEvent(bevent_t *bevent, bool wait) { bool InputManager::getEvent(bevent_t *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
SDL_JoystickUpdate(); SDL_JoystickUpdate();
SDL_Event event; SDL_Event event;
if (wait) {
if (wait)
SDL_WaitEvent(&event); SDL_WaitEvent(&event);
else { } else {
bevent->state = RELEASED; bevent->state = RELEASED;
if (!SDL_PollEvent(&event)) return false; if (!SDL_PollEvent(&event)) {
return false;
}
} }
enum source_type_e source; enum source_type_e source;
switch(event.type) { switch(event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
bevent->state = PRESSED; bevent->state = PRESSED;
@ -166,24 +161,28 @@ bool InputManager::getEvent(bevent_t *bevent, bool wait) {
break; break;
default: default:
return false; return false;
break;
} }
if (source == KEYBOARD) { if (source == KEYBOARD) {
for (int i=0; i<BUTTONTYPE_T_SIZE; i++) for (int i=0; i<BUTTONTYPE_T_SIZE; i++) {
if (ButtonMap[i].source == KEYBOARD && (unsigned int)event.key.keysym.sym == ButtonMap[i].code) { if (ButtonMap[i].source == KEYBOARD
&& (unsigned int)event.key.keysym.sym == ButtonMap[i].code) {
bevent->button = (buttontype_t)i; bevent->button = (buttontype_t)i;
break; break;
} }
}
} else { } else {
for (int i=0; i<BUTTONTYPE_T_SIZE; i++) for (int i=0; i<BUTTONTYPE_T_SIZE; i++) {
if (ButtonMap[i].source == JOYSTICK && (unsigned int)event.jbutton.button == ButtonMap[i].code) { if (ButtonMap[i].source == JOYSTICK
&& (unsigned int)event.jbutton.button == ButtonMap[i].code) {
bevent->button = (buttontype_t)i; bevent->button = (buttontype_t)i;
break; break;
} }
} }
if ( wait && PowerSaver::isRunning()) }
if (wait && PowerSaver::isRunning()) {
PowerSaver::getInstance()->resetScreenTimer(); PowerSaver::getInstance()->resetScreenTimer();
}
return true; return true;
} }

View File

@ -17,8 +17,9 @@
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef NEWINPUT_H
#define NEWINPUT_H #ifndef INPUTMANAGER_H
#define INPUTMANAGER_H
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <sstream> #include <sstream>
@ -51,14 +52,6 @@ typedef struct {
class InputManager { class InputManager {
private:
input_t ButtonMap[BUTTONTYPE_T_SIZE];
bool readConfFile(const string &conffile);
void initJoystick();
bool getEvent(bevent_t *bevent, bool wait);
buttontype_t waitForButton(enum state_e state);
public: public:
InputManager(); InputManager();
~InputManager(); ~InputManager();
@ -68,6 +61,14 @@ public:
buttontype_t waitForPressedButton(); buttontype_t waitForPressedButton();
buttontype_t waitForReleasedButton(); buttontype_t waitForReleasedButton();
bool pollEvent(bevent_t *event); bool pollEvent(bevent_t *event);
private:
input_t ButtonMap[BUTTONTYPE_T_SIZE];
bool readConfFile(const string &conffile);
void initJoystick();
bool getEvent(bevent_t *bevent, bool wait);
buttontype_t waitForButton(enum state_e state);
}; };
#endif #endif