From ab27096f1063f7c826e5978510cc701dc987ed56 Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Sun, 10 Aug 2014 23:38:10 +0000 Subject: [PATCH] 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. --- src/gmenu2x.cpp | 12 +---- src/inputmanager.cpp | 106 ++++++++++++++++++++++++------------------- src/inputmanager.h | 4 +- 3 files changed, 64 insertions(+), 58 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 26525ba..4ca4d59 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -279,18 +279,10 @@ GMenu2X::GMenu2X() monitor = new MediaMonitor(CARD_ROOT); #endif - /* If a user-specified input.conf file exists, we load it; - * otherwise, we load the default one. */ - 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()); + if (!input.init(this, menu.get())) { + exit(EXIT_FAILURE); } - input.init(this, input_file, menu.get()); - if (confInt["backlightTimeout"] > 0) PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); #ifdef ENABLE_CPUFREQ diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index cf35cf8..62d1488 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -30,7 +30,7 @@ using namespace std; -void InputManager::init(GMenu2X *gmenu2x, const string &conffile, Menu *menu) { +bool InputManager::init(GMenu2X *gmenu2x, Menu *menu) { this->gmenu2x = gmenu2x; this->menu = menu; @@ -40,7 +40,21 @@ void InputManager::init(GMenu2X *gmenu2x, const string &conffile, Menu *menu) { buttonMap[i].js_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() @@ -73,56 +87,56 @@ InputManager::~InputManager() #endif } -void InputManager::readConfFile(const string &conffile) { +bool InputManager::readConfFile(const string &conffile) { ifstream inf(conffile.c_str(), ios_base::in); - if (inf.fail()) { - ERROR("InputManager: failed to open config file\n"); - return; - } + if (inf.is_open()) { + string line; + 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; - 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())); + Button button; + if (name == "up") button = UP; + else if (name == "down") button = DOWN; + else if (name == "left") button = LEFT; + 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; - if (name == "up") button = UP; - else if (name == "down") button = DOWN; - else if (name == "left") button = LEFT; - 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; + pos = line.find(","); + string sourceStr = trim(line.substr(0,pos)); + line = trim(line.substr(pos+1, line.length())); + + 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; + } } - pos = line.find(","); - string sourceStr = trim(line.substr(0,pos)); - line = trim(line.substr(pos+1, line.length())); - - 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(); + return true; + } else { + return false; } - - inf.close(); } InputManager::Button InputManager::waitForPressedButton() { diff --git a/src/inputmanager.h b/src/inputmanager.h index 7763d07..65e525b 100644 --- a/src/inputmanager.h +++ b/src/inputmanager.h @@ -64,7 +64,7 @@ public: InputManager(); ~InputManager(); - void init(GMenu2X *gmenu2x, const std::string &conffile, Menu *menu); + bool init(GMenu2X *gmenu2x, Menu *menu); Button waitForPressedButton(); void repeatRateChanged(); Uint32 joystickRepeatCallback(Uint32 timeout, struct Joystick *joystick); @@ -72,7 +72,7 @@ public: bool getButton(Button *button, bool wait); private: - void readConfFile(const std::string &conffile); + bool readConfFile(const std::string &conffile); struct ButtonMapEntry { bool kb_mapped, js_mapped;