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
146 if (!m_ui.hwAutodetect->isChecked()) {
147 gba->override.hardware = HW_NONE;
148 if (m_ui.hwRTC->isChecked()) {
149 gba->override.hardware |= HW_RTC;
150 }
151 if (m_ui.hwGyro->isChecked()) {
152 gba->override.hardware |= HW_GYRO;
153 }
154 if (m_ui.hwLight->isChecked()) {
155 gba->override.hardware |= HW_LIGHT_SENSOR;
156 }
157 if (m_ui.hwTilt->isChecked()) {
158 gba->override.hardware |= HW_TILT;
159 }
160 if (m_ui.hwRumble->isChecked()) {
161 gba->override.hardware |= HW_RUMBLE;
162 }
163 }
164 if (m_ui.hwGBPlayer->isChecked()) {
165 gba->override.hardware |= HW_GB_PLAYER_DETECTION;
166 }
167
168 bool ok;
169 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
170 if (ok) {
171 gba->override.idleLoop = parsedIdleLoop;
172 }
173
174 if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
175 gba->override.idleLoop != IDLE_LOOP_NONE) {
176 m_controller->setOverride(std::move(gba));
177 } else {
178 m_controller->clearOverride();
179 }
180 }
181#endif
182#ifdef M_CORE_GB
183 if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
184 std::unique_ptr<GBOverride> gb(new GBOverride);
185 gb->override.mbc = static_cast<GBMemoryBankControllerType>(m_ui.mbc->currentData().toInt());
186 gb->override.model = static_cast<GBModel>(m_ui.gbModel->currentData().toInt());
187 bool hasColor = false;
188 for (int i = 0; i < 12; ++i) {
189 gb->override.gbColors[i] = m_gbColors[i];
190 hasColor = hasColor || (m_gbColors[i] & 0xFF000000);
191 }
192 bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
193 hasOverride = hasOverride || hasColor;
194 if (hasOverride) {
195 m_controller->setOverride(std::move(gb));
196 } else {
197 m_controller->clearOverride();
198 }
199 }
200#endif
201}
202
203void OverrideView::gameStarted() {
204 CoreController::Interrupter interrupter(m_controller);
205 mCoreThread* thread = m_controller->thread();
206
207 m_ui.tabWidget->setEnabled(false);
208 m_recheck.start();
209
210 switch (thread->core->platform(thread->core)) {
211#ifdef M_CORE_GBA
212 case PLATFORM_GBA: {
213 m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
214 GBA* gba = static_cast<GBA*>(thread->core->board);
215 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
216 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
217 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
218 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
219 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
220 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
221 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
222
223 if (gba->idleLoop != IDLE_LOOP_NONE) {
224 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
225 } else {
226 m_ui.idleLoop->clear();
227 }
228 break;
229 }
230#endif
231#ifdef M_CORE_GB
232 case PLATFORM_GB: {
233 m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
234 GB* gb = static_cast<GB*>(thread->core->board);
235 int index = m_ui.mbc->findData(gb->memory.mbcType);
236 if (index >= 0) {
237 m_ui.mbc->setCurrentIndex(index);
238 } else {
239 m_ui.mbc->setCurrentIndex(0);
240 }
241 int model = m_ui.gbModel->findData(gb->model);
242 if (model >= 0) {
243 m_ui.gbModel->setCurrentIndex(model);
244 } else {
245 m_ui.gbModel->setCurrentIndex(0);
246 }
247 break;
248 }
249#endif
250 case PLATFORM_NONE:
251 break;
252 }
253
254 if (m_savePending) {
255 m_savePending = false;
256 saveOverride();
257 }
258}
259
260void OverrideView::gameStopped() {
261 m_recheck.stop();
262 m_controller.reset();
263 m_ui.tabWidget->setEnabled(true);
264 m_ui.savetype->setCurrentIndex(0);
265 m_ui.idleLoop->clear();
266
267 m_ui.mbc->setCurrentIndex(0);
268 m_ui.gbModel->setCurrentIndex(0);
269}