all repos — mgba @ 456dbc482f9ee76e8b632e296ff4162ac38f18a1

mGBA Game Boy Advance Emulator

src/feature/gui/gui-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 "gui-config.h"
  7
  8#include <mgba/core/config.h>
  9#include <mgba/core/core.h>
 10#include "feature/gui/gui-runner.h"
 11#include "feature/gui/remap.h"
 12#include <mgba/internal/gba/gba.h>
 13#ifdef M_CORE_GB
 14#include <mgba/internal/gb/gb.h>
 15#endif
 16#include <mgba-util/gui/file-select.h>
 17#include <mgba-util/gui/menu.h>
 18#include <mgba-util/vfs.h>
 19
 20#ifndef GUI_MAX_INPUTS
 21#define GUI_MAX_INPUTS 7
 22#endif
 23
 24static bool _biosNamed(const char* name) {
 25	char ext[PATH_MAX + 1] = {};
 26	separatePath(name, NULL, NULL, ext);
 27
 28	if (strstr(name, "bios")) {
 29		return true;
 30	}
 31	if (!strncmp(ext, "bin", PATH_MAX)) {
 32		return true;
 33	}
 34	return false;
 35}
 36
 37void mGUIShowConfig(struct mGUIRunner* runner, struct GUIMenuItem* extra, size_t nExtra) {
 38	struct GUIMenu menu = {
 39		.title = "Configure",
 40		.index = 0,
 41		.background = &runner->background.d
 42	};
 43	GUIMenuItemListInit(&menu.items, 0);
 44	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 45		.title = "Frameskip",
 46		.data = "frameskip",
 47		.submenu = 0,
 48		.state = 0,
 49		.validStates = (const char*[]) {
 50			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
 51		},
 52		.nStates = 10
 53	};
 54	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 55		.title = "Show framerate",
 56		.data = "fpsCounter",
 57		.submenu = 0,
 58		.state = false,
 59		.validStates = (const char*[]) {
 60			"Off", "On"
 61		},
 62		.nStates = 2
 63	};
 64	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 65		.title = "Autosave state",
 66		.data = "autosave",
 67		.submenu = 0,
 68		.state = true,
 69		.validStates = (const char*[]) {
 70			"Off", "On"
 71		},
 72		.nStates = 2
 73	};
 74	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 75		.title = "Autoload state",
 76		.data = "autoload",
 77		.submenu = 0,
 78		.state = true,
 79		.validStates = (const char*[]) {
 80			"Off", "On"
 81		},
 82		.nStates = 2
 83	};
 84	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 85		.title = "Use BIOS if found",
 86		.data = "useBios",
 87		.submenu = 0,
 88		.state = true,
 89		.validStates = (const char*[]) {
 90			"Off", "On"
 91		},
 92		.nStates = 2
 93	};
 94	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 95		.title = "Select GBA BIOS path",
 96		.data = "gba.bios",
 97	};
 98#ifdef M_CORE_GB
 99	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
