src/platform/qt/OverrideView.cpp (view raw)
1/* Copyright (c) 2013-2016 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 "OverrideView.h"
7
8#include <QPushButton>
9#include <QStandardItemModel>
10
11#include "ConfigController.h"
12#include "CoreController.h"
13
14#ifdef M_CORE_GBA
15#include "GBAOverride.h"
16#include <mgba/internal/gba/gba.h>
17#endif
18
19#ifdef M_CORE_GB
20#include "GameBoy.h"
21#include "GBOverride.h"
22#include <mgba/internal/gb/gb.h>
23#endif
24
25using namespace QGBA;
26
27OverrideView::OverrideView(ConfigController* config, QWidget* parent)
28 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
29 , m_config(config)
30{
31 m_ui.setupUi(this);
32
33 connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
34 m_ui.hwRTC->setEnabled(!enabled);
35 m_ui.hwGyro->setEnabled(!enabled);
36 m_ui.hwLight->setEnabled(!enabled);
37 m_ui.hwTilt->setEnabled(!enabled);
38 m_ui.hwRumble->setEnabled(!enabled);
39 });
40
41#ifdef M_CORE_GB
42 m_ui.gbModel->setItemData(0, GB_MODEL_AUTODETECT);
43 m_ui.mbc->setItemData(0, GB_MBC_AUTODETECT);
44
45 for (auto model : GameBoy::modelList()) {
46 m_ui.gbModel->addItem(GameBoy::modelName(model), model);
47 }
48
49 QStandardItemModel* model = static_cast<QStandardItemModel*>(m_ui.mbc->model());
50 int bitsSeen = 0;
51 for (auto mbc : GameBoy::mbcList()) {
52 int mbcValue = static_cast<int>(mbc);
53 if ((mbcValue & ~bitsSeen) & 0x001) {
54 m_ui.mbc->addItem(tr("Official MBCs"), -2);
55 model->item(m_ui.mbc->count() - 1)->setFlags(Qt::NoItemFlags);
56 }
57 if ((mbcValue & ~bitsSeen) & 0x010) {
58 m_ui.mbc->addItem(tr("Licensed MBCs"), -3);
59 model->item(m_ui.mbc->count() - 1)->setFlags(Qt::NoItemFlags);
60 }
61 if ((mbcValue & ~bitsSeen) & 0x200) {
62 m_ui.mbc->addItem(tr("Unlicensed MBCs"), -4);
63 model->item(m_ui.mbc->count() - 1)->setFlags(Qt::NoItemFlags);
64 }
65 bitsSeen |= mbcValue;
66 m_ui.mbc->addItem(GameBoy::mbcName(mbc), mbc);
67 }
68
69 m_colorPickers[0] = ColorPicker(m_ui.color0, QColor(0xF8, 0xF8, 0xF8));
70 m_colorPickers[1] = ColorPicker(m_ui.color1, QColor(0xA8, 0xA8, 0xA8));
71 m_colorPickers[2] = ColorPicker(m_ui.color2, QColor(0x50, 0x50, 0x50));
72 m_colorPickers[3] = ColorPicker(m_ui.color3, QColor(0x00, 0x00, 0x00));
73 m_colorPickers[4] = ColorPicker(m_ui.color4, QColor(0xF8, 0xF8, 0xF8));
74 m_colorPickers[5] = ColorPicker(m_ui.color5, QColor(0xA8, 0xA8, 0xA8));
75 m_colorPickers[6] = ColorPicker(m_ui.color6, QColor(0x50, 0x50, 0x50));
76 m_colorPickers[7] = ColorPicker(m_ui.color7, QColor(0x00, 0x00, 0x00));
77 m_colorPickers[8] = ColorPicker(m_ui.color8, QColor(0xF8, 0xF8, 0xF8));
78 m_colorPickers[9] = ColorPicker(m_ui.color9, QColor(0xA8, 0xA8, 0xA8));
79 m_colorPickers[10] = ColorPicker(m_ui.color10, QColor(0x50, 0x50, 0x50));
80 m_colorPickers[11] = ColorPicker(m_ui.color11, QColor(0x00, 0x00, 0x00));
81 for (int colorId = 0; colorId < 12; ++colorId) {
82 connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
83 m_gbColors[colorId] = color.rgb() | 0xFF000000;
84 });
85 }
86#endif
87
88#ifndef M_CORE_GBA
89 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
90#endif
91#ifndef M_CORE_GB
92 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
93#endif
94
95 connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
96 connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
97
98 m_recheck.setInterval(200);
99 connect(&m_recheck, &QTimer::timeout, this, &OverrideView::recheck);
100}
101
102void OverrideView::setController(std::shared_ptr<CoreController> controller) {
103 m_controller = controller;
104 connect(controller.get(), &CoreController::started, this, &OverrideView::gameStarted);
105 connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
106 recheck();
107}
108
109void OverrideView::saveOverride() {
110 if (!m_controller) {
111 m_savePending = true;
112 return;
113 }
114
115 Override* override = m_controller->override();
116 if (!override) {
117 return;
118 }
119 m_config->saveOverride(*override);
120}
121
122void OverrideView::recheck() {
123 if (!m_controller) {
124 return;
125 }
126 if (m_controller->hasStarted()) {
127 gameStarted();
128 } else {
129 updateOverrides();
130 }
131}
132
133void OverrideView::updateOverrides() {
134 if (!m_controller) {
135 return;
136 }
137#ifdef M_CORE_GBA
138 if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
139 std::unique_ptr<GBAOverride> gba(new GBAOverride);
140 memset(gba->override.id, 0, 4);
141 gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
142 gba->override.hardware = HW_NO_OVERRIDE;
143 gba->override.idleLoop = IDLE_LOOP_NONE;
144 gba->override.mirroring = false;
145 gba->override.vbaBugCompat = false;
146
147 if (!m_ui.hwAutodetect->isChecked()) {
148 gba->override.hardware = HW_NONE;
149 if (m_ui.hwRTC->isChecked()) {
150 gba->override.hardware |= HW_RTC;
151 }
152 if (m_ui.hwGyro->isChecked()) {
153 gba->override.hardware |= HW_GYRO;
154 }
155 if (m_ui.hwLight->isChecked()) {
156 gba->override.hardware |= HW_LIGHT_SENSOR;
157 }
158 if (m_ui.hwTilt->isChecked()) {
159 gba->override.hardware |= HW_TILT;
160 }
161 if (m_ui.hwRumble->isChecked()) {
162 gba->override.hardware |= HW_RUMBLE;
163 }
164 }
165 if (m_ui.hwGBPlayer->isChecked()) {
166 gba->override.hardware |= HW_GB_PLAYER_DETECTION;
167 }
168 if (m_ui.vbaBugCompat->isChecked()) {
169 gba->override.vbaBugCompat = true;
170 }
171
172 bool ok;
173 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
174 if (ok) {
175 gba->override.idleLoop = parsedIdleLoop;
176 }
177
178 if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
179 gba->override.idleLoop != IDLE_LOOP_NONE) {
180 m_controller->setOverride(std::move(gba));
181 } else {
182 m_controller->clearOverride();
183 }
184 }
185#endif
186#ifdef M_CORE_GB
187 if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
188 std::unique_ptr<GBOverride> gb(new GBOverride);
189 gb->override.mbc = static_cast<GBMemoryBankControllerType>(m_ui.mbc->currentData().toInt());
190 gb->override.model = static_cast<GBModel>(m_ui.gbModel->currentData().toInt());
191 bool hasColor = false;
192 for (int i = 0; i < 12; ++i) {
193 gb->override.gbColors[i] = m_gbColors[i];
194 hasColor = hasColor || (m_gbColors[i] & 0xFF000000);
195 }
196 bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
197 hasOverride = hasOverride || hasColor;
198 if (hasOverride) {
199 m_controller->setOverride(std::move(gb));
200 } else {
201 m_controller->clearOverride();
202 }
203 }
204#endif
205}
206
207void OverrideView::gameStarted() {
208 CoreController::Interrupter interrupter(m_controller);
209 mCoreThread* thread = m_controller->thread();
210
211 m_ui.tabWidget->setEnabled(false);
212 m_recheck.start();
213
214 switch (thread->core->platform(thread->core)) {
215#ifdef M_CORE_GBA
216 case mPLATFORM_GBA: {
217 m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
218 GBA* gba = static_cast<GBA*>(thread->core->board);
219 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
220 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
221 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
222 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
223 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
224 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
225 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
226 m_ui.vbaBugCompat->setChecked(gba->vbaBugCompat);
227
228 if (gba->idleLoop != IDLE_LOOP_NONE) {
229 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
230 } else {
231 m_ui.idleLoop->clear();
232 }
233 break;
234 }
235#endif
236#ifdef M_CORE_GB
237 case mPLATFORM_GB: {
238 m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
239 GB* gb = static_cast<GB*>(thread->core->board);
240 int index = m_ui.mbc->findData(gb->memory.mbcType);
241 if (index >= 0) {
242 m_ui.mbc->setCurrentIndex(index);
243 } else {
244 m_ui.mbc->setCurrentIndex(0);
245 }
246 int model = m_ui.gbModel->findData(gb->model);
247 if (model >= 0) {
248 m_ui.gbModel->setCurrentIndex(model);
249 } else {
250 m_ui.gbModel->setCurrentIndex(0);
251 }
252 break;
253 }
254#endif
255 case mPLATFORM_NONE:
256 break;
257 }
258
259 if (m_savePending) {
260 m_savePending = false;
261 saveOverride();
262 }
263}
264
265void OverrideView::gameStopped() {
266 m_recheck.stop();
267 m_controller.reset();
268 m_ui.tabWidget->setEnabled(true);
269 m_ui.savetype->setCurrentIndex(0);
270 m_ui.idleLoop->clear();
271
272 m_ui.mbc->setCurrentIndex(0);
273 m_ui.gbModel->setCurrentIndex(0);
274}