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
10#include "ConfigController.h"
11#include "CoreController.h"
12
13#ifdef M_CORE_GBA
14#include "GBAOverride.h"
15#include <mgba/internal/gba/gba.h>
16#endif
17
18#ifdef M_CORE_GB
19#include "GBOverride.h"
20#include <mgba/internal/gb/gb.h>
21#endif
22
23using namespace QGBA;
24
25#ifdef M_CORE_GB
26QList<enum GBModel> OverrideView::s_gbModelList;
27QList<enum GBMemoryBankControllerType> OverrideView::s_mbcList;
28#endif
29
30OverrideView::OverrideView(ConfigController* config, QWidget* parent)
31 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
32 , m_config(config)
33{
34#ifdef M_CORE_GB
35 if (s_mbcList.isEmpty()) {
36 // NB: Keep in sync with OverrideView.ui
37 s_mbcList.append(GB_MBC_AUTODETECT);
38 s_mbcList.append(GB_MBC_NONE);
39 s_mbcList.append(GB_MBC1);
40 s_mbcList.append(GB_MBC2);
41 s_mbcList.append(GB_MBC3);
42 s_mbcList.append(GB_MBC3_RTC);
43 s_mbcList.append(GB_MBC5);
44 s_mbcList.append(GB_MBC5_RUMBLE);
45 s_mbcList.append(GB_MBC6);
46 s_mbcList.append(GB_MBC7);
47 s_mbcList.append(GB_MMM01);
48 s_mbcList.append(GB_POCKETCAM);
49 s_mbcList.append(GB_TAMA5);
50 s_mbcList.append(GB_HuC1);
51 s_mbcList.append(GB_HuC3);
52 s_mbcList.append(GB_UNL_WISDOM_TREE);
53 }
54 if (s_gbModelList.isEmpty()) {
55 // NB: Keep in sync with OverrideView.ui
56 s_gbModelList.append(GB_MODEL_AUTODETECT);
57 s_gbModelList.append(GB_MODEL_DMG);
58 s_gbModelList.append(GB_MODEL_SGB);
59 s_gbModelList.append(GB_MODEL_CGB);
60 s_gbModelList.append(GB_MODEL_AGB);
61 }
62#endif
63 m_ui.setupUi(this);
64
65 connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
66 m_ui.hwRTC->setEnabled(!enabled);
67 m_ui.hwGyro->setEnabled(!enabled);
68 m_ui.hwLight->setEnabled(!enabled);
69 m_ui.hwTilt->setEnabled(!enabled);
70 m_ui.hwRumble->setEnabled(!enabled);
71 });
72
73#ifdef M_CORE_GB
74 m_colorPickers[0] = ColorPicker(m_ui.color0, QColor(0xF8, 0xF8, 0xF8));
75 m_colorPickers[1] = ColorPicker(m_ui.color1, QColor(0xA8, 0xA8, 0xA8));
76 m_colorPickers[2] = ColorPicker(m_ui.color2, QColor(0x50, 0x50, 0x50));
77 m_colorPickers[3] = ColorPicker(m_ui.color3, QColor(0x00, 0x00, 0x00));
78 m_colorPickers[4] = ColorPicker(m_ui.color4, QColor(0xF8, 0xF8, 0xF8));
79 m_colorPickers[5] = ColorPicker(m_ui.color5, QColor(0xA8, 0xA8, 0xA8));
80 m_colorPickers[6] = ColorPicker(m_ui.color6, QColor(0x50, 0x50, 0x50));
81 m_colorPickers[7] = ColorPicker(m_ui.color7, QColor(0x00, 0x00, 0x00));
82 m_colorPickers[8] = ColorPicker(m_ui.color8, QColor(0xF8, 0xF8, 0xF8));
83 m_colorPickers[9] = ColorPicker(m_ui.color9, QColor(0xA8, 0xA8, 0xA8));
84 m_colorPickers[10] = ColorPicker(m_ui.color10, QColor(0x50, 0x50, 0x50));
85 m_colorPickers[11] = ColorPicker(m_ui.color11, QColor(0x00, 0x00, 0x00));
86 for (int colorId = 0; colorId < 12; ++colorId) {
87 connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
88 m_gbColors[colorId] = color.rgb() | 0xFF000000;
89 });
90 }
91#endif
92
93#ifndef M_CORE_GBA
94 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
95#endif
96#ifndef M_CORE_GB
97 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
98#endif
99
100 connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
101 connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
102
103 m_recheck.setInterval(200);
104 connect(&m_recheck, &QTimer::timeout, this, &OverrideView::recheck);
105}
106
107void OverrideView::setController(std::shared_ptr<CoreController> controller) {
108 m_controller = controller;
109 connect(controller.get(), &CoreController::started, this, &OverrideView::gameStarted);
110 connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
111 recheck();
112}
113
114void OverrideView::saveOverride() {
115 if (!m_controller) {
116 m_savePending = true;
117 return;
118 }
119
120 Override* override = m_controller->override();
121 if (!override) {
122 return;
123 }
124 m_config->saveOverride(*override);
125}
126
127void OverrideView::recheck() {
128 if (!m_controller) {
129 return;
130 }
131 if (m_controller->hasStarted()) {
132 gameStarted();
133 } else {
134 updateOverrides();
135 }
136}
137
138void OverrideView::updateOverrides() {
139 if (!m_controller) {
140 return;
141 }
142#ifdef M_CORE_GBA
143 if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
144 std::unique_ptr<GBAOverride> gba(new GBAOverride);
145 memset(gba->override.id, 0, 4);
146 gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
147 gba->override.hardware = HW_NO_OVERRIDE;
148 gba->override.idleLoop = IDLE_LOOP_NONE;
149 gba->override.mirroring = false;
150
151 if (!m_ui.hwAutodetect->isChecked()) {
152 gba->override.hardware = HW_NONE;
153 if (m_ui.hwRTC->isChecked()) {
154 gba->override.hardware |= HW_RTC;
155 }
156 if (m_ui.hwGyro->isChecked()) {
157 gba->override.hardware |= HW_GYRO;
158 }
159 if (m_ui.hwLight->isChecked()) {
160 gba->override.hardware |= HW_LIGHT_SENSOR;
161 }
162 if (m_ui.hwTilt->isChecked()) {
163 gba->override.hardware |= HW_TILT;
164 }
165 if (m_ui.hwRumble->isChecked()) {
166 gba->override.hardware |= HW_RUMBLE;
167 }
168 }
169 if (m_ui.hwGBPlayer->isChecked()) {
170 gba->override.hardware |= HW_GB_PLAYER_DETECTION;
171 }
172
173 bool ok;
174 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
175 if (ok) {
176 gba->override.idleLoop = parsedIdleLoop;
177 }
178
179 if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
180 gba->override.idleLoop != IDLE_LOOP_NONE) {
181 m_controller->setOverride(std::move(gba));
182 } else {
183 m_controller->clearOverride();
184 }
185 }
186#endif
187#ifdef M_CORE_GB
188 if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
189 std::unique_ptr<GBOverride> gb(new GBOverride);
190 gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
191 gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
192 bool hasColor = false;
193 for (int i = 0; i < 12; ++i) {
194 gb->override.gbColors[i] = m_gbColors[i];
195 hasColor = hasColor || (m_gbColors[i] & 0xFF000000);
196 }
197 bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
198 hasOverride = hasOverride || hasColor;
199 if (hasOverride) {
200 m_controller->setOverride(std::move(gb));
201 } else {
202 m_controller->clearOverride();
203 }
204 }
205#endif
206}
207
208void OverrideView::gameStarted() {
209 CoreController::Interrupter interrupter(m_controller);
210 mCoreThread* thread = m_controller->thread();
211
212 m_ui.tabWidget->setEnabled(false);
213 m_recheck.start();
214
215 switch (thread->core->platform(thread->core)) {
216#ifdef M_CORE_GBA
217 case PLATFORM_GBA: {
218 m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
219 GBA* gba = static_cast<GBA*>(thread->core->board);
220 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
221 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
222 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
223 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
224 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
225 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
226 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
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 PLATFORM_GB: {
238 m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
239 GB* gb = static_cast<GB*>(thread->core->board);
240 int mbc = s_mbcList.indexOf(gb->memory.mbcType);
241 if (mbc >= 0) {
242 m_ui.mbc->setCurrentIndex(mbc);
243 } else {
244 m_ui.mbc->setCurrentIndex(0);
245 }
246 int model = s_gbModelList.indexOf(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 PLATFORM_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}