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 = 1536;
111 m_opts.sampleRate = 44100;
112 m_opts.volume = GBA_AUDIO_VOLUME_MAX;
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 GBAConfigLoad(&m_config);
120 GBAConfigLoadDefaults(&m_config, &m_opts);
121 GBAConfigMap(&m_config, &m_opts);
122}
123
124ConfigController::~ConfigController() {
125 GBAConfigDeinit(&m_config);
126 GBAConfigFreeOpts(&m_opts);
127}
128
129bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[], SubParser* subparser) {
130 if (::parseArguments(args, &m_config, argc, argv, subparser)) {
131 GBAConfigMap(&m_config, &m_opts);
132 return true;
133 }
134 return false;
135}
136
137ConfigOption* ConfigController::addOption(const char* key) {
138 QString optionName(key);
139
140 if (m_optionSet.contains(optionName)) {
141 return m_optionSet[optionName];
142 }
143 ConfigOption* newOption = new ConfigOption(this);
144 m_optionSet[optionName] = newOption;
145 connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) {
146 setOption(key, value);
147 });
148 return newOption;
149}
150
151void ConfigController::updateOption(const char* key) {
152 if (!key) {
153 return;
154 }
155
156 QString optionName(key);
157
158 if (!m_optionSet.contains(optionName)) {
159 return;
160 }
161 m_optionSet[optionName]->setValue(GBAConfigGetValue(&m_config, key));
162}
163
164QString ConfigController::getOption(const char* key) const {
165 return QString(GBAConfigGetValue(&m_config, key));
166}
167
168QVariant ConfigController::getQtOption(const QString& key, const QString& group) const {
169 if (!group.isNull()) {
170 m_settings->beginGroup(group);
171 }
172 QVariant value = m_settings->value(key);
173 if (!group.isNull()) {
174 m_settings->endGroup();
175 }
176 return value;
177}
178
179void ConfigController::saveOverride(const GBACartridgeOverride& override) {
180 GBAOverrideSave(overrides(), &override);
181 write();
182}
183
184void ConfigController::setOption(const char* key, bool value) {
185 GBAConfigSetIntValue(&m_config, key, value);
186 QString optionName(key);
187 if (m_optionSet.contains(optionName)) {
188 m_optionSet[optionName]->setValue(value);
189 }
190}
191
192void ConfigController::setOption(const char* key, int value) {
193 GBAConfigSetIntValue(&m_config, key, value);
194 QString optionName(key);
195 if (m_optionSet.contains(optionName)) {
196 m_optionSet[optionName]->setValue(value);
197 }
198}
199
200void ConfigController::setOption(const char* key, unsigned value) {
201 GBAConfigSetUIntValue(&m_config, key, value);
202 QString optionName(key);
203 if (m_optionSet.contains(optionName)) {
204 m_optionSet[optionName]->setValue(value);
205 }
206}
207
208void ConfigController::setOption(const char* key, const char* value) {
209 GBAConfigSetValue(&m_config, key, value);
210 QString optionName(key);
211 if (m_optionSet.contains(optionName)) {
212 m_optionSet[optionName]->setValue(value);
213 }
214}
215
216void ConfigController::setOption(const char* key, const QVariant& value) {
217 if (value.type() == QVariant::Bool) {
218 setOption(key, value.toBool());
219 return;
220 }
221 QString stringValue(value.toString());
222 setOption(key, stringValue.toUtf8().constData());
223}
224
225void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) {
226 if (!group.isNull()) {
227 m_settings->beginGroup(group);
228 }
229 m_settings->setValue(key, value);
230 if (!group.isNull()) {
231 m_settings->endGroup();
232 }
233}
234
235QList<QString> ConfigController::getMRU() const {
236 QList<QString> mru;
237 m_settings->beginGroup("mru");
238 for (int i = 0; i < MRU_LIST_SIZE; ++i) {
239 QString item = m_settings->value(QString::number(i)).toString();
240 if (item.isNull()) {
241 continue;
242 }
243 mru.append(item);
244 }
245 m_settings->endGroup();
246 return mru;
247}
248
249void ConfigController::setMRU(const QList<QString>& mru) {
250 int i = 0;
251 m_settings->beginGroup("mru");
252 for (const QString& item : mru) {
253 m_settings->setValue(QString::number(i), item);
254 ++i;
255 if (i >= MRU_LIST_SIZE) {
256 break;
257 }
258 }
259 m_settings->endGroup();
260}
261
262void ConfigController::write() {
263 GBAConfigSave(&m_config);
264 m_settings->sync();
265}
266
267void ConfigController::makePortable() {
268 GBAConfigMakePortable(&m_config);
269
270 char path[PATH_MAX];
271 GBAConfigDirectory(path, sizeof(path));
272 QString fileName(path);
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}