all repos — mgba @ 631b287b3a6ecc66eec765a9cf7987a2e7cfb364

mGBA Game Boy Advance Emulator

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
135#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
136bool GBAConfigLoad(struct GBAConfig* config) {
137	char path[PATH_MAX];
138	GBAConfigDirectory(path, PATH_MAX);
139	strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
140	return GBAConfigLoadPath(config, path);
141}
142
143bool GBAConfigSave(const struct GBAConfig* config) {
144	char path[PATH_MAX];
145	GBAConfigDirectory(path, PATH_MAX);
146	strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
147	return GBAConfigSavePath(config, path);
148}
149
150bool GBAConfigLoadPath(struct GBAConfig* config, const char* path) {
151	return ConfigurationRead(&config->configTable, path);
152}
153
154bool GBAConfigSavePath(const struct GBAConfig* config, const char* path) {
155	return ConfigurationWrite(&config->configTable, path);
156}
157
158void GBAConfigMakePortable(const struct GBAConfig* config) {
159	struct VFile* portable = 0;
160#ifdef _WIN32
161	char out[MAX_PATH];
162	wchar_t wpath[MAX_PATH];
163	wchar_t wprojectName[MAX_PATH];
164	MultiByteToWideChar(CP_UTF8, 0, projectName, -1, wprojectName, MAX_PATH);
165	HMODULE hModule = GetModuleHandleW(NULL);
166	GetModuleFileNameW(hModule, wpath, MAX_PATH);
167	PathRemoveFileSpecW(wpath);
168	WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, MAX_PATH, 0, 0);
169	StringCchCatA(out, MAX_PATH, "\\portable.ini");
170	portable = VFileOpen(out, O_WRONLY | O_CREAT);
171#elif defined(PSP2) || defined(_3DS) || defined(GEKKO)
172	// Already portable
173#else
174	char out[PATH_MAX];
175	getcwd(out, PATH_MAX);
176	strncat(out, PATH_SEP "portable.ini", PATH_MAX - strlen(out));
177	portable = VFileOpen(out, O_WRONLY | O_CREAT);
178#endif
179	if (portable) {
180		portable->close(portable);
181		GBAConfigSave(config);
182	}
183}
184
185void GBAConfigDirectory(char* out, size_t outLength) {
186	struct VFile* portable;
187#ifdef _WIN32
188	wchar_t wpath[MAX_PATH];
189	wchar_t wprojectName[MAX_PATH];
190	MultiByteToWideChar(CP_UTF8, 0, projectName, -1, wprojectName, MAX_PATH);
191	HMODULE hModule = GetModuleHandleW(NULL);
192	GetModuleFileNameW(hModule, wpath, MAX_PATH);
193	PathRemoveFileSpecW(wpath);
194	WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, outLength, 0, 0);
195	StringCchCatA(out, outLength, "\\portable.ini");
196	portable = VFileOpen(out, O_RDONLY);
197	if (portable) {
198		portable->close(portable);
199	} else {
200		wchar_t* home;
201		SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &home);
202		StringCchPrintfW(wpath, MAX_PATH, L"%ws\\%ws", home, wprojectName);
203		CoTaskMemFree(home);
204		CreateDirectoryW(wpath, NULL);
205	}
206	WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, outLength, 0, 0);
207#elif defined(PSP2)
208	UNUSED(portable);
209	snprintf(out, outLength, "cache0:/%s", projectName);
210	sceIoMkdir(out, 0777);
211#elif defined(GEKKO)
212	UNUSED(portable);
213	snprintf(out, outLength, "/%s", projectName);
214	mkdir(out, 0777);
215#elif defined(_3DS)
216	snprintf(out, outLength, "/%s", projectName);
217	FSUSER_CreateDirectory(0, sdmcArchive, FS_makePath(PATH_CHAR, out));
218#else
219	getcwd(out, outLength);
220	strncat(out, PATH_SEP "portable.ini", outLength - strlen(out));
221	portable = VFileOpen(out, O_RDONLY);
222	if (portable) {
223		getcwd(out, outLength);
224		portable->close(portable);
225		return;
226	}
227
228	char* home = getenv("HOME");
229	snprintf(out, outLength, "%s/.config", home);
230	mkdir(out, 0755);
231	snprintf(out, outLength, "%s/.config/%s", home, binaryName);
232	mkdir(out, 0755);
233#endif
234}
235#endif
236
237const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) {
238	return _lookupValue(config, key);
239}
240
241bool GBAConfigGetIntValue(const struct GBAConfig* config, const char* key, int* value) {
242	return _lookupIntValue(config, key, value);
243}
244
245bool GBAConfigGetUIntValue(const struct GBAConfig* config, const char* key, unsigned* value) {
246	return _lookupUIntValue(config, key, value);
247}
248
249bool GBAConfigGetFloatValue(const struct GBAConfig* config, const char* key, float* value) {
250	return _lookupFloatValue(config, key, value);
251}
252
253void GBAConfigSetValue(struct GBAConfig* config, const char* key, const char* value) {
254	ConfigurationSetValue(&config->configTable, config->port, key, value);
255}
256
257void GBAConfigSetIntValue(struct GBAConfig* config, const char* key, int value) {
258	ConfigurationSetIntValue(&config->configTable, config->port, key, value);
259}
260
261void GBAConfigSetUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
262	ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
263}
264
265void GBAConfigSetFloatValue(struct GBAConfig* config, const char* key, float value) {
266	ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
267}
268
269void GBAConfigSetDefaultValue(struct GBAConfig* config, const char* key, const char* value) {
270	ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
271}
272
273void GBAConfigSetDefaultIntValue(struct GBAConfig* config, const char* key, int value) {
274	ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
275}
276
277void GBAConfigSetDefaultUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
278	ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
279}
280
281void GBAConfigSetDefaultFloatValue(struct GBAConfig* config, const char* key, float value) {
282	ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
283}
284
285void GBAConfigSetOverrideValue(struct GBAConfig* config, const char* key, const char* value) {
286	ConfigurationSetValue(&config->overridesTable, config->port, key, value);
287}
288
289void GBAConfigSetOverrideIntValue(struct GBAConfig* config, const char* key, int value) {
290	ConfigurationSetIntValue(&config->overridesTable, config->port, key, value);
291}
292
293void GBAConfigSetOverrideUIntValue(struct GBAConfig* config, const char* key, unsigned value) {
294	ConfigurationSetUIntValue(&config->overridesTable, config->port, key, value);
295}
296
297void GBAConfigSetOverrideFloatValue(struct GBAConfig* config, const char* key, float value) {
298	ConfigurationSetFloatValue(&config->overridesTable, config->port, key, value);
299}
300
301void GBAConfigMap(const struct GBAConfig* config, struct GBAOptions* opts) {
302	_lookupCharValue(config, "bios", &opts->bios);
303	_lookupCharValue(config, "shader", &opts->shader);
304	_lookupIntValue(config, "logLevel", &opts->logLevel);
305	_lookupIntValue(config, "frameskip", &opts->frameskip);
306	_lookupIntValue(config, "volume", &opts->volume);
307	_lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
308	_lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
309	_lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
310	unsigned audioBuffers;
311	if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
312		opts->audioBuffers = audioBuffers;
313	}
314	_lookupUIntValue(config, "sampleRate", &opts->sampleRate);
315
316	int fakeBool;
317	if (_lookupIntValue(config, "useBios", &fakeBool)) {
318		opts->useBios = fakeBool;
319	}
320	if (_lookupIntValue(config, "audioSync", &fakeBool)) {
321		opts->audioSync = fakeBool;
322	}
323	if (_lookupIntValue(config, "videoSync", &fakeBool)) {
324		opts->videoSync = fakeBool;
325	}
326	if (_lookupIntValue(config, "lockAspectRatio", &fakeBool)) {
327		opts->lockAspectRatio = fakeBool;
328	}
329	if (_lookupIntValue(config, "resampleVideo", &fakeBool)) {
330		opts->resampleVideo = fakeBool;
331	}
332	if (_lookupIntValue(config, "suspendScreensaver", &fakeBool)) {
333		opts->suspendScreensaver = fakeBool;
334	}
335	if (_lookupIntValue(config, "mute", &fakeBool)) {
336		opts->mute = fakeBool;
337	}
338	if (_lookupIntValue(config, "skipBios", &fakeBool)) {
339		opts->skipBios = fakeBool;
340	}
341	if (_lookupIntValue(config, "rewindEnable", &fakeBool)) {
342		opts->rewindEnable = fakeBool;
343	}
344
345	_lookupIntValue(config, "fullscreen", &opts->fullscreen);
346	_lookupIntValue(config, "width", &opts->width);
347	_lookupIntValue(config, "height", &opts->height);
348
349	char* idleOptimization = 0;
350	if (_lookupCharValue(config, "idleOptimization", &idleOptimization)) {
351		if (strcasecmp(idleOptimization, "ignore") == 0) {
352			opts->idleOptimization = IDLE_LOOP_IGNORE;
353		} else if (strcasecmp(idleOptimization, "remove") == 0) {
354			opts->idleOptimization = IDLE_LOOP_REMOVE;
355		} else if (strcasecmp(idleOptimization, "detect") == 0) {
356			opts->idleOptimization = IDLE_LOOP_DETECT;
357		}
358		free(idleOptimization);
359	}
360}
361
362void GBAConfigLoadDefaults(struct GBAConfig* config, const struct GBAOptions* opts) {
363	ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
364	ConfigurationSetValue(&config->defaultsTable, 0, "shader", opts->shader);
365	ConfigurationSetIntValue(&config->defaultsTable, 0, "skipBios", opts->skipBios);
366	ConfigurationSetIntValue(&config->defaultsTable, 0, "useBios", opts->useBios);
367	ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
368	ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
369	ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindEnable", opts->rewindEnable);
370	ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
371	ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
372	ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
373	ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
374	ConfigurationSetUIntValue(&config->defaultsTable, 0, "sampleRate", opts->sampleRate);
375	ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
376	ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
377	ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
378	ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
379	ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
380	ConfigurationSetIntValue(&config->defaultsTable, 0, "volume", opts->volume);
381	ConfigurationSetIntValue(&config->defaultsTable, 0, "mute", opts->mute);
382	ConfigurationSetIntValue(&config->defaultsTable, 0, "lockAspectRatio", opts->lockAspectRatio);
383	ConfigurationSetIntValue(&config->defaultsTable, 0, "resampleVideo", opts->resampleVideo);
384	ConfigurationSetIntValue(&config->defaultsTable, 0, "suspendScreensaver", opts->suspendScreensaver);
385
386	switch (opts->idleOptimization) {
387	case IDLE_LOOP_IGNORE:
388		ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "ignore");
389		break;
390	case IDLE_LOOP_REMOVE:
391		ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "remove");
392		break;
393	case IDLE_LOOP_DETECT:
394		ConfigurationSetValue(&config->defaultsTable, 0, "idleOptimization", "detect");
395		break;
396	}
397}
398
399// These two are basically placeholders in case the internal layout changes, e.g. for loading separate files
400struct Configuration* GBAConfigGetInput(struct GBAConfig* config) {
401	return &config->configTable;
402}
403
404struct Configuration* GBAConfigGetOverrides(struct GBAConfig* config) {
405	return &config->configTable;
406}
407
408void GBAConfigFreeOpts(struct GBAOptions* opts) {
409	free(opts->bios);
410	free(opts->shader);
411	opts->bios = 0;
412	opts->shader = 0;
413}