all repos — mgba @ b51e72fcab27f8ca85c3c535cd1f06077bb59bae

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