src/gba/context/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 <shlwapi.h>
17#include <shlobj.h>
18#include <strsafe.h>
19#endif
20
21#ifdef PSP2
22#include <psp2/io/stat.h>
23#endif
24
25#ifdef _3DS
26#include "platform/3ds/3ds-vfs.h"
27#endif
28
29#define SECTION_NAME_MAX 128
30
31static const char* _lookupValue(const struct GBAConfig* config, const char* key) {
32 const char* value;
33 if (config->port) {
34 value = ConfigurationGetValue(&config->overridesTable, config->port, key);
35 if (value) {
36 return value;
37 }
38 }
39 value = ConfigurationGetValue(&config->overridesTable, 0, key);
40 if (value) {
41 return value;
42 }
43 if (config->port) {
44 value = ConfigurationGetValue(&config->configTable, config->port, key);
45 if (value) {
46 return value;
47 }
48 }
49 value = ConfigurationGetValue(&config->configTable, 0, key);
50 if (value) {
51 return value;
52 }
53 if (config->port) {
54 value = ConfigurationGetValue(&config->defaultsTable, config->port, key);
55 if (value) {
56 return value;
57 }
58 }
59 return ConfigurationGetValue(&config->defaultsTable, 0, key);
60}
61
62static bool _lookupCharValue(const struct GBAConfig* config, const char* key, char** out) {
63 const char* value = _lookupValue(config, key);
64 if (!value) {
65 return false;
66 }
67 if (*out) {
68 free(*out);
69 }
70 *out = strdup(value);
71 return true;
72}
73
74static bool _lookupIntValue(const struct GBAConfig* config, const char* key, int* out) {
75 const char* charValue = _lookupValue(config, key);
76 if (!charValue) {
77 return false;
78 }
79 char* end;
80 long value = strtol(charValue, &end, 10);
81 if (*end) {
82 return false;
83 }
84 *out = value;
85 return true;
86}
87
88static bool _lookupUIntValue(const struct GBAConfig* config, const char* key, unsigned* out) {
89 const char* charValue = _lookupValue(config, key);
90 if (!charValue) {
91 return false;
92 }
93 char* end;
94 unsigned long value = strtoul(charValue, &end, 10);
95 if (*end) {
96 return false;
97 }
98 *out = value;
99 return true;
100}
101
102static bool _lookupFloatValue(const struct GBAConfig* config, const char* key, float* out) {
103 const char* charValue = _lookupValue(config, key);
104 if (!charValue) {
105 return false;
106 }
107 char* end;
108 float value = strtof_u(charValue, &end);
109 if (*end) {
110 return false;
111 }
112 *out = value;
113 return true;
114}
115
116void GBAConfigInit(struct GBAConfig* config, const char* port) {
117 ConfigurationInit(&config->configTable);
118 ConfigurationInit(&config->defaultsTable);
119 ConfigurationInit(&config->overridesTable);
120 if (port) {
121 config->port = malloc(strlen("ports.") + strlen(port) + 1);
122 snprintf(config->port, strlen("ports.") + strlen(port) + 1, "ports.%s", port);
123 } else {
124 config->port = 0;
125 }
126}
127
128void GBAConfigDeinit(struct GBAConfig* config) {
129 ConfigurationDeinit(&config->configTable);
130 ConfigurationDeinit(&config->defaultsTable);
131 ConfigurationDeinit(&config->overridesTable);
132 free(config->port);
133}
134
135bool GBAConfigLoad(struct GBAConfig* config) {
136 char path[PATH_MAX];
137 GBAConfigDirectory(path, PATH_MAX);
138 strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
139 return GBAConfigLoadPath(config, path);
140}
141
142bool GBAConfigSave(const struct GBAConfig* config) {
143 char path[PATH_MAX];
144 GBAConfigDirectory(path, PATH_MAX);
145 strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
146 return GBAConfigSavePath(config, path);
147}
148
149bool GBAConfigLoadPath(struct GBAConfig* config, const char* path) {
150 return ConfigurationRead(&config->configTable, path);
151}
152
153bool GBAConfigSavePath(const struct GBAConfig* config, const char* path) {
154 return ConfigurationWrite(&config->configTable, path);
155}
156
157void GBAConfigMakePortable(const struct GBAConfig* config) {
158 struct VFile* portable = 0;
159#ifdef _WIN32
160 char out[MAX_PATH];
161 wchar_t wpath[MAX_PATH];
162 wchar_t wprojectName[MAX_PATH];
163 MultiByteToWideChar(CP_UTF8, 0, projectName, -1, wprojectName, MAX_PATH);
164 HMODULE hModule = GetModuleHandleW(NULL);
165 GetModuleFileNameW(hModule, wpath, MAX_PATH);
166 PathRemoveFileSpecW(wpath);
167 WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, MAX_PATH, 0, 0);
168 StringCchCatA(out, MAX_PATH, "\\portable.ini");
169 portable = VFileOpen(out, O_WRONLY | O_CREAT);
170#elif defined(PSP2) || defined(_3DS) || defined(GEKKO)
171 // Already portable
172#else
173 char out[PATH_MAX];
174 getcwd(out, PATH_MAX);
175 strncat(out, PATH_SEP "portable.ini", PATH_MAX - strlen(out));
176 portable = VFileOpen(out, O_WRONLY | O_CREAT);
177#endif
178 if (portable) {
179 portable->close(portable);
180 GBAConfigSave(config);
181 }
182}
183
184void GBAConfigDirectory(char* out, size_t outLength) {
185 struct VFile* portable;
186#ifdef _WIN32
187 wchar_t wpath[MAX_PATH];
188 wchar_t wprojectName[MAX_PATH];
189 MultiByteToWideChar(CP_UTF8, 0, projectName, -1, wprojectName, MAX_PATH);
190 HMODULE hModule = GetModuleHandleW(NULL);
191 GetModuleFileNameW(hModule, wpath, MAX_PATH);
192 PathRemoveFileSpecW(wpath);
193 WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, outLength, 0, 0);
194 StringCchCatA(out, outLength, "\\portable.ini");
195 portable = VFileOpen(out, O_RDONLY);
196 if (portable) {
197 portable->close(portable);
198 } else {
199 wchar_t* home;
200 SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &home);
201 StringCchPrintfW(wpath, MAX_PATH, L"%ws\\%ws", home, wprojectName);
202 CoTaskMemFree(home);
203 CreateDirectoryW(wpath, NULL);
204 }
205 WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, outLength, 0, 0);
206#elif defined(PSP2)
207 UNUSED(portable);
208 snprintf(out, outLength, "cache0:/%s", projectName);
209 sceIoMkdir(out, 0777);
210#elif defined(GEKKO)
211 UNUSED(portable);
212 snprintf(out, outLength, "/%s", projectName);
213 mkdir(out, 0777);
214#elif defined(_3DS)
215 snprintf(out, outLength, "/%s", projectName);
216 FSUSER_CreateDirectory(0, sdmcArchive, FS_makePath(PATH_CHAR, out));
217#else
218 getcwd(out, outLength);
219 strncat(out, PATH_SEP "portable.ini", outLength - strlen(out));
220 portable = VFileOpen(out, O_RDONLY);
221 if (portable) {
222 getcwd(out, outLength);
223 portable->close(portable);
224 return;
225 }
226
227 char* home = getenv("HOME");
228 snprintf(out, outLength, "%s/.config", home);
229 mkdir(out, 0755);
230 snprintf(out, outLength, "%s/.config/%s", home, binaryName);
231 mkdir(out, 0755);
232#endif
233}
234
235const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) {
236 return _lookupValue(config, key);
237}
238
239bool GBAConfigGetIntValue(const struct GBAConfig* config, const char* key, int* value) {
240 return _lookupIntValue(config, key, value);
241}
242
243bool GBAConfigGetUIntValue(const struct GBAConfig* config, const char* key, unsigned* value) {
244 return _lookupUIntValue(config, key, value);
245}
246
247bool GBAConfigGetFloatValue(const struct GBAConfig* config, const char* key, float* value) {
248 return _lookupFloatValue(config, key, value);
249}
250
251void GBAConfigSetValue(struct GBAConfig* config, const char* key, const char* value) {
252 ConfigurationSetValue(&config->configTable, config->port, key, value);
253}
254
255void GBAConfigSetIntValue(struct GBAConfig* config, const char* key, int value) {
256 ConfigurationSetIntValue(&config->configTable, config->port, key, value);
257}
258
259void GBAConfigSetUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
260 ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
261}
262
263void GBAConfigSetFloatValue(struct GBAConfig* config, const char* key, float value) {
264 ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
265}
266
267void GBAConfigSetDefaultValue(struct GBAConfig* config, const char* key, const char* value) {
268 ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
269}
270
271void GBAConfigSetDefaultIntValue(struct GBAConfig* config, const char* key, int value) {
272 ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
273}
274
275void GBAConfigSetDefaultUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
276 ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
277}
278
279void GBAConfigSetDefaultFloatValue(struct GBAConfig* config, const char* key, float value) {
280 ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
281}
282
283void GBAConfigSetOverrideValue(struct GBAConfig* config, const char* key, const char* value) {
284 ConfigurationSetValue(&config->overridesTable, config->port, key, value);
285}
286
287void GBAConfigSetOverrideIntValue(struct GBAConfig* config, const char* key, int value) {
288 ConfigurationSetIntValue(&config->overridesTable, config->port, key, value);
289}
290
291void GBAConfigSetOverrideUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
292 ConfigurationSetUIntValue(&config->overridesTable, config->port, key, value);
293}
294
295void GBAConfigSetOverrideFloatValue(struct GBAConfig* config, const char* key, float value) {
296 ConfigurationSetFloatValue(&config->overridesTable, config->port, key, value);
297}
298
299void GBAConfigMap(const struct GBAConfig* config, struct GBAOptions* opts) {
300 _lookupCharValue(config, "bios", &opts->bios);
301 _lookupIntValue(config, "logLevel", &opts->logLevel);
302 _lookupIntValue(config, "frameskip", &opts->frameskip);
303 _lookupIntValue(config, "volume", &opts->volume);
304 _lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
305 _lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
306 _lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
307 unsigned audioBuffers;
308 if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
309 opts->audioBuffers = audioBuffers;
310 }
311 _lookupUIntValue(config, "sampleRate", &opts->sampleRate);
312
313 int fakeBool;
314 if (_lookupIntValue(config, "useBios", &fakeBool)) {
315 opts->useBios = fakeBool;
316 }
317 if (_lookupIntValue(config, "audioSync", &fakeBool)) {
318 opts->audioSync = fakeBool;
319 }
320 if (_lookupIntValue(config, "videoSync", &fakeBool)) {
321 opts->videoSync = fakeBool;
322 }
323 if (_lookupIntValue(config, "lockAspectRatio", &fakeBool)) {
324 opts->lockAspectRatio = fakeBool;
325 }
326 if (_lookupIntValue(config, "resampleVideo", &fakeBool)) {
327 opts->resampleVideo = fakeBool;
328 }
329 if (_lookupIntValue(config, "suspendScreensaver", &fakeBool)) {
330 opts->suspendScreensaver = fakeBool;
331 }
332 if (_lookupIntValue(config, "mute", &fakeBool)) {
333 opts->mute = fakeBool;
334 }
335 if (_lookupIntValue(config, "skipBios", &fakeBool)) {
336 opts->skipBios = fakeBool;
337 }
338 if (_lookupIntValue(config, "rewindEnable", &fakeBool)) {
339 opts->rewindEnable = fakeBool;
340 }
341
342 _lookupIntValue(config, "fullscreen", &opts->fullscreen);
343 _lookupIntValue(config, "width", &opts->width);
344 _lookupIntValue(config, "height", &opts->height);
345
346 char* idleOptimization = 0;
347 if (_lookupCharValue(config, "idleOptimization", &idleOptimization)) {
348 if (strcasecmp(idleOptimization, "ignore") == 0) {
349 opts->idleOptimization = IDLE_LOOP_IGNORE;
350 } else if (strcasecmp(idleOptimization, "remove") == 0) {
351 opts->idleOptimization = IDLE_LOOP_REMOVE;
352 } else if (strcasecmp(idleOptimization, "detect") == 0) {
353 opts->idleOptimization = IDLE_LOOP_DETECT;
354 }
355 free(idleOptimization);
356 }
357}
358
359void GBAConfigLoadDefaults(struct GBAConfig* config, const struct GBAOptions* opts) {
360 ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
361 ConfigurationSetIntValue(&config->defaultsTable, 0, "skipBios", opts->skipBios);
362 ConfigurationSetIntValue(&config->defaultsTable, 0, "useBios", opts->useBios);
363 ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
364 ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
365 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindEnable", opts->rewindEnable);
366 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
367 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
368 ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
369 ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
370 ConfigurationSetUIntValue(&config->defaultsTable, 0, "sampleRate", opts->sampleRate);
371 ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
372 ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
373 ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
374 ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
375 ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
376 ConfigurationSetIntValue(&config->defaultsTable, 0, "volume", opts->volume);
377 ConfigurationSetIntValue(&config->defaultsTable, 0, "mute", opts->mute);
378 ConfigurationSetIntValue(&config->defaultsTable, 0, "lockAspectRatio", opts->lockAspectRatio);
379 ConfigurationSetIntValue(&config->defaultsTable, 0, "resampleVideo", opts->resampleVideo);
380 ConfigurationSetIntValue(&config->defaultsTable, 0, "suspendScreensaver", opts->suspendScreensaver);
381
382 switch (opts->idleOptimization) {
383 case IDLE_LOOP_IGNORE:
384 ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "ignore");
385 break;
386 case IDLE_LOOP_REMOVE:
387 ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "remove");
388 break;
389 case IDLE_LOOP_DETECT:
390 ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "detect");
391 break;
392 }
393}
394
395// These two are basically placeholders in case the internal layout changes, e.g. for loading separate files
396struct Configuration* GBAConfigGetInput(struct GBAConfig* config) {
397 return &config->configTable;
398}
399
400struct Configuration* GBAConfigGetOverrides(struct GBAConfig* config) {
401 return &config->configTable;
402}
403
404void GBAConfigFreeOpts(struct GBAOptions* opts) {
405 free(opts->bios);
406 opts->bios = 0;
407}