all repos — mgba @ e9a2b2a57c4f478fb3600fe9d87e1b97804d73de

mGBA Game Boy Advance Emulator

src/platform/qt/ConfigController.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 "ConfigController.h"
  7
  8#include "GameController.h"
  9
 10#include <QAction>
 11#include <QDir>
 12#include <QMenu>
 13
 14extern "C" {
 15#include "gba-overrides.h"
 16#include "platform/commandline.h"
 17}
 18
 19using namespace QGBA;
 20
 21ConfigOption::ConfigOption(QObject* parent)
 22	: QObject(parent)
 23{
 24}
 25
 26void ConfigOption::connect(std::function<void(const QVariant&)> slot) {
 27	m_slot = slot;
 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	parent->addAction(action);
 37	m_actions.append(qMakePair(action, value));
 38	return action;
 39}
 40
 41QAction* ConfigOption::addValue(const QString& text, const char* value, QMenu* parent) {
 42	return addValue(text, QString(value), parent);
 43}
 44
 45QAction* ConfigOption::addBoolean(const QString& text, QMenu* parent) {
 46	QAction* action = new QAction(text, parent);
 47	action->setCheckable(true);
 48	QObject::connect(action, &QAction::triggered, [this, action]() {
 49		emit valueChanged(action->isChecked());
 50	});
 51	parent->addAction(action);
 52	m_actions.append(qMakePair(action, 1));
 53	return action;
 54}
 55
 56void ConfigOption::setValue(bool value) {
 57	setValue(QVariant(value));
 58}
 59
 60void ConfigOption::setValue(int value) {
 61	setValue(QVariant(value));
 62}
 63
 64void ConfigOption::setValue(unsigned value) {
 65	setValue(QVariant(value));
 66}
 67
 68void ConfigOption::setValue(const char* value) {
 69	setValue(QVariant(QString(value)));
 70}
 71
 72void ConfigOption::setValue(const QVariant& value) {
 73	QPair<QAction*, QVariant> action;
 74	foreach(action, m_actions) {
 75		bool signalsEnabled = action.first->blockSignals(true);
 76		action.first->setChecked(value == action.second);
 77		action.first->blockSignals(signalsEnabled);
 78	}
 79	m_slot(value);
 80}
 81
 82ConfigController::ConfigController(QObject* parent)
 83	: QObject(parent)
 84	, m_opts()
 85{
 86	char path[PATH_MAX];
 87	GBAConfigDirectory(path, sizeof(path));
 88	QString fileName(path);
 89	fileName.append(QDir::separator());
 90	fileName.append("qt.ini");
 91	m_settings = new QSettings(fileName, QSettings::IniFormat, this);
 92
 93	GBAConfigInit(&m_config, PORT);
 94
 95	m_opts.audioSync = GameController::AUDIO_SYNC;
 96	m_opts.videoSync = GameController::VIDEO_SYNC;
 97	m_opts.fpsTarget = 60;
 98	m_opts.audioBuffers = 2048;
 99	m_opts.logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
100	m_opts.rewindEnable = false;
101	m_opts.rewindBufferInterval = 0;
102	m_opts.rewindBufferCapacity = 0;
103	GBAConfigLoadDefaults(&m_config, &m_opts);
104	GBAConfigLoad(&m_config);
105	GBAConfigMap(&m_config, &m_opts);
106}
107
108ConfigController::~ConfigController() {
109	write();
110
111	GBAConfigDeinit(&m_config);
112	GBAConfigFreeOpts(&m_opts);
113}
114
115bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) {
116	return ::parseArguments(args, &m_config, argc, argv, 0);
117}
118
119ConfigOption* ConfigController::addOption(const char* key) {
120	QString optionName(key);
121
122	if (m_optionSet.contains(optionName)) {
123		return m_optionSet[optionName];
124	}
125	ConfigOption* newOption = new ConfigOption(this);
126	m_optionSet[optionName] = newOption;
127	connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
128		setOption(key, value);
129	});
130	return newOption;
131}
132
133void ConfigController::updateOption(const char* key) {
134	if (!key) {
135		return;
136	}
137
138	QString optionName(key);
139
140	if (!m_optionSet.contains(optionName)) {
141		return;
142	}
143	m_optionSet[optionName]->setValue(GBAConfigGetValue(&m_config, key));
144}
145
146QString ConfigController::getOption(const char* key) const {
147	return QString(GBAConfigGetValue(&m_config, key));
148}
149
150QVariant ConfigController::getQtOption(const QString& key, const QString& group) const {
151	if (!group.isNull()) {
152		m_settings->beginGroup(group);
153	}
154	QVariant value = m_settings->value(key);
155	if (!group.isNull()) {
156		m_settings->endGroup();
157	}
158	return value;
159}
160
161void ConfigController::saveOverride(const GBACartridgeOverride& override) {
162	GBAOverrideSave(overrides(), &override);
163	write();
164}
165
166void ConfigController::setOption(const char* key, bool value) {
167	GBAConfigSetIntValue(&m_config, key, value);
168	QString optionName(key);
169	if (m_optionSet.contains(optionName)) {
170		m_optionSet[optionName]->setValue(value);
171	}
172}
173
174void ConfigController::setOption(const char* key, int value) {
175	GBAConfigSetIntValue(&m_config, key, value);
176	QString optionName(key);
177	if (m_optionSet.contains(optionName)) {
178		m_optionSet[optionName]->setValue(value);
179	}
180}
181
182void ConfigController::setOption(const char* key, unsigned value) {
183	GBAConfigSetUIntValue(&m_config, key, value);
184	QString optionName(key);
185	if (m_optionSet.contains(optionName)) {
186		m_optionSet[optionName]->setValue(value);
187	}
188}
189
190void ConfigController::setOption(const char* key, const char* value) {
191	GBAConfigSetValue(&m_config, key, value);
192	QString optionName(key);
193	if (m_optionSet.contains(optionName)) {
194		m_optionSet[optionName]->setValue(value);
195	}
196}
197
198void ConfigController::setOption(const char* key, const QVariant& value) {
199	if (value.type() == QVariant::Bool) {
200		setOption(key, value.toBool());
201		return;
202	}
203	QString stringValue(value.toString());
204	setOption(key, stringValue.toLocal8Bit().constData());
205}
206
207void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) {
208	if (!group.isNull()) {
209		m_settings->beginGroup(group);
210	}
211	m_settings->setValue(key, value);
212	if (!group.isNull()) {
213		m_settings->endGroup();
214	}
215}
216
217QList<QString> ConfigController::getMRU() const {
218	QList<QString> mru;
219	m_settings->beginGroup("mru");
220	for (int i = 0; i < MRU_LIST_SIZE; ++i) {
221		QString item = m_settings->value(QString::number(i)).toString();
222		if (item.isNull()) {
223			continue;
224		}
225		mru.append(item);
226	}
227	m_settings->endGroup();
228	return mru;
229}
230
231void ConfigController::setMRU(const QList<QString>& mru) {
232	int i = 0;
233	m_settings->beginGroup("mru");
234	for (const QString& item : mru) {
235		m_settings->setValue(QString::number(i), item);
236		++i;
237		if (i >= MRU_LIST_SIZE) {
238			break;
239		}
240	}
241	m_settings->endGroup();
242}
243
244void ConfigController::write() {
245	GBAConfigSave(&m_config);
246	m_settings->sync();
247}