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

Change API of InputManager: we don't care about key release events

This commit is contained in:
Paul Cercueil 2013-09-07 11:00:57 -04:00
parent ba5ef51269
commit dba6c32109
4 changed files with 23 additions and 52 deletions

View File

@ -628,17 +628,15 @@ void GMenu2X::main() {
} }
// Handle other input events. // Handle other input events.
InputManager::ButtonEvent event; InputManager::Button button;
bool gotEvent; bool gotEvent;
const bool wait = !animating; const bool wait = !animating;
do { do {
do { gotEvent = input.getButton(&button, wait);
gotEvent = input.getEvent(&event, wait);
} while (gotEvent && event.state != InputManager::PRESSED);
} while (wait && !gotEvent); } while (wait && !gotEvent);
if (gotEvent) { if (gotEvent) {
for (auto it = layers.rbegin(); it != layers.rend(); ++it) { for (auto it = layers.rbegin(); it != layers.rend(); ++it) {
if ((*it)->handleButtonPress(event.button)) { if ((*it)->handleButtonPress(button)) {
break; break;
} }
} }

View File

@ -117,20 +117,16 @@ void InputManager::readConfFile(const string &conffile) {
} }
InputManager::Button InputManager::waitForPressedButton() { InputManager::Button InputManager::waitForPressedButton() {
ButtonEvent event; Button button;
while (!waitForEvent(&event) || event.state != PRESSED); while (!getButton(&button, true));
return event.button; return button;
} }
bool InputManager::waitForEvent(ButtonEvent *event) { bool InputManager::pollButton(Button *button) {
return getEvent(event, true); return getButton(button, false);
} }
bool InputManager::pollEvent(ButtonEvent *event) { bool InputManager::getButton(Button *button, bool wait) {
return getEvent(event, false);
}
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
@ -142,32 +138,18 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
#endif #endif
SDL_Event event; SDL_Event event;
if (wait) { if (wait)
SDL_WaitEvent(&event); SDL_WaitEvent(&event);
} else { else if (!SDL_PollEvent(&event))
bevent->state = RELEASED;
if (!SDL_PollEvent(&event)) {
return false; return false;
}
}
ButtonSource source; ButtonSource source;
switch(event.type) { switch(event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
bevent->state = PRESSED;
source = KEYBOARD;
break;
case SDL_KEYUP:
bevent->state = RELEASED;
source = KEYBOARD; source = KEYBOARD;
break; break;
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
bevent->state = PRESSED;
source = JOYSTICK;
break;
case SDL_JOYBUTTONUP:
bevent->state = RELEASED;
source = JOYSTICK; source = JOYSTICK;
break; break;
#endif #endif
@ -191,8 +173,7 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
if (event.user.data1) if (event.user.data1)
free(event.user.data1); free(event.user.data1);
bevent->state = PRESSED; *button = REPAINT;
bevent->button = REPAINT;
return true; return true;
default: default:
@ -203,7 +184,7 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
for (i = 0; i < BUTTON_TYPE_SIZE; i++) { for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
if (buttonMap[i].source == KEYBOARD if (buttonMap[i].source == KEYBOARD
&& (unsigned int)event.key.keysym.sym == buttonMap[i].code) { && (unsigned int)event.key.keysym.sym == buttonMap[i].code) {
bevent->button = static_cast<Button>(i); *button = static_cast<Button>(i);
break; break;
} }
} }
@ -212,7 +193,7 @@ bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
for (i = 0; i < BUTTON_TYPE_SIZE; i++) { for (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) {
bevent->button = static_cast<Button>(i); *button = static_cast<Button>(i);
break; break;
} }
} }

View File

@ -45,22 +45,17 @@ public:
}; };
#define BUTTON_TYPE_SIZE 10 #define BUTTON_TYPE_SIZE 10
enum ButtonState { PRESSED, RELEASED };
struct ButtonEvent {
Button button;
ButtonState state;
};
InputManager(); InputManager();
~InputManager(); ~InputManager();
void init(const std::string &conffile, Menu *menu); void init(const std::string &conffile, Menu *menu);
bool waitForEvent(ButtonEvent *event);
Button waitForPressedButton(); Button waitForPressedButton();
bool pollEvent(ButtonEvent *event); bool pollButton(Button *button);
bool getEvent(ButtonEvent *bevent, bool wait); bool getButton(Button *button, bool wait);
private: private:
void readConfFile(const std::string &conffile);
enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK }; enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK };
struct ButtonMapEntry { struct ButtonMapEntry {
ButtonSource source; ButtonSource source;
@ -69,8 +64,6 @@ private:
Menu *menu; Menu *menu;
void readConfFile(const std::string &conffile);
ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE];
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED
std::vector<SDL_Joystick *> joysticks; std::vector<SDL_Joystick *> joysticks;

View File

@ -99,11 +99,10 @@ int MessageBox::exec() {
int result = -1; int result = -1;
while (result < 0) { while (result < 0) {
InputManager::ButtonEvent event; InputManager::Button button;
if (gmenu2x->input.pollEvent(&event) if (gmenu2x->input.pollButton(&button)
&& event.state == InputManager::PRESSED && !buttons[button].empty()) {
&& !buttons[event.button].empty()) { result = button;
result = event.button;
} }
usleep(LOOP_DELAY); usleep(LOOP_DELAY);