all repos — mgba @ a75c019fab24f045069cb9a923d02795f4d6f564

mGBA Game Boy Advance Emulator

src/core/config.c (view raw)

  1/* Copyright (c) 2013-2016 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 mCoreConfig* 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 mCoreConfig* 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 mCoreConfig* 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 mCoreConfig* 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 mCoreConfig* 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 mCoreConfigInit(struct mCoreConfig* 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 mCoreConfigDeinit(struct mCoreConfig* 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 mCoreConfigLoad(struct mCoreConfig* config) {
137	char path[PATH_MAX];
138	mCoreConfigDirectory(path, PATH_MAX);
139	strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
140	return mCoreConfigLoadPath(config, path);
141}
142
143bool mCoreConfigSave(const struct mCoreConfig* config) {
144	char path[PATH_MAX];
145	mCoreConfigDirectory(path, PATH_MAX);
146	strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
147	return mCoreConfigSavePath(config, path);
148}
149
150bool mCoreConfigLoadPath(struct mCoreConfig* config, const char* path) {
151	return ConfigurationRead(&config->configTable, path);
152}
153
154bool mCoreConfigSavePath(const struct mCoreConfig* config, const char* path) {
155	return ConfigurationWrite(&config->configTable, path);
156}
157
158void mCoreConfigMakePortable(const struct mCoreConfig* 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		mCoreConfigSave(config);
182	}
183}
184
185void mCoreConfigDirectory(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	UNUSED(portable);
217	snprintf(out, outLength, "/%s", projectName);
218	FSUSER_CreateDirectory(sdmcArchive, fsMakePath(PATH_ASCII, out), 0);
219#else
220	getcwd(out, outLength);
221	strncat(out, PATH_SEP "portable.ini", outLength - strlen(out));
222	portable = VFileOpen(out, O_RDONLY);
223	if (portable) {
224		getcwd(out, outLength);
225		portable->close(portable);
226		return;
227	}
228
229	char* home = getenv("HOME");
230	snprintf(out, outLength, "%s/.config", home);
231	mkdir(out, 0755);
232	snprintf(out, outLength, "%s/.config/%s", home, binaryName);
233	mkdir(out, 0755);
234#endif
235}
236#endif
237
238const char* mCoreConfigGetValue(const struct mCoreConfig* config, const char* key) {
239	return _lookupValue(config, key);
240}
241
242bool mCoreConfigGetIntValue(const struct mCoreConfig* config, const char* key, int* value) {
243	return _lookupIntValue(config, key, value);
244}
245
246bool mCoreConfigGetUIntValue(const struct mCoreConfig* config, const char* key, unsigned* value) {
247	return _lookupUIntValue(config, key, value);
248}
249
250bool mCoreConfigGetFloatValue(const struct mCoreConfig* config, const char* key, float* value) {
251	return _lookupFloatValue(config, key, value);
252}
253
254void mCoreConfigSetValue(struct mCoreConfig* config, const char* key, const char* value) {
255	ConfigurationSetValue(&config->configTable, config->port, key, value);
256}
257
258void mCoreConfigSetIntValue(struct mCoreConfig* config, const char* key, int value) {
259	ConfigurationSetIntValue(&config->configTable, config->port, key, value);
260}
261
262void mCoreConfigSetUIntValue(struct mCoreConfig* config, const char* key, unsigned value) {
263	ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
264}
265
266void mCoreConfigSetFloatValue(struct mCoreConfig* config, const char* key, float value) {
267	ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
268}
269
270void mCoreConfigSetDefaultValue(struct mCoreConfig* config, const char* key, const char* value) {
271	ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
272}
273
274void mCoreConfigSetDefaultIntValue(struct mCoreConfig* config, const char* key, int value) {
275	ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
276}
277
278void mCoreConfigSetDefaultUIntValue(struct mCoreConfig* config, const char* key, unsigned value) {
279	ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
280}
281
282void mCoreConfigSetDefaultFloatValue(struct mCoreConfig* config, const char* key, float value) {
283	ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
284}
285
286void mCoreConfigSetOverrideValue(struct mCoreConfig* config, const char* key, const char* value) {
287	ConfigurationSetValue(&config->overridesTable, config->port, key, value);
288}
289
290void mCoreConfigSetOverrideIntValue(struct mCoreConfig* config, const char* key, int value) {
291	ConfigurationSetIntValue(&config->overridesTable, config->port, key, value);
292}
293
294void mCoreConfigSetOverrideUIntValue(struct mCoreConfig* config, const char* key, unsigned value) {
295	ConfigurationSetUIntValue(&config->overridesTable, config->port, key, value);
296}
297
298void mCoreConfigSetOverrideFloatValue(struct mCoreConfig* config, const char* key, float value) {
299	ConfigurationSetFloatValue(&config->overridesTable, config->port, key, value);
300}
301
302void mCoreConfigMap(const struct mCoreConfig* config, struct mCoreOptions* opts) {
303	_lookupCharValue(config, "bios", &opts->bios);
304	_lookupCharValue(config, "shader", &opts->shader);
305	_lookupIntValue(config, "logLevel", &opts->logLevel);
306	_lookupIntValue(config, "frameskip", &opts->frameskip);
307	_lookupIntValue(config, "volume", &opts->volume);
308	_lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
309	_lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
310	_lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
311	unsigned audioBuffers;
312	if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
313		opts->audioBuffers = audioBuffers;
314	}
315	_lookupUIntValue(config, "sampleRate", &opts->sampleRate);
316
317	int fakeBool;
318	if (_lookupIntValue(config, "useBios", &fakeBool)) {
319		opts->useBios = fakeBool;
320	}
321	if (_lookupIntValue(config, "audioSync", &fakeBool)) {
322		opts->audioSync = fakeBool;
323	}
324	if (_lookupIntValue(config, "videoSync", &fakeBool)) {
325		opts->videoSync = fakeBool;
326	}
327	if (_lookupIntValue(config, "lockAspectRatio", &fakeBool)) {
328		opts->lockAspectRatio = fakeBool;
329	}
330	if (_lookupIntValue(config, "resampleVideo", &fakeBool)) {
331		opts->resampleVideo = fakeBool;
332	}
333	if (_lookupIntValue(config, "suspendScreensaver", &fakeBool)) {
334		opts->suspendScreensaver = fakeBool;
335	}
336	if (_lookupIntValue(config, "mute", &fakeBool)) {
337		opts->mute = fakeBool;
338	}
339	if (_lookupIntValue(config, "skipBios", &fakeBool)) {
340		opts->skipBios = fakeBool;
341	}
342	if (_lookupIntValue(config, "rewindEnable", &fakeBool)) {
343		opts->rewindEnable = fakeBool;
344	}
345
346	_lookupIntValue(config, "fullscreen", &opts->fullscreen);
347	_lookupIntValue(config, "width", &opts->width);
348	_lookupIntValue(config, "height", &opts->height);
349
350	_lookupCharValue(config, "savegamePath", &opts->savegamePath);
351	_lookupCharValue(config, "savestatePath", &opts->savestatePath);
352	_lookupCharValue(config, "screenshotPath", &opts->screenshotPath);
353	_lookupCharValue(config, "patchPath", &opts->patchPath);
354}
355
356void mCoreConfigLoadDefaults(struct mCoreConfig* config, const struct mCoreOptions* opts) {
357	ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
358	ConfigurationSetValue(&config->defaultsTable, 0, "shader", opts->shader);
359	ConfigurationSetIntValue(&config->defaultsTable, 0, "skipBios", opts->skipBios);
360	ConfigurationSetIntValue(&config->defaultsTable, 0, "useBios", opts->useBios);
361	ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
362	ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
363	ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindEnable", opts->rewindEnable);
364	ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
365	ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
366	ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
367	ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
368	ConfigurationSetUIntValue(&config->defaultsTable, 0, "sampleRate", opts->sampleRate);
369	ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
370	ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
371	ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
372	ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
373	ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
374	ConfigurationSetIntValue(&config->defaultsTable, 0, "volume", opts->volume);
375	ConfigurationSetIntValue(&config->defaultsTable, 0, "mute", opts->mute);
376	ConfigurationSetIntValue(&config->defaultsTable, 0, "lockAspectRatio", opts->lockAspectRatio);
377	ConfigurationSetIntValue(&config->defaultsTable, 0, "resampleVideo", opts->resampleVideo);
378	ConfigurationSetIntValue(&config->defaultsTable, 0, "suspendScreensaver", opts->suspendScreensaver);
379}
380
381// These two are basically placeholders in case the internal layout changes, e.g. for loading separate files
382struct Configuration* mCoreConfigGetInput(struct mCoreConfig* config) {
383	return &config->configTable;
384}
385
386struct Configuration* mCoreConfigGetOverrides(struct mCoreConfig* config) {
387	return &config->configTable;
388}
389
390const struct Configuration* mCoreConfigGetOverridesConst(const struct mCoreConfig* config) {
391	return &config->configTable;
392}
393
394void mCoreConfigFreeOpts(struct mCoreOptions* opts) {
395	free(opts->bios);
396	free(opts->shader);
397	free(opts->savegamePath);
398	free(opts->savestatePath);
399	free(opts->screenshotPath);
400	free(opts->patchPath);
401	opts->bios = 0;
402	opts->shader = 0;
403	opts->savegamePath = 0;
404	opts->savestatePath = 0;
405	opts->screenshotPath = 0;
406	opts->patchPath = 0;
407}