all repos — mgba @ 17fa98fe4e90de445af406e962d1b49ce8865265

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