From 67dec216d2b11a7d6948b3dc047026330448ee63 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 30 May 2020 12:31:26 +1000 Subject: [PATCH] [host] search the applications local directory for the config --- VERSION | 2 +- host/include/interface/platform.h | 1 + host/platform/Linux/src/platform.c | 14 +++++++++++++- host/platform/Windows/src/platform.c | 18 ++++++++++++++++++ host/src/app.c | 19 ++++++++++++++++++- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 739d8b17..deaf99cd 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B2-rc2-7-ge8c949c1e7+1 \ No newline at end of file +B2-rc2-8-gfcbdf7ba4f+1 \ No newline at end of file diff --git a/host/include/interface/platform.h b/host/include/interface/platform.h index f5165179..0d8e9a39 100644 --- a/host/include/interface/platform.h +++ b/host/include/interface/platform.h @@ -27,3 +27,4 @@ void app_quit(); // these must be implemented for each OS const char * os_getExecutable(); +const char * os_getDataPath(); diff --git a/host/platform/Linux/src/platform.c b/host/platform/Linux/src/platform.c index cd953f3c..afe371bc 100644 --- a/host/platform/Linux/src/platform.c +++ b/host/platform/Linux/src/platform.c @@ -30,6 +30,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA struct app { const char * executable; + char * dataPath; }; struct app app = { 0 }; @@ -37,7 +38,13 @@ struct app app = { 0 }; int main(int argc, char * argv[]) { app.executable = argv[0]; + + struct passwd * pw = getpwuid(getuid()); + alloc_sprintf(&app.dataPath, "%s/", pw->pw_dir); + int result = app_main(argc, argv); + + free(app.dataPath); return result; } @@ -56,4 +63,9 @@ bool app_init() const char * os_getExecutable() { return app.executable; -} \ No newline at end of file +} + +const char * os_getDataPath() +{ + return app.dataPath; +} diff --git a/host/platform/Windows/src/platform.c b/host/platform/Windows/src/platform.c index 10d392e8..f63e9f0e 100644 --- a/host/platform/Windows/src/platform.c +++ b/host/platform/Windows/src/platform.c @@ -303,6 +303,24 @@ const char * os_getExecutable() return app.executable; } +const char * os_getDataPath() +{ + static char path[MAX_PATH] = { 0 }; + if (!path[0]) + { + if (!GetModuleFileName(NULL, path, MAX_PATH)) + return NULL; + + char *p = strrchr(path, '\\'); + if (!p) + return NULL; + + ++p; + *p = '\0'; + } + return path; +} + HWND os_getMessageWnd() { return app.messageWnd; diff --git a/host/src/app.c b/host/src/app.c index 22e48012..44f83cb2 100644 --- a/host/src/app.c +++ b/host/src/app.c @@ -38,6 +38,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include +#define CONFIG_FILE "looking-glass-host.ini" + #define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F) #define ALIGN_UP(x) ALIGN_DN(x + 0x7F) @@ -400,7 +402,22 @@ int app_main(int argc, char * argv[]) CaptureInterfaces[i]->initOptions(); // try load values from a config file - option_load("looking-glass-host.ini"); + const char * dataPath = os_getDataPath(); + if (!dataPath) + { + option_free(); + DEBUG_ERROR("Failed to get the application's data path"); + return -1; + } + + const size_t len = strlen(dataPath) + sizeof(CONFIG_FILE) + 1; + char configFile[len]; + snprintf(configFile, sizeof(configFile), "%s%s", dataPath, CONFIG_FILE); + DEBUG_INFO("Looking for configuration file at: %s", configFile); + if (option_load(configFile)) + DEBUG_INFO("Configuration file loaded"); + else + DEBUG_INFO("Configuration file not found or invalid"); // parse the command line arguments if (!option_parse(argc, argv))