all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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 "ConfigController.h"
  7
  8#include "GameController.h"
  9
 10#include <QAction>
 11#include <QDir>
 12#include <QMenu>
 13
 14#include <mgba/feature/commandline.h>
 15
 16using namespace QGBA;
 17
 18ConfigOption::ConfigOption(QObject* parent)
 19	: QObject(parent)
 20{
 21}
 22
 23void ConfigOption::connect(std::function<void(const QVariant&)> slot, QObject* parent) {
 24	m_slots[parent] = slot;
 25	QObject::connect(parent, &QAction::destroyed, [this, slot, parent]() {
 26		m_slots.remove(parent);
 27	});
 28}
 29
 30QAction* ConfigOption::addValue(const QString& text, const QVariant& value, QMenu* parent) {
 31	QAction* action = new QAction(text, parent);
 32	action->setCheckable(true);
 33	QObject::connect(action, &QAction::triggered, [this, value]() {
 34		emit valueChanged(value);
 35	});
 36	QObject::connect(parent, &QAction::destroyed, [this, action, value]() {
 37		m_actions.removeAll(qMakePair(action, value));
 38	});
 39	parent->addAction(action);
 40	m_actions.append(qMakePair(action, value));
 41	return action;
 42}
 43
 44QAction* ConfigOption::addValue(const QString& text, const char* value, QMenu* parent) {
 45	return addValue(text, QString(value), parent);
 46}
 47
 48QAction* ConfigOption::addBoolean(const QString& text, QMenu* parent) {
 49	QAction* action = new QAction(text, parent);
 50	action->setCheckable(true);
 51	QObject::connect(action, &QAction::triggered, [this, action]() {
 52		emit valueChanged(action->isChecked());
 53	});
 54	QObject::connect(parent, &QAction::destroyed, [this, action]() {
 55		m_actions.removeAll(qMakePair(action, 1));
 56	});
 57	parent->addAction(action);
 58	m_actions.append(qMakePair(action, 1));
 59	return action;
 60}
 61
 62void ConfigOption::setValue(bool value) {
 63	setValue(QVariant(value));
 64}
 65
 66void ConfigOption::setValue(int value) {
 67	setValue(QVariant(value));
 68}
 69
 70void ConfigOption::setValue(unsigned value) {
 71	setValue(QVariant(value));
 72}
 73
 74void ConfigOption::setValue(const char* value) {
 75	setValue(QVariant(QString(value)));
 76}
 77
 78void ConfigOption::setValue(const QVariant& value) {
 79	for (QPair<QAction*, QVariant>& action : m_actions) {
 80		bool signalsEnabled = action.first->blockSignals(true);
 81		action.first->setChecked(value == action.second);
 82		action.first->blockSignals(signalsEnabled);
 83	}
 84	for (std::function<void(const QVariant&)>& slot : m_slots.values()) {
 85		slot(value);
 86	}
 87}
 88
 89QString ConfigController::s_configDir;
 90
 91ConfigController::ConfigController(QObject* parent)
 92	: QObject(parent)
 93	, m_opts{}
 94{
 95	QString fileName = configDir();
 96	fileName.append(QDir::separator());
 97	fileName.append("qt.ini");
 98	m_settings = new QSettings(fileName, QSettings::IniFormat, this);
 99
100	mCoreConfigInit(&m_config, PORT);
101
102	m_opts.audioSync = GameController::AUDIO_SYNC;
103	m_opts.videoSync = GameController::VIDEO_SYNC;
104	m_opts.fpsTarget = 60;
105	m_opts.audioBuffers = 1536;
106	m_opts.sampleRate = 44100;
107	m_opts.volume = 0x100;
108	m_opts.logLevel = mLOG_WARN | mLOG_ERROR | mLOG_FATAL;
109	m_opts.rewindEnable = false;
110	m_opts.rewindBufferCapacity = 300;
111	m_opts.rewindSave = true;
112	m_opts.useBios = true;
113	m_opts.suspendScreensaver = true;
114	m_opts.lockAspectRatio = true;
115	mCoreConfigLoad(&m_config);
116	mCoreConfigLoadDefaults(&m_config, &m_opts);
117	mCoreConfigMap(&m_config, &m_opts);
118}
119
120ConfigController::~ConfigController() {
121	mCoreConfigDeinit(&m_config);
122	mCoreConfigFreeOpts(&m_opts);
123}
124
125bool ConfigController::parseArguments(mArguments* args, int argc, char* argv[], mSubParser* subparser) {
126	if (::parseArguments(args, argc, argv, subparser)) {
127		mCoreConfigFreeOpts(&m_opts);
128		applyArguments(args, subparser, &m_config);
129		mCoreConfigMap(&m_config, &m_opts);
130		return true;
131	}
132	return false;
133}
134
135ConfigOption* ConfigController::addOption(const char* key) {
136	QString optionName(key);
137
138	if (m_optionSet.contains(optionName)) {
139		return m_optionSet[optionName];
140	}
141	ConfigOption* newOption = new ConfigOption(this);
142	m_optionSet[optionName] = newOption;
143	connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
144		setOption(key, value);
145	});
146	return newOption;
147}
148
149void ConfigController::updateOption(const char* key) {
150	if (!key) {
151		return;
152	}
153
154	QString optionName(key);
155
156	if (!m_optionSet.contains(optionName)) {
157		return;
158	}
159	m_optionSet[optionName]->setValue(mCoreConfigGetValue(&m_config, key));
160}
161
162QString ConfigController::getOption(const char* key) const {
163	return QString(mCoreConfigGetValue(&m_config, key));
164}
165
166QString ConfigController::getOption(const QString& key) const {
167	return getOption(key.toUtf8().constData());
168}
169
170QVariant ConfigController::getQtOption(const QString& key, const QString& group) const {
171	if (!group.isNull()) {
172		m_settings->beginGroup(group);
173	}
174	QVariant value = m_settings->value(key);
175	if (!group.isNull()) {
176		m_settings->endGroup();
177	}
178	return value;
179}
180
181void ConfigController::saveOverride(const Override& override) {
182	override.save(overrides());
183	write();
184}
185
186void ConfigController::setOption(const char* key, bool value) {
187	mCoreConfigSetIntValue(&m_config, key, value);
188	QString optionName(key);
189	if (m_optionSet.contains(optionName)) {
190		m_optionSet[optionName]->setValue(value);
191	}
192}
193
194void ConfigController::setOption(const char* key, int value) {
195	mCoreConfigSetIntValue(&m_config, key, value);
196	QString optionName(key);
197	if (m_optionSet.contains(optionName)) {
198		m_optionSet[optionName]->setValue(value);
199	}
200}
201
202void ConfigController::setOption(const char* key, unsigned value) {
203	mCoreConfigSetUIntValue(&m_config, key, value);
204	QString optionName(key);
205	if (m_optionSet.contains(optionName)) {
206		m_optionSet[optionName]->setValue(value);
207	}
208}
209
210void ConfigController::setOption(const char* key, const char* value) {
211	mCoreConfigSetValue(&m_config, key, value);
212	QString optionName(key);
213	if (m_optionSet.contains(optionName)) {
214		m_optionSet[optionName]->setValue(value);
215	}
216}
217
218void ConfigController::setOption(const char* key, const QVariant& value) {
219	if (value.type() == QVariant::Bool) {
220		setOption(key, value.toBool());
221		return;
222	}
223	QString stringValue(value.toString());
224	setOption(key, stringValue.toUtf8().constData());
225}
226
227void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) {
228	if (!group.isNull()) {
229		m_settings->beginGroup(group);
230	}
231	m_settings->setValue(key, value);
232	if (!group.isNull()) {
233		m_settings->endGroup();
234	}
235}
236
237QList<QString> ConfigController::getMRU() const {
238	QList<QString> mru;
239	m_settings->beginGroup("mru");
240	for (int i = 0; i < MRU_LIST_SIZE; ++i) {
241		QString item = m_settings->value(QString::number(i)).toString();
242		if (item.isNull()) {
243			continue;
244		}
245		mru.append(item);
246	}
247	m_settings->endGroup();
248	return mru;
249}
250
251void ConfigController::setMRU(const QList<QString>& mru) {
252	int i = 0;
253	m_settings->beginGroup("mru");
254	for (const QString& item : mru) {
255		m_settings->setValue(QString::number(i), item);
256		++i;
257		if (i >= MRU_LIST_SIZE) {
258			break;
259		}
260	}
261	m_settings->endGroup();
262}
263
264void ConfigController::write() {
265	mCoreConfigSave(&m_config);
266	m_settings->sync();
267
268	mCoreConfigFreeOpts(&m_opts);
269	mCoreConfigMap(&m_config, &m_opts);
270}
271
272void ConfigController::makePortable() {
273	mCoreConfigMakePortable(&m_config);
274
275	QString fileName(configDir());
276	fileName.append(QDir::separator());
277	fileName.append("qt.ini");
278	QSettings* settings2 = new QSettings(fileName, QSettings::IniFormat, this);
279	for (const auto& key : m_settings->allKeys()) {
280		settings2->setValue(key, m_settings->value(key));
281	}
282	delete m_settings;
283	m_settings = settings2;
284}
285
286const QString& ConfigController::configDir() {
287	if (s_configDir.isNull()) {
288		char path[PATH_MAX];
289		mCoreConfigDirectory(path, sizeof(path));
290		s_configDir = QString::fromUtf8(path);
291	}
292	return s_configDir;
293}