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_MBC7);
46 s_mbcList.append(GB_POCKETCAM);
47 s_mbcList.append(GB_TAMA5);
48 s_mbcList.append(GB_HuC3);
49 }
50 if (s_gbModelList.isEmpty()) {
51 // NB: Keep in sync with OverrideView.ui
52 s_gbModelList.append(GB_MODEL_AUTODETECT);
53 s_gbModelList.append(GB_MODEL_DMG);
54 s_gbModelList.append(GB_MODEL_SGB);
55 s_gbModelList.append(GB_MODEL_CGB);
56 s_gbModelList.append(GB_MODEL_AGB);
57 }
58#endif
59 m_ui.setupUi(this);
60
61 connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
62 m_ui.hwRTC->setEnabled(!enabled);
63 m_ui.hwGyro->setEnabled(!enabled);
64 m_ui.hwLight->setEnabled(!enabled);
65 m_ui.hwTilt->setEnabled(!enabled);
66 m_ui.hwRumble->setEnabled(!enabled);
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 for (int colorId = 0; colorId < 4; ++colorId) {
74 connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
75 m_gbColors[colorId] = color.rgb();
76 });
77 }
78
79#ifndef M_CORE_GBA
80 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
81#endif
82#ifndef M_CORE_GB
83 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
84#endif
85
86 connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
87 connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
88 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
89}
90
91void OverrideView::setController(std::shared_ptr<CoreController> controller) {
92 m_controller = controller;
93 connect(controller.get(), &CoreController::started, this, &OverrideView::gameStarted);
94 connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
95 updateOverrides();
96}
97
98void OverrideView::saveOverride() {
99 if (!m_config || !m_controller) {
100 return;
101 }
102 m_config->saveOverride(*m_controller->override());
103}
104
105void OverrideView::updateOverrides() {
106 if (!m_controller) {
107 return;
108 }
109#ifdef M_CORE_GBA
110 if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
111 std::unique_ptr<GBAOverride> gba(new GBAOverride);
112 memset(gba->override.id, 0, 4);
113 gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
114 gba->override.hardware = HW_NO_OVERRIDE;
115 gba->override.idleLoop = IDLE_LOOP_NONE;
116 gba->override.mirroring = false;
117
118 if (!m_ui.hwAutodetect->isChecked()) {
119 gba->override.hardware = HW_NONE;
120 if (m_ui.hwRTC->isChecked()) {
121 gba->override.hardware |= HW_RTC;
122 }
123 if (m_ui.hwGyro->isChecked()) {
124 gba->override.hardware |= HW_GYRO;
125 }
126 if (m_ui.hwLight->isChecked()) {
127 gba->override.hardware |= HW_LIGHT_SENSOR;
128 }
129 if (m_ui.hwTilt->isChecked()) {
130 gba->override.hardware |= HW_TILT;
131 }
132 if (m_ui.hwRumble->isChecked()) {
133 gba->override.hardware |= HW_RUMBLE;
134 }
135 }
136 if (m_ui.hwGBPlayer->isChecked()) {
137 gba->override.hardware |= HW_GB_PLAYER_DETECTION;
138 }
139
140 bool ok;
141 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
142 if (ok) {
143 gba->override.idleLoop = parsedIdleLoop;
144 }
145
146 if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
147 gba->override.idleLoop != IDLE_LOOP_NONE) {
148 m_controller->setOverride(std::move(gba));
149 } else {
150 m_controller->clearOverride();
151 }
152 }
153#endif
154#ifdef M_CORE_GB
155 if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
156 std::unique_ptr<GBOverride> gb(new GBOverride);
157 gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
158 gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
159 gb->override.gbColors[0] = m_gbColors[0];
160 gb->override.gbColors[1] = m_gbColors[1];
161 gb->override.gbColors[2] = m_gbColors[2];
162 gb->override.gbColors[3] = m_gbColors[3];
163 bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
164 hasOverride = hasOverride || (m_gbColors[0] | m_gbColors[1] | m_gbColors[2] | m_gbColors[3]);
165 if (hasOverride) {
166 m_controller->setOverride(std::move(gb));
167 } else {
168 m_controller->clearOverride();
169 }
170 }
171#endif
172}
173
174void OverrideView::gameStarted() {
175 CoreController::Interrupter interrupter(m_controller);
176 mCoreThread* thread = m_controller->thread();
177
178 m_ui.tabWidget->setEnabled(false);
179 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
180
181 switch (thread->core->platform(thread->core)) {
182#ifdef M_CORE_GBA
183 case PLATFORM_GBA: {
184 m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
185 GBA* gba = static_cast<GBA*>(thread->core->board);
186 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
187 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
188 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
189 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
190 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
191 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
192 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
193
194 if (gba->idleLoop != IDLE_LOOP_NONE) {
195 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
196 } else {
197 m_ui.idleLoop->clear();
198 }
199 break;
200 }
201#endif
202#ifdef M_CORE_GB
203 case PLATFORM_GB: {
204 m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
205 GB* gb = static_cast<GB*>(thread->core->board);
206 int mbc = s_mbcList.indexOf(gb->memory.mbcType);
207 if (mbc >= 0) {
208 m_ui.mbc->setCurrentIndex(mbc);
209 } else {
210 m_ui.mbc->setCurrentIndex(0);
211 }
212 int model = s_gbModelList.indexOf(gb->model);
213 if (model >= 0) {
214 m_ui.gbModel->setCurrentIndex(model);
215 } else {
216 m_ui.gbModel->setCurrentIndex(0);
217 }
218 break;
219 }
220#endif
221 case PLATFORM_NONE:
222 break;
223 }
224}
225
226void OverrideView::gameStopped() {
227 m_controller.reset();
228 m_ui.tabWidget->setEnabled(true);
229 m_ui.savetype->setCurrentIndex(0);
230 m_ui.idleLoop->clear();
231 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
232
233 m_ui.mbc->setCurrentIndex(0);
234 m_ui.gbModel->setCurrentIndex(0);
235}