1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-05 04:27:37 +03:00

Remove unnecessary file existence check in GMenu2X -> InputManager

Instead of checking which input configuration file exists among 2
choices, then asking InputManager to load that file, InputManager
itself now performs the resolution based on whether ifstream::is_open
returns true for each choice.
This commit is contained in:
Nebuleon Fumika 2014-08-10 23:38:10 +00:00 committed by Maarten ter Huurne
parent d6b2643610
commit ab27096f10
3 changed files with 64 additions and 58 deletions

View File

@ -279,18 +279,10 @@ GMenu2X::GMenu2X()
monitor = new MediaMonitor(CARD_ROOT); monitor = new MediaMonitor(CARD_ROOT);
#endif #endif
/* If a user-specified input.conf file exists, we load it; if (!input.init(this, menu.get())) {
* otherwise, we load the default one. */ exit(EXIT_FAILURE);
string input_file = getHome() + "/input.conf";
if (fileExists(input_file.c_str())) {
DEBUG("Loading user-specific input.conf file: %s.\n", input_file.c_str());
} else {
input_file = GMENU2X_SYSTEM_DIR "/input.conf";
DEBUG("Loading system input.conf file: %s.\n", input_file.c_str());
} }
input.init(this, input_file, menu.get());
if (confInt["backlightTimeout"] > 0) if (confInt["backlightTimeout"] > 0)
PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
#ifdef ENABLE_CPUFREQ #ifdef ENABLE_CPUFREQ

View File

@ -30,7 +30,7 @@
using namespace std; using namespace std;
void InputManager::init(GMenu2X *gmenu2x, const string &conffile, Menu *menu) { bool InputManager::init(GMenu2X *gmenu2x, Menu *menu) {
this->gmenu2x = gmenu2x; this->gmenu2x = gmenu2x;
this->menu = menu; this->menu = menu;
@ -40,7 +40,21 @@ void InputManager::init(GMenu2X *gmenu2x, const string &conffile, Menu *menu) {
buttonMap[i].js_mapped = false; buttonMap[i].js_mapped = false;
buttonMap[i].kb_mapped = false; buttonMap[i].kb_mapped = false;
} }
readConfFile(conffile);
/* If a user-specified input.conf file exists, we load it;
* otherwise, we load the default one. */
string input_file = gmenu2x->getHome() + "/input.conf";
DEBUG("Loading user-specific input.conf file: %s.\n", input_file.c_str());
if (!readConfFile(input_file)) {
input_file = GMENU2X_SYSTEM_DIR "/input.conf";
DEBUG("Loading system input.conf file: %s.\n", input_file.c_str());
if (!readConfFile(input_file)) {
ERROR("InputManager: failed to open config file\n");
return false;
}
}
return true;
} }
InputManager::InputManager() InputManager::InputManager()
@ -73,56 +87,56 @@ InputManager::~InputManager()
#endif #endif
} }
void InputManager::readConfFile(const string &conffile) { bool InputManager::readConfFile(const string &conffile) {
ifstream inf(conffile.c_str(), ios_base::in); ifstream inf(conffile.c_str(), ios_base::in);
if (inf.fail()) { if (inf.is_open()) {
ERROR("InputManager: failed to open config file\n"); string line;
return; while (getline(inf, line, '\n')) {
} string::size_type pos = line.find("=");
string name = trim(line.substr(0,pos));
line = trim(line.substr(pos+1,line.length()));
string line; Button button;
while (getline(inf, line, '\n')) { if (name == "up") button = UP;
string::size_type pos = line.find("="); else if (name == "down") button = DOWN;
string name = trim(line.substr(0,pos)); else if (name == "left") button = LEFT;
line = trim(line.substr(pos+1,line.length())); else if (name == "right") button = RIGHT;
else if (name == "accept") button = ACCEPT;
else if (name == "cancel") button = CANCEL;
else if (name == "altleft") button = ALTLEFT;
else if (name == "altright") button = ALTRIGHT;
else if (name == "menu") button = MENU;
else if (name == "settings") button = SETTINGS;
else {
WARNING("InputManager: Ignoring unknown button name \"%s\"\n",
name.c_str());
continue;
}
Button button; pos = line.find(",");
if (name == "up") button = UP; string sourceStr = trim(line.substr(0,pos));
else if (name == "down") button = DOWN; line = trim(line.substr(pos+1, line.length()));
else if (name == "left") button = LEFT;
else if (name == "right") button = RIGHT; if (sourceStr == "keyboard") {
else if (name == "accept") button = ACCEPT; buttonMap[button].kb_mapped = true;
else if (name == "cancel") button = CANCEL; buttonMap[button].kb_code = atoi(line.c_str());
else if (name == "altleft") button = ALTLEFT; #ifndef SDL_JOYSTICK_DISABLED
else if (name == "altright") button = ALTRIGHT; } else if (sourceStr == "joystick") {
else if (name == "menu") button = MENU; buttonMap[button].js_mapped = true;
else if (name == "settings") button = SETTINGS; buttonMap[button].js_code = atoi(line.c_str());
else { #endif
WARNING("InputManager: Ignoring unknown button name \"%s\"\n", } else {
name.c_str()); WARNING("InputManager: Ignoring unknown button source \"%s\"\n",
continue; sourceStr.c_str());
continue;
}
} }
pos = line.find(","); inf.close();
string sourceStr = trim(line.substr(0,pos)); return true;
line = trim(line.substr(pos+1, line.length())); } else {
return false;
if (sourceStr == "keyboard") {
buttonMap[button].kb_mapped = true;
buttonMap[button].kb_code = atoi(line.c_str());
#ifndef SDL_JOYSTICK_DISABLED
} else if (sourceStr == "joystick") {
buttonMap[button].js_mapped = true;
buttonMap[button].js_code = atoi(line.c_str());
#endif
} else {
WARNING("InputManager: Ignoring unknown button source \"%s\"\n",
sourceStr.c_str());
continue;
}
} }
inf.close();
} }
InputManager::Button InputManager::waitForPressedButton() { InputManager::Button InputManager::waitForPressedButton() {

View File

@ -64,7 +64,7 @@ public:
InputManager(); InputManager();
~InputManager(); ~InputManager();
void init(GMenu2X *gmenu2x, const std::string &conffile, Menu *menu); bool init(GMenu2X *gmenu2x, Menu *menu);
Button waitForPressedButton(); Button waitForPressedButton();
void repeatRateChanged(); void repeatRateChanged();
Uint32 joystickRepeatCallback(Uint32 timeout, struct Joystick *joystick); Uint32 joystickRepeatCallback(Uint32 timeout, struct Joystick *joystick);
@ -72,7 +72,7 @@ public:
bool getButton(Button *button, bool wait); bool getButton(Button *button, bool wait);
private: private:
void readConfFile(const std::string &conffile); bool readConfFile(const std::string &conffile);
struct ButtonMapEntry { struct ButtonMapEntry {
bool kb_mapped, js_mapped; bool kb_mapped, js_mapped;