Move platform-specific ROM path adding code into plat modules

This commit is contained in:
Cacodemon345
2022-04-06 16:16:25 +06:00
parent 2c63f5b497
commit 1a3a7bec0e
6 changed files with 145 additions and 49 deletions

View File

@@ -385,7 +385,7 @@ pc_log(const char *fmt, ...)
#define pc_log(fmt, ...)
#endif
static void
void
add_rom_path(const char* path)
{
static char cwd[1024];
@@ -673,54 +673,7 @@ usage:
}
}
#if !defined __APPLE__ && !defined _WIN32
if (getenv("XDG_DATA_HOME")) {
char xdg_rom_path[1024] = { 0 };
strncpy(xdg_rom_path, getenv("XDG_DATA_HOME"), 1024);
plat_path_slash(xdg_rom_path);
strncat(xdg_rom_path, "86Box/", 1024);
if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path);
strcat(xdg_rom_path, "roms/");
if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path);
add_rom_path(xdg_rom_path);
} else {
char home_rom_path[1024] = { 0 };
snprintf(home_rom_path, 1024, "%s/.local/share/86Box/", getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir);
if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path);
strcat(home_rom_path, "roms/");
if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path);
add_rom_path(home_rom_path);
}
if (getenv("XDG_DATA_DIRS")) {
char* xdg_rom_paths = strdup(getenv("XDG_DATA_DIRS"));
char* xdg_rom_paths_orig = xdg_rom_paths;
char* cur_xdg_rom_path = NULL;
if (xdg_rom_paths) {
while (xdg_rom_paths[strlen(xdg_rom_paths) - 1] == ':') {
xdg_rom_paths[strlen(xdg_rom_paths) - 1] = '\0';
}
while ((cur_xdg_rom_path = local_strsep(&xdg_rom_paths, ";")) != NULL) {
char real_xdg_rom_path[1024] = { '\0' };
strcat(real_xdg_rom_path, cur_xdg_rom_path);
plat_path_slash(real_xdg_rom_path);
strcat(real_xdg_rom_path, "86Box/roms/");
add_rom_path(real_xdg_rom_path);
}
}
free(xdg_rom_paths_orig);
} else {
add_rom_path("/usr/local/share/86Box/roms/");
add_rom_path("/usr/share/86Box/roms/");
}
#endif
plat_init_rom_paths();
/*
* If the user provided a path for ROMs, use that

View File

@@ -104,6 +104,7 @@ 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_init_rom_paths();
extern char *plat_get_basename(const char *path);
extern void plat_get_dirname(char *dest, const char *path);
extern char *plat_get_filename(char *s);

View File

@@ -48,6 +48,8 @@ typedef struct rom_path_t {
extern rom_path_t rom_paths;
extern void add_rom_path(const char* path);
extern uint8_t rom_read(uint32_t addr, void *p);
extern uint16_t rom_readw(uint32_t addr, void *p);
extern uint32_t rom_readl(uint32_t addr, void *p);

View File

@@ -88,6 +88,8 @@ extern "C" {
#include <86box/timer.h>
#include <86box/nvr.h>
#include <86box/plat_dynld.h>
#include <86box/mem.h>
#include <86box/rom.h>
#include <86box/config.h>
#include <86box/ui.h>
#include <86box/discord.h>
@@ -583,3 +585,60 @@ plat_chdir(char *path)
{
return QDir::setCurrent(QString(path)) ? 0 : -1;
}
void
plat_init_rom_paths()
{
#if !defined __APPLE__ && !defined _WIN32
if (getenv("XDG_DATA_HOME")) {
char xdg_rom_path[1024] = { 0 };
strncpy(xdg_rom_path, getenv("XDG_DATA_HOME"), 1024);
plat_path_slash(xdg_rom_path);
strncat(xdg_rom_path, "86Box/", 1024);
if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path);
strcat(xdg_rom_path, "roms/");
if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path);
add_rom_path(xdg_rom_path);
} else {
char home_rom_path[1024] = { 0 };
snprintf(home_rom_path, 1024, "%s/.local/share/86Box/", getenv("HOME") ? getenv("HOME") : QDir::homePath().toUtf8().constData());
if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path);
strcat(home_rom_path, "roms/");
if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path);
add_rom_path(home_rom_path);
}
if (getenv("XDG_DATA_DIRS")) {
char* xdg_rom_paths = strdup(getenv("XDG_DATA_DIRS"));
char* xdg_rom_paths_orig = xdg_rom_paths;
char* cur_xdg_rom_path = NULL;
if (xdg_rom_paths) {
while (xdg_rom_paths[strlen(xdg_rom_paths) - 1] == ':') {
xdg_rom_paths[strlen(xdg_rom_paths) - 1] = '\0';
}
QStringList path_list = QString(xdg_rom_paths).split(":", Qt::SkipEmptyParts);
for (auto& cur_path : path_list) {
if (cur_path.right(1) != '/')
cur_path.push_back('/');
add_rom_path((cur_path + "86Box/roms").toUtf8().constData());
}
}
free(xdg_rom_paths_orig);
} else {
add_rom_path("/usr/local/share/86Box/roms/");
add_rom_path("/usr/share/86Box/roms/");
}
#elif _WIN32
auto appDataDir = QDir(qEnvironmentVariable("LOCALAPPDATA"));
appDataDir.mkdir("86Box");
appDataDir.mkdir("86Box/roms");
add_rom_path((appDataDir.path().replace("\\","/") + "/86Box/roms").toUtf8().constData());
#endif
}

View File

@@ -21,6 +21,8 @@
#include <stdatomic.h>
#include <86box/86box.h>
#include <86box/mem.h>
#include <86box/rom.h>
#include <86box/keyboard.h>
#include <86box/mouse.h>
#include <86box/config.h>
@@ -744,6 +746,59 @@ plat_pause(int p)
}
}
void
plat_init_rom_paths()
{
#ifndef __APPLE__
if (getenv("XDG_DATA_HOME")) {
char xdg_rom_path[1024] = { 0 };
strncpy(xdg_rom_path, getenv("XDG_DATA_HOME"), 1024);
plat_path_slash(xdg_rom_path);
strncat(xdg_rom_path, "86Box/", 1024);
if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path);
strcat(xdg_rom_path, "roms/");
if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path);
add_rom_path(xdg_rom_path);
} else {
char home_rom_path[1024] = { 0 };
snprintf(home_rom_path, 1024, "%s/.local/share/86Box/", getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir);
if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path);
strcat(home_rom_path, "roms/");
if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path);
add_rom_path(home_rom_path);
}
if (getenv("XDG_DATA_DIRS")) {
char* xdg_rom_paths = strdup(getenv("XDG_DATA_DIRS"));
char* xdg_rom_paths_orig = xdg_rom_paths;
char* cur_xdg_rom_path = NULL;
if (xdg_rom_paths) {
while (xdg_rom_paths[strlen(xdg_rom_paths) - 1] == ':') {
xdg_rom_paths[strlen(xdg_rom_paths) - 1] = '\0';
}
while ((cur_xdg_rom_path = local_strsep(&xdg_rom_paths, ";")) != NULL) {
char real_xdg_rom_path[1024] = { '\0' };
strcat(real_xdg_rom_path, cur_xdg_rom_path);
plat_path_slash(real_xdg_rom_path);
strcat(real_xdg_rom_path, "86Box/roms/");
add_rom_path(real_xdg_rom_path);
}
}
free(xdg_rom_paths_orig);
} else {
add_rom_path("/usr/local/share/86Box/roms/");
add_rom_path("/usr/share/86Box/roms/");
}
#endif
}
bool process_media_commands_3(uint8_t* id, char* fn, uint8_t* wp, int cmdargc)
{
bool err = false;

View File

@@ -45,6 +45,8 @@
#include <86box/timer.h>
#include <86box/nvr.h>
#include <86box/video.h>
#include <86box/mem.h>
#include <86box/rom.h>
#define GLOBAL
#include <86box/plat.h>
#include <86box/ui.h>
@@ -910,6 +912,30 @@ plat_mmap(size_t size, uint8_t executable)
}
void
plat_init_rom_paths()
{
wchar_t appdata_dir[1024] = { L'\0' };
if (_wgetenv("LOCALAPPDATA") && _wgetenv("LOCALAPPDATA")[0] != L'\0') {
char appdata_dir_a[1024] = { '\0' };
size_t len = 0;
wcsncpy(appdata_dir, _wgetenv("LOCALAPPDATA"), 1024);
len = wcslen(appdata_dir);
if (appdata_dir[len - 1] != L'\\') {
appdata_dir[len] = L'\\';
appdata_dir[len + 1] = L'\0';
}
wcscat(appdata_dir, "86box");
CreateDirectoryW(appdata_dir, NULL);
wcscat(appdata_dir, "\\roms");
CreateDirectoryW(appdata_dir, NULL);
wcscat(appdata_dir, "\\");
c16stombs(appdata_dir_a, appdata_dir, 1024);
add_rom_path(appdata_dir_a);
}
}
void
plat_munmap(void *ptr, size_t size)
{