From 4648cea7be3864fff423f29030247f561d848455 Mon Sep 17 00:00:00 2001 From: Synth_Magic Date: Sat, 4 Jan 2025 11:01:47 +0800 Subject: [PATCH 1/4] Add config path sepcification --- src/lindbergh/config.c | 21 ++++++++++++++------- src/lindbergh/config.h | 2 +- src/lindbergh/hook.c | 6 ++++-- src/lindbergh/lindbergh.c | 27 ++++++++++++++++++++++++--- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/lindbergh/config.c b/src/lindbergh/config.c index 447da67..c1e0023 100644 --- a/src/lindbergh/config.c +++ b/src/lindbergh/config.c @@ -1297,7 +1297,7 @@ KeyMapping getDefaultKeymap() return defaultKeyMapping; } -int initConfig() +int initConfig(const char* configPath) { config.emulateRideboard = 0; config.emulateDriveboard = 0; @@ -1349,14 +1349,21 @@ int initConfig() config.inputMode = 0; // Default to all inputs - configFile = fopen(CONFIG_PATH, "r"); - - if (configFile == NULL) - { - printf("Warning: Cannot open %s, using default values.\n", CONFIG_PATH); - return 1; + // first we try to read the external config path + configFile = fopen(configPath, "r"); + if (configFile != NULL) { + printf("Found lindbergh loader config at %s in system environment\n",configPath); + } else { + // then we try to read through default config path + configFile = fopen(CONFIG_PATH, "r"); + if (configFile == NULL) + { + printf("Warning: Cannot open %s, using default values.\n", CONFIG_PATH); + return 1; + } } + readConfig(configFile, &config); fclose(configFile); diff --git a/src/lindbergh/config.h b/src/lindbergh/config.h index 8c42a2a..8a793ac 100644 --- a/src/lindbergh/config.h +++ b/src/lindbergh/config.h @@ -264,7 +264,7 @@ typedef struct } EmulatorConfig; KeyMapping getDefaultKeymap(); -int initConfig(); +int initConfig(const char* configPath); EmulatorConfig *getConfig(); char *getGameName(); char *getDVPName(); diff --git a/src/lindbergh/hook.c b/src/lindbergh/hook.c index bc1869a..3b5914c 100644 --- a/src/lindbergh/hook.c +++ b/src/lindbergh/hook.c @@ -49,7 +49,6 @@ #include "evdevinput.h" #define HOOK_FILE_NAME "/dev/zero" - #define BASEBOARD 0 #define EEPROM 1 #define SERIAL0 2 @@ -171,8 +170,11 @@ void __attribute__((constructor)) hook_init() act.sa_sigaction = handleSegfault; act.sa_flags = SA_SIGINFO; sigaction(SIGSEGV, &act, NULL); + // use unmodified version of getenv + char *(*_getenv)(const char *name) = dlsym(RTLD_NEXT, "getenv"); + char* envPath = _getenv("LINDBERGH_CONFIG_PATH"); - initConfig(); + initConfig(envPath); if (getConfig()->fpsLimiter == 1) { diff --git a/src/lindbergh/lindbergh.c b/src/lindbergh/lindbergh.c index 3dc41fe..985a05c 100644 --- a/src/lindbergh/lindbergh.c +++ b/src/lindbergh/lindbergh.c @@ -8,6 +8,7 @@ #include "version.h" #define LD_LIBRARY_PATH "LD_LIBRARY_PATH" +#define LINDBERGH_CONFIG_PATH "LINDBERGH_CONFIG_PATH" #define LD_PRELOAD "LD_PRELOAD" #define PRELOAD_FILE_NAME "lindbergh.so" #define TEAM "bobbydilley, retrofan, dkeruza-neo, doozer, francesco, rolel, caviar-x" @@ -117,6 +118,7 @@ void printUsage(char *argv[]) printf(" --list-controllers Lists available controllers and inputs\n"); printf(" --version Displays the version of the loader and team's names\n"); printf(" --help Displays this usage text\n"); + printf(" --config | -c Specifies configuration path\n"); } /** @@ -246,8 +248,10 @@ int main(int argc, char *argv[]) int gdb = 0; int forceGame = 0; int segaboot = 0; - + int extConfig = 0; + char envConfigPath[256] = {0}; char forceGamePath[128] = {0}; + for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--test") == 0) @@ -267,12 +271,29 @@ int main(int argc, char *argv[]) gdb = 1; continue; } - + if (strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-c") == 0) + { + // prevent wild pointer + if (i+1 >= argc) + { + printf("Unable to read config file because it's the end of argument list\n"); + break; + } + extConfig = 1; + strcpy(envConfigPath, argv[i+1]); + // skip the next argument + i += 1; + continue; + } // Treat the argument as the game name strcpy(forceGamePath, argv[i]); forceGame = 1; } - + if (extConfig) + { + // always override the old environment + setenv(LINDBERGH_CONFIG_PATH, envConfigPath, 1); + } char command[128] = {0}; strcpy(command, "./"); if (forceGame) From ea4745f7585fd5b3c3d0d125a370dac17b30a58e Mon Sep 17 00:00:00 2001 From: Synth_Magic Date: Sun, 5 Jan 2025 11:51:47 +0800 Subject: [PATCH 2/4] Do changes as requested --- src/lindbergh/config.c | 23 +++++++++++------------ src/lindbergh/lindbergh.c | 6 +++++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/lindbergh/config.c b/src/lindbergh/config.c index c1e0023..dffa1ef 100644 --- a/src/lindbergh/config.c +++ b/src/lindbergh/config.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "config.h" #include "gpuvendor.h" @@ -1348,22 +1349,20 @@ int initConfig(const char* configPath) } config.inputMode = 0; // Default to all inputs + char* configFilePath = CONFIG_PATH; + if (configPath != NULL) + strcpy(configFilePath, configPath); - // first we try to read the external config path - configFile = fopen(configPath, "r"); - if (configFile != NULL) { - printf("Found lindbergh loader config at %s in system environment\n",configPath); - } else { - // then we try to read through default config path - configFile = fopen(CONFIG_PATH, "r"); - if (configFile == NULL) - { - printf("Warning: Cannot open %s, using default values.\n", CONFIG_PATH); - return 1; - } + configFile = fopen(configFilePath, "r"); + + if (configFile == NULL) + { + printf("Warning: Cannot open %s, using default config values.\n", configFilePath); + return 1; } + readConfig(configFile, &config); fclose(configFile); diff --git a/src/lindbergh/lindbergh.c b/src/lindbergh/lindbergh.c index 985a05c..bc7dce3 100644 --- a/src/lindbergh/lindbergh.c +++ b/src/lindbergh/lindbergh.c @@ -1,8 +1,10 @@ #include +#include #include #include #include #include +#include #include "evdevinput.h" #include "version.h" @@ -249,7 +251,7 @@ int main(int argc, char *argv[]) int forceGame = 0; int segaboot = 0; int extConfig = 0; - char envConfigPath[256] = {0}; + char envConfigPath[PATH_MAX] = {0}; char forceGamePath[128] = {0}; for (int i = 1; i < argc; i++) @@ -279,8 +281,10 @@ int main(int argc, char *argv[]) printf("Unable to read config file because it's the end of argument list\n"); break; } + extConfig = 1; strcpy(envConfigPath, argv[i+1]); + // skip the next argument i += 1; continue; From 13118eb0558e2c5769237afd02b2d23c1ed3cbc4 Mon Sep 17 00:00:00 2001 From: Synth_Magic Date: Sun, 5 Jan 2025 11:56:51 +0800 Subject: [PATCH 3/4] Delete linux/limits.h because clangd automatically append it --- src/lindbergh/lindbergh.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lindbergh/lindbergh.c b/src/lindbergh/lindbergh.c index bc7dce3..bca92ec 100644 --- a/src/lindbergh/lindbergh.c +++ b/src/lindbergh/lindbergh.c @@ -1,5 +1,4 @@ #include -#include #include #include #include From 5f0923f89292f09e7a7144c02c75193db26e0248 Mon Sep 17 00:00:00 2001 From: Synth_Magic Date: Sun, 5 Jan 2025 11:59:21 +0800 Subject: [PATCH 4/4] remove time.h because clangd automatically appended it --- src/lindbergh/config.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lindbergh/config.c b/src/lindbergh/config.c index dffa1ef..9fa0767 100644 --- a/src/lindbergh/config.c +++ b/src/lindbergh/config.c @@ -2,7 +2,6 @@ #include #include #include -#include #include "config.h" #include "gpuvendor.h"