2015-02-03 07:52:37 +00:00
|
|
|
/* Copyright (c) 2013-2015 Jeffrey Pfau
|
2014-12-03 08:39:06 +00:00
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2015-02-03 07:52:37 +00:00
|
|
|
#include "config.h"
|
2014-11-01 10:04:10 +00:00
|
|
|
|
2015-02-11 08:19:55 +00:00
|
|
|
#include "util/formatting.h"
|
|
|
|
|
2014-11-10 07:27:43 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2014-11-11 08:46:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <strsafe.h>
|
2014-12-21 02:41:41 +00:00
|
|
|
#define PATH_SEP "\\"
|
|
|
|
#else
|
|
|
|
#define PATH_SEP "/"
|
2014-11-11 08:46:30 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-01 10:04:10 +00:00
|
|
|
#define SECTION_NAME_MAX 128
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
static const char* _lookupValue(const struct GBAConfig* config, const char* key) {
|
|
|
|
const char* value;
|
|
|
|
if (config->port) {
|
|
|
|
value = ConfigurationGetValue(&config->configTable, config->port, key);
|
2014-11-01 10:04:10 +00:00
|
|
|
if (value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 07:54:09 +00:00
|
|
|
value = ConfigurationGetValue(&config->configTable, 0, key);
|
|
|
|
if (value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (config->port) {
|
|
|
|
value = ConfigurationGetValue(&config->defaultsTable, config->port, key);
|
|
|
|
if (value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ConfigurationGetValue(&config->defaultsTable, 0, key);
|
2014-11-01 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
static bool _lookupCharValue(const struct GBAConfig* config, const char* key, char** out) {
|
|
|
|
const char* value = _lookupValue(config, key);
|
2014-11-02 08:31:12 +00:00
|
|
|
if (!value) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-05 07:54:09 +00:00
|
|
|
if (*out) {
|
|
|
|
free(*out);
|
|
|
|
}
|
2014-11-02 08:31:12 +00:00
|
|
|
*out = strdup(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
static bool _lookupIntValue(const struct GBAConfig* config, const char* key, int* out) {
|
|
|
|
const char* charValue = _lookupValue(config, key);
|
2014-11-01 10:04:10 +00:00
|
|
|
if (!charValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char* end;
|
|
|
|
long value = strtol(charValue, &end, 10);
|
|
|
|
if (*end) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
static bool _lookupUIntValue(const struct GBAConfig* config, const char* key, unsigned* out) {
|
|
|
|
const char* charValue = _lookupValue(config, key);
|
2014-11-02 10:34:16 +00:00
|
|
|
if (!charValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char* end;
|
|
|
|
unsigned long value = strtoul(charValue, &end, 10);
|
|
|
|
if (*end) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
static bool _lookupFloatValue(const struct GBAConfig* config, const char* key, float* out) {
|
|
|
|
const char* charValue = _lookupValue(config, key);
|
2014-11-02 10:34:16 +00:00
|
|
|
if (!charValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char* end;
|
2015-02-11 08:19:55 +00:00
|
|
|
float value = strtof_u(charValue, &end);
|
2014-11-02 10:34:16 +00:00
|
|
|
if (*end) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
void GBAConfigInit(struct GBAConfig* config, const char* port) {
|
|
|
|
ConfigurationInit(&config->configTable);
|
|
|
|
ConfigurationInit(&config->defaultsTable);
|
2014-11-17 09:17:56 +00:00
|
|
|
if (port) {
|
|
|
|
config->port = malloc(strlen("ports.") + strlen(port) + 1);
|
|
|
|
snprintf(config->port, strlen("ports.") + strlen(port) + 1, "ports.%s", port);
|
|
|
|
} else {
|
|
|
|
config->port = 0;
|
|
|
|
}
|
2014-11-05 07:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigDeinit(struct GBAConfig* config) {
|
|
|
|
ConfigurationDeinit(&config->configTable);
|
|
|
|
ConfigurationDeinit(&config->defaultsTable);
|
|
|
|
free(config->port);
|
2014-11-01 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
bool GBAConfigLoad(struct GBAConfig* config) {
|
2014-11-10 07:27:43 +00:00
|
|
|
char path[PATH_MAX];
|
2014-12-21 02:41:41 +00:00
|
|
|
GBAConfigDirectory(path, PATH_MAX);
|
|
|
|
strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
|
2014-11-10 07:27:43 +00:00
|
|
|
return ConfigurationRead(&config->configTable, path);
|
2014-11-05 07:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GBAConfigSave(const struct GBAConfig* config) {
|
2014-11-10 07:27:43 +00:00
|
|
|
char path[PATH_MAX];
|
2014-12-21 02:41:41 +00:00
|
|
|
GBAConfigDirectory(path, PATH_MAX);
|
|
|
|
strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
|
|
|
|
return ConfigurationWrite(&config->configTable, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigDirectory(char* out, size_t outLength) {
|
2014-11-11 08:46:30 +00:00
|
|
|
#ifndef _WIN32
|
2014-11-10 07:27:43 +00:00
|
|
|
char* home = getenv("HOME");
|
2014-12-21 02:41:41 +00:00
|
|
|
snprintf(out, outLength, "%s/.config", home);
|
|
|
|
mkdir(out, 0755);
|
|
|
|
snprintf(out, outLength, "%s/.config/%s", home, BINARY_NAME);
|
|
|
|
mkdir(out, 0755);
|
2014-11-11 08:46:30 +00:00
|
|
|
#else
|
|
|
|
char home[MAX_PATH];
|
|
|
|
SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, home);
|
2014-12-21 02:41:41 +00:00
|
|
|
snprintf(out, outLength, "%s\\%s", home, PROJECT_NAME);
|
|
|
|
CreateDirectoryA(out, NULL);
|
2014-11-11 08:46:30 +00:00
|
|
|
#endif
|
2014-11-05 07:54:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 10:16:35 +00:00
|
|
|
const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) {
|
|
|
|
return _lookupValue(config, key);
|
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
void GBAConfigSetValue(struct GBAConfig* config, const char* key, const char* value) {
|
|
|
|
ConfigurationSetValue(&config->configTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetIntValue(struct GBAConfig* config, const char* key, int value) {
|
|
|
|
ConfigurationSetIntValue(&config->configTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
|
|
|
|
ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetFloatValue(struct GBAConfig* config, const char* key, float value) {
|
|
|
|
ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetDefaultValue(struct GBAConfig* config, const char* key, const char* value) {
|
|
|
|
ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetDefaultIntValue(struct GBAConfig* config, const char* key, int value) {
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetDefaultUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
|
|
|
|
ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigSetDefaultFloatValue(struct GBAConfig* config, const char* key, float value) {
|
|
|
|
ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBAConfigMap(const struct GBAConfig* config, struct GBAOptions* opts) {
|
|
|
|
_lookupCharValue(config, "bios", &opts->bios);
|
|
|
|
_lookupIntValue(config, "logLevel", &opts->logLevel);
|
|
|
|
_lookupIntValue(config, "frameskip", &opts->frameskip);
|
|
|
|
_lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
|
|
|
|
_lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
|
|
|
|
_lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
|
2014-11-02 10:34:16 +00:00
|
|
|
unsigned audioBuffers;
|
2014-11-05 07:54:09 +00:00
|
|
|
if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
|
2014-11-02 10:34:16 +00:00
|
|
|
opts->audioBuffers = audioBuffers;
|
|
|
|
}
|
2014-11-04 09:19:10 +00:00
|
|
|
|
|
|
|
int fakeBool;
|
2015-03-29 06:19:21 +00:00
|
|
|
if (_lookupIntValue(config, "useBios", &fakeBool)) {
|
2015-03-17 06:14:52 +00:00
|
|
|
opts->useBios = fakeBool;
|
|
|
|
}
|
2014-11-05 07:54:09 +00:00
|
|
|
if (_lookupIntValue(config, "audioSync", &fakeBool)) {
|
2014-11-04 09:19:10 +00:00
|
|
|
opts->audioSync = fakeBool;
|
|
|
|
}
|
2014-11-05 07:54:09 +00:00
|
|
|
if (_lookupIntValue(config, "videoSync", &fakeBool)) {
|
2014-11-04 09:19:10 +00:00
|
|
|
opts->videoSync = fakeBool;
|
|
|
|
}
|
2014-12-16 09:33:08 +00:00
|
|
|
if (_lookupIntValue(config, "lockAspectRatio", &fakeBool)) {
|
|
|
|
opts->lockAspectRatio = fakeBool;
|
|
|
|
}
|
2014-12-22 00:50:55 +00:00
|
|
|
if (_lookupIntValue(config, "resampleVideo", &fakeBool)) {
|
|
|
|
opts->resampleVideo = fakeBool;
|
|
|
|
}
|
2014-12-22 01:48:36 +00:00
|
|
|
if (_lookupIntValue(config, "skipBios", &fakeBool)) {
|
|
|
|
opts->skipBios = fakeBool;
|
|
|
|
}
|
2015-01-06 07:11:56 +00:00
|
|
|
if (_lookupIntValue(config, "rewindEnable", &fakeBool)) {
|
|
|
|
opts->rewindEnable = fakeBool;
|
|
|
|
}
|
2014-11-05 07:54:09 +00:00
|
|
|
|
|
|
|
_lookupIntValue(config, "fullscreen", &opts->fullscreen);
|
|
|
|
_lookupIntValue(config, "width", &opts->width);
|
|
|
|
_lookupIntValue(config, "height", &opts->height);
|
2015-01-13 10:39:48 +00:00
|
|
|
|
2015-01-22 07:07:04 +00:00
|
|
|
char* idleOptimization = 0;
|
2015-01-13 10:39:48 +00:00
|
|
|
if (_lookupCharValue(config, "idleOptimization", &idleOptimization)) {
|
|
|
|
if (strcasecmp(idleOptimization, "ignore") == 0) {
|
|
|
|
opts->idleOptimization = IDLE_LOOP_IGNORE;
|
|
|
|
} else if (strcasecmp(idleOptimization, "remove") == 0) {
|
|
|
|
opts->idleOptimization = IDLE_LOOP_REMOVE;
|
|
|
|
} else if (strcasecmp(idleOptimization, "detect") == 0) {
|
|
|
|
opts->idleOptimization = IDLE_LOOP_DETECT;
|
|
|
|
}
|
|
|
|
free(idleOptimization);
|
|
|
|
}
|
2014-11-01 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 07:54:09 +00:00
|
|
|
void GBAConfigLoadDefaults(struct GBAConfig* config, const struct GBAOptions* opts) {
|
|
|
|
ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
|
2014-12-22 01:48:36 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "skipBios", opts->skipBios);
|
2015-03-17 06:14:52 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "useBios", opts->useBios);
|
2014-11-05 07:54:09 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
|
2015-01-06 07:11:56 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindEnable", opts->rewindEnable);
|
2014-11-05 07:54:09 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
|
|
|
|
ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
|
|
|
|
ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
|
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
|
2014-12-16 09:33:08 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "lockAspectRatio", opts->lockAspectRatio);
|
2014-12-22 00:50:55 +00:00
|
|
|
ConfigurationSetIntValue(&config->defaultsTable, 0, "resampleVideo", opts->resampleVideo);
|
2015-01-13 10:39:48 +00:00
|
|
|
|
|
|
|
switch (opts->idleOptimization) {
|
|
|
|
case IDLE_LOOP_IGNORE:
|
|
|
|
ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "ignore");
|
|
|
|
break;
|
|
|
|
case IDLE_LOOP_REMOVE:
|
|
|
|
ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "remove");
|
|
|
|
break;
|
|
|
|
case IDLE_LOOP_DETECT:
|
|
|
|
ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "detect");
|
|
|
|
break;
|
|
|
|
}
|
2014-11-01 10:04:10 +00:00
|
|
|
}
|
2014-11-02 09:49:15 +00:00
|
|
|
|
2015-01-30 07:16:25 +00:00
|
|
|
// These two are basically placeholders in case the internal layout changes, e.g. for loading separate files
|
|
|
|
struct Configuration* GBAConfigGetInput(struct GBAConfig* config) {
|
|
|
|
return &config->configTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Configuration* GBAConfigGetOverrides(struct GBAConfig* config) {
|
|
|
|
return &config->configTable;
|
|
|
|
}
|
|
|
|
|
2014-11-02 09:49:15 +00:00
|
|
|
void GBAConfigFreeOpts(struct GBAOptions* opts) {
|
|
|
|
free(opts->bios);
|
|
|
|
opts->bios = 0;
|
|
|
|
}
|