From ba86b58b806078a46e47c73db914dee7e57bb355 Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 31 Dec 2019 10:49:07 +0000 Subject: [PATCH] Added better resolution of exe directory; should fix #1 --- src/main.c | 62 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 51587c48..93749eeb 100644 --- a/src/main.c +++ b/src/main.c @@ -2,13 +2,58 @@ #include #include "api/api.h" #include "renderer.h" + #ifdef _WIN32 #include +#elif __linux__ + #include +#elif __APPLE__ + #include #endif SDL_Window *window; + +static double get_scale(void) { + float dpi; + SDL_GetDisplayDPI(0, NULL, &dpi, NULL); +#if _WIN32 + return dpi / 96.0; +#elif __APPLE__ + return dpi / 72.0; +#else + return 1.0; +#endif +} + + +const void get_exe_dir(char *buf, int sz) { +#if _WIN32 + int len = GetModuleFileName(NULL, buf, sz - 1); + buf[len] = '\0'; +#elif __linux__ + char path[512]; + sprintf(path, "/proc/%d/exe", getpid()); + int len = readlink(path, buf, sz - 1); + buf[len] = '\0'; +#elif __APPLE__ + unsigned size = sz; + _NSGetExecutablePath(buf, &size); +#else + strcpy(buf, ".") +#endif + + for (int i = strlen(buf) - 1; i > 0; i--) { + if (buf[i] == '/' || buf[i] == '\\') { + buf[i] = '\0'; + break; + } + } +} + + + int main(int argc, char **argv) { #ifdef _WIN32 HINSTANCE lib = LoadLibrary("user32.dll"); @@ -46,24 +91,21 @@ int main(int argc, char **argv) { lua_setglobal(L, "_ARGS"); - float dpi; - SDL_GetDisplayDPI(0, NULL, &dpi, NULL); -#if _WIN32 - lua_pushnumber(L, dpi / 96.0); -#elif __APPLE__ - lua_pushnumber(L, dpi / 72.0); -#else - lua_pushnumber(L, 1.0); -#endif + lua_pushnumber(L, get_scale()); lua_setglobal(L, "_SCALE"); + char exedir[2048]; + get_exe_dir(exedir, sizeof(exedir)); + lua_pushstring(L, exedir); + lua_setglobal(L, "_EXEDIR"); + + (void) luaL_dostring(L, "local core\n" "xpcall(function()\n" " _SCALE = tonumber(os.getenv(\"LITE_SCALE\")) or _SCALE\n" " _PATHSEP = package.config:sub(1, 1)\n" - " _EXEDIR = _ARGS[1]:match('(.*)[/\\\\].*$')\n" " package.path = _EXEDIR .. '/data/?.lua;' .. package.path\n" " package.path = _EXEDIR .. '/data/?/init.lua;' .. package.path\n" " core = require('core')\n"