all repos — mgba @ medusa

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