all repos — mgba @ 37212c1f238d25448cfde5a3ec16ca23ab036658

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