all repos — mgba @ 62d70757515b8b32f67c35235c769b9a773a6ef5

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