Skip to content

Commit 4e9b31b

Browse files
committed
applying keymap files if found. Fixed sound regression due to pclose
1 parent 152bae7 commit 4e9b31b

File tree

5 files changed

+107
-43
lines changed

5 files changed

+107
-43
lines changed

RetroFE/Source/Database/Configuration.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -228,48 +228,48 @@ bool Configuration::importCurrentLayout(std::string folder, std::string file, bo
228228
// line empty
229229
if(line.empty() || (line.find_first_not_of(" \t\r") == std::string::npos))
230230
{
231-
retVal = false;
231+
retVal = false;
232232
}
233233
// finding layout in existing list
234234
else
235235
{
236-
std::string seekedLayoutName = trimEnds(line);
237-
std::string layoutPathFound;
238-
bool layoutFoundInList = false;
239-
240-
// check existing layout list */
241-
for(std::vector<std::string>::iterator it = layouts_.begin(); it != layouts_.end(); ++it){
242-
std::string curLayoutName = Utils::getFileName(*it);
243-
if(!curLayoutName.compare(seekedLayoutName)){
244-
layoutPathFound = *it;
245-
layoutFoundInList = true;
246-
break;
247-
}
248-
index++;
249-
}
250-
251-
if (layoutFoundInList){
252-
253-
/* remove layout properties if they already exist */
254-
if(properties_.find("layout") != properties_.end())
255-
{
256-
properties_.erase("layout");
257-
}
258-
259-
/* Set new pair <key, value> for key = layout */
260-
properties_.insert(PropertiesPair("layout", seekedLayoutName));
261-
262-
std::stringstream ss;
263-
//printf("Found layout: %s at idx %d\n", layoutPathFound.c_str(), index);
264-
ss << "Found layout: " << "\"" << layoutPathFound << "\" in layouts list at idx " << index;
265-
Logger::write(Logger::ZONE_INFO, "Configuration", ss.str());
266-
retVal = true;
267-
268-
break;
269-
}
270-
else{
271-
index = 0;
272-
}
236+
std::string seekedLayoutName = trimEnds(line);
237+
std::string layoutPathFound;
238+
bool layoutFoundInList = false;
239+
240+
// check existing layout list */
241+
for(std::vector<std::string>::iterator it = layouts_.begin(); it != layouts_.end(); ++it){
242+
std::string curLayoutName = Utils::getFileName(*it);
243+
if(!curLayoutName.compare(seekedLayoutName)){
244+
layoutPathFound = *it;
245+
layoutFoundInList = true;
246+
break;
247+
}
248+
index++;
249+
}
250+
251+
if (layoutFoundInList){
252+
253+
/* remove layout properties if they already exist */
254+
if(properties_.find("layout") != properties_.end())
255+
{
256+
properties_.erase("layout");
257+
}
258+
259+
/* Set new pair <key, value> for key = layout */
260+
properties_.insert(PropertiesPair("layout", seekedLayoutName));
261+
262+
std::stringstream ss;
263+
//printf("Found layout: %s at idx %d\n", layoutPathFound.c_str(), index);
264+
ss << "Found layout: " << "\"" << layoutPathFound << "\" in layouts list at idx " << index;
265+
Logger::write(Logger::ZONE_INFO, "Configuration", ss.str());
266+
retVal = true;
267+
268+
break;
269+
}
270+
else{
271+
index = 0;
272+
}
273273
}
274274
}
275275

RetroFE/Source/Execute/Launcher.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,44 @@ bool Launcher::run(std::string collection, Item *collectionItem)
120120
selectedItemsDirectory,
121121
collection);
122122