100		.title = "Select GB BIOS path",
101		.data = "gb.bios",
102	};
103	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
104		.title = "Select GBC BIOS path",
105		.data = "gbc.bios",
106	};
107	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
108		.title = "Select SGB BIOS path",
109		.data = "sgb.bios",
110	};
111	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
112		.title = "Enable SGB borders",
113		.data = "sgb.borders",
114		.submenu = 0,
115		.state = true,
116		.validStates = (const char*[]) {
117			"Off", "On"
118		},
119		.nStates = 2
120	};
121#endif
122	size_t i;
123	const char* mapNames[GUI_MAX_INPUTS + 1];
124	if (runner->keySources) {
125		for (i = 0; runner->keySources[i].id && i < GUI_MAX_INPUTS; ++i) {
126			mapNames[i] = runner->keySources[i].name;
127		}
128		if (i == 1) {
129			// Don't display a name if there's only one input source
130			i = 0;
131		}
132		*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
133			.title = "Remap controls",
134			.data = "*REMAP",
135			.state = 0,
136			.validStates = i ? mapNames : 0,
137			.nStates = i
138		};
139	}
140	for (i = 0; i < nExtra; ++i) {
141		*GUIMenuItemListAppend(&menu.items) = extra[i];
142	}
143	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
144		.title = "Save",
145		.data = "*SAVE",
146	};
147	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
148		.title = "Cancel",
149		.data = 0,
150	};
151	enum GUIMenuExitReason reason;
152	char gbaBiosPath[256] = "";
153#ifdef M_CORE_GB
154	char gbBiosPath[256] = "";
155	char gbcBiosPath[256] = "";
156	char sgbBiosPath[256] = "";
157#endif
158
159	struct GUIMenuItem* item;
160	for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
161		item = GUIMenuItemListGetPointer(&menu.items, i);
162		if (!item->validStates || !item->data) {
163			continue;
164		}
165		if (item->stateMappings) {
166			item->state = 0;
167
168			size_t j;
169			for (j = 0; j < item->nStates; ++j) {
170				const struct GUIVariant* v = &item->stateMappings[j];
171				struct GUIVariant test;
172				switch (v->type) {
173				case GUI_VARIANT_VOID:
174					if (!mCoreConfigGetValue(&runner->config, item->data)) {
175						item->state = j;
176						break;
177					}
178					break;
179				case GUI_VARIANT_UNSIGNED:
180					if (mCoreConfigGetUIntValue(&runner->config, item->data, &test.v.u) && test.v.u == v->v.u) {
181						item->state = j;
182						break;
183					}
184					break;
185				case GUI_VARIANT_INT:
186					if (mCoreConfigGetIntValue(&runner->config, item->data, &test.v.i) && test.v.i == v->v.i) {
187						item->state = j;
188						break;
189					}
190					break;
191				case GUI_VARIANT_FLOAT:
192					if (mCoreConfigGetFloatValue(&runner->config, item->data, &test.v.f) && fabsf(test.v.f - v->v.f) <= 1e-3f) {
193						item->state = j;
194						break;
195					}
196					break;
197				case GUI_VARIANT_STRING:
198					test.v.s = mCoreConfigGetValue(&runner->config, item->data);
199					if (test.v.s && strcmp(test.v.s, v->v.s) == 0) {
200						item->state = j;
201						break;						
202					}
203					break;
204				}
205			}
206		} else {
207			mCoreConfigGetUIntValue(&runner->config, item->data, &item->state);
208		}
209	}
210
211	while (true) {
212		reason = GUIShowMenu(&runner->params, &menu, &item);
213		if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
214			break;
215		}
216		if (!strcmp(item->data, "*SAVE")) {
217			if (gbaBiosPath[0]) {
218				mCoreConfigSetValue(&runner->config, "gba.bios", gbaBiosPath);
219			}
220			if (gbBiosPath[0]) {
221				mCoreConfigSetValue(&runner->config, "gb.bios", gbBiosPath);
222			}
223			if (gbcBiosPath[0]) {
224				mCoreConfigSetValue(&runner->config, "gbc.bios", gbcBiosPath);
225			}
226			if (sgbBiosPath[0]) {
227				mCoreConfigSetValue(&runner->config, "sgb.bios", sgbBiosPath);
228			}
229			for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
230				item = GUIMenuItemListGetPointer(&menu.items, i);
231				if (!item->validStates || !item->data || ((const char*) item->data)[0] == '*') {
232					continue;
233				}
234				if (item->stateMappings) {
235					const struct GUIVariant* v = &item->stateMappings[item->state];
236					switch (v->type) {
237					case GUI_VARIANT_VOID:
238						mCoreConfigSetValue(&runner->config, item->data, NULL);
239						break;
240					case GUI_VARIANT_UNSIGNED:
241						mCoreConfigSetUIntValue(&runner->config, item->data, v->v.u);
242						break;
243					case GUI_VARIANT_INT:
244						mCoreConfigSetUIntValue(&runner->config, item->data, v->v.i);
245						break;
246					case GUI_VARIANT_FLOAT:
247						mCoreConfigSetFloatValue(&runner->config, item->data, v->v.f);
248						break;
249					case GUI_VARIANT_STRING:
250						mCoreConfigSetValue(&runner->config, item->data, v->v.s);
251						break;
252					}
253				} else {
254					mCoreConfigSetUIntValue(&runner->config, item->data, item->state);
255				}
256			}
257			if (runner->keySources) {
258				size_t i;
259				for (i = 0; runner->keySources[i].id; ++i) {
260					mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
261					mInputMapSave(&runner->params.keyMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
262				}
263			}
264			mCoreConfigSave(&runner->config);
265			mCoreLoadForeignConfig(runner->core, &runner->config);
266			break;
267		}
268		if (!strcmp(item->data, "*REMAP")) {
269			mGUIRemapKeys(&runner->params, &runner->core->inputMap, &runner->keySources[item->state]);
270			continue;
271		}
272		if (!strcmp(item->data, "gba.bios")) {
273			// TODO: show box if failed
274			if (!GUISelectFile(&runner->params, gbaBiosPath, sizeof(gbaBiosPath), _biosNamed, GBAIsBIOS)) {
275				gbaBiosPath[0] = '\0';
276			}
277			continue;
278		}
279#ifdef M_CORE_GB
280		if (!strcmp(item->data, "gb.bios")) {
281			// TODO: show box if failed
282			if (!GUISelectFile(&runner->params, gbBiosPath, sizeof(gbBiosPath), _biosNamed, GBIsBIOS)) {
283				gbBiosPath[0] = '\0';
284			}
285			continue;
286		}
287		if (!strcmp(item->data, "gbc.bios")) {
288			// TODO: show box if failed
289			if (!GUISelectFile(&runner->params, gbcBiosPath, sizeof(gbcBiosPath), _biosNamed, GBIsBIOS)) {
290				gbcBiosPath[0] = '\0';
291			}
292			continue;
293		}
294		if (!strcmp(item->data, "sgb.bios")) {
295			// TODO: show box if failed
296			if (!GUISelectFile(&runner->params, sgbBiosPath, sizeof(sgbBiosPath), _biosNamed, GBIsBIOS)) {
297				sgbBiosPath[0] = '\0';
298			}
299			continue;
300		}
301#endif
302		if (item->validStates) {
303			++item->state;
304			if (item->state >= item->nStates) {
305				item->state = 0;
306			}
307		}
308	}
309}