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