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 (strnstr(name, "bios", PATH_MAX)) {
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#endif
112 size_t i;
113 const char* mapNames[GUI_MAX_INPUTS + 1];
114 if (runner->keySources) {
115 for (i = 0; runner->keySources[i].id && i < GUI_MAX_INPUTS; ++i) {
116 mapNames[i] = runner->keySources[i].name;
117 }
118 if (i == 1) {
119 // Don't display a name if there's only one input source
120 i = 0;
121 }
122 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
123 .title = "Remap controls",
124 .data = "*REMAP",
125 .state = 0,
126 .validStates = i ? mapNames : 0,
127 .nStates = i
128 };
129 }
130 for (i = 0; i < nExtra; ++i) {
131 *GUIMenuItemListAppend(&menu.items) = extra[i];
132 }
133 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
134 .title = "Save",
135 .data = "*SAVE",
136 };
137 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
138 .title = "Cancel",
139 .data = 0,
140 };
141 enum GUIMenuExitReason reason;
142 char gbaBiosPath[256] = "";
143#ifdef M_CORE_GB
144 char gbBiosPath[256] = "";
145 char gbcBiosPath[256] = "";
146 char sgbBiosPath[256] = "";
147#endif
148
149 struct GUIMenuItem* item;
150 for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
151 item = GUIMenuItemListGetPointer(&menu.items, i);
152 if (!item->validStates || !item->data) {
153 continue;
154 }
155 mCoreConfigGetUIntValue(&runner->config, item->data, &item->state);
156 }
157
158 while (true) {
159 reason = GUIShowMenu(&runner->params, &menu, &item);
160 if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
161 break;
162 }
163 if (!strcmp(item->data, "*SAVE")) {
164 if (gbaBiosPath[0]) {
165 mCoreConfigSetValue(&runner->config, "gba.bios", gbaBiosPath);
166 }
167 if (gbBiosPath[0]) {
168 mCoreConfigSetValue(&runner->config, "gb.bios", gbBiosPath);
169 }
170 if (gbcBiosPath[0]) {
171 mCoreConfigSetValue(&runner->config, "gbc.bios", gbcBiosPath);
172 }
173 if (sgbBiosPath[0]) {
174 mCoreConfigSetValue(&runner->config, "sgb.bios", sgbBiosPath);
175 }
176 for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
177 item = GUIMenuItemListGetPointer(&menu.items, i);
178 if (!item->validStates || !item->data) {
179 continue;
180 }
181 mCoreConfigSetUIntValue(&runner->config, item->data, item->state);
182 }
183 if (runner->keySources) {
184 size_t i;
185 for (i = 0; runner->keySources[i].id; ++i) {
186 mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
187 mInputMapSave(&runner->params.keyMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
188 }
189 }
190 mCoreConfigSave(&runner->config);
191 mCoreLoadForeignConfig(runner->core, &runner->config);
192 break;
193 }
194 if (!strcmp(item->data, "*REMAP")) {
195 mGUIRemapKeys(&runner->params, &runner->core->inputMap, &runner->keySources[item->state]);
196 continue;
197 }
198 if (!strcmp(item->data, "gba.bios")) {
199 // TODO: show box if failed
200 if (!GUISelectFile(&runner->params, gbaBiosPath, sizeof(gbaBiosPath), _biosNamed, GBAIsBIOS)) {
201 gbaBiosPath[0] = '\0';
202 }
203 continue;
204 }
205#ifdef M_CORE_GB
206 if (!strcmp(item->data, "gb.bios")) {
207 // TODO: show box if failed
208 if (!GUISelectFile(&runner->params, gbBiosPath, sizeof(gbBiosPath), _biosNamed, GBIsBIOS)) {
209 gbBiosPath[0] = '\0';
210 }
211 continue;
212 }
213 if (!strcmp(item->data, "gbc.bios")) {
214 // TODO: show box if failed
215 if (!GUISelectFile(&runner->params, gbcBiosPath, sizeof(gbcBiosPath), _biosNamed, GBIsBIOS)) {
216 gbcBiosPath[0] = '\0';
217 }
218 continue;
219 }
220 if (!strcmp(item->data, "sgb.bios")) {
221 // TODO: show box if failed
222 if (!GUISelectFile(&runner->params, sgbBiosPath, sizeof(sgbBiosPath), _biosNamed, GBIsBIOS)) {
223 sgbBiosPath[0] = '\0';
224 }
225 continue;
226 }
227#endif
228 if (item->validStates) {
229 ++item->state;
230 if (item->state >= item->nStates) {
231 item->state = 0;
232 }
233 }
234 }
235}