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
14extern "C" {
15#include "gba/context/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, QObject* parent) {
27 m_slots[parent] = slot;
28 QObject::connect(parent, &QAction::destroyed, [this, slot, parent]() {
29 m_slots.remove(parent);
30 });
31}
32
33QAction* ConfigOption::addValue(const QString& text, const QVariant& value, QMenu* parent) {
34 QAction* action = new QAction(text, parent);
35 action->setCheckable(true);
36 QObject::connect(action, &QAction::triggered, [this, value]() {
37 emit valueChanged(value);
38 });
39 QObject::connect(parent, &QAction::destroyed, [this, action, value]() {
40 m_actions.removeAll(qMakePair(action, value));
41 });
42 parent->addAction(action);
43 m_actions.append(qMakePair(action, value));
44 return action;
45}
46
47QAction* ConfigOption::addValue(const QString& text, const char* value, QMenu* parent) {
48 return addValue(text, QString(value), parent);
49}
50
51QAction* ConfigOption::addBoolean(const QString& text, QMenu* parent) {
52 QAction* action = new QAction(text, parent);
53 action->setCheckable(true);
54 QObject::connect(action, &QAction::triggered, [this, action]() {
55 emit valueChanged(action->isChecked());
56 });
57 QObject::connect(parent, &QAction::destroyed, [this, action]() {
58 m_actions.removeAll(qMakePair(action, 1));
59 });
60 parent->addAction(action);
61 m_actions.append(qMakePair(action, 1));
62 return action;
63}
64
65void ConfigOption::setValue(bool value) {
66 setValue(QVariant(value));
67}
68
69void ConfigOption::setValue(int value) {
70 setValue(QVariant(value));
71}
72
73void ConfigOption::setValue(unsigned value) {
74 setValue(QVariant(value));
75}
76
77void ConfigOption::setValue(const char* value) {
78 setValue(QVariant(QString(value)));
79}
80
81void ConfigOption::setValue(const QVariant& value) {
82 QPair<QAction*, QVariant> action;
83 foreach (action, m_actions) {
84 bool signalsEnabled = action.first->blockSignals(true);
85 action.first->setChecked(value == action.second);
86 action.first->blockSignals(signalsEnabled);
87 }
88 std::function<void(const QVariant&)> slot;
89 foreach(slot, m_slots.values()) {
90 slot(value);
91 }
92}
93
94ConfigController::ConfigController(QObject* parent)
95 : QObject(parent)
96 , m_opts()
97{
98 char path[PATH_MAX];
99 mCoreConfigDirectory(path, sizeof(path));
100 QString fileName(path);
101 fileName.append(QDir::separator());
102 fileName.append("qt.ini");
103 m_settings = new QSettings(fileName, QSettings::IniFormat, this);
104
105 mCoreConfigInit(&m_config, PORT);
106
107 m_opts.audioSync = GameController::AUDIO_SYNC;
108 m_opts.videoSync = GameController::VIDEO_SYNC;
109 m_opts.fpsTarget = 60;
110 m_opts.audioBuffers = 1536;
111 m_opts.sampleRate = 44100;
112 m_opts.volume = 0x100;
113 m_opts.logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS;
114 m_opts.rewindEnable = false;
115 m_opts.rewindBufferInterval = 0;
116 m_opts.rewindBufferCapacity = 0;
117 m_opts.useBios = true;
118 m_opts.suspendScreensaver = true;
119 mCoreConfigLoad(&m_config);
120 mCoreConfigLoadDefaults(&m_config, &m_opts);
121 mCoreConfigMap(&m_config, &m_opts);
122}
123
124ConfigController::~ConfigController() {
125 mCoreConfigDeinit(&m_config);
126 mCoreConfigFreeOpts(&m_opts);
127}
128
129bool ConfigController::parseArguments(mArguments* args, int argc, char* argv[], mSubParser* subparser) {
130 if (::parseArguments(args, argc, argv, subparser)) {
131 mCoreConfigFreeOpts(&m_opts);
132 applyArguments(args, subparser, &m_config);
133 mCoreConfigMap(&m_config, &m_opts);
134 return true;
135 }
136 return false;
137}
138
139ConfigOption* ConfigController::addOption(const char* key) {
140 QString optionName(key);
141
142 if (m_optionSet.contains(optionName)) {
143 return m_optionSet[optionName];
144 }
145 ConfigOption* newOption = new ConfigOption(this);
146 m_optionSet[optionName] = newOption;
147 connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
148 setOption(key, value);
149 });
150 return newOption;
151}
152
153void ConfigController::updateOption(const char* key) {
154 if (!key) {
155 return;
156 }
157
158 QString optionName(key);
159
160 if (!m_optionSet.contains(optionName)) {
161 return;
162 }
163 m_optionSet[optionName]->setValue(mCoreConfigGetValue(&m_config, key));
164}
165
166QString ConfigController::getOption(const char* key) const {
167 return QString(mCoreConfigGetValue(&m_config, key));
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 GBACartridgeOverride& override) {
182 GBAOverrideSave(overrides(), &override);
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 char path[PATH_MAX];
276 mCoreConfigDirectory(path, sizeof(path));
277 QString fileName(path);
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}