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 = "Interframe blending",
113 .data = "interframeBlending",
114 .submenu = 0,
115 .state = false,
116 .validStates = (const char*[]) {
117 "Off", "On"
118 },
119 .nStates = 2
120 };
121 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
122 .title = "Enable SGB borders",
123 .data = "sgb.borders",
124 .submenu = 0,
125 .state = true,
126 .validStates = (const char*[]) {
127 "Off", "On"
128 },
129 .nStates = 2
130 };
131#endif
132 size_t i;
133 const char* mapNames[GUI_MAX_INPUTS + 1];
134 if (runner->keySources) {
135 for (i = 0; runner->keySources[i].id && i < GUI_MAX_INPUTS; ++i) {
136 mapNames[i] = runner->keySources[i].name;
137 }
138 if (i == 1) {
139 // Don't display a name if there's only one input source
140 i = 0;
141 }
142 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
143 .title = "Remap controls",
144 .data = "*REMAP",
145 .state = 0,
146 .validStates = i ? mapNames : 0,
147 .nStates = i
148 };
149 }
150 for (i = 0; i < nExtra; ++i) {
151 *GUIMenuItemListAppend(&menu.items) = extra[i];
152 }
153 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
154 .title = "Save",
155 .data = "*SAVE",
156 };
157 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
158 .title = "Cancel",
159 .data = 0,
160 };
161 enum GUIMenuExitReason reason;
162 char gbaBiosPath[256] = "";
163#ifdef M_CORE_GB
164 char gbBiosPath[256] = "";
165 char gbcBiosPath[256] = "";
166 char sgbBiosPath[256] = "";
167#endif
168
169 struct GUIMenuItem* item;
170 for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
171 item = GUIMenuItemListGetPointer(&menu.items, i);
172 if (!item->validStates || !item->data) {
173 continue;
174 }
175 if (item->stateMappings) {
176 item->state = 0;
177
178 size_t j;
179 for (j = 0; j < item->nStates; ++j) {
180 const struct GUIVariant* v = &item->stateMappings[j];
181 struct GUIVariant test;
182 switch (v->type) {
183 case GUI_VARIANT_VOID:
184 if (!mCoreConfigGetValue(&runner->config, item->data)) {
185 item->state = j;
186 break;
187 }
188 break;
189 case GUI_VARIANT_UNSIGNED:
190 if (mCoreConfigGetUIntValue(&runner->config, item->data, &test.v.u) && test.v.u == v->v.u) {
191 item->state = j;
192 break;
193 }
194 break;
195 case GUI_VARIANT_INT:
196 if (mCoreConfigGetIntValue(&runner->config, item->data, &test.v.i) && test.v.i == v->v.i) {
197 item->state = j;
198 break;
199 }
200 break;
201 case GUI_VARIANT_FLOAT:
202 if (mCoreConfigGetFloatValue(&runner->config, item->data, &test.v.f) && fabsf(test.v.f - v->v.f) <= 1e-3f) {
203 item->state = j;
204 break;
205 }
206 break;
207 case GUI_VARIANT_STRING:
208 test.v.s = mCoreConfigGetValue(&runner->config, item->data);
209 if (test.v.s && strcmp(test.v.s, v->v.s) == 0) {
210 item->state = j;
211 break;
212 }
213 break;
214 }
215 }
216 } else {
217 mCoreConfigGetUIntValue(&runner->config, item->data, &item->state);
218 }
219 }
220
221 while (true) {
222 reason = GUIShowMenu(&runner->params, &menu, &item);
223 if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
224 break;
225 }
226 if (!strcmp(item->data, "*SAVE")) {
227 if (gbaBiosPath[0]) {
228 mCoreConfigSetValue(&runner->config, "gba.bios", gbaBiosPath);
229 }
230 if (gbBiosPath[0]) {
231 mCoreConfigSetValue(&runner->config, "gb.bios", gbBiosPath);
232 }
233 if (gbcBiosPath[0]) {
234 mCoreConfigSetValue(&runner->config, "gbc.bios", gbcBiosPath);
235 }
236 if (sgbBiosPath[0]) {
237 mCoreConfigSetValue(&runner->config, "sgb.bios", sgbBiosPath);
238 }
239 for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
240 item = GUIMenuItemListGetPointer(&menu.items, i);
241 if (!item->validStates || !item->data || ((const char*) item->data)[0] == '*') {
242 continue;
243 }
244 if (item->stateMappings) {
245 const struct GUIVariant* v = &item->stateMappings[item->state];
246 switch (v->type) {
247 case GUI_VARIANT_VOID:
248 mCoreConfigSetValue(&runner->config, item->data, NULL);
249 break;
250 case GUI_VARIANT_UNSIGNED:
251 mCoreConfigSetUIntValue(&runner->config, item->data, v->v.u);
252 break;
253 case GUI_VARIANT_INT:
254 mCoreConfigSetUIntValue(&runner->config, item->data, v->v.i);
255 break;
256 case GUI_VARIANT_FLOAT:
257 mCoreConfigSetFloatValue(&runner->config, item->data, v->v.f);
258 break;
259 case GUI_VARIANT_STRING:
260 mCoreConfigSetValue(&runner->config, item->data, v->v.s);
261 break;
262 }
263 } else {
264 mCoreConfigSetUIntValue(&runner->config, item->data, item->state);
265 }
266 }
267 if (runner->keySources) {
268 size_t i;
269 for (i = 0; runner->keySources[i].id; ++i) {
270 mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
271 mInputMapSave(&runner->params.keyMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
272 }
273 }
274 mCoreConfigSave(&runner->config);
275 mCoreLoadForeignConfig(runner->core, &runner->config);
276 break;
277 }
278 if (!strcmp(item->data, "*REMAP")) {
279 mGUIRemapKeys(&runner->params, &runner->core->inputMap, &runner->keySources[item->state]);
280 continue;
281 }
282 if (!strcmp(item->data, "gba.bios")) {
283 // TODO: show box if failed
284 if (!GUISelectFile(&runner->params, gbaBiosPath, sizeof(gbaBiosPath), _biosNamed, GBAIsBIOS)) {
285 gbaBiosPath[0] = '\0';
286 }
287 continue;
288 }
289#ifdef M_CORE_GB
290 if (!strcmp(item->data, "gb.bios")) {
291 // TODO: show box if failed
292 if (!GUISelectFile(&runner->params, gbBiosPath, sizeof(gbBiosPath), _biosNamed, GBIsBIOS)) {
293 gbBiosPath[0] = '\0';
294 }
295 continue;
296 }
297 if (!strcmp(item->data, "gbc.bios")) {
298 // TODO: show box if failed
299 if (!GUISelectFile(&runner->params, gbcBiosPath, sizeof(gbcBiosPath), _biosNamed, GBIsBIOS)) {
300 gbcBiosPath[0] = '\0';
301 }
302 continue;
303 }
304 if (!strcmp(item->data, "sgb.bios")) {
305 // TODO: show box if failed
306 if (!GUISelectFile(&runner->params, sgbBiosPath, sizeof(sgbBiosPath), _biosNamed, GBIsBIOS)) {
307 sgbBiosPath[0] = '\0';
308 }
309 continue;
310 }
311#endif
312 if (item->validStates) {
313 ++item->state;
314 if (item->state >= item->nStates) {
315 item->state = 0;
316 }
317 }
318 }
319}