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/supervisor/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 GBAConfigDirectory(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 GBAConfigInit(&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 = 2048;
111 m_opts.volume = GBA_AUDIO_VOLUME_MAX;
112 m_opts.logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS;
113 m_opts.rewindEnable = false;
114 m_opts.rewindBufferInterval = 0;
115 m_opts.rewindBufferCapacity = 0;
116 m_opts.useBios = true;
117 m_opts.suspendScreensaver = true;
118 GBAConfigLoad(&m_config);
119 GBAConfigLoadDefaults(&m_config, &m_opts);
120 GBAConfigMap(&m_config, &m_opts);
121}
122
123ConfigController::~ConfigController() {
124 GBAConfigDeinit(&m_config);
125 GBAConfigFreeOpts(&m_opts);
126}
127
128bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) {
129 if (::parseArguments(args, &m_config, argc, argv, 0)) {
130 GBAConfigMap(&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(GBAConfigGetValue(&m_config, key));
161}
162
163QString ConfigController::getOption(const char* key) const {
164 return QString(GBAConfigGetValue(&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 GBACartridgeOverride& override) {
179 GBAOverrideSave(overrides(), &override);
180 write();
181}
182
183void ConfigController::setOption(const char* key, bool value) {
184 GBAConfigSetIntValue(&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 GBAConfigSetIntValue(&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 GBAConfigSetUIntValue(&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 GBAConfigSetValue(&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 GBAConfigSave(&m_config);
263 m_settings->sync();
264}
265
266void ConfigController::makePortable() {
267 GBAConfigMakePortable(&m_config);
268
269 char path[PATH_MAX];
270 GBAConfigDirectory(path, sizeof(path));
271 QString fileName(path);
272 fileName.append(QDir::separator());
273 fileName.append("qt.ini");
274 QSettings* settings2 = new QSettings(fileName, QSettings::IniFormat, this);
275 for (const auto& key : m_settings->allKeys()) {
276 settings2->setValue(key, m_settings->value(key));
277 }
278 delete m_settings;
279 m_settings = settings2;
280}