all repos — mgba @ 636e01d8df67c86176562e1e5f67c98d6fe788b6

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