all repos — mgba @ a3fff6d1a5a93bd7eb36f174d498ccf1770cb26e

mGBA Game Boy Advance Emulator

src/platform/qt/Window.cpp (view raw)

  1#include "Window.h"
  2
  3#include <QFileDialog>
  4#include <QKeyEvent>
  5#include <QKeySequence>
  6#include <QMenuBar>
  7#include <QStackedLayout>
  8
  9#include "ConfigController.h"
 10#include "GameController.h"
 11#include "GDBController.h"
 12#include "GDBWindow.h"
 13#include "LoadSaveState.h"
 14#include "LogView.h"
 15#include "VideoView.h"
 16
 17extern "C" {
 18#include "platform/commandline.h"
 19}
 20
 21using namespace QGBA;
 22
 23Window::Window(ConfigController* config, QWidget* parent)
 24	: QMainWindow(parent)
 25	, m_logView(new LogView())
 26	, m_stateWindow(nullptr)
 27	, m_screenWidget(new WindowBackground())
 28	, m_logo(":/res/mgba-1024.png")
 29	, m_config(config)
 30#ifdef USE_FFMPEG
 31	, m_videoView(nullptr)
 32#endif
 33#ifdef USE_GDB_STUB
 34	, m_gdbController(nullptr)
 35#endif
 36{
 37	setWindowTitle(PROJECT_NAME);
 38	m_controller = new GameController(this);
 39
 40	QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
 41	format.setSwapInterval(1);
 42	m_display = new Display(format);
 43
 44	m_screenWidget->setMinimumSize(m_display->minimumSize());
 45	m_screenWidget->setSizePolicy(m_display->sizePolicy());
 46	m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
 47	setCentralWidget(m_screenWidget);
 48
 49	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
 50	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
 51	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
 52	connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
 53	connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
 54	connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
 55	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 56	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 57	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 58	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 59	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 60
 61	setupMenu(menuBar());
 62}
 63
 64Window::~Window() {
 65	delete m_logView;
 66	delete m_videoView;
 67}
 68
 69GBAKey Window::mapKey(int qtKey) {
 70	switch (qtKey) {
 71	case Qt::Key_Z:
 72		return GBA_KEY_A;
 73		break;
 74	case Qt::Key_X:
 75		return GBA_KEY_B;
 76		break;
 77	case Qt::Key_A:
 78		return GBA_KEY_L;
 79		break;
 80	case Qt::Key_S:
 81		return GBA_KEY_R;
 82		break;
 83	case Qt::Key_Return:
 84		return GBA_KEY_START;
 85		break;
 86	case Qt::Key_Backspace:
 87		return GBA_KEY_SELECT;
 88		break;
 89	case Qt::Key_Up:
 90		return GBA_KEY_UP;
 91		break;
 92	case Qt::Key_Down:
 93		return GBA_KEY_DOWN;
 94		break;
 95	case Qt::Key_Left:
 96		return GBA_KEY_LEFT;
 97		break;
 98	case Qt::Key_Right:
 99		return GBA_KEY_RIGHT;
100		break;
101	default:
102		return GBA_KEY_NONE;
103	}
104}
105
106void Window::argumentsPassed(GBAArguments* args) {
107	loadConfig();
108
109	if (args->patch) {
110		m_controller->loadPatch(args->patch);
111	}
112
113	if (args->fname) {
114		m_controller->loadGame(args->fname, args->dirmode);
115	}
116}
117
118void Window::setConfig(ConfigController* config) {
119	m_config = config;
120}
121
122void Window::loadConfig() {
123	const GBAOptions* opts = m_config->options();
124
125	m_logView->setLevels(opts->logLevel);
126	// TODO: Have these show up as modified in the menu
127	m_controller->setFrameskip(opts->frameskip);
128	m_controller->setAudioSync(opts->audioSync);
129	m_controller->setVideoSync(opts->videoSync);
130
131	if (opts->bios) {
132		m_controller->loadBIOS(opts->bios);
133	}
134
135	if (opts->fpsTarget) {
136		emit fpsTargetChanged(opts->fpsTarget);
137	}
138
139	if (opts->audioBuffers) {
140		emit audioBufferSamplesChanged(opts->audioBuffers);
141	}
142
143	if (opts->width && opts->height) {
144		m_screenWidget->setSizeHint(QSize(opts->width, opts->height));
145	}
146}
147
148void Window::saveConfig() {
149	m_config->write();
150}
151
152void Window::selectROM() {
153	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
154	if (!filename.isEmpty()) {
155		m_controller->loadGame(filename);
156	}
157}
158
159void Window::selectBIOS() {
160	QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
161	if (!filename.isEmpty()) {
162		m_controller->loadBIOS(filename);
163	}
164}
165
166void Window::selectPatch() {
167	QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), QString(), tr("Patches (*.ips *.ups)"));
168	if (!filename.isEmpty()) {
169		m_controller->loadPatch(filename);
170	}
171}
172
173#ifdef USE_FFMPEG
174void Window::openVideoWindow() {
175	if (!m_videoView) {
176		m_videoView = new VideoView();
177		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
178		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
179		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
180		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
181		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
182	}
183	m_videoView->show();
184}
185#endif
186
187#ifdef USE_GDB_STUB
188void Window::gdbOpen() {
189	if (!m_gdbController) {
190		m_gdbController = new GDBController(m_controller, this);
191	}
192	GDBWindow* window = new GDBWindow(m_gdbController);
193	window->show();
194}
195#endif
196
197void Window::keyPressEvent(QKeyEvent* event) {
198	if (event->isAutoRepeat()) {
199		QWidget::keyPressEvent(event);
200		return;
201	}
202	if (event->key() == Qt::Key_Tab) {
203		m_controller->setTurbo(true, false);
204	}
205	GBAKey key = mapKey(event->key());
206	if (key == GBA_KEY_NONE) {
207		QWidget::keyPressEvent(event);
208		return;
209	}
210	m_controller->keyPressed(key);
211	event->accept();
212}
213
214void Window::keyReleaseEvent(QKeyEvent* event) {
215	if (event->isAutoRepeat()) {
216		QWidget::keyReleaseEvent(event);
217		return;
218	}
219	if (event->key() == Qt::Key_Tab) {
220		m_controller->setTurbo(false, false);
221	}
222	GBAKey key = mapKey(event->key());
223	if (key == GBA_KEY_NONE) {
224		QWidget::keyPressEvent(event);
225		return;
226	}
227	m_controller->keyReleased(key);
228	event->accept();
229}
230
231void Window::resizeEvent(QResizeEvent*) {
232	redoLogo();
233	m_config->setOption("height", m_screenWidget->height());
234	m_config->setOption("width", m_screenWidget->width());
235}
236
237void Window::closeEvent(QCloseEvent* event) {
238	emit shutdown();
239	QMainWindow::closeEvent(event);
240}
241
242void Window::toggleFullScreen() {
243	if (isFullScreen()) {
244		showNormal();
245	} else {
246		showFullScreen();
247	}
248}
249
250void Window::gameStarted(GBAThread* context) {
251	emit startDrawing(m_controller->drawContext(), context);
252	foreach (QAction* action, m_gameActions) {
253		action->setDisabled(false);
254	}
255	char title[13] = { '\0' };
256	GBAGetGameTitle(context->gba, title);
257	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
258	attachWidget(m_display);
259	m_screenWidget->setScaledContents(true);
260}
261
262void Window::gameStopped() {
263	foreach (QAction* action, m_gameActions) {
264		action->setDisabled(true);
265	}
266	setWindowTitle(tr(PROJECT_NAME));
267	detachWidget(m_display);
268	m_screenWidget->setScaledContents(false);
269	redoLogo();
270}
271
272void Window::redoLogo() {
273	if (m_controller->isLoaded()) {
274		return;
275	}
276	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
277	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
278	m_screenWidget->setPixmap(logo);
279}
280
281void Window::openStateWindow(LoadSave ls) {
282	if (m_stateWindow) {
283		return;
284	}
285	bool wasPaused = m_controller->isPaused();
286	m_stateWindow = new LoadSaveState(m_controller);
287	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
288	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
289	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
290		m_screenWidget->layout()->removeWidget(m_stateWindow);
291		m_stateWindow = nullptr;
292		setFocus();
293	});
294	if (!wasPaused) {
295		m_controller->setPaused(true);
296		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
297	}
298	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
299	m_stateWindow->setMode(ls);
300	attachWidget(m_stateWindow);
301}
302
303void Window::setupMenu(QMenuBar* menubar) {
304	menubar->clear();
305	QMenu* fileMenu = menubar->addMenu(tr("&File"));
306	fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
307	fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS()));
308	fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch()));
309
310	fileMenu->addSeparator();
311
312	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
313	loadState->setShortcut(tr("Ctrl+L"));
314	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
315	m_gameActions.append(loadState);
316	fileMenu->addAction(loadState);
317
318	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
319	saveState->setShortcut(tr("Ctrl+S"));
320	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
321	m_gameActions.append(saveState);
322	fileMenu->addAction(saveState);
323
324	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
325	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
326	int i;
327	for (i = 1; i < 10; ++i) {
328		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
329		quickLoad->setShortcut(tr("F%1").arg(i));
330		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
331		m_gameActions.append(quickLoad);
332		quickLoadMenu->addAction(quickLoad);
333
334		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
335		quickSave->setShortcut(tr("Shift+F%1").arg(i));
336		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
337		m_gameActions.append(quickSave);
338		quickSaveMenu->addAction(quickSave);
339	}
340
341#if defined(USE_PNG) || defined(USE_FFMPEG)
342	fileMenu->addSeparator();
343#endif
344
345#ifdef USE_PNG
346	QAction* screenshot = new QAction(tr("Take &screenshot"), fileMenu);
347	screenshot->setShortcut(tr("F12"));
348	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
349	m_gameActions.append(screenshot);
350	fileMenu->addAction(screenshot);
351#endif
352
353#ifdef USE_FFMPEG
354	QAction* recordOutput = new QAction(tr("Record output..."), fileMenu);
355	recordOutput->setShortcut(tr("F11"));
356	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
357	fileMenu->addAction(recordOutput);
358#endif
359
360#ifndef Q_OS_MAC
361	fileMenu->addSeparator();
362	fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
363#endif
364
365	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
366	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
367	reset->setShortcut(tr("Ctrl+R"));
368	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
369	m_gameActions.append(reset);
370	emulationMenu->addAction(reset);
371
372	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
373	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
374	m_gameActions.append(shutdown);
375	emulationMenu->addAction(shutdown);
376	emulationMenu->addSeparator();
377
378	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
379	pause->setChecked(false);
380	pause->setCheckable(true);
381	pause->setShortcut(tr("Ctrl+P"));
382	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
383	connect(m_controller, &GameController::gamePaused, [this, pause]() {
384		pause->setChecked(true);
385
386		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
387		QPixmap pixmap;
388		pixmap.convertFromImage(currentImage.rgbSwapped());
389		m_screenWidget->setPixmap(pixmap);
390	});
391	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
392	m_gameActions.append(pause);
393	emulationMenu->addAction(pause);
394
395	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
396	frameAdvance->setShortcut(tr("Ctrl+N"));
397	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
398	m_gameActions.append(frameAdvance);
399	emulationMenu->addAction(frameAdvance);
400
401	QMenu* target = emulationMenu->addMenu("FPS target");
402	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
403	fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
404	fpsTargetOption->addValue(tr("15"), 15, target);
405	fpsTargetOption->addValue(tr("30"), 30, target);
406	fpsTargetOption->addValue(tr("45"), 45, target);
407	fpsTargetOption->addValue(tr("60"), 60, target);
408	fpsTargetOption->addValue(tr("90"), 90, target);
409	fpsTargetOption->addValue(tr("120"), 120, target);
410	fpsTargetOption->addValue(tr("240"), 240, target);
411	m_config->updateOption("fpsTarget");
412
413	emulationMenu->addSeparator();
414
415	QAction* turbo = new QAction(tr("T&urbo"), emulationMenu);
416	turbo->setCheckable(true);
417	turbo->setChecked(false);
418	turbo->setShortcut(tr("Shift+Tab"));
419	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
420	emulationMenu->addAction(turbo);
421
422	ConfigOption* videoSync = m_config->addOption("videoSync");
423	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
424	videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
425	m_config->updateOption("videoSync");
426
427	ConfigOption* audioSync = m_config->addOption("audioSync");
428	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
429	audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
430	m_config->updateOption("audioSync");
431
432	QMenu* videoMenu = menubar->addMenu(tr("&Video"));
433	QMenu* frameMenu = videoMenu->addMenu(tr("Frame size"));
434	QAction* setSize = new QAction(tr("1x"), videoMenu);
435	connect(setSize, &QAction::triggered, [this]() {
436		showNormal();
437		resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
438	});
439	frameMenu->addAction(setSize);
440	setSize = new QAction(tr("2x"), videoMenu);
441	connect(setSize, &QAction::triggered, [this]() {
442		showNormal();
443		resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
444	});
445	frameMenu->addAction(setSize);
446	setSize = new QAction(tr("3x"), videoMenu);
447	connect(setSize, &QAction::triggered, [this]() {
448		showNormal();
449		resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
450	});
451	frameMenu->addAction(setSize);
452	setSize = new QAction(tr("4x"), videoMenu);
453	connect(setSize, &QAction::triggered, [this]() {
454		showNormal();
455		resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
456	});
457	frameMenu->addAction(setSize);
458	frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F"));
459
460	QMenu* skipMenu = videoMenu->addMenu(tr("Frame&skip"));
461	ConfigOption* skip = m_config->addOption("frameskip");
462	skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
463	for (int i = 0; i <= 10; ++i) {
464		skip->addValue(QString::number(i), i, skipMenu);
465	}
466	m_config->updateOption("frameskip");
467
468	QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
469	QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
470	ConfigOption* buffers = m_config->addOption("audioBuffers");
471	buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
472	buffers->addValue(tr("512"), 512, buffersMenu);
473	buffers->addValue(tr("768"), 768, buffersMenu);
474	buffers->addValue(tr("1024"), 1024, buffersMenu);
475	buffers->addValue(tr("2048"), 2048, buffersMenu);
476	buffers->addValue(tr("4096"), 4096, buffersMenu);
477	m_config->updateOption("audioBuffers");
478
479	QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
480	QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
481	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
482	debuggingMenu->addAction(viewLogs);
483#ifdef USE_GDB_STUB
484	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
485	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
486	debuggingMenu->addAction(gdbWindow);
487#endif
488
489	foreach (QAction* action, m_gameActions) {
490		action->setDisabled(true);
491	}
492}
493
494void Window::attachWidget(QWidget* widget) {
495	m_screenWidget->layout()->addWidget(widget);
496	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
497}
498
499void Window::detachWidget(QWidget* widget) {
500	m_screenWidget->layout()->removeWidget(widget);
501}
502
503WindowBackground::WindowBackground(QWidget* parent)
504	: QLabel(parent)
505{
506	setLayout(new QStackedLayout());
507	layout()->setContentsMargins(0, 0, 0, 0);
508	setAlignment(Qt::AlignCenter);
509	QPalette p = palette();
510	p.setColor(backgroundRole(), Qt::black);
511	setPalette(p);
512	setAutoFillBackground(true);
513}
514
515void WindowBackground::setSizeHint(const QSize& hint) {
516	m_sizeHint = hint;
517}
518
519QSize WindowBackground::sizeHint() const {
520	return m_sizeHint;
521}