src/gba/gba-config.c (view raw)
1#include "gba-config.h"
2
3#include "platform/commandline.h"
4
5#define SECTION_NAME_MAX 128
6
7static const char* _lookupValue(const struct GBAConfig* config, const char* key) {
8 const char* value;
9 if (config->port) {
10 value = ConfigurationGetValue(&config->configTable, config->port, key);
11 if (value) {
12 return value;
13 }
14 }
15 value = ConfigurationGetValue(&config->configTable, 0, key);
16 if (value) {
17 return value;
18 }
19 if (config->port) {
20 value = ConfigurationGetValue(&config->defaultsTable, config->port, key);
21 if (value) {
22 return value;
23 }
24 }
25 return ConfigurationGetValue(&config->defaultsTable, 0, key);
26}
27
28static bool _lookupCharValue(const struct GBAConfig* config, const char* key, char** out) {
29 const char* value = _lookupValue(config, key);
30 if (!value) {
31 return false;
32 }
33 if (*out) {
34 free(*out);
35 }
36 *out = strdup(value);
37 return true;
38}
39
40static bool _lookupIntValue(const struct GBAConfig* config, const char* key, int* out) {
41 const char* charValue = _lookupValue(config, key);
42 if (!charValue) {
43 return false;
44 }
45 char* end;
46 long value = strtol(charValue, &end, 10);
47 if (*end) {
48 return false;
49 }
50 *out = value;
51 return true;
52}
53
54static bool _lookupUIntValue(const struct GBAConfig* config, const char* key, unsigned* out) {
55 const char* charValue = _lookupValue(config, key);
56 if (!charValue) {
57 return false;
58 }
59 char* end;
60 unsigned long value = strtoul(charValue, &end, 10);
61 if (*end) {
62 return false;
63 }
64 *out = value;
65 return true;
66}
67
68static bool _lookupFloatValue(const struct GBAConfig* config, const char* key, float* out) {
69 const char* charValue = _lookupValue(config, key);
70 if (!charValue) {
71 return false;
72 }
73 char* end;
74 float value = strtof(charValue, &end);
75 if (*end) {
76 return false;
77 }
78 *out = value;
79 return true;
80}
81
82void GBAConfigInit(struct GBAConfig* config, const char* port) {
83 ConfigurationInit(&config->configTable);
84 ConfigurationInit(&config->defaultsTable);
85 config->port = malloc(strlen("ports.") + strlen(port) + 1);
86 sprintf(config->port, "ports.%s", port);
87}
88
89void GBAConfigDeinit(struct GBAConfig* config) {
90 ConfigurationDeinit(&config->configTable);
91 ConfigurationDeinit(&config->defaultsTable);
92 free(config->port);
93}
94
95bool GBAConfigLoad(struct GBAConfig* config) {
96 return ConfigurationRead(&config->configTable, BINARY_NAME ".ini");
97}
98
99bool GBAConfigSave(const struct GBAConfig* config) {
100 return ConfigurationWrite(&config->configTable, BINARY_NAME ".ini");
101}
102
103const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) {
104 return _lookupValue(config, key);
105}
106
107void GBAConfigSetValue(struct GBAConfig* config, const char* key, const char* value) {
108 ConfigurationSetValue(&config->configTable, config->port, key, value);
109}
110
111void GBAConfigSetIntValue(struct GBAConfig* config, const char* key, int value) {
112 ConfigurationSetIntValue(&config->configTable, config->port, key, value);
113}
114
115void GBAConfigSetUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
116 ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
117}
118
119void GBAConfigSetFloatValue(struct GBAConfig* config, const char* key, float value) {
120 ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
121}
122
123void GBAConfigSetDefaultValue(struct GBAConfig* config, const char* key, const char* value) {
124 ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
125}
126
127void GBAConfigSetDefaultIntValue(struct GBAConfig* config, const char* key, int value) {
128 ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
129}
130
131void GBAConfigSetDefaultUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
132 ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
133}
134
135void GBAConfigSetDefaultFloatValue(struct GBAConfig* config, const char* key, float value) {
136 ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
137}
138
139void GBAConfigMap(const struct GBAConfig* config, struct GBAOptions* opts) {
140 _lookupCharValue(config, "bios", &opts->bios);
141 _lookupIntValue(config, "logLevel", &opts->logLevel);
142 _lookupIntValue(config, "frameskip", &opts->frameskip);
143 _lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
144 _lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
145 _lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
146 unsigned audioBuffers;
147 if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
148 opts->audioBuffers = audioBuffers;
149 }
150
151 int fakeBool;
152 if (_lookupIntValue(config, "audioSync", &fakeBool)) {
153 opts->audioSync = fakeBool;
154 }
155 if (_lookupIntValue(config, "videoSync", &fakeBool)) {
156 opts->videoSync = fakeBool;
157 }
158
159 _lookupIntValue(config, "fullscreen", &opts->fullscreen);
160 _lookupIntValue(config, "width", &opts->width);
161 _lookupIntValue(config, "height", &opts->height);
162}
163
164void GBAConfigLoadDefaults(struct GBAConfig* config, const struct GBAOptions* opts) {
165 ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
166 ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
167 ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
168 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
169 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
170 ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
171 ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
172 ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
173 ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
174 ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
175 ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
176 ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
177}
178
179void GBAConfigFreeOpts(struct GBAOptions* opts) {
180 free(opts->bios);
181 opts->bios = 0;
182}