all repos — mgba @ 11bf4fdfdad16a2a3b8059d652f41c2c6b2e2581

mGBA Game Boy Advance Emulator

src/gba/gba-config.c (view raw)

  1#include "gba-config.h"
  2
  3#include "platform/commandline.h"
  4#include "util/configuration.h"
  5
  6#define SECTION_NAME_MAX 128
  7
  8static const char* _lookupValue(const struct Configuration* config, const char* key, const char* port) {
  9	if (port) {
 10		char sectionName[SECTION_NAME_MAX];
 11		snprintf(sectionName, SECTION_NAME_MAX, "ports.%s", port);
 12		sectionName[SECTION_NAME_MAX - 1] = '\0';
 13		const char* value = ConfigurationGetValue(config, sectionName, key);
 14		if (value) {
 15			return value;
 16		}
 17	}
 18	return ConfigurationGetValue(config, 0, key);
 19}
 20
 21static bool _lookupCharValue(const struct Configuration* config, const char* key, const char* port, char** out) {
 22	const char* value = _lookupValue(config, key, port);
 23	if (!value) {
 24		return false;
 25	}
 26	*out = strdup(value);
 27	return true;
 28}
 29
 30static bool _lookupIntValue(const struct Configuration* config, const char* key, const char* port, int* out) {
 31	const char* charValue = _lookupValue(config, key, port);
 32	if (!charValue) {
 33		return false;
 34	}
 35	char* end;
 36	long value = strtol(charValue, &end, 10);
 37	if (*end) {
 38		return false;
 39	}
 40	*out = value;
 41	return true;
 42}
 43
 44static bool _lookupUIntValue(const struct Configuration* config, const char* key, const char* port, unsigned* out) {
 45	const char* charValue = _lookupValue(config, key, port);
 46	if (!charValue) {
 47		return false;
 48	}
 49	char* end;
 50	unsigned long value = strtoul(charValue, &end, 10);
 51	if (*end) {
 52		return false;
 53	}
 54	*out = value;
 55	return true;
 56}
 57
 58static bool _lookupFloatValue(const struct Configuration* config, const char* key, const char* port, float* out) {
 59	const char* charValue = _lookupValue(config, key, port);
 60	if (!charValue) {
 61		return false;
 62	}
 63	char* end;
 64	float value = strtof(charValue, &end);
 65	if (*end) {
 66		return false;
 67	}
 68	*out = value;
 69	return true;
 70}
 71
 72bool GBAConfigLoad(struct Configuration* config) {
 73	return ConfigurationRead(config, BINARY_NAME ".ini");
 74}
 75
 76void GBAConfigMapGeneralOpts(const struct Configuration* config, const char* port, struct GBAOptions* opts) {
 77	_lookupCharValue(config, "bios", port, &opts->bios);
 78	_lookupIntValue(config, "logLevel", port, &opts->logLevel);
 79	_lookupIntValue(config, "frameskip", port, &opts->frameskip);
 80	_lookupIntValue(config, "rewindBufferCapacity", port, &opts->rewindBufferCapacity);
 81	_lookupIntValue(config, "rewindBufferInterval", port, &opts->rewindBufferInterval);
 82	_lookupFloatValue(config, "fpsTarget", port, &opts->fpsTarget);
 83	unsigned audioBuffers;
 84	if (_lookupUIntValue(config, "audioBuffers", port, &audioBuffers)) {
 85		opts->audioBuffers = audioBuffers;
 86	}
 87
 88	int fakeBool;
 89	if (_lookupIntValue(config, "audioSync", port, &fakeBool)) {
 90		opts->audioSync = fakeBool;
 91	}
 92	if (_lookupIntValue(config, "videoSync", port, &fakeBool)) {
 93		opts->videoSync = fakeBool;
 94	}
 95}
 96
 97void GBAConfigMapGraphicsOpts(const struct Configuration* config, const char* port, struct GBAOptions* opts) {
 98	_lookupIntValue(config, "fullscreen", port, &opts->fullscreen);
 99	_lookupIntValue(config, "width", port, &opts->width);
100	_lookupIntValue(config, "height", port, &opts->height);
101}
102
103void GBAConfigFreeOpts(struct GBAOptions* opts) {
104	free(opts->bios);
105	opts->bios = 0;
106}