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