all repos — mgba @ ad203c17bd7f204a9e5df8499c76c909c75a93e0

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 "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	QPair<QAction*, QVariant> action;
 80	foreach (action, m_actions) {
 81		bool signalsEnabled = action.first->blockSignals(true);
 82		action.first->setChecked(value == action.second);
 83		action.first->blockSignals(signalsEnabled);
 84	}
 85	std::function<void(const QVariant&)> slot;
 86	foreach(slot, m_slots.values()) {
 87		slot(value);
 88	}
 89}
 90
 91QString ConfigController::s_configDir;
 92
 93ConfigController::ConfigController(QObject* parent)
 94	: QObject(parent)
 95	, m_opts{}
 96{
 97	QString fileName = configDir();
 98	fileName.append(QDir::separator());
 99	fileName.append("qt.ini");
100	m_settings = new QSettings(fileName, QSettings::IniFormat, this);
101
102	mCoreConfigInit(&m_config, PORT);
103
104	m_opts.audioSync = GameController::AUDIO_SYNC;
105	m_opts.videoSync = GameController::VIDEO_SYNC;
106	m_opts.fpsTarget = 60;
107	m_opts.audioBuffers = 1536;
108	m_opts.sampleRate = 44100;
109	m_opts.volume = 0x100;
110	m_opts.logLevel = mLOG_WARN | mLOG_ERROR | mLOG_FATAL;
111	m_opts.rewindEnable = false;
112	m_opts.rewindBufferCapacity = 300;
113	m_opts.rewindSave = true;
114	m_opts.useBios = true;
115	m_opts.suspendScreensaver = true;
116	m_opts.lockAspectRatio = true;
117	mCoreConfigLoad(&m_config);
118	mCoreConfigLoadDefaults(&m_config, &m_opts);
119	mCoreConfigMap(&m_config, &m_opts);
120}
121
122ConfigController::~ConfigController() {
123	mCoreConfigDeinit(&m_config);
124	mCoreConfigFreeOpts(&m_opts);
125}
126
127bool ConfigController::parseArguments(mArguments* args, int argc, char* argv[], mSubParser* subparser) {
128	if (::parseArguments(args, argc, argv, subparser)) {
129		mCoreConfigFreeOpts(&m_opts);
130		applyArguments(args, subparser, &m_config);
131		mCoreConfigMap(&m_config, &m_opts);
132		return true;
133	}
134	return false;
135}
136
137ConfigOption* ConfigController::addOption(const char* key) {
138	QString optionName(key);
139
140	if (m_optionSet.contains(optionName)) {
141		return m_optionSet[optionName];
142	}
143	ConfigOption* newOption = new ConfigOption(this);
144	m_optionSet[optionName] = newOption;
145	connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
146		setOption(key, value);
147	});
148	return newOption;
149}
150
151void ConfigController::updateOption(const char* key) {
152	if (!key) {
153		return;
154	}
155
156	QString optionName(key);
157
158	if (!m_optionSet.contains(optionName)) {
159		return;
160	}
161	m_optionSet[optionName]->setValue(mCoreConfigGetValue(&m_config, key));
162}
163
164QString ConfigController::getOption(const char* key) const {
165	return QString(mCoreConfigGetValue(&m_config, key));
166}
167
168QString ConfigController::getOption(const QString& key) const {
169	return getOption(key.toUtf8().constData());
170}
171
172QVariant ConfigController::getQtOption(const QString& key, const QString& group) const {
173	if (!group.isNull()) {
174		m_settings->beginGroup(group);
175	}
176	QVariant value = m_settings->value(key);
177	if (!group.isNull()) {
178		m_settings->endGroup();
179	}
180	return value;
181}
182
183void ConfigController::saveOverride(const Override& override) {
184	override.save(overrides());
185	write();
186}
187
188void ConfigController::setOption(const char* key, bool value) {
189	mCoreConfigSetIntValue(&m_config, key, value);
190	QString optionName(key);
191	if (m_optionSet.contains(optionName)) {
192		m_optionSet[optionName]->setValue(value);
193	}
194}
195
196void ConfigController::setOption(const char* key, int value) {
197	mCoreConfigSetIntValue(&m_config, key, value);
198	QString optionName(key);
199	if (m_optionSet.contains(optionName)) {
200		m_optionSet[optionName]->setValue(value);
201	}
202}
203
204void ConfigController::setOption(const char* key, unsigned value) {
205	mCoreConfigSetUIntValue(&m_config, key, value);
206	QString optionName(key);
207	if (m_optionSet.contains(optionName)) {
208		m_optionSet[optionName]->setValue(value);
209	}
210}
211
212void ConfigController::setOption(const char* key, const char* value) {
213	mCoreConfigSetValue(&m_config, key, value);
214	QString optionName(key);
215	if (m_optionSet.contains(optionName)) {
216		m_optionSet[optionName]->setValue(value);
217	}
218}
219
220void ConfigController::setOption(const char* key, const QVariant& value) {
221	if (value.type() == QVariant::Bool) {
222		setOption(key, value.toBool());
223		return;
224	}
225	QString stringValue(value.toString());
226	setOption(key, stringValue.toUtf8().constData());
227}
228
229void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) {
230	if (!group.isNull()) {
231		m_settings->beginGroup(group);
232	}
233	m_settings->setValue(key, value);
234	if (!group.isNull()) {
235		m_settings->endGroup();
236	}
237}
238
239QList<QString> ConfigController::getMRU() const {
240	QList<QString> mru;
241	m_settings->beginGroup("mru");
242	for (int i = 0; i < MRU_LIST_SIZE; ++i) {
243		QString item = m_settings->value(QString::number(i)).toString();
244		if (item.isNull()) {
245			continue;
246		}
247		mru.append(item);
248	}
249	m_settings->endGroup();
250	return mru;
251}
252
253void ConfigController::setMRU(const QList<QString>& mru) {
254	int i = 0;
255	m_settings->beginGroup("mru");
256	for (const QString& item : mru) {
257		m_settings->setValue(QString::number(i), item);
258		++i;
259		if (i >= MRU_LIST_SIZE) {
260			break;
261		}
262	}
263	m_settings->endGroup();
264}
265
266void ConfigController::write() {
267	mCoreConfigSave(&m_config);
268	m_settings->sync();
269
270	mCoreConfigFreeOpts(&m_opts);
271	mCoreConfigMap(&m_config, &m_opts);
272}
273
274void ConfigController::makePortable() {
275	mCoreConfigMakePortable(&m_config);
276
277	QString fileName(configDir());
278	fileName.append(QDir::separator());
279	fileName.append("qt.ini");
280	QSettings* settings2 = new QSettings(fileName, QSettings::IniFormat, this);
281	for (const auto& key : m_settings->allKeys()) {
282		settings2->setValue(key, m_settings->value(key));
283	}
284	delete m_settings;
285	m_settings = settings2;
286}
287
288const QString& ConfigController::configDir() {
289	if (s_configDir.isNull()) {
290		char path[PATH_MAX];
291		mCoreConfigDirectory(path, sizeof(path));
292		s_configDir = QString::fromUtf8(path);
293	}
294	return s_configDir;
295}