From 4e9b31baff481cb514a1b2be3e7ff776722cbaf5 Mon Sep 17 00:00:00 2001 From: Vincent-FK Date: Mon, 19 Apr 2021 18:39:13 +0200 Subject: [PATCH] applying keymap files if found. Fixed sound regression due to pclose --- RetroFE/Source/Database/Configuration.cpp | 64 +++++++++++------------ RetroFE/Source/Execute/Launcher.cpp | 44 ++++++++++++++++ RetroFE/Source/Sound/Sound.cpp | 10 ++-- RetroFE/Source/Utility/Utils.cpp | 17 ++++++ RetroFE/Source/Utility/Utils.h | 3 ++ 5 files changed, 101 insertions(+), 37 deletions(-) diff --git a/RetroFE/Source/Database/Configuration.cpp b/RetroFE/Source/Database/Configuration.cpp index ec390db..723611c 100755 --- a/RetroFE/Source/Database/Configuration.cpp +++ b/RetroFE/Source/Database/Configuration.cpp @@ -228,48 +228,48 @@ bool Configuration::importCurrentLayout(std::string folder, std::string file, bo // line empty if(line.empty() || (line.find_first_not_of(" \t\r") == std::string::npos)) { - retVal = false; + retVal = false; } // finding layout in existing list else { - std::string seekedLayoutName = trimEnds(line); - std::string layoutPathFound; - bool layoutFoundInList = false; + std::string seekedLayoutName = trimEnds(line); + std::string layoutPathFound; + bool layoutFoundInList = false; - // check existing layout list */ - for(std::vector::iterator it = layouts_.begin(); it != layouts_.end(); ++it){ - std::string curLayoutName = Utils::getFileName(*it); - if(!curLayoutName.compare(seekedLayoutName)){ - layoutPathFound = *it; - layoutFoundInList = true; - break; - } - index++; - } + // check existing layout list */ + for(std::vector::iterator it = layouts_.begin(); it != layouts_.end(); ++it){ + std::string curLayoutName = Utils::getFileName(*it); + if(!curLayoutName.compare(seekedLayoutName)){ + layoutPathFound = *it; + layoutFoundInList = true; + break; + } + index++; + } - if (layoutFoundInList){ + if (layoutFoundInList){ - /* remove layout properties if they already exist */ - if(properties_.find("layout") != properties_.end()) - { - properties_.erase("layout"); - } + /* remove layout properties if they already exist */ + if(properties_.find("layout") != properties_.end()) + { + properties_.erase("layout"); + } - /* Set new pair for key = layout */ - properties_.insert(PropertiesPair("layout", seekedLayoutName)); + /* Set new pair for key = layout */ + properties_.insert(PropertiesPair("layout", seekedLayoutName)); - std::stringstream ss; - //printf("Found layout: %s at idx %d\n", layoutPathFound.c_str(), index); - ss << "Found layout: " << "\"" << layoutPathFound << "\" in layouts list at idx " << index; - Logger::write(Logger::ZONE_INFO, "Configuration", ss.str()); - retVal = true; + std::stringstream ss; + //printf("Found layout: %s at idx %d\n", layoutPathFound.c_str(), index); + ss << "Found layout: " << "\"" << layoutPathFound << "\" in layouts list at idx " << index; + Logger::write(Logger::ZONE_INFO, "Configuration", ss.str()); + retVal = true; - break; - } - else{ - index = 0; - } + break; + } + else{ + index = 0; + } } } diff --git a/RetroFE/Source/Execute/Launcher.cpp b/RetroFE/Source/Execute/Launcher.cpp index 0404bb8..8180fde 100755 --- a/RetroFE/Source/Execute/Launcher.cpp +++ b/RetroFE/Source/Execute/Launcher.cpp @@ -120,6 +120,44 @@ bool Launcher::run(std::string collection, Item *collectionItem) selectedItemsDirectory, collection); + /* Apply key mapping for selected item's directory if found */ + std::string selectedItemDirnameKeyfile = Utils::getDirectory(selectedItemsPath) + "/default_config.key"; + if (!access(selectedItemDirnameKeyfile.c_str(), R_OK)) { + + /* Create shell cmd */ + std::string cmd = SHELL_CMD_MAPPING_SET; + cmd += " " + selectedItemDirnameKeyfile; + + /* Log shell cmd */ + Logger::write(Logger::ZONE_INFO, "Launcher", "Applying keymap file: " + selectedItemDirnameKeyfile); + printf("Applying keymap file cmd: \"%s\"\n", cmd.c_str()); + + /* Launch shell cmd */ + fp = popen(cmd.c_str(), "r"); + if (fp != NULL) { + pclose(fp); + } + } + + /* Apply specific key mapping for selected item if found */ + std::string selectedItemBasenameKeyfile = Utils::removeExtension(selectedItemsPath) + ".key"; + if (!access(selectedItemBasenameKeyfile.c_str(), R_OK)) { + + /* Create shell cmd */ + std::string cmd = SHELL_CMD_MAPPING_SET; + cmd += " " + selectedItemBasenameKeyfile; + + /* Log shell cmd */ + Logger::write(Logger::ZONE_INFO, "Launcher", "Applying keymap file: " + selectedItemBasenameKeyfile); + printf("Applying keymap file cmd: \"%s\"\n", cmd.c_str()); + + /* Launch shell cmd */ + fp = popen(cmd.c_str(), "r"); + if (fp != NULL) { + pclose(fp); + } + } + /* Restart audio amp */ fp = popen(SHELL_CMD_TURN_AMPLI_ON, "r"); if (fp != NULL) { @@ -139,6 +177,12 @@ bool Launcher::run(std::string collection, Item *collectionItem) pclose(fp); } + /* Reset default key mapping */ + fp = popen(SHELL_CMD_MAPPING_RESET, "r"); + if (fp != NULL) { + pclose(fp); + } + /* Restore stored PID */ char shellCmd[20]; sprintf(shellCmd, "%s %d", SHELL_CMD_RECORD_PID, getpid()); diff --git a/RetroFE/Source/Sound/Sound.cpp b/RetroFE/Source/Sound/Sound.cpp index d6103c8..d361efc 100755 --- a/RetroFE/Source/Sound/Sound.cpp +++ b/RetroFE/Source/Sound/Sound.cpp @@ -54,10 +54,10 @@ void Sound::play() SDL_RemoveTimer(idTimer); if(!ampliStarted){ fp = popen(SHELL_CMD_TURN_AMPLI_ON, "r"); - if (fp != NULL) { - ampliStarted = 1; - pclose(fp); - } + if (fp != NULL) { + ampliStarted = 1; + //pclose(fp); // --> regression, to investigate + } } if(chunk_) @@ -75,7 +75,7 @@ uint32_t Sound::turnOffAmpli(uint32_t interval, void *param) fp = popen(SHELL_CMD_TURN_AMPLI_OFF, "r"); if (fp != NULL) { ampliStarted = 0; - pclose(fp); + //pclose(fp); // --> regression, to investigate } return 0; } diff --git a/RetroFE/Source/Utility/Utils.cpp b/RetroFE/Source/Utility/Utils.cpp index 0a9dd75..d1e5149 100755 --- a/RetroFE/Source/Utility/Utils.cpp +++ b/RetroFE/Source/Utility/Utils.cpp @@ -255,6 +255,23 @@ std::string Utils::getFileName(std::string filePath) } +std::string Utils::removeExtension(std::string filePath) +{ + + /** Declared static to be kept in memory even after this function's scope */ + static std::string filename; + filename = filePath; + + const size_t lastPoint = filename.find_last_of("."); + if (std::string::npos != lastPoint) + { + filename = filePath.substr(0, lastPoint); + } + + return filename; +} + + std::string Utils::trimEnds(std::string str) { // strip off any initial tabs or spaces diff --git a/RetroFE/Source/Utility/Utils.h b/RetroFE/Source/Utility/Utils.h index 2b80728..a1def8a 100755 --- a/RetroFE/Source/Utility/Utils.h +++ b/RetroFE/Source/Utility/Utils.h @@ -25,6 +25,8 @@ #define SHELL_CMD_RECORD_PID "record_pid" #define SHELL_CMD_TURN_AMPLI_ON "start_audio_amp 1" #define SHELL_CMD_TURN_AMPLI_OFF "start_audio_amp 0" +#define SHELL_CMD_MAPPING_SET "keymap" +#define SHELL_CMD_MAPPING_RESET "keymap reset" class Utils { @@ -38,6 +40,7 @@ public: static std::string getDirectory(std::string filePath); static std::string getParentDirectory(std::string filePath); static std::string getFileName(std::string filePath); + static std::string removeExtension(std::string filePath); static bool findMatchingFile(std::string prefix, std::vector &extensions, std::string &file); static std::string toLower(std::string str); static std::string uppercaseFirst(std::string str);