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 connect(m_ui.buttonBox, SIGNAL(rejected()), this, SLOT(close()));
92
93 if (controller->isLoaded()) {
94 gameStarted(controller->thread());
95 }
96}
97
98void OverrideView::saveOverride() {
99 if (!m_config) {
100 return;
101 }
102 m_config->saveOverride(*m_controller->override());
103}
104
105void OverrideView::updateOverrides() {
106#ifdef M_CORE_GBA
107 if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
108 GBAOverride* gba = new GBAOverride;
109 memset(gba->override.id, 0, 4);
110 gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
111 gba->override.hardware = HW_NO_OVERRIDE;
112 gba->override.idleLoop = IDLE_LOOP_NONE;
113 gba->override.mirroring = false;
114
115 if (!m_ui.hwAutodetect->isChecked()) {
116 gba->override.hardware = HW_NONE;
117 if (m_ui.hwRTC->isChecked()) {
118 gba->override.hardware |= HW_RTC;
119 }
120 if (m_ui.hwGyro->isChecked()) {
121 gba->override.hardware |= HW_GYRO;
122 }
123 if (m_ui.hwLight->isChecked()) {
124 gba->override.hardware |= HW_LIGHT_SENSOR;
125 }
126 if (m_ui.hwTilt->isChecked()) {
127 gba->override.hardware |= HW_TILT;
128 }
129 if (m_ui.hwRumble->isChecked()) {
130 gba->override.hardware |= HW_RUMBLE;
131 }
132 }
133 if (m_ui.hwGBPlayer->isChecked()) {
134 gba->override.hardware |= HW_GB_PLAYER_DETECTION;
135 }
136
137 bool ok;
138 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
139 if (ok) {
140 gba->override.idleLoop = parsedIdleLoop;
141 }
142
143 if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
144 gba->override.idleLoop != IDLE_LOOP_NONE) {
145 m_controller->setOverride(gba);
146 } else {
147 m_controller->clearOverride();
148 delete gba;
149 }
150 }
151#endif
152#ifdef M_CORE_GB
153 if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
154 GBOverride* gb = new GBOverride;
155 gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
156 gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
157 if (gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT) {
158 m_controller->setOverride(gb);
159 } else {
160 m_controller->clearOverride();
161 delete gb;
162 }
163 }
164#endif
165}
166
167void OverrideView::gameStarted(mCoreThread* thread) {
168 if (!thread->core) {
169 gameStopped();
170 return;
171 }
172
173 m_ui.tabWidget->setEnabled(false);
174
175 switch (thread->core->platform(thread->core)) {
176#ifdef M_CORE_GBA
177 case PLATFORM_GBA: {
178 m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
179 GBA* gba = static_cast<GBA*>(thread->core->board);
180 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
181 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
182 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
183 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
184 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
185 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
186 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
187
188 if (gba->idleLoop != IDLE_LOOP_NONE) {
189 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
190 } else {
191 m_ui.idleLoop->clear();
192 }
193 break;
194 }
195#endif
196#ifdef M_CORE_GB
197 case PLATFORM_GB: {
198 m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
199 GB* gb = static_cast<GB*>(thread->core->board);
200 int mbc = s_mbcList.indexOf(gb->memory.mbcType);
201 if (mbc >= 0) {
202 m_ui.mbc->setCurrentIndex(mbc);
203 } else {
204 m_ui.mbc->setCurrentIndex(0);
205 }
206 int model = s_gbModelList.indexOf(gb->model);
207 if (model >= 0) {
208 m_ui.gbModel->setCurrentIndex(model);
209 } else {
210 m_ui.gbModel->setCurrentIndex(0);
211 }
212 break;
213 }
214#endif
215 case PLATFORM_NONE:
216 break;
217 }
218}
219
220void OverrideView::gameStopped() {
221 m_ui.tabWidget->setEnabled(true);
222 m_ui.savetype->setCurrentIndex(0);
223 m_ui.idleLoop->clear();
224
225 m_ui.mbc->setCurrentIndex(0);
226 m_ui.gbModel->setCurrentIndex(0);
227
228 updateOverrides();
229}