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