all repos — mgba @ 9fac945e1c46f332acad6dcab3ff31dd8ba7216d

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
 19#ifndef GUI_MAX_INPUTS
 20#define GUI_MAX_INPUTS 7
 21#endif
 22
 23void mGUIShowConfig(struct mGUIRunner* runner, struct GUIMenuItem* extra, size_t nExtra) {
 24	struct GUIMenu menu = {
 25		.title = "Configure",
 26		.index = 0,
 27		.background = &runner->background.d
 28	};
 29	GUIMenuItemListInit(&menu.items, 0);
 30	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 31		.title = "Frameskip",
 32		.data = "frameskip",
 33		.submenu = 0,
 34		.state = 0,
 35		.validStates = (const char*[]) {
 36			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
 37		},
 38		.nStates = 10
 39	};
 40	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 41		.title = "Show framerate",
 42		.data = "fpsCounter",
 43		.submenu = 0,
 44		.state = false,
 45		.validStates = (const char*[]) {
 46			"Off", "On"
 47		},
 48		.nStates = 2
 49	};
 50	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 51		.title = "Use BIOS if found",
 52		.data = "useBios",
 53		.submenu = 0,
 54		.state = true,
 55		.validStates = (const char*[]) {
 56			"Off", "On"
 57		},
 58		.nStates = 2
 59	};
 60	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 61		.title = "Select GBA BIOS path",
 62		.data = "gba.bios",
 63	};
 64#ifdef M_CORE_GB
 65	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 66		.title = "Select GB BIOS path",
 67		.data = "gb.bios",
 68	};
 69	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 70		.title = "Select GBC BIOS path",
 71		.data = "gbc.bios",
 72	};
 73	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 74		.title = "Select SGB BIOS path",
 75		.data = "sgb.bios",
 76	};
 77#endif
 78	size_t i;
 79	const char* mapNames[GUI_MAX_INPUTS + 1];
 80	if (runner->keySources) {
 81		for (i = 0; runner->keySources[i].id && i < GUI_MAX_INPUTS; ++i) {
 82			mapNames[i] = runner->keySources[i].name;
 83		}
 84		if (i == 1) {
 85			// Don't display a name if there's only one input source
 86			i = 0;
 87		}
 88		*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 89			.title = "Remap controls",
 90			.data = "*REMAP",
 91			.state = 0,
 92			.validStates = i ? mapNames : 0,
 93			.nStates = i
 94		};
 95	}
 96	for (i = 0; i < nExtra; ++i) {
 97		*GUIMenuItemListAppend(&menu.items) = extra[i];
 98	}
 99	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
100		.title = "Save",
101		.data = "*SAVE",
102	};
103	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
104		.title = "Cancel",
105		.data = 0,
106	};
107	enum GUIMenuExitReason reason;
108	char gbaBiosPath[256] = "";
109#ifdef M_CORE_GB
110	char gbBiosPath[256] = "";
111	char gbcBiosPath[256] = "";
112	char sgbBiosPath[256] = "";
113#endif
114
115	struct GUIMenuItem* item;
116	for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
117		item = GUIMenuItemListGetPointer(&menu.items, i);
118		if (!item->validStates || !item->data) {
119			continue;
120		}
121		mCoreConfigGetUIntValue(&runner->config, item->data, &item->state);
122	}
123
124	while (true) {
125		reason = GUIShowMenu(&runner->params, &menu, &item);
126		if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
127			break;
128		}
129		if (!strcmp(item->data, "*SAVE")) {
130			if (gbaBiosPath[0]) {
131				mCoreConfigSetValue(&runner->config, "gba.bios", gbaBiosPath);
132			}
133			if (gbBiosPath[0]) {
134				mCoreConfigSetValue(&runner->config, "gb.bios", gbBiosPath);
135			}
136			if (gbcBiosPath[0]) {
137				mCoreConfigSetValue(&runner->config, "gbc.bios", gbcBiosPath);
138			}
139			if (sgbBiosPath[0]) {
140				mCoreConfigSetValue(&runner->config, "sgb.bios", sgbBiosPath);
141			}
142			for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
143				item = GUIMenuItemListGetPointer(&menu.items, i);
144				if (!item->validStates || !item->data) {
145					continue;
146				}
147				mCoreConfigSetUIntValue(&runner->config, item->data, item->state);
148			}
149			if (runner->keySources) {
150				size_t i;
151				for (i = 0; runner->keySources[i].id; ++i) {
152					mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
153					mInputMapSave(&runner->params.keyMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->config));
154				}
155			}
156			mCoreConfigSave(&runner->config);
157			mCoreLoadForeignConfig(runner->core, &runner->config);
158			break;
159		}
160		if (!strcmp(item->data, "*REMAP")) {
161			mGUIRemapKeys(&runner->params, &runner->core->inputMap, &runner->keySources[item->state]);
162			continue;
163		}
164		if (!strcmp(item->data, "gba.bios")) {
165			// TODO: show box if failed
166			if (!GUISelectFile(&runner->params, gbaBiosPath, sizeof(gbaBiosPath), GBAIsBIOS)) {
167				gbaBiosPath[0] = '\0';
168			}
169			continue;
170		}
171#ifdef M_CORE_GB
172		if (!strcmp(item->data, "gb.bios")) {
173			// TODO: show box if failed
174			if (!GUISelectFile(&runner->params, gbBiosPath, sizeof(gbBiosPath), GBIsBIOS)) {
175				gbBiosPath[0] = '\0';
176			}
177			continue;
178		}
179		if (!strcmp(item->data, "gbc.bios")) {
180			// TODO: show box if failed
181			if (!GUISelectFile(&runner->params, gbcBiosPath, sizeof(gbcBiosPath), GBIsBIOS)) {
182				gbcBiosPath[0] = '\0';
183			}
184			continue;
185		}
186		if (!strcmp(item->data, "sgb.bios")) {
187			// TODO: show box if failed
188			if (!GUISelectFile(&runner->params, sgbBiosPath, sizeof(sgbBiosPath), GBIsBIOS)) {
189				sgbBiosPath[0] = '\0';
190			}
191			continue;
192		}
193#endif
194		if (item->validStates) {
195			++item->state;
196			if (item->state >= item->nStates) {
197				item->state = 0;
198			}
199		}
200	}
201}