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