all repos — mgba @ 9dc6dc7e658be648d875d31098a577b58ad73c3a

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