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 mCoreConfigGetUIntValue(&runner->config, item->data, &item->state);
166 }
167
168 while (true) {
169 reason = GUIShowMenu(&runner->params, &menu, &item);
170 if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
171 break;
172 }
173 if (!strcmp(item->data, "*SAVE")) {
174 if (gbaBiosPath[0]) {
175 mCoreConfigSetValue(&runner->config, "gba.bios", gbaBiosPath);
176 }
177 if (gbBiosPath[0]) {
178 mCoreConfigSetValue(&runner->config, "gb.bios", gbBiosPath);
179 }
180 if (gbcBiosPath[0]) {
181 mCoreConfigSetValue(&runner->config, "gbc.bios", gbcBiosPath);
182 }
183 if (sgbBiosPath[0]) {
184 mCoreConfigSetValue(&runner->config, "sgb.bios", sgbBiosPath);
185 }
186 for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
187 item = GUIMenuItemListGetPointer(&menu.items, i);
188 if (!item->validStates || !item->data) {
189 continue;
190 }
191 mCoreConfigSetUIntValue(&runner->config, item->data, item->state);
192 }
193 if (runner->keySources) {
194 size_t i;
195 for (i = 0; runner->keySources[i].id; ++i) {
196 mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
197 mInputMapSave(&runner->params.keyMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
198 }
199 }
200 mCoreConfigSave(&runner->config);
201 mCoreLoadForeignConfig(runner->core, &runner->config);
202 break;
203 }
204 if (!strcmp(item->data, "*REMAP")) {
205 mGUIRemapKeys(&runner->params, &runner->core->inputMap, &runner->keySources[item->state]);
206 continue;
207 }
208 if (!strcmp(item->data, "gba.bios")) {
209 // TODO: show box if failed
210 if (!GUISelectFile(&runner->params, gbaBiosPath, sizeof(gbaBiosPath), _biosNamed, GBAIsBIOS)) {
211 gbaBiosPath[0] = '\0';
212 }
213 continue;
214 }
215#ifdef M_CORE_GB
216 if (!strcmp(item->data, "gb.bios")) {
217 // TODO: show box if failed
218 if (!GUISelectFile(&runner->params, gbBiosPath, sizeof(gbBiosPath), _biosNamed, GBIsBIOS)) {
219 gbBiosPath[0] = '\0';
220 }
221 continue;
222 }
223 if (!strcmp(item->data, "gbc.bios")) {
224 // TODO: show box if failed
225 if (!GUISelectFile(&runner->params, gbcBiosPath, sizeof(gbcBiosPath), _biosNamed, GBIsBIOS)) {
226 gbcBiosPath[0] = '\0';
227 }
228 continue;
229 }
230 if (!strcmp(item->data, "sgb.bios")) {
231 // TODO: show box if failed
232 if (!GUISelectFile(&runner->params, sgbBiosPath, sizeof(sgbBiosPath), _biosNamed, GBIsBIOS)) {
233 sgbBiosPath[0] = '\0';
234 }
235 continue;
236 }
237#endif
238 if (item->validStates) {
239 ++item->state;
240 if (item->state >= item->nStates) {
241 item->state = 0;
242 }
243 }
244 }
245}