123+
/* Apply key mapping for selected item's directory if found */
124+
std::string selectedItemDirnameKeyfile = Utils::getDirectory(selectedItemsPath) + "/default_config.key";
125+
if (!access(selectedItemDirnameKeyfile.c_str(), R_OK)) {
126+
127+
/* Create shell cmd */
128+
std::string cmd = SHELL_CMD_MAPPING_SET;
129+
cmd += " " + selectedItemDirnameKeyfile;
130+
131+
/* Log shell cmd */
132+
Logger::write(Logger::ZONE_INFO, "Launcher", "Applying keymap file: " + selectedItemDirnameKeyfile);
133+
printf("Applying keymap file cmd: \"%s\"\n", cmd.c_str());
134+
135+
/* Launch shell cmd */
136+
fp = popen(cmd.c_str(), "r");
137+
if (fp != NULL) {
138+
pclose(fp);
139+
}
140+
}
141+
142+
/* Apply specific key mapping for selected item if found */
143+
std::string selectedItemBasenameKeyfile = Utils::removeExtension(selectedItemsPath) + ".key";
144+
if (!access(selectedItemBasenameKeyfile.c_str(), R_OK)) {
145+
146+
/* Create shell cmd */
147+
std::string cmd = SHELL_CMD_MAPPING_SET;
148+
cmd += " " + selectedItemBasenameKeyfile;
149+
150+
/* Log shell cmd */
151+
Logger::write(Logger::ZONE_INFO, "Launcher", "Applying keymap file: " + selectedItemBasenameKeyfile);
152+
printf("Applying keymap file cmd: \"%s\"\n", cmd.c_str());
153+
154+
/* Launch shell cmd */
155+
fp = popen(cmd.c_str(), "r");
156+
if (fp != NULL) {
157+
pclose(fp);
158+
}
159+
}
160+
123161
/* Restart audio amp */
124162
fp = popen(SHELL_CMD_TURN_AMPLI_ON, "r");
125163
if (fp != NULL) {
@@ -139,6 +177,12 @@ bool Launcher::run(std::string collection, Item *collectionItem)
139177
pclose(fp);
140178
}
141179

180+
/* Reset default key mapping */
181+
fp = popen(SHELL_CMD_MAPPING_RESET, "r");
182+
if (fp != NULL) {
183+
pclose(fp);
184+
}
185+
142186
/* Restore stored PID */
143187
char shellCmd[20];
144188
sprintf(shellCmd, "%s %d", SHELL_CMD_RECORD_PID, getpid());

RetroFE/Source/Sound/Sound.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ void Sound::play()
5454
SDL_RemoveTimer(idTimer);
5555
if(!ampliStarted){
5656
fp = popen(SHELL_CMD_TURN_AMPLI_ON, "r");
57-
if (fp != NULL) {
58-
ampliStarted = 1;
59-
pclose(fp);
60-
}
57+
if (fp != NULL) {
58+
ampliStarted = 1;
59+
//pclose(fp); // --> regression, to investigate
60+
}
6161
}
6262

6363
if(chunk_)
@@ -75,7 +75,7 @@ uint32_t Sound::turnOffAmpli(uint32_t interval, void *param)
7575
fp = popen(SHELL_CMD_TURN_AMPLI_OFF, "r");
7676
if (fp != NULL) {
7777
ampliStarted = 0;
78-
pclose(fp);
78+
//pclose(fp); // --> regression, to investigate
7979
}
8080
return 0;
8181
}

RetroFE/Source/Utility/Utils.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ std::string Utils::getFileName(std::string filePath)
255255
}
256256

257257

258+
std::string Utils::removeExtension(std::string filePath)
259+
{
260+
261+
/** Declared static to be kept in memory even after this function's scope */
262+
static std::string filename;
263+
filename = filePath;
264+
265+
const size_t lastPoint = filename.find_last_of(".");
266+
if (std::string::npos != lastPoint)
267+
{
268+
filename = filePath.substr(0, lastPoint);
269+
}
270+
271+
return filename;
272+
}
273+
274+
258275
std::string Utils::trimEnds(std::string str)
259276
{
260277
// strip off any initial tabs or spaces

RetroFE/Source/Utility/Utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#define SHELL_CMD_RECORD_PID "record_pid"
2626
#define SHELL_CMD_TURN_AMPLI_ON "start_audio_amp 1"
2727
#define SHELL_CMD_TURN_AMPLI_OFF "start_audio_amp 0"
28+
#define SHELL_CMD_MAPPING_SET "keymap"
29+
#define SHELL_CMD_MAPPING_RESET "keymap reset"
2830

2931
class Utils
3032
{
@@ -38,6 +40,7 @@ class Utils
3840
static std::string getDirectory(std::string filePath);
3941
static std::string getParentDirectory(std::string filePath);
4042
static std::string getFileName(std::string filePath);
43+
static std::string removeExtension(std::string filePath);
4144
static bool findMatchingFile(std::string prefix, std::vector<std::string> &extensions, std::string &file);
4245
static std::string toLower(std::string str);
4346
static std::string uppercaseFirst(std::string str);

0 commit comments

Comments
 (0)