all repos — mgba @ b920758dc10371c79cede9374a14b1d2386f8d5d

mGBA Game Boy Advance Emulator

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	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
 87#ifndef M_CORE_GBA
 88	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
 89#endif
 90#ifndef M_CORE_GB
 91	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
 92#endif
 93
 94	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
 95	connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
 96
 97	m_recheck.setInterval(200);
 98	connect(&m_recheck, &QTimer::timeout, this, &OverrideView::recheck);
 99}
100
101void OverrideView::setController(std::shared_ptr<CoreController> controller) {
102	m_controller = controller;
103	connect(controller.get(), &CoreController::started, this, &OverrideView::gameStarted);
104	connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
105	recheck();
106}
107
108void OverrideView::saveOverride() {
109	if (!m_controller) {
110		m_savePending = true;
111		return;
112	}
113
114	Override* override = m_controller->override();
115	if (!override) {
116		return;
117	}
118	m_config->saveOverride(*override);
119}
120
121void OverrideView::recheck() {
122	if (!m_controller) {
123		return;
124	}
125	if (m_controller->hasStarted()) {
126		gameStarted();
127	} else {
128		updateOverrides();
129	}
130}
131
132void OverrideView::updateOverrides() {
133	if (!m_controller) {
134		return;
135	}
136#ifdef M_CORE_GBA
137	if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
138		std::unique_ptr<GBAOverride> gba(new GBAOverride);
139		memset(gba->override.id, 0, 4);
140		gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
141		gba->override.hardware = HW_NO_OVERRIDE;
142		gba->override.idleLoop = IDLE_LOOP_NONE;
143		gba->override.mirroring = false;
144
145		if (!m_ui.hwAutodetect->isChecked()) {
146			gba->override.hardware = HW_NONE;
147			if (m_ui.hwRTC->isChecked()) {
148				gba->override.hardware |= HW_RTC;
149			}
150			if (m_ui.hwGyro->isChecked()) {
151				gba->override.hardware |= HW_GYRO;
152			}
153			if (m_ui.hwLight->isChecked()) {
154				gba->override.hardware |= HW_LIGHT_SENSOR;
155			}
156			if (m_ui.hwTilt->isChecked()) {
157				gba->override.hardware |= HW_TILT;
158			}
159			if (m_ui.hwRumble->isChecked()) {
160				gba->override.hardware |= HW_RUMBLE;
161			}
162		}
163		if (m_ui.hwGBPlayer->isChecked()) {
164			gba->override.hardware |= HW_GB_PLAYER_DETECTION;
165		}
166
167		bool ok;
168		uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
169		if (ok) {
170			gba->override.idleLoop = parsedIdleLoop;
171		}
172
173		if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
174		    gba->override.idleLoop != IDLE_LOOP_NONE) {
175			m_controller->setOverride(std::move(gba));
176		} else {
177			m_controller->clearOverride();
178		}
179	}
180#endif
181#ifdef M_CORE_GB
182	if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
183		std::unique_ptr<GBOverride> gb(new GBOverride);
184		gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
185		gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
186		bool hasColor = false;
187		for (int i = 0; i < 12; ++i) {
188			gb->override.gbColors[i] = m_gbColors[i];
189			hasColor = hasColor || (m_gbColors[i] & 0xFF000000);
190		}
191		bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
192		hasOverride = hasOverride || hasColor;
193		if (hasOverride) {
194			m_controller->setOverride(std::move(gb));
195		} else {
196			m_controller->clearOverride();
197		}
198	}
199#endif
200}
201
202void OverrideView::gameStarted() {
203	CoreController::Interrupter interrupter(m_controller);
204	mCoreThread* thread = m_controller->thread();
205
206	m_ui.tabWidget->setEnabled(false);
207	m_recheck.start();
208
209	switch (thread->core->platform(thread->core)) {
210#ifdef M_CORE_GBA
211	case PLATFORM_GBA: {
212		m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
213		GBA* gba = static_cast<GBA*>(thread->core->board);
214		m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
215		m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
216		m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
217		m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
218		m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
219		m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
220		m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
221
222		if (gba->idleLoop != IDLE_LOOP_NONE) {
223			m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
224		} else {
225			m_ui.idleLoop->clear();
226		}
227		break;
228	}
229#endif
230#ifdef M_CORE_GB
231	case PLATFORM_GB: {
232		m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
233		GB* gb = static_cast<GB*>(thread->core->board);
234		int mbc = s_mbcList.indexOf(gb->memory.mbcType);
235		if (mbc >= 0) {
236			m_ui.mbc->setCurrentIndex(mbc);
237		} else {
238			m_ui.mbc->setCurrentIndex(0);
239		}
240		int model = s_gbModelList.indexOf(gb->model);
241		if (model >= 0) {
242			m_ui.gbModel->setCurrentIndex(model);
243		} else {
244			m_ui.gbModel->setCurrentIndex(0);
245		}
246		break;
247	}
248#endif
249	case PLATFORM_NONE:
250		break;
251	}
252
253	if (m_savePending) {
254		m_savePending = false;
255		saveOverride();
256	}
257}
258
259void OverrideView::gameStopped() {
260	m_recheck.stop();
261	m_controller.reset();
262	m_ui.tabWidget->setEnabled(true);
263	m_ui.savetype->setCurrentIndex(0);
264	m_ui.idleLoop->clear();
265
266	m_ui.mbc->setCurrentIndex(0);
267	m_ui.gbModel->setCurrentIndex(0);
268}