all repos — mgba @ 79c40d9359dae14defd753caf4068bf3798052ec

mGBA Game Boy Advance Emulator

src/platform/qt/SettingsView.cpp (view raw)

  1/* Copyright (c) 2013-2014 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 "SettingsView.h"
  7
  8#include "AudioProcessor.h"
  9#include "ConfigController.h"
 10#include "Display.h"
 11#include "GBAApp.h"
 12#include "GBAKeyEditor.h"
 13#include "InputController.h"
 14#include "RotatedHeaderView.h"
 15#include "ShaderSelector.h"
 16#include "ShortcutView.h"
 17
 18#ifdef M_CORE_GB
 19#include "GameBoy.h"
 20#endif
 21
 22#include <mgba/core/serialize.h>
 23#include <mgba/core/version.h>
 24#include <mgba/internal/gba/gba.h>
 25
 26using namespace QGBA;
 27
 28SettingsView::SettingsView(ConfigController* controller, InputController* inputController, ShortcutController* shortcutController, LogController* logController, QWidget* parent)
 29	: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
 30	, m_controller(controller)
 31	, m_logModel(logController)
 32{
 33	m_ui.setupUi(this);
 34
 35	m_pageIndex[Page::AV] = 0;
 36	m_pageIndex[Page::INTERFACE] = 1;
 37	m_pageIndex[Page::EMULATION] = 2;
 38	m_pageIndex[Page::ENHANCEMENTS] = 3;
 39	m_pageIndex[Page::BIOS] = 4;
 40	m_pageIndex[Page::PATHS] = 5;
 41	m_pageIndex[Page::LOGGING] = 6;
 42
 43#ifdef M_CORE_GB
 44	m_pageIndex[Page::GB] = 7;
 45
 46	for (auto model : GameBoy::modelList()) {
 47		m_ui.gbModel->addItem(GameBoy::modelName(model), model);
 48		m_ui.sgbModel->addItem(GameBoy::modelName(model), model);
 49		m_ui.cgbModel->addItem(GameBoy::modelName(model), model);
 50		m_ui.cgbHybridModel->addItem(GameBoy::modelName(model), model);
 51		m_ui.cgbSgbModel->addItem(GameBoy::modelName(model), model);
 52	}
 53
 54	m_ui.gbModel->setCurrentIndex(m_ui.gbModel->findData(GB_MODEL_DMG));
 55	m_ui.sgbModel->setCurrentIndex(m_ui.gbModel->findData(GB_MODEL_SGB));
 56	m_ui.cgbModel->setCurrentIndex(m_ui.gbModel->findData(GB_MODEL_CGB));
 57	m_ui.cgbHybridModel->setCurrentIndex(m_ui.gbModel->findData(GB_MODEL_CGB));
 58	m_ui.cgbSgbModel->setCurrentIndex(m_ui.gbModel->findData(GB_MODEL_CGB));
 59#endif
 60
 61	reloadConfig();
 62
 63	connect(m_ui.volume, static_cast<void (QSlider::*)(int)>(&QSlider::valueChanged), [this](int v) {
 64		if (v < m_ui.volumeFf->value()) {
 65			m_ui.volumeFf->setValue(v);
 66		}
 67	});
 68
 69	connect(m_ui.mute, &QAbstractButton::toggled, [this](bool e) {
 70		if (e) {
 71			m_ui.muteFf->setChecked(e);
 72		}
 73	});
 74
 75	connect(m_ui.nativeGB, &QAbstractButton::pressed, [this]() {
 76		m_ui.fpsTarget->setValue(double(GBA_ARM7TDMI_FREQUENCY) / double(VIDEO_TOTAL_LENGTH));
 77	});
 78
 79	if (m_ui.savegamePath->text().isEmpty()) {
 80		m_ui.savegameSameDir->setChecked(true);
 81	}
 82	connect(m_ui.savegameSameDir, &QAbstractButton::toggled, [this] (bool e) {
 83		if (e) {
 84			m_ui.savegamePath->clear();
 85		}
 86	});
 87	connect(m_ui.savegameBrowse, &QAbstractButton::pressed, [this] () {
 88		selectPath(m_ui.savegamePath, m_ui.savegameSameDir);
 89	});
 90
 91	if (m_ui.savestatePath->text().isEmpty()) {
 92		m_ui.savestateSameDir->setChecked(true);
 93	}
 94	connect(m_ui.savestateSameDir, &QAbstractButton::toggled, [this] (bool e) {
 95		if (e) {
 96			m_ui.savestatePath->clear();
 97		}
 98	});
 99	connect(m_ui.savestateBrowse, &QAbstractButton::pressed, [this] () {
100		selectPath(m_ui.savestatePath, m_ui.savestateSameDir);
101	});
102
103	if (m_ui.screenshotPath->text().isEmpty()) {
104		m_ui.screenshotSameDir->setChecked(true);
105	}
106	connect(m_ui.screenshotSameDir, &QAbstractButton::toggled, [this] (bool e) {
107		if (e) {
108			m_ui.screenshotPath->clear();
109		}
110	});
111	connect(m_ui.screenshotBrowse, &QAbstractButton::pressed, [this] () {
112		selectPath(m_ui.screenshotPath, m_ui.screenshotSameDir);
113	});
114
115	if (m_ui.patchPath->text().isEmpty()) {
116		m_ui.patchSameDir->setChecked(true);
117	}
118	connect(m_ui.patchSameDir, &QAbstractButton::toggled, [this] (bool e) {
119		if (e) {
120			m_ui.patchPath->clear();
121		}
122	});
123	connect(m_ui.patchBrowse, &QAbstractButton::pressed, [this] () {
124		selectPath(m_ui.patchPath, m_ui.patchSameDir);
125	});
126
127	if (m_ui.cheatsPath->text().isEmpty()) {
128		m_ui.cheatsSameDir->setChecked(true);
129	}
130	connect(m_ui.cheatsSameDir, &QAbstractButton::toggled, [this] (bool e) {
131		if (e) {
132			m_ui.cheatsPath->clear();
133		}
134	});
135	connect(m_ui.cheatsBrowse, &QAbstractButton::pressed, [this] () {
136		selectPath(m_ui.cheatsPath, m_ui.cheatsSameDir);
137	});
138	connect(m_ui.clearCache, &QAbstractButton::pressed, this, &SettingsView::libraryCleared);
139
140	// TODO: Move to reloadConfig()
141	QVariant audioDriver = m_controller->getQtOption("audioDriver");
142#ifdef BUILD_QT_MULTIMEDIA
143	m_ui.audioDriver->addItem(tr("Qt Multimedia"), static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA));
144	if (!audioDriver.isNull() && audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::QT_MULTIMEDIA)) {
145		m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
146	}
147#endif
148
149#ifdef BUILD_SDL
150	m_ui.audioDriver->addItem(tr("SDL"), static_cast<int>(AudioProcessor::Driver::SDL));
151	if (audioDriver.isNull() || audioDriver.toInt() == static_cast<int>(AudioProcessor::Driver::SDL)) {
152		m_ui.audioDriver->setCurrentIndex(m_ui.audioDriver->count() - 1);
153	}
154#endif
155
156	// TODO: Move to reloadConfig()
157	QVariant displayDriver = m_controller->getQtOption("displayDriver");
158	m_ui.displayDriver->addItem(tr("Software (Qt)"), static_cast<int>(Display::Driver::QT));
159	if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::QT)) {
160		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
161	}
162
163#if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY)
164	m_ui.displayDriver->addItem(tr("OpenGL"), static_cast<int>(Display::Driver::OPENGL));
165	if (displayDriver.isNull() || displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL)) {
166		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
167	}
168#endif
169
170#ifdef BUILD_GL
171	m_ui.displayDriver->addItem(tr("OpenGL (force version 1.x)"), static_cast<int>(Display::Driver::OPENGL1));
172	if (!displayDriver.isNull() && displayDriver.toInt() == static_cast<int>(Display::Driver::OPENGL1)) {
173		m_ui.displayDriver->setCurrentIndex(m_ui.displayDriver->count() - 1);
174	}
175#endif
176
177	// TODO: Move to reloadConfig()
178	QVariant cameraDriver = m_controller->getQtOption("cameraDriver");
179	m_ui.cameraDriver->addItem(tr("None (Still Image)"), static_cast<int>(InputController::CameraDriver::NONE));
180	if (cameraDriver.isNull() || cameraDriver.toInt() == static_cast<int>(InputController::CameraDriver::NONE)) {
181		m_ui.cameraDriver->setCurrentIndex(m_ui.cameraDriver->count() - 1);
182		m_ui.camera->setEnabled(false);
183	}
184
185#ifdef BUILD_QT_MULTIMEDIA
186	m_ui.cameraDriver->addItem(tr("Qt Multimedia"), static_cast<int>(InputController::CameraDriver::QT_MULTIMEDIA));
187	if (!cameraDriver.isNull() && cameraDriver.toInt() == static_cast<int>(InputController::CameraDriver::QT_MULTIMEDIA)) {
188		m_ui.cameraDriver->setCurrentIndex(m_ui.cameraDriver->count() - 1);
189		m_ui.camera->setEnabled(true);
190	}
191	QList<QPair<QByteArray, QString>> cameras = inputController->listCameras();
192	QByteArray currentCamera = m_controller->getQtOption("camera").toByteArray();
193	for (const auto& camera : cameras) {
194		m_ui.camera->addItem(camera.second, camera.first);
195		if (camera.first == currentCamera) {
196			m_ui.camera->setCurrentIndex(m_ui.camera->count() - 1);
197		}
198	}
199#endif
200
201#ifdef M_CORE_GBA
202	connect(m_ui.gbaBiosBrowse, &QPushButton::clicked, [this]() {
203		selectBios(m_ui.gbaBios);
204	});
205#else
206	m_ui.gbaBiosBrowse->hide();
207#endif
208
209#ifdef M_CORE_GB
210	connect(m_ui.gbBiosBrowse, &QPushButton::clicked, [this]() {
211		selectBios(m_ui.gbBios);
212	});
213	connect(m_ui.gbcBiosBrowse, &QPushButton::clicked, [this]() {
214		selectBios(m_ui.gbcBios);
215	});
216	connect(m_ui.sgbBiosBrowse, &QPushButton::clicked, [this]() {
217		selectBios(m_ui.sgbBios);
218	});
219
220	QList<QColor> defaultColors;
221	defaultColors.append(QColor(0xF8, 0xF8, 0xF8));
222	defaultColors.append(QColor(0xA8, 0xA8, 0xA8));
223	defaultColors.append(QColor(0x50, 0x50, 0x50));
224	defaultColors.append(QColor(0x00, 0x00, 0x00));
225	defaultColors.append(QColor(0xF8, 0xF8, 0xF8));
226	defaultColors.append(QColor(0xA8, 0xA8, 0xA8));
227	defaultColors.append(QColor(0x50, 0x50, 0x50));
228	defaultColors.append(QColor(0x00, 0x00, 0x00));
229	defaultColors.append(QColor(0xF8, 0xF8, 0xF8));
230	defaultColors.append(QColor(0xA8, 0xA8, 0xA8));
231	defaultColors.append(QColor(0x50, 0x50, 0x50));
232	defaultColors.append(QColor(0x00, 0x00, 0x00));
233	QList<QWidget*> colors{
234		m_ui.color0,
235		m_ui.color1,
236		m_ui.color2,
237		m_ui.color3,
238		m_ui.color4,
239		m_ui.color5,
240		m_ui.color6,
241		m_ui.color7,
242		m_ui.color8,
243		m_ui.color9,
244		m_ui.color10,
245		m_ui.color11
246	};
247	for (int colorId = 0; colorId < 12; ++colorId) {
248		bool ok;
249		uint color = m_controller->getOption(QString("gb.pal[%0]").arg(colorId)).toUInt(&ok);
250		if (ok) {
251			defaultColors[colorId] = QColor::fromRgb(color);
252			m_gbColors[colorId] = color | 0xFF000000;
253		} else {
254			m_gbColors[colorId] = defaultColors[colorId].rgb() & ~0xFF000000;
255		}
256		m_colorPickers[colorId] = ColorPicker(colors[colorId], defaultColors[colorId]);
257		connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
258			m_gbColors[colorId] = color.rgb();
259		});
260	}
261#else
262	m_ui.gbBiosBrowse->hide();
263	m_ui.gbcBiosBrowse->hide();
264	m_ui.sgbBiosBrowse->hide();
265	m_ui.gb->hide();
266#endif
267
268	GBAKeyEditor* editor = new GBAKeyEditor(inputController, InputController::KEYBOARD, QString(), this);
269	addPage(tr("Keyboard"), editor, Page::KEYBOARD);
270	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, editor, &GBAKeyEditor::save);
271
272	GBAKeyEditor* buttonEditor = nullptr;
273#ifdef BUILD_SDL
274	inputController->recalibrateAxes();
275	const char* profile = inputController->profileForType(SDL_BINDING_BUTTON);
276	buttonEditor = new GBAKeyEditor(inputController, SDL_BINDING_BUTTON, profile);
277	addPage(tr("Controllers"), buttonEditor, Page::CONTROLLERS);
278	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, buttonEditor, &GBAKeyEditor::save);
279#endif
280
281	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &SettingsView::updateConfig);
282	connect(m_ui.buttonBox, &QDialogButtonBox::clicked, [this, editor, buttonEditor](QAbstractButton* button) {
283		if (m_ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
284			updateConfig();
285			editor->save();
286			if (buttonEditor) {
287				buttonEditor->save();
288			}
289		}
290	});
291
292	m_ui.languages->setItemData(0, QLocale("en"));
293	QDir ts(":/translations/");
294	for (auto name : ts.entryList()) {
295		if (!name.endsWith(".qm") || !name.startsWith(binaryName)) {
296			continue;
297		}
298		QLocale locale(name.remove(QString("%0-").arg(binaryName)).remove(".qm"));
299		if (locale.language() == QLocale::English) {
300			continue;
301		}
302		m_ui.languages->addItem(locale.nativeLanguageName(), locale);
303		if (locale.bcp47Name() == QLocale().bcp47Name()) {
304			m_ui.languages->setCurrentIndex(m_ui.languages->count() - 1);
305		}
306	}
307
308	m_ui.loggingView->setModel(&m_logModel);
309	m_ui.loggingView->setHorizontalHeader(new RotatedHeaderView(Qt::Horizontal));
310	m_ui.loggingView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
311	m_ui.loggingView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
312
313	connect(m_ui.logFileBrowse, &QAbstractButton::pressed, [this] () {
314		QString path = GBAApp::app()->getSaveFileName(this, "Select log file");
315		if (!path.isNull()) {
316			m_ui.logFile->setText(path);
317		}
318	});
319
320	ShortcutView* shortcutView = new ShortcutView();
321	shortcutView->setController(shortcutController);
322	shortcutView->setInputController(inputController);
323	addPage(tr("Shortcuts"), shortcutView, Page::SHORTCUTS);
324}
325
326SettingsView::~SettingsView() {
327#if defined(BUILD_GL) || defined(BUILD_GLES2)
328	setShaderSelector(nullptr);
329#endif
330}
331
332void SettingsView::setShaderSelector(ShaderSelector* shaderSelector) {
333#if defined(BUILD_GL) || defined(BUILD_GLES2)
334	if (m_shader) {
335		auto items = m_ui.tabs->findItems(tr("Shaders"), Qt::MatchFixedString);
336		for (const auto& item : items) {
337			m_ui.tabs->removeItemWidget(item);
338		}
339		m_ui.stackedWidget->removeWidget(m_shader);
340		m_shader->setParent(nullptr);
341	}
342	m_shader = shaderSelector;
343	if (shaderSelector) {
344		m_ui.stackedWidget->addWidget(m_shader);
345		m_ui.tabs->addItem(tr("Shaders"));
346		connect(m_ui.buttonBox, &QDialogButtonBox::accepted, m_shader, &ShaderSelector::saved);
347	}
348#endif
349}
350
351void SettingsView::selectPage(SettingsView::Page page) {
352	m_ui.tabs->setCurrentRow(m_pageIndex[page]);
353}
354
355QString SettingsView::makePortablePath(const QString& path) {
356	if (m_controller->isPortable()) {
357		QDir configDir(m_controller->configDir());
358		QFileInfo pathInfo(path);
359		if (pathInfo.canonicalPath() == configDir.canonicalPath()) {
360			return configDir.relativeFilePath(pathInfo.canonicalFilePath());
361		}
362	}
363	return path;
364}
365
366void SettingsView::selectBios(QLineEdit* bios) {
367	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
368	if (!filename.isEmpty()) {
369		bios->setText(makePortablePath(filename));
370	}
371}
372
373void SettingsView::selectPath(QLineEdit* field, QCheckBox* sameDir) {
374	QString path = GBAApp::app()->getOpenDirectoryName(this, tr("Select directory"));
375	if (!path.isNull()) {
376		sameDir->setChecked(false);
377		field->setText(makePortablePath(path));
378	}
379}
380
381void SettingsView::updateConfig() {
382	saveSetting("gba.bios", m_ui.gbaBios);
383	saveSetting("gb.bios", m_ui.gbBios);
384	saveSetting("gbc.bios", m_ui.gbcBios);
385	saveSetting("sgb.bios", m_ui.sgbBios);
386	saveSetting("sgb.borders", m_ui.sgbBorders);
387	saveSetting("useCgbColors", m_ui.useCgbColors);
388	saveSetting("useBios", m_ui.useBios);
389	saveSetting("skipBios", m_ui.skipBios);
390	saveSetting("sampleRate", m_ui.sampleRate);
391	saveSetting("videoSync", m_ui.videoSync);
392	saveSetting("audioSync", m_ui.audioSync);
393	saveSetting("frameskip", m_ui.frameskip);
394	saveSetting("autofireThreshold", m_ui.autofireThreshold);
395	saveSetting("lockAspectRatio", m_ui.lockAspectRatio);
396	saveSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
397	saveSetting("interframeBlending", m_ui.interframeBlending);
398	saveSetting("showOSD", m_ui.showOSD);
399	saveSetting("volume", m_ui.volume);
400	saveSetting("mute", m_ui.mute);
401	saveSetting("fastForwardVolume", m_ui.volumeFf);
402	saveSetting("fastForwardMute", m_ui.muteFf);
403	saveSetting("rewindEnable", m_ui.rewind);
404	saveSetting("rewindBufferCapacity", m_ui.rewindCapacity);
405	saveSetting("resampleVideo", m_ui.resampleVideo);
406	saveSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
407	saveSetting("suspendScreensaver", m_ui.suspendScreensaver);
408	saveSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
409	saveSetting("pauseOnMinimize", m_ui.pauseOnMinimize);
410	saveSetting("savegamePath", m_ui.savegamePath);
411	saveSetting("savestatePath", m_ui.savestatePath);
412	saveSetting("screenshotPath", m_ui.screenshotPath);
413	saveSetting("patchPath", m_ui.patchPath);
414	saveSetting("cheatsPath", m_ui.cheatsPath);
415	saveSetting("libraryStyle", m_ui.libraryStyle->currentIndex());
416	saveSetting("showLibrary", m_ui.showLibrary);
417	saveSetting("preload", m_ui.preload);
418	saveSetting("showFps", m_ui.showFps);
419	saveSetting("cheatAutoload", m_ui.cheatAutoload);
420	saveSetting("cheatAutosave", m_ui.cheatAutosave);
421	saveSetting("showFilename", m_ui.showFilename);
422	saveSetting("autoload", m_ui.autoload);
423	saveSetting("autosave", m_ui.autosave);
424	saveSetting("logToFile", m_ui.logToFile);
425	saveSetting("logToStdout", m_ui.logToStdout);
426	saveSetting("logFile", m_ui.logFile);
427	saveSetting("useDiscordPresence", m_ui.useDiscordPresence);
428	saveSetting("gba.audioHle", m_ui.audioHle);
429	saveSetting("dynamicTitle", m_ui.dynamicTitle);
430	saveSetting("videoScale", m_ui.videoScale);
431	saveSetting("gba.forceGbp", m_ui.forceGbp);
432
433	if (m_ui.audioBufferSize->currentText().toInt() > 8192) {
434		m_ui.audioBufferSize->setCurrentText("8192");
435	}
436	saveSetting("audioBuffers", m_ui.audioBufferSize);
437
438	if (m_ui.fastForwardUnbounded->isChecked()) {
439		saveSetting("fastForwardRatio", "-1");
440	} else {
441		saveSetting("fastForwardRatio", m_ui.fastForwardRatio);
442	}
443
444	double nativeFps = double(GBA_ARM7TDMI_FREQUENCY) / double(VIDEO_TOTAL_LENGTH);
445	if (fabs(nativeFps - m_ui.fpsTarget->value()) < 0.0001) {
446		m_controller->setOption("fpsTarget", QVariant(nativeFps));
447	} else {
448		saveSetting("fpsTarget", m_ui.fpsTarget);
449	}
450
451	if (m_ui.fastForwardHeldUnbounded->isChecked()) {
452		saveSetting("fastForwardHeldRatio", "-1");
453	} else {
454		saveSetting("fastForwardHeldRatio", m_ui.fastForwardHeldRatio);
455	}
456
457	switch (m_ui.idleOptimization->currentIndex() + IDLE_LOOP_IGNORE) {
458	case IDLE_LOOP_IGNORE:
459		saveSetting("idleOptimization", "ignore");
460		break;
461	case IDLE_LOOP_REMOVE:
462		saveSetting("idleOptimization", "remove");
463		break;
464	case IDLE_LOOP_DETECT:
465		saveSetting("idleOptimization", "detect");
466		break;
467	}
468
469	int loadState = SAVESTATE_RTC;
470	loadState |= m_ui.loadStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
471	loadState |= m_ui.loadStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
472	loadState |= m_ui.loadStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
473	saveSetting("loadStateExtdata", loadState);
474
475	int saveState = SAVESTATE_RTC | SAVESTATE_METADATA;
476	saveState |= m_ui.saveStateScreenshot->isChecked() ? SAVESTATE_SCREENSHOT : 0;
477	saveState |= m_ui.saveStateSave->isChecked() ? SAVESTATE_SAVEDATA : 0;
478	saveState |= m_ui.saveStateCheats->isChecked() ? SAVESTATE_CHEATS : 0;
479	saveSetting("saveStateExtdata", saveState);
480
481	QVariant audioDriver = m_ui.audioDriver->itemData(m_ui.audioDriver->currentIndex());
482	if (audioDriver != m_controller->getQtOption("audioDriver")) {
483		m_controller->setQtOption("audioDriver", audioDriver);
484		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(audioDriver.toInt()));
485		emit audioDriverChanged();
486	}
487
488	QVariant displayDriver = m_ui.displayDriver->itemData(m_ui.displayDriver->currentIndex());
489	if (displayDriver != m_controller->getQtOption("displayDriver")) {
490		m_controller->setQtOption("displayDriver", displayDriver);
491		Display::setDriver(static_cast<Display::Driver>(displayDriver.toInt()));
492		setShaderSelector(nullptr);
493		emit displayDriverChanged();
494	}
495
496	QVariant cameraDriver = m_ui.cameraDriver->itemData(m_ui.cameraDriver->currentIndex());
497	QVariant oldCameraDriver = m_controller->getQtOption("cameraDriver");
498	if (cameraDriver != oldCameraDriver) {
499		m_controller->setQtOption("cameraDriver", cameraDriver);
500		if (cameraDriver.toInt() != static_cast<int>(InputController::CameraDriver::NONE) || !oldCameraDriver.isNull()) {
501			emit cameraDriverChanged();
502		}
503	}
504
505	QVariant camera = m_ui.camera->itemData(m_ui.camera->currentIndex());
506	if (camera != m_controller->getQtOption("camera")) {
507		m_controller->setQtOption("camera", camera);
508		emit cameraChanged(camera.toByteArray());
509	}
510
511	QLocale language = m_ui.languages->itemData(m_ui.languages->currentIndex()).toLocale();
512	if (language != m_controller->getQtOption("language").toLocale() && !(language.bcp47Name() == QLocale::system().bcp47Name() && m_controller->getQtOption("language").isNull())) {
513		m_controller->setQtOption("language", language.bcp47Name());
514		emit languageChanged();
515	}
516
517	int hwaccelVideo = m_controller->getOption("hwaccelVideo").toInt();
518	saveSetting("hwaccelVideo", m_ui.hwaccelVideo->currentIndex());
519	if (hwaccelVideo != m_ui.hwaccelVideo->currentIndex()) {
520		emit videoRendererChanged();
521	}
522
523	m_logModel.save(m_controller);
524	m_logModel.logger()->setLogFile(m_ui.logFile->text());
525	m_logModel.logger()->logToFile(m_ui.logToFile->isChecked());
526	m_logModel.logger()->logToStdout(m_ui.logToStdout->isChecked());
527
528#ifdef M_CORE_GB
529	QVariant modelGB = m_ui.gbModel->currentData();
530	if (modelGB.isValid()) {
531		m_controller->setOption("gb.model", GBModelToName(static_cast<GBModel>(modelGB.toInt())));
532	}
533
534	QVariant modelSGB = m_ui.sgbModel->currentData();
535	if (modelSGB.isValid()) {
536		m_controller->setOption("sgb.model", GBModelToName(static_cast<GBModel>(modelSGB.toInt())));
537	}
538
539	QVariant modelCGB = m_ui.cgbModel->currentData();
540	if (modelCGB.isValid()) {
541		m_controller->setOption("cgb.model", GBModelToName(static_cast<GBModel>(modelCGB.toInt())));
542	}
543
544	QVariant modelCGBHybrid = m_ui.cgbHybridModel->currentData();
545	if (modelCGBHybrid.isValid()) {
546		m_controller->setOption("cgb.hybridModel", GBModelToName(static_cast<GBModel>(modelCGBHybrid.toInt())));
547	}
548
549	QVariant modelCGBSGB = m_ui.cgbSgbModel->currentData();
550	if (modelCGBSGB.isValid()) {
551		m_controller->setOption("cgb.sgbModel", GBModelToName(static_cast<GBModel>(modelCGBSGB.toInt())));
552	}
553
554	for (int colorId = 0; colorId < 12; ++colorId) {
555		if (!(m_gbColors[colorId] & 0xFF000000)) {
556			continue;
557		}
558		QString color = QString("gb.pal[%0]").arg(colorId);
559		m_controller->setOption(color.toUtf8().constData(), m_gbColors[colorId] & ~0xFF000000);
560
561	}
562#endif
563
564	m_controller->write();
565
566	emit pathsChanged();
567	emit biosLoaded(mPLATFORM_GBA, m_ui.gbaBios->text());
568}
569
570void SettingsView::reloadConfig() {	
571	loadSetting("bios", m_ui.gbaBios);
572	loadSetting("gba.bios", m_ui.gbaBios);
573	loadSetting("gb.bios", m_ui.gbBios);
574	loadSetting("gbc.bios", m_ui.gbcBios);
575	loadSetting("sgb.bios", m_ui.sgbBios);
576	loadSetting("sgb.borders", m_ui.sgbBorders, true);
577	loadSetting("useCgbColors", m_ui.useCgbColors, true);
578	loadSetting("useBios", m_ui.useBios);
579	loadSetting("skipBios", m_ui.skipBios);
580	loadSetting("audioBuffers", m_ui.audioBufferSize);
581	loadSetting("sampleRate", m_ui.sampleRate);
582	loadSetting("videoSync", m_ui.videoSync);
583	loadSetting("audioSync", m_ui.audioSync);
584	loadSetting("frameskip", m_ui.frameskip);
585	loadSetting("fpsTarget", m_ui.fpsTarget);
586	loadSetting("autofireThreshold", m_ui.autofireThreshold);
587	loadSetting("lockAspectRatio", m_ui.lockAspectRatio);
588	loadSetting("lockIntegerScaling", m_ui.lockIntegerScaling);
589	loadSetting("interframeBlending", m_ui.interframeBlending);
590	loadSetting("showOSD", m_ui.showOSD, true);
591	loadSetting("volume", m_ui.volume, 0x100);
592	loadSetting("mute", m_ui.mute, false);
593	loadSetting("fastForwardVolume", m_ui.volumeFf, m_ui.volume->value());
594	loadSetting("fastForwardMute", m_ui.muteFf, m_ui.mute->isChecked());
595	loadSetting("rewindEnable", m_ui.rewind);
596	loadSetting("rewindBufferCapacity", m_ui.rewindCapacity);
597	loadSetting("resampleVideo", m_ui.resampleVideo);
598	loadSetting("allowOpposingDirections", m_ui.allowOpposingDirections);
599	loadSetting("suspendScreensaver", m_ui.suspendScreensaver);
600	loadSetting("pauseOnFocusLost", m_ui.pauseOnFocusLost);
601	loadSetting("pauseOnMinimize", m_ui.pauseOnMinimize);
602	loadSetting("savegamePath", m_ui.savegamePath);
603	loadSetting("savestatePath", m_ui.savestatePath);
604	loadSetting("screenshotPath", m_ui.screenshotPath);
605	loadSetting("patchPath", m_ui.patchPath);
606	loadSetting("cheatsPath", m_ui.cheatsPath);
607	loadSetting("showLibrary", m_ui.showLibrary);
608	loadSetting("preload", m_ui.preload);
609	loadSetting("showFps", m_ui.showFps, true);
610	loadSetting("cheatAutoload", m_ui.cheatAutoload, true);
611	loadSetting("cheatAutosave", m_ui.cheatAutosave, true);
612	loadSetting("showFilename", m_ui.showFilename, false);
613	loadSetting("autoload", m_ui.autoload, true);
614	loadSetting("autosave", m_ui.autosave, false);
615	loadSetting("logToFile", m_ui.logToFile);
616	loadSetting("logToStdout", m_ui.logToStdout);
617	loadSetting("logFile", m_ui.logFile);
618	loadSetting("useDiscordPresence", m_ui.useDiscordPresence);
619	loadSetting("gba.audioHle", m_ui.audioHle);
620	loadSetting("dynamicTitle", m_ui.dynamicTitle, true);
621	loadSetting("gba.forceGbp", m_ui.forceGbp);
622
623	m_ui.libraryStyle->setCurrentIndex(loadSetting("libraryStyle").toInt());
624
625	double fastForwardRatio = loadSetting("fastForwardRatio").toDouble();
626	if (fastForwardRatio <= 0) {
627		m_ui.fastForwardUnbounded->setChecked(true);
628		m_ui.fastForwardRatio->setEnabled(false);
629	} else {
630		m_ui.fastForwardUnbounded->setChecked(false);
631		m_ui.fastForwardRatio->setEnabled(true);
632		m_ui.fastForwardRatio->setValue(fastForwardRatio);
633	}
634
635	double fastForwardHeldRatio = loadSetting("fastForwardHeldRatio").toDouble();
636	if (fastForwardHeldRatio <= 0) {
637		m_ui.fastForwardHeldUnbounded->setChecked(true);
638		m_ui.fastForwardHeldRatio->setEnabled(false);
639	} else {
640		m_ui.fastForwardHeldUnbounded->setChecked(false);
641		m_ui.fastForwardHeldRatio->setEnabled(true);
642		m_ui.fastForwardHeldRatio->setValue(fastForwardHeldRatio);
643	}
644
645	QString idleOptimization = loadSetting("idleOptimization");
646	if (idleOptimization == "ignore") {
647		m_ui.idleOptimization->setCurrentIndex(0);
648	} else if (idleOptimization == "remove") {
649		m_ui.idleOptimization->setCurrentIndex(1);
650	} else if (idleOptimization == "detect") {
651		m_ui.idleOptimization->setCurrentIndex(2);
652	}
653
654	bool ok;
655	int loadState = loadSetting("loadStateExtdata").toInt(&ok);
656	if (!ok) {
657		loadState = SAVESTATE_SCREENSHOT | SAVESTATE_RTC;
658	}
659	m_ui.loadStateScreenshot->setChecked(loadState & SAVESTATE_SCREENSHOT);
660	m_ui.loadStateSave->setChecked(loadState & SAVESTATE_SAVEDATA);
661	m_ui.loadStateCheats->setChecked(loadState & SAVESTATE_CHEATS);
662
663	int saveState = loadSetting("saveStateExtdata").toInt(&ok);
664	if (!ok) {
665		saveState = SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA | SAVESTATE_CHEATS | SAVESTATE_RTC | SAVESTATE_METADATA;
666	}
667	m_ui.saveStateScreenshot->setChecked(saveState & SAVESTATE_SCREENSHOT);
668	m_ui.saveStateSave->setChecked(saveState & SAVESTATE_SAVEDATA);
669	m_ui.saveStateCheats->setChecked(saveState & SAVESTATE_CHEATS);
670
671	m_logModel.reset();
672
673#ifdef M_CORE_GB
674	QString modelGB = m_controller->getOption("gb.model");
675	if (!modelGB.isNull()) {
676		GBModel model = GBNameToModel(modelGB.toUtf8().constData());
677		int index = m_ui.gbModel->findData(model);
678		m_ui.gbModel->setCurrentIndex(index >= 0 ? index : 0);
679	}
680
681	QString modelSGB = m_controller->getOption("sgb.model");
682	if (!modelSGB.isNull()) {
683		GBModel model = GBNameToModel(modelSGB.toUtf8().constData());
684		int index = m_ui.sgbModel->findData(model);
685		m_ui.sgbModel->setCurrentIndex(index >= 0 ? index : 0);
686	}
687
688	QString modelCGB = m_controller->getOption("cgb.model");
689	if (!modelCGB.isNull()) {
690		GBModel model = GBNameToModel(modelCGB.toUtf8().constData());
691		int index = m_ui.cgbModel->findData(model);
692		m_ui.cgbModel->setCurrentIndex(index >= 0 ? index : 0);
693	}
694
695	QString modelCGBHybrid = m_controller->getOption("cgb.hybridModel");
696	if (!modelCGBHybrid.isNull()) {
697		GBModel model = GBNameToModel(modelCGBHybrid.toUtf8().constData());
698		int index = m_ui.cgbHybridModel->findData(model);
699		m_ui.cgbHybridModel->setCurrentIndex(index >= 0 ? index : 0);
700	}
701
702	QString modelCGBSGB = m_controller->getOption("cgb.sgbModel");
703	if (!modelCGBSGB.isNull()) {
704		GBModel model = GBNameToModel(modelCGBSGB.toUtf8().constData());
705		int index = m_ui.cgbSgbModel->findData(model);
706		m_ui.cgbSgbModel->setCurrentIndex(index >= 0 ? index : 0);
707	}
708#endif
709
710	int hwaccelVideo = m_controller->getOption("hwaccelVideo", 0).toInt();
711	m_ui.hwaccelVideo->setCurrentIndex(hwaccelVideo);
712
713	connect(m_ui.videoScale, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this](int value) {
714		m_ui.videoScaleSize->setText(tr("(%1×%2)").arg(GBA_VIDEO_HORIZONTAL_PIXELS * value).arg(GBA_VIDEO_VERTICAL_PIXELS * value));
715	});
716	loadSetting("videoScale", m_ui.videoScale, 1);
717}
718
719void SettingsView::addPage(const QString& name, QWidget* view, Page index) {
720	m_pageIndex[index] = m_ui.tabs->count();
721	m_ui.tabs->addItem(name);
722	m_ui.stackedWidget->addWidget(view);
723}
724
725void SettingsView::saveSetting(const char* key, const QAbstractButton* field) {
726	m_controller->setOption(key, field->isChecked());
727	m_controller->updateOption(key);
728}
729
730void SettingsView::saveSetting(const char* key, const QComboBox* field) {
731	saveSetting(key, field->lineEdit());
732}
733
734void SettingsView::saveSetting(const char* key, const QDoubleSpinBox* field) {
735	saveSetting(key, field->value());
736}
737
738void SettingsView::saveSetting(const char* key, const QLineEdit* field) {
739	saveSetting(key, field->text());
740}
741
742void SettingsView::saveSetting(const char* key, const QSlider* field) {
743	saveSetting(key, field->value());
744}
745
746void SettingsView::saveSetting(const char* key, const QSpinBox* field) {
747	saveSetting(key, field->value());
748}
749
750void SettingsView::saveSetting(const char* key, const QVariant& field) {
751	m_controller->setOption(key, field);
752	m_controller->updateOption(key);
753}
754
755void SettingsView::loadSetting(const char* key, QAbstractButton* field, bool defaultVal) {
756	QString option = loadSetting(key);
757	field->setChecked(option.isNull() ? defaultVal : option != "0");
758}
759
760void SettingsView::loadSetting(const char* key, QComboBox* field) {
761	loadSetting(key, field->lineEdit());
762}
763
764void SettingsView::loadSetting(const char* key, QDoubleSpinBox* field) {
765	QString option = loadSetting(key);
766	field->setValue(option.toDouble());
767}
768
769void SettingsView::loadSetting(const char* key, QLineEdit* field) {
770	QString option = loadSetting(key);
771	field->setText(option);
772}
773
774void SettingsView::loadSetting(const char* key, QSlider* field, int defaultVal) {
775	QString option = loadSetting(key);
776	field->setValue(option.isNull() ? defaultVal : option.toInt());
777}
778
779void SettingsView::loadSetting(const char* key, QSpinBox* field, int defaultVal) {
780	QString option = loadSetting(key);
781	field->setValue(option.isNull() ? defaultVal : option.toInt());
782}
783
784QString SettingsView::loadSetting(const char* key) {
785	return m_controller->getOption(key);
786}