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