From aaf3ab575ee7a5b3904f101ec6c424dce007d21f Mon Sep 17 00:00:00 2001 From: cold-brewed <47337035+cold-brewed@users.noreply.github.com> Date: Fri, 17 May 2024 15:09:04 -0400 Subject: [PATCH] Add new platform functions for global directories --- src/include/86box/plat.h | 4 +++- src/qt/qt_platform.cpp | 36 ++++++++++++++++++++++++++++-------- src/unix/unix.c | 31 ++++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/include/86box/plat.h b/src/include/86box/plat.h index 5b36bab7e..975133f6c 100644 --- a/src/include/86box/plat.h +++ b/src/include/86box/plat.h @@ -138,7 +138,9 @@ extern int plat_getcwd(char *bufp, int max); extern int plat_chdir(char *path); extern void plat_tempfile(char *bufp, char *prefix, char *suffix); extern void plat_get_exe_name(char *s, int size); -extern void plat_get_global_config_dir(char* strptr); +extern void plat_get_global_config_dir(char *outbuf, uint8_t len); +extern void plat_get_global_data_dir(char *outbuf, uint8_t len); +extern void plat_get_temp_dir(char *outbuf, uint8_t len); extern void plat_init_rom_paths(void); extern int plat_dir_check(char *path); extern int plat_dir_create(char *path); diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index e0b11ad7b..ef1942b3d 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -634,15 +634,34 @@ plat_chdir(char *path) } void -plat_get_global_config_dir(char* strptr) +plat_get_global_config_dir(char *outbuf, const uint8_t len) { -#ifdef __APPLE__ - auto dir = QDir(QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)[0] + "/net.86Box.86Box/"); -#else - auto dir = QDir(QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)[0] + "/86Box/"); -#endif - if (!dir.exists()) dir.mkpath("."); - strncpy(strptr, dir.canonicalPath().toUtf8().constData(), 1024); + const auto dir = QDir(QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation)[0]); + if (!dir.exists()) { + if (!dir.mkpath(".")) { + qWarning("Failed to create global configuration directory %s", dir.absolutePath().toUtf8().constData()); + } + } + strncpy(outbuf, dir.canonicalPath().toUtf8().constData(), len); +} + +void +plat_get_global_data_dir(char *outbuf, const uint8_t len) +{ + const auto dir = QDir(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)[0]); + if (!dir.exists()) { + if (!dir.mkpath(".")) { + qWarning("Failed to create global data directory %s", dir.absolutePath().toUtf8().constData()); + } + } + strncpy(outbuf, dir.canonicalPath().toUtf8().constData(), len); +} + +void +plat_get_temp_dir(char *outbuf, const uint8_t len) +{ + const auto dir = QDir(QStandardPaths::standardLocations(QStandardPaths::TempLocation)[0]); + strncpy(outbuf, dir.canonicalPath().toUtf8().constData(), len); } void @@ -662,6 +681,7 @@ plat_init_rom_paths(void) for (auto &path : paths) { #ifdef __APPLE__ rom_add_path(QDir(path).filePath("net.86Box.86Box/roms").toUtf8().constData()); + rom_add_path(QDir(path).filePath("86Box/roms").toUtf8().constData()); #else rom_add_path(QDir(path).filePath("86Box/roms").toUtf8().constData()); #endif diff --git a/src/unix/unix.c b/src/unix/unix.c index 3eddc2bbb..58cb1448f 100644 --- a/src/unix/unix.c +++ b/src/unix/unix.c @@ -826,15 +826,32 @@ plat_init_rom_paths(void) } void -plat_get_global_config_dir(char *strptr) +plat_get_global_config_dir(char *outbuf, const uint8_t len) { -#ifdef __APPLE__ - char *prefPath = SDL_GetPrefPath(NULL, "net.86Box.86Box"); -#else char *prefPath = SDL_GetPrefPath(NULL, "86Box"); -#endif - strncpy(strptr, prefPath, 1024); - path_slash(strptr); + strncpy(outbuf, prefPath, len); + path_slash(outbuf); + SDL_free(prefPath); +} + +void +plat_get_global_data_dir(char *outbuf, const uint8_t len) +{ + char *prefPath = SDL_GetPrefPath(NULL, "86Box"); + strncpy(outbuf, prefPath, len); + path_slash(outbuf); + SDL_free(prefPath); +} + +void +plat_get_temp_dir(char *outbuf, uint8_t len) +{ + const char *tmpdir = getenv("TMPDIR"); + if (tmpdir == NULL) { + tmpdir = "/tmp"; + } + strncpy(outbuf, tmpdir, len); + path_slash(outbuf); } bool