mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-12-26 21:18:58 +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:
parent
0cb1645c03
commit
f9696971ad
@ -29,36 +29,36 @@
|
||||
static SDL_Joystick *joystick;
|
||||
|
||||
void InputManager::init(const string &conffile) {
|
||||
if (!readConfFile(conffile))
|
||||
if (!readConfFile(conffile)) {
|
||||
ERROR("InputManager initialization from config file failed.\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
InputManager::InputManager() {
|
||||
initJoystick();
|
||||
}
|
||||
|
||||
|
||||
InputManager::~InputManager() {
|
||||
if (SDL_NumJoysticks() > 0) {
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void InputManager::initJoystick() {
|
||||
if (SDL_NumJoysticks() > 0) {
|
||||
joystick = SDL_JoystickOpen(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool InputManager::readConfFile(const string &conffile) {
|
||||
if (!fileExists(conffile)) return false;
|
||||
if (!fileExists(conffile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ifstream inf(conffile.c_str(), ios_base::in);
|
||||
if (!(inf.is_open()))
|
||||
if (!(inf.is_open())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
string line, name, source;
|
||||
string::size_type pos;
|
||||
@ -102,12 +102,10 @@ bool InputManager::readConfFile(const string &conffile) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
buttontype_t InputManager::waitForPressedButton() {
|
||||
return waitForButton(PRESSED);
|
||||
}
|
||||
|
||||
|
||||
buttontype_t InputManager::waitForReleasedButton() {
|
||||
return waitForButton(RELEASED);
|
||||
}
|
||||
@ -120,33 +118,30 @@ buttontype_t InputManager::waitForButton(enum state_e state) {
|
||||
return event.button;
|
||||
}
|
||||
|
||||
|
||||
void InputManager::waitForEvent(bevent_t *event) {
|
||||
getEvent(event, true);
|
||||
}
|
||||
|
||||
|
||||
bool InputManager::pollEvent(bevent_t *event) {
|
||||
return getEvent(event, false);
|
||||
}
|
||||
|
||||
|
||||
bool InputManager::getEvent(bevent_t *bevent, bool wait) {
|
||||
//TODO: when an event is processed, program a new event
|
||||
//in some time, and when it occurs, do a key repeat
|
||||
|
||||
SDL_JoystickUpdate();
|
||||
SDL_Event event;
|
||||
|
||||
if (wait)
|
||||
if (wait) {
|
||||
SDL_WaitEvent(&event);
|
||||
else {
|
||||
} else {
|
||||
bevent->state = RELEASED;
|
||||
if (!SDL_PollEvent(&event)) return false;
|
||||
if (!SDL_PollEvent(&event)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
enum source_type_e source;
|
||||
|
||||
switch(event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
bevent->state = PRESSED;
|
||||
@ -166,24 +161,28 @@ bool InputManager::getEvent(bevent_t *bevent, bool wait) {
|
||||
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) {
|
||||
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) {
|
||||
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())
|
||||
}
|
||||
if (wait && PowerSaver::isRunning()) {
|
||||
PowerSaver::getInstance()->resetScreenTimer();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -17,8 +17,9 @@
|
||||
* Free Software Foundation, Inc., *
|
||||
* 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 <sstream>
|
||||
@ -51,14 +52,6 @@ typedef struct {
|
||||
|
||||
|
||||
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:
|
||||
InputManager();
|
||||
~InputManager();
|
||||
@ -68,6 +61,14 @@ public:
|
||||
buttontype_t waitForPressedButton();
|
||||
buttontype_t waitForReleasedButton();
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user