1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-23 03:14:03 +02:00

InputManager: improved config file parsing.

Do not test whether config file exists; just open it and handle failure.
Config file was not closed on errors, fixed now.
Make sure unconfigured entries of the button map are properly initialized.
Ignore lines with invalid keywords instead of aborting parsing.
Made log messages more specific.
This commit is contained in:
Maarten ter Huurne 2011-10-23 10:38:39 +02:00
parent 9548aa0a41
commit a685b436ec
2 changed files with 34 additions and 36 deletions

View File

@ -29,9 +29,10 @@
using namespace std; using namespace std;
void InputManager::init(const string &conffile) { void InputManager::init(const string &conffile) {
if (!readConfFile(conffile)) { for (int i = 0; i < BUTTON_TYPE_SIZE; i++) {
ERROR("InputManager initialization from config file failed.\n"); buttonMap[i].source = UNMAPPED;
} }
readConfFile(conffile);
} }
InputManager::InputManager() InputManager::InputManager()
@ -53,25 +54,20 @@ InputManager::~InputManager() {
#endif #endif
} }
bool InputManager::readConfFile(const string &conffile) { void InputManager::readConfFile(const string &conffile) {
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.fail()) {
return false; ERROR("InputManager: failed to open config file\n");
return;
} }
string line, name, source; string line;
string::size_type pos; while (getline(inf, line, '\n')) {
Button button; string::size_type pos = line.find("=");
string name = trim(line.substr(0,pos));
while(getline(inf, line, '\n')) {
pos = line.find("=");
name = trim(line.substr(0,pos));
line = trim(line.substr(pos+1,line.length())); line = trim(line.substr(pos+1,line.length()));
Button button;
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;
@ -88,31 +84,33 @@ bool InputManager::readConfFile(const string &conffile) {
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 {
WARNING("InputManager: Ignoring unknown button name \"%s\"\n",
pos = line.find(","); name.c_str());
source = trim(line.substr(0,pos));
line = trim(line.substr(pos+1, line.length()));
if (source == "keyboard") {
buttonMap[button].source = KEYBOARD;
} else if (source == "joystick") {
#ifdef SDL_JOYSTICK_DISABLED
WARNING("Ignoring joystick button mapping "
"because SDL was compiled without joystick support\n");
continue; continue;
#else
buttonMap[button].source = JOYSTICK;
#endif
} else {
return false;
} }
pos = line.find(",");
string sourceStr = trim(line.substr(0,pos));
line = trim(line.substr(pos+1, line.length()));
ButtonSource source;
if (sourceStr == "keyboard") {
source = KEYBOARD;
#ifndef SDL_JOYSTICK_DISABLED
} else if (sourceStr == "joystick") {
source = JOYSTICK;
#endif
} else {
WARNING("InputManager: Ignoring unknown button source \"%s\"\n",
sourceStr.c_str());
continue;
}
buttonMap[button].source = source;
buttonMap[button].code = atoi(line.c_str()); buttonMap[button].code = atoi(line.c_str());
} }
inf.close(); inf.close();
return true;
} }
InputManager::Button InputManager::waitForPressedButton() { InputManager::Button InputManager::waitForPressedButton() {

View File

@ -53,13 +53,13 @@ public:
bool pollEvent(ButtonEvent *event); bool pollEvent(ButtonEvent *event);
private: private:
enum ButtonSource { KEYBOARD, JOYSTICK }; enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK };
struct ButtonMapEntry { struct ButtonMapEntry {
ButtonSource source; ButtonSource source;
unsigned int code; unsigned int code;
}; };
bool readConfFile(const std::string &conffile); void readConfFile(const std::string &conffile);
bool getEvent(ButtonEvent *bevent, bool wait); bool getEvent(ButtonEvent *bevent, bool wait);
Button waitForButton(ButtonState state); Button waitForButton(ButtonState state);