1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-20 20:05:20 +02: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

@ -1,6 +1,6 @@
/*************************************************************************** /***************************************************************************
* Copyright (C) 2006 by Massimiliano Torromeo * * Copyright (C) 2006 by Massimiliano Torromeo *
* massimiliano.torromeo@gmail.com * * massimiliano.torromeo@gmail.com *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
@ -29,161 +29,160 @@
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;
buttontype_t button; buttontype_t button;
while(getline(inf, line, '\n')) { while(getline(inf, line, '\n')) {
pos = line.find("="); pos = line.find("=");
name = trim(line.substr(0,pos)); name = trim(line.substr(0,pos));
line = trim(line.substr(pos+1,line.length())); line = trim(line.substr(pos+1,line.length()));
if (name == "up") button = UP; if (name == "up") button = UP;
else if (name == "down") button = DOWN; else if (name == "down") button = DOWN;
else if (name == "left") button = LEFT; else if (name == "left") button = LEFT;
else if (name == "right") button = RIGHT; else if (name == "right") button = RIGHT;
else if (name == "accept") button = ACCEPT; else if (name == "accept") button = ACCEPT;
else if (name == "cancel") button = CANCEL; else if (name == "cancel") button = CANCEL;
else if (name == "clear") button = CLEAR; else if (name == "clear") button = CLEAR;
else if (name == "manual") button = MANUAL; else if (name == "manual") button = MANUAL;
else if (name == "altleft") button = ALTLEFT; else if (name == "altleft") button = ALTLEFT;
else if (name == "altright") button = ALTRIGHT; else if (name == "altright") button = ALTRIGHT;
else if (name == "menu") button = MENU; else if (name == "menu") button = MENU;
else if (name == "settings") button = SETTINGS; else if (name == "settings") button = SETTINGS;
else if (name == "volup") button = VOLUP; else if (name == "volup") button = VOLUP;
else if (name == "voldown") button = VOLDOWN; else if (name == "voldown") button = VOLDOWN;
else if (name == "power") button = POWER; else if (name == "power") button = POWER;
else if (name == "lock") button = LOCK; else if (name == "lock") button = LOCK;
else return false; else return false;
pos = line.find(","); pos = line.find(",");
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") ButtonMap[button].source = KEYBOARD;
else if (source == "joystick") ButtonMap[button].source = JOYSTICK; else if (source == "joystick") ButtonMap[button].source = JOYSTICK;
else return false; else return false;
ButtonMap[button].code = atoi(line.c_str()); ButtonMap[button].code = atoi(line.c_str());
} }
inf.close(); inf.close();
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);
} }
buttontype_t InputManager::waitForButton(enum state_e state) { 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) {
SDL_WaitEvent(&event);
} else {
bevent->state = RELEASED;
if (!SDL_PollEvent(&event)) {
return false;
}
}
if (wait) enum source_type_e source;
SDL_WaitEvent(&event); switch(event.type) {
else { case SDL_KEYDOWN:
bevent->state = RELEASED; bevent->state = PRESSED;
if (!SDL_PollEvent(&event)) return false; source = KEYBOARD;
} break;
case SDL_KEYUP:
bevent->state = RELEASED;
source = KEYBOARD;
break;
case SDL_JOYBUTTONDOWN:
bevent->state = PRESSED;
source = JOYSTICK;
break;
case SDL_JOYBUTTONUP:
bevent->state = RELEASED;
source = JOYSTICK;
break;
default:
return false;
}
enum source_type_e source; if (source == KEYBOARD) {
for (int i=0; i<BUTTONTYPE_T_SIZE; i++) {
if (ButtonMap[i].source == KEYBOARD
&& (unsigned int)event.key.keysym.sym == ButtonMap[i].code) {
bevent->button = (buttontype_t)i;
break;
}
}
} else {
for (int i=0; i<BUTTONTYPE_T_SIZE; i++) {
if (ButtonMap[i].source == JOYSTICK
&& (unsigned int)event.jbutton.button == ButtonMap[i].code) {
bevent->button = (buttontype_t)i;
break;
}
}
}
if (wait && PowerSaver::isRunning()) {
PowerSaver::getInstance()->resetScreenTimer();
}
switch(event.type) { return true;
case SDL_KEYDOWN:
bevent->state = PRESSED;
source = KEYBOARD;
break;
case SDL_KEYUP:
bevent->state = RELEASED;
source = KEYBOARD;
break;
case SDL_JOYBUTTONDOWN:
bevent->state = PRESSED;
source = JOYSTICK;
break;
case SDL_JOYBUTTONUP:
bevent->state = RELEASED;
source = JOYSTICK;
break;
default:
return false;
break;
}
if (source == KEYBOARD) {
for (int i=0; i<BUTTONTYPE_T_SIZE; i++)
if (ButtonMap[i].source == KEYBOARD && (unsigned int)event.key.keysym.sym == ButtonMap[i].code) {
bevent->button = (buttontype_t)i;
break;
}
} else {
for (int i=0; i<BUTTONTYPE_T_SIZE; i++)
if (ButtonMap[i].source == JOYSTICK && (unsigned int)event.jbutton.button == ButtonMap[i].code) {
bevent->button = (buttontype_t)i;
break;
}
}
if ( wait && PowerSaver::isRunning())
PowerSaver::getInstance()->resetScreenTimer();
return true;
} }

View File

@ -1,6 +1,6 @@
/*************************************************************************** /***************************************************************************
* Copyright (C) 2006 by Massimiliano Torromeo * * Copyright (C) 2006 by Massimiliano Torromeo *
* massimiliano.torromeo@gmail.com * * massimiliano.torromeo@gmail.com *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
@ -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>
@ -26,13 +27,13 @@
using namespace std; using namespace std;
typedef enum buttontype_e { typedef enum buttontype_e {
UP, DOWN, LEFT, RIGHT, UP, DOWN, LEFT, RIGHT,
ACCEPT, CANCEL, ACCEPT, CANCEL,
CLEAR, MANUAL, CLEAR, MANUAL,
ALTLEFT, ALTRIGHT, ALTLEFT, ALTRIGHT,
MENU, SETTINGS, MENU, SETTINGS,
VOLUP, VOLDOWN, VOLUP, VOLDOWN,
POWER, LOCK POWER, LOCK
} buttontype_t; } buttontype_t;
#define BUTTONTYPE_T_SIZE 16 #define BUTTONTYPE_T_SIZE 16
@ -40,34 +41,34 @@ enum source_type_e {KEYBOARD, JOYSTICK};
enum state_e {PRESSED, RELEASED}; enum state_e {PRESSED, RELEASED};
typedef struct { typedef struct {
source_type_e source; source_type_e source;
Uint32 code; Uint32 code;
} input_t; } input_t;
typedef struct { typedef struct {
buttontype_t button; buttontype_t button;
state_e state; state_e state;
} bevent_t; } bevent_t;
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();
void init(const string &conffile); void init(const string &conffile);
void waitForEvent(bevent_t *event); void waitForEvent(bevent_t *event);
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