src/gba/supervisor/config.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "config.h"
7
8#include "util/formatting.h"
9#include "util/string.h"
10#include "util/vfs.h"
11
12#include <sys/stat.h>
13
14#ifdef _WIN32
15#include <windows.h>
16#include <shlobj.h>
17#include <strsafe.h>
18#endif
19
20#define SECTION_NAME_MAX 128
21
22static const char* _lookupValue(const struct GBAConfig* config, const char* key) {
23 const char* value;
24 if (config->port) {
25 value = ConfigurationGetValue(&config->configTable, config->port, key);
26 if (value) {
27 return value;
28 }
29 }
30 value = ConfigurationGetValue(&config->configTable, 0, key);
31 if (value) {
32 return value;
33 }
34 if (config->port) {
35 value = ConfigurationGetValue(&config->defaultsTable, config->port, key);
36 if (value) {
37 return value;
38 }
39 }
40 return ConfigurationGetValue(&config->defaultsTable, 0, key);
41}
42
43static bool _lookupCharValue(const struct GBAConfig* config, const char* key, char** out) {
44 const char* value = _lookupValue(config, key);
45 if (!value) {
46 return false;
47 }
48 if (*out) {
49 free(*out);
50 }
51 *out = strdup(value);
52 return true;
53}
54
55static bool _lookupIntValue(const struct GBAConfig* config, const char* key, int* out) {
56 const char* charValue = _lookupValue(config, key);
57 if (!charValue) {
58 return false;
59 }
60 char* end;
61 long value = strtol(charValue, &end, 10);
62 if (*end) {
63 return false;
64 }
65 *out = value;
66 return true;
67}
68
69static bool _lookupUIntValue(const struct GBAConfig* config, const char* key, unsigned* out) {
70 const char* charValue = _lookupValue(config, key);
71 if (!charValue) {
72 return false;
73 }
74 char* end;
75 unsigned long value = strtoul(charValue, &end, 10);
76 if (*end) {
77 return false;
78 }
79 *out = value;
80 return true;
81}
82
83static bool _lookupFloatValue(const struct GBAConfig* config, const char* key, float* out) {
84 const char* charValue = _lookupValue(config, key);
85 if (!charValue) {
86 return false;
87 }
88 char* end;
89 float value = strtof_u(charValue, &end);
90 if (*end) {
91 return false;
92 }
93 *out = value;
94 return true;
95}
96
97void GBAConfigInit(struct GBAConfig* config, const char* port) {
98 ConfigurationInit(&config->configTable);
99 ConfigurationInit(&config->defaultsTable);
100 if (port) {
101 config->port = malloc(strlen("ports.") + strlen(port) + 1);
102 snprintf(config->port, strlen("ports.") + strlen(port) + 1, "ports.%s", port);
103 } else {
104 config->port = 0;
105 }
106}
107
108void GBAConfigDeinit(struct GBAConfig* config) {
109 ConfigurationDeinit(&config->configTable);
110 ConfigurationDeinit(&config->defaultsTable);
111 free(config->port);
112}
113
114bool GBAConfigLoad(struct GBAConfig* config) {
115 char path[PATH_MAX];
116 GBAConfigDirectory(path, PATH_MAX);
117 strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
118 return ConfigurationRead(&config->configTable, path);
119}
120
121bool GBAConfigSave(const struct GBAConfig* config) {
122 char path[PATH_MAX];
123 GBAConfigDirectory(path, PATH_MAX);
124 strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
125 return ConfigurationWrite(&config->configTable, path);
126}
127
128void GBAConfigDirectory(char* out, size_t outLength) {
129#ifndef _WIN32
130 char* home = getenv("HOME");
131 snprintf(out, outLength, "%s/.config", home);
132 mkdir(out, 0755);
133 snprintf(out, outLength, "%s/.config/%s", home, binaryName);
134 mkdir(out, 0755);
135#else
136 char home[MAX_PATH];
137 SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, home);
138 snprintf(out, outLength, "%s\\%s", home, projectName);
139 CreateDirectoryA(out, NULL);
140#endif
141}
142
143const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) {
144 return _lookupValue(config, key);
145}
146
147void GBAConfigSetValue(struct GBAConfig* config, const char* key, const char* value) {
148 ConfigurationSetValue(&config->configTable, config->port, key, value);
149}
150
151void GBAConfigSetIntValue(struct GBAConfig* config, const char* key, int value) {
152 ConfigurationSetIntValue(&config->configTable, config->port, key, value);
153}
154
155void GBAConfigSetUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
156 ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
157}
158
159void GBAConfigSetFloatValue(struct GBAConfig* config, const char* key, float value) {
160 ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
161}
162
163void GBAConfigSetDefaultValue(struct GBAConfig* config, const char* key, const char* value) {
164 ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
165}
166
167void GBAConfigSetDefaultIntValue(struct GBAConfig* config, const char* key, int value) {
168 ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
169}
170
171void GBAConfigSetDefaultUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
172 ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
173}
174
175void GBAConfigSetDefaultFloatValue(struct GBAConfig* config, const char* key, float value) {
176 ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
177}
178
179void GBAConfigMap(const struct GBAConfig* config, struct GBAOptions* opts) {
180 _lookupCharValue(config, "bios", &opts->bios);
181 _lookupIntValue(config, "logLevel", &opts->logLevel);
182 _lookupIntValue(config, "frameskip", &opts->frameskip);
183 _lookupIntValue(config, "volume", &opts->volume);
184 _lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
185 _lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
186 _lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
187 unsigned audioBuffers;
188 if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
189 opts->audioBuffers = audioBuffers;
190 }
191
192 int fakeBool;
193 if (_lookupIntValue(config, "useBios", &fakeBool)) {
194 opts->useBios = fakeBool;
195 }
196 if (_lookupIntValue(config, "audioSync", &fakeBool)) {
197 opts->audioSync = fakeBool;
198 }
199 if (_lookupIntValue(config, "videoSync", &fakeBool)) {
200 opts->videoSync = fakeBool;
201 }
202 if (_lookupIntValue(config, "lockAspectRatio", &fakeBool)) {
203 opts->lockAspectRatio = fakeBool;
204 }
205 if (_lookupIntValue(config, "resampleVideo", &fakeBool)) {
206 opts->resampleVideo = fakeBool;
207 }
208 if (_lookupIntValue(config, "suspendScreensaver", &fakeBool)) {
209 opts->suspendScreensaver = fakeBool;
210 }
211 if (_lookupIntValue(config, "mute", &fakeBool)) {
212 opts->mute = fakeBool;
213 }
214 if (_lookupIntValue(config, "skipBios", &fakeBool)) {
215 opts->skipBios = fakeBool;
216 }
217 if (_lookupIntValue(config, "rewindEnable", &fakeBool)) {
218 opts->rewindEnable = fakeBool;
219 }
220
221 _lookupIntValue(config, "fullscreen", &opts->fullscreen);
222 _lookupIntValue(config, "width", &opts->width);
223 _lookupIntValue(config, "height", &opts->height);
224
225 char* idleOptimization = 0;
226 if (_lookupCharValue(config, "idleOptimization", &idleOptimization)) {
227 if (strcasecmp(idleOptimization, "ignore") == 0) {
228 opts->idleOptimization = IDLE_LOOP_IGNORE;
229 } else if (strcasecmp(idleOptimization, "remove") == 0) {
230 opts->idleOptimization = IDLE_LOOP_REMOVE;
231 } else if (strcasecmp(idleOptimization, "detect") == 0) {
232 opts->idleOptimization = IDLE_LOOP_DETECT;
233 }
234 free(idleOptimization);
235 }
236}
237
238void GBAConfigLoadDefaults(struct GBAConfig* config, const struct GBAOptions* opts) {
239 ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
240 ConfigurationSetIntValue(&config->defaultsTable, 0, "skipBios", opts->skipBios);
241 ConfigurationSetIntValue(&config->defaultsTable, 0, "useBios", opts->useBios);
242 ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
243 ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
244 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindEnable", opts->rewindEnable);
245 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
246 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
247 ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
248 ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
249 ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
250 ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
251 ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
252 ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
253 ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
254 ConfigurationSetIntValue(&config->defaultsTable, 0, "volume", opts->volume);
255 ConfigurationSetIntValue(&config->defaultsTable, 0, "mute", opts->mute);
256 ConfigurationSetIntValue(&config->defaultsTable, 0, "lockAspectRatio", opts->lockAspectRatio);
257 ConfigurationSetIntValue(&config->defaultsTable, 0, "resampleVideo", opts->resampleVideo);
258 ConfigurationSetIntValue(&config->defaultsTable, 0, "suspendScreensaver", opts->suspendScreensaver);
259
260 switch (opts->idleOptimization) {
261 case IDLE_LOOP_IGNORE:
262 ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "ignore");
263 break;
264 case IDLE_LOOP_REMOVE:
265 ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "remove");
266 break;
267 case IDLE_LOOP_DETECT:
268 ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "detect");
269 break;
270 }
271}
272
273// These two are basically placeholders in case the internal layout changes, e.g. for loading separate files
274struct Configuration* GBAConfigGetInput(struct GBAConfig* config) {
275 return &config->configTable;
276}
277
278struct Configuration* GBAConfigGetOverrides(struct GBAConfig* config) {
279 return &config->configTable;
280}
281
282void GBAConfigFreeOpts(struct GBAOptions* opts) {
283 free(opts->bios);
284 opts->bios = 0;
285}