all repos — mgba @ 78002b4df7cf200fa612a4c3b0d0cc8c8b7165fc

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2014 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 "Window.h"
  7
  8#include <QFileDialog>
  9#include <QFileInfo>
 10#include <QKeyEvent>
 11#include <QKeySequence>
 12#include <QMenuBar>
 13#include <QMessageBox>
 14#include <QStackedLayout>
 15
 16#include "ConfigController.h"
 17#include "GameController.h"
 18#include "GBAKeyEditor.h"
 19#include "GDBController.h"
 20#include "GDBWindow.h"
 21#include "GIFView.h"
 22#include "GamePakView.h"
 23#include "LoadSaveState.h"
 24#include "LogView.h"
 25#include "SettingsView.h"
 26#include "ShortcutController.h"
 27#include "ShortcutView.h"
 28#include "VideoView.h"
 29
 30extern "C" {
 31#include "platform/commandline.h"
 32}
 33
 34using namespace QGBA;
 35
 36Window::Window(ConfigController* config, QWidget* parent)
 37	: QMainWindow(parent)
 38	, m_logView(new LogView())
 39	, m_stateWindow(nullptr)
 40	, m_screenWidget(new WindowBackground())
 41	, m_logo(":/res/mgba-1024.png")
 42	, m_config(config)
 43#ifdef USE_FFMPEG
 44	, m_videoView(nullptr)
 45#endif
 46#ifdef USE_MAGICK
 47	, m_gifView(nullptr)
 48#endif
 49#ifdef USE_GDB_STUB
 50	, m_gdbController(nullptr)
 51#endif
 52	, m_mruMenu(nullptr)
 53	, m_shortcutController(new ShortcutController(this))
 54{
 55	setWindowTitle(PROJECT_NAME);
 56	setFocusPolicy(Qt::StrongFocus);
 57	m_controller = new GameController(this);
 58	m_controller->setInputController(&m_inputController);
 59
 60	QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
 61	format.setSwapInterval(1);
 62	m_display = new Display(format);
 63
 64	m_screenWidget->setMinimumSize(m_display->minimumSize());
 65	m_screenWidget->setSizePolicy(m_display->sizePolicy());
 66	m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
 67	setCentralWidget(m_screenWidget);
 68
 69	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
 70	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
 71	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
 72	connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
 73	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
 74#ifndef Q_OS_MAC
 75	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
 76	connect(m_controller, &GameController::gameUnpaused, [this]() {
 77		if(isFullScreen()) {
 78			menuBar()->hide();
 79		}
 80	});
 81#endif
 82	connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
 83	connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
 84	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
 85	connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
 86	connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
 87	connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
 88	connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
 89	connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
 90	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 91	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 92	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 93	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 94	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 95	connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
 96
 97	m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL);
 98	m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
 99
100	m_shortcutController->setConfigController(m_config);
101	setupMenu(menuBar());
102}
103
104Window::~Window() {
105	delete m_logView;
106
107#ifdef USE_FFMPEG
108	delete m_videoView;
109#endif
110
111#ifdef USE_MAGICK
112	delete m_gifView;
113#endif
114}
115
116void Window::argumentsPassed(GBAArguments* args) {
117	loadConfig();
118
119	if (args->patch) {
120		m_controller->loadPatch(args->patch);
121	}
122
123	if (args->fname) {
124		m_controller->loadGame(args->fname, args->dirmode);
125	}
126}
127
128void Window::resizeFrame(int width, int height) {
129	QSize newSize(width, height);
130	newSize -= m_screenWidget->size();
131	newSize += size();
132	resize(newSize);
133}
134
135void Window::setConfig(ConfigController* config) {
136	m_config = config;
137}
138
139void Window::loadConfig() {
140	const GBAOptions* opts = m_config->options();
141
142	m_logView->setLevels(opts->logLevel);
143
144	m_controller->setFrameskip(opts->frameskip);
145	m_controller->setAudioSync(opts->audioSync);
146	m_controller->setVideoSync(opts->videoSync);
147	m_controller->setSkipBIOS(opts->skipBios);
148	m_controller->setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
149	m_display->lockAspectRatio(opts->lockAspectRatio);
150	m_display->filter(opts->resampleVideo);
151
152	if (opts->bios) {
153		m_controller->loadBIOS(opts->bios);
154	}
155
156	if (opts->fpsTarget) {
157		emit fpsTargetChanged(opts->fpsTarget);
158	}
159
160	if (opts->audioBuffers) {
161		emit audioBufferSamplesChanged(opts->audioBuffers);
162	}
163
164	if (opts->width && opts->height) {
165		resizeFrame(opts->width, opts->height);
166	}
167
168	m_mruFiles = m_config->getMRU();
169	updateMRU();
170
171	m_inputController.setConfiguration(m_config);
172}
173
174void Window::saveConfig() {
175	m_config->write();
176}
177
178void Window::selectROM() {
179	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"), m_config->getQtOption("lastDirectory").toString());
180	if (!filename.isEmpty()) {
181		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
182		m_controller->loadGame(filename);
183	}
184}
185
186void Window::selectBIOS() {
187	QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"), m_config->getQtOption("lastDirectory").toString());
188	if (!filename.isEmpty()) {
189		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
190		m_config->setOption("bios", filename);
191		m_config->updateOption("bios");
192		m_controller->loadBIOS(filename);
193	}
194}
195
196void Window::selectPatch() {
197	QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), m_config->getQtOption("lastDirectory").toString(), tr("Patches (*.ips *.ups)"));
198	if (!filename.isEmpty()) {
199		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
200		m_controller->loadPatch(filename);
201	}
202}
203
204void Window::openKeymapWindow() {
205	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
206	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
207	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
208	keyEditor->show();
209}
210
211void Window::openSettingsWindow() {
212	SettingsView* settingsWindow = new SettingsView(m_config);
213	connect(this, SIGNAL(shutdown()), settingsWindow, SLOT(close()));
214	connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
215	settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
216	settingsWindow->show();
217}
218
219void Window::openShortcutWindow() {
220	ShortcutView* shortcutView = new ShortcutView();
221	shortcutView->setController(m_shortcutController);
222	connect(this, SIGNAL(shutdown()), shortcutView, SLOT(close()));
223	shortcutView->setAttribute(Qt::WA_DeleteOnClose);
224	shortcutView->show();
225}
226
227void Window::openGamePakWindow() {
228	GamePakView* gamePakWindow = new GamePakView(m_controller);
229	connect(this, SIGNAL(shutdown()), gamePakWindow, SLOT(close()));
230	gamePakWindow->setAttribute(Qt::WA_DeleteOnClose);
231	gamePakWindow->show();
232}
233
234#ifdef BUILD_SDL
235void Window::openGamepadWindow() {
236	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON);
237	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
238	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
239	keyEditor->show();
240}
241#endif
242
243#ifdef USE_FFMPEG
244void Window::openVideoWindow() {
245	if (!m_videoView) {
246		m_videoView = new VideoView();
247		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
248		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
249		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
250		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
251		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
252	}
253	m_videoView->show();
254}
255#endif
256
257#ifdef USE_MAGICK
258void Window::openGIFWindow() {
259	if (!m_gifView) {
260		m_gifView = new GIFView();
261		connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
262		connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
263		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
264		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
265		connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
266	}
267	m_gifView->show();
268}
269#endif
270
271#ifdef USE_GDB_STUB
272void Window::gdbOpen() {
273	if (!m_gdbController) {
274		m_gdbController = new GDBController(m_controller, this);
275	}
276	GDBWindow* window = new GDBWindow(m_gdbController);
277	window->show();
278}
279#endif
280
281void Window::keyPressEvent(QKeyEvent* event) {
282	if (event->isAutoRepeat()) {
283		QWidget::keyPressEvent(event);
284		return;
285	}
286	GBAKey key = m_inputController.mapKeyboard(event->key());
287	if (key == GBA_KEY_NONE) {
288		QWidget::keyPressEvent(event);
289		return;
290	}
291	m_controller->keyPressed(key);
292	event->accept();
293}
294
295void Window::keyReleaseEvent(QKeyEvent* event) {
296	if (event->isAutoRepeat()) {
297		QWidget::keyReleaseEvent(event);
298		return;
299	}
300	GBAKey key = m_inputController.mapKeyboard(event->key());
301	if (key == GBA_KEY_NONE) {
302		QWidget::keyPressEvent(event);
303		return;
304	}
305	m_controller->keyReleased(key);
306	event->accept();
307}
308
309void Window::resizeEvent(QResizeEvent*) {
310	redoLogo();
311	m_config->setOption("height", m_screenWidget->height());
312	m_config->setOption("width", m_screenWidget->width());
313}
314
315void Window::closeEvent(QCloseEvent* event) {
316	emit shutdown();
317	QMainWindow::closeEvent(event);
318}
319
320void Window::focusOutEvent(QFocusEvent*) {
321	m_controller->setTurbo(false, false);
322	m_controller->clearKeys();
323}
324
325void Window::toggleFullScreen() {
326	if (isFullScreen()) {
327		showNormal();
328		menuBar()->show();
329	} else {
330		showFullScreen();
331#ifndef Q_OS_MAC
332		if (m_controller->isLoaded() && !m_controller->isPaused()) {
333			menuBar()->hide();
334		}
335#endif
336	}
337}
338
339void Window::gameStarted(GBAThread* context) {
340	char title[13] = { '\0' };
341	MutexLock(&context->stateMutex);
342	if (context->state < THREAD_EXITING) {
343		emit startDrawing(m_controller->drawContext(), context);
344		GBAGetGameTitle(context->gba, title);
345	} else {
346		MutexUnlock(&context->stateMutex);
347		return;
348	}
349	MutexUnlock(&context->stateMutex);
350	foreach (QAction* action, m_gameActions) {
351		action->setDisabled(false);
352	}
353	appendMRU(context->fname);
354	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
355	attachWidget(m_display);
356	m_screenWidget->setScaledContents(true);
357
358#ifndef Q_OS_MAC
359	if(isFullScreen()) {
360		menuBar()->hide();
361	}
362#endif
363
364	m_fpsTimer.start();
365}
366
367void Window::gameStopped() {
368	foreach (QAction* action, m_gameActions) {
369		action->setDisabled(true);
370	}
371	setWindowTitle(tr(PROJECT_NAME));
372	detachWidget(m_display);
373	m_screenWidget->setScaledContents(false);
374	redoLogo();
375
376	m_fpsTimer.stop();
377}
378
379void Window::gameCrashed(const QString& errorMessage) {
380	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
381		tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
382		QMessageBox::Ok, this,  Qt::Sheet);
383	crash->setAttribute(Qt::WA_DeleteOnClose);
384	crash->show();
385}
386
387void Window::redoLogo() {
388	if (m_controller->isLoaded()) {
389		return;
390	}
391	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
392	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
393	m_screenWidget->setPixmap(logo);
394}
395
396void Window::recordFrame() {
397	m_frameList.append(QDateTime::currentDateTime());
398	while (m_frameList.count() > FRAME_LIST_SIZE) {
399		m_frameList.removeFirst();
400	}
401}
402
403void Window::showFPS() {
404	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
405	float fps = (m_frameList.count() - 1) * 10000.f / interval;
406	fps = round(fps) / 10.f;
407	char title[13] = { '\0' };
408	GBAGetGameTitle(m_controller->thread()->gba, title);
409	setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
410}
411
412void Window::openStateWindow(LoadSave ls) {
413	if (m_stateWindow) {
414		return;
415	}
416	bool wasPaused = m_controller->isPaused();
417	m_stateWindow = new LoadSaveState(m_controller);
418	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
419	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
420	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
421		m_screenWidget->layout()->removeWidget(m_stateWindow);
422		m_stateWindow = nullptr;
423		setFocus();
424	});
425	if (!wasPaused) {
426		m_controller->setPaused(true);
427		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
428	}
429	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
430	m_stateWindow->setMode(ls);
431	attachWidget(m_stateWindow);
432}
433
434void Window::setupMenu(QMenuBar* menubar) {
435	menubar->clear();
436	QMenu* fileMenu = menubar->addMenu(tr("&File"));
437	m_shortcutController->addMenu(fileMenu);
438	installEventFilter(m_shortcutController);
439	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
440	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
441	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
442
443	m_mruMenu = fileMenu->addMenu(tr("Recent"));
444
445	fileMenu->addSeparator();
446
447	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
448	loadState->setShortcut(tr("F10"));
449	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
450	m_gameActions.append(loadState);
451	addControlledAction(fileMenu, loadState, "loadState");
452
453	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
454	saveState->setShortcut(tr("Shift+F10"));
455	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
456	m_gameActions.append(saveState);
457	addControlledAction(fileMenu, saveState, "saveState");
458
459	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
460	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
461	int i;
462	for (i = 1; i < 10; ++i) {
463		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
464		quickLoad->setShortcut(tr("F%1").arg(i));
465		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
466		m_gameActions.append(quickLoad);
467		addAction(quickLoad);
468		quickLoadMenu->addAction(quickLoad);
469
470		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
471		quickSave->setShortcut(tr("Shift+F%1").arg(i));
472		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
473		m_gameActions.append(quickSave);
474		addAction(quickSave);
475		quickSaveMenu->addAction(quickSave);
476	}
477
478#ifndef Q_OS_MAC
479	fileMenu->addSeparator();
480	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
481#endif
482
483	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
484	m_shortcutController->addMenu(emulationMenu);
485	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
486	reset->setShortcut(tr("Ctrl+R"));
487	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
488	m_gameActions.append(reset);
489	addControlledAction(emulationMenu, reset, "reset");
490
491	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
492	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
493	m_gameActions.append(shutdown);
494	addControlledAction(emulationMenu, shutdown, "shutdown");
495	emulationMenu->addSeparator();
496
497	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
498	pause->setChecked(false);
499	pause->setCheckable(true);
500	pause->setShortcut(tr("Ctrl+P"));
501	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
502	connect(m_controller, &GameController::gamePaused, [this, pause]() {
503		pause->setChecked(true);
504
505		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
506		QPixmap pixmap;
507		pixmap.convertFromImage(currentImage.rgbSwapped());
508		m_screenWidget->setPixmap(pixmap);
509	});
510	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
511	m_gameActions.append(pause);
512	addControlledAction(emulationMenu, pause, "pause");
513
514	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
515	frameAdvance->setShortcut(tr("Ctrl+N"));
516	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
517	m_gameActions.append(frameAdvance);
518	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
519
520	emulationMenu->addSeparator();
521
522	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
523	turbo->setCheckable(true);
524	turbo->setChecked(false);
525	turbo->setShortcut(tr("Shift+Tab"));
526	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
527	addControlledAction(emulationMenu, turbo, "fastForward");
528
529	QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
530	rewind->setShortcut(tr("`"));
531	connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
532	m_gameActions.append(rewind);
533	addControlledAction(emulationMenu, rewind, "rewind");
534
535	ConfigOption* videoSync = m_config->addOption("videoSync");
536	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
537	videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
538	m_config->updateOption("videoSync");
539
540	ConfigOption* audioSync = m_config->addOption("audioSync");
541	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
542	audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
543	m_config->updateOption("audioSync");
544
545	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
546	m_shortcutController->addMenu(avMenu);
547	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
548	m_shortcutController->addMenu(frameMenu, avMenu);
549	for (int i = 1; i <= 6; ++i) {
550		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
551		connect(setSize, &QAction::triggered, [this, i]() {
552			showNormal();
553			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
554		});
555		addControlledAction(frameMenu, setSize, tr("frame%1x").arg(QString::number(i)));
556	}
557	addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
558
559	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
560	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
561	lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
562	m_config->updateOption("lockAspectRatio");
563
564	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
565	resampleVideo->addBoolean(tr("Resample video"), avMenu);
566	resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
567	m_config->updateOption("resampleVideo");
568
569	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
570	ConfigOption* skip = m_config->addOption("frameskip");
571	skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
572	for (int i = 0; i <= 10; ++i) {
573		skip->addValue(QString::number(i), i, skipMenu);
574	}
575	m_config->updateOption("frameskip");
576
577	avMenu->addSeparator();
578
579	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
580	ConfigOption* buffers = m_config->addOption("audioBuffers");
581	buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
582	buffers->addValue(tr("512"), 512, buffersMenu);
583	buffers->addValue(tr("768"), 768, buffersMenu);
584	buffers->addValue(tr("1024"), 1024, buffersMenu);
585	buffers->addValue(tr("2048"), 2048, buffersMenu);
586	buffers->addValue(tr("4096"), 4096, buffersMenu);
587	m_config->updateOption("audioBuffers");
588
589	avMenu->addSeparator();
590
591	QMenu* target = avMenu->addMenu("FPS target");
592	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
593	fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
594	fpsTargetOption->addValue(tr("15"), 15, target);
595	fpsTargetOption->addValue(tr("30"), 30, target);
596	fpsTargetOption->addValue(tr("45"), 45, target);
597	fpsTargetOption->addValue(tr("60"), 60, target);
598	fpsTargetOption->addValue(tr("90"), 90, target);
599	fpsTargetOption->addValue(tr("120"), 120, target);
600	fpsTargetOption->addValue(tr("240"), 240, target);
601	m_config->updateOption("fpsTarget");
602
603#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
604	avMenu->addSeparator();
605#endif
606
607#ifdef USE_PNG
608	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
609	screenshot->setShortcut(tr("F12"));
610	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
611	m_gameActions.append(screenshot);
612	addControlledAction(avMenu, screenshot, "screenshot");
613#endif
614
615#ifdef USE_FFMPEG
616	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
617	recordOutput->setShortcut(tr("F11"));
618	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
619	addControlledAction(avMenu, recordOutput, "recordOutput");
620#endif
621
622#ifdef USE_MAGICK
623	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
624	recordGIF->setShortcut(tr("Shift+F11"));
625	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
626	addControlledAction(avMenu, recordGIF, "recordGIF");
627#endif
628
629	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
630	m_shortcutController->addMenu(toolsMenu);
631	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
632	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
633	addControlledAction(toolsMenu, viewLogs, "viewLogs");
634
635	QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
636	connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
637	addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
638
639#ifdef USE_GDB_STUB
640	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
641	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
642	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
643#endif
644
645	toolsMenu->addSeparator();
646	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
647	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
648
649	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
650	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
651	addControlledAction(toolsMenu, keymap, "remapKeyboard");
652
653#ifdef BUILD_SDL
654	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
655	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
656	addControlledAction(toolsMenu, gamepad, "remapGamepad");
657#endif
658
659	ConfigOption* skipBios = m_config->addOption("skipBios");
660	skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
661
662	ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
663	rewindEnable->connect([this](const QVariant& value) { m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
664
665	ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
666	rewindBufferCapacity->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
667
668	ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
669	rewindBufferInterval->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); });
670
671	QMenu* other = new QMenu(tr("Other"), this);
672	m_shortcutController->addMenu(other);
673	m_shortcutController->addFunctions(other, [this]() {
674		m_controller->setTurbo(true, false);
675	}, [this]() {
676		m_controller->setTurbo(false, false);
677	}, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
678
679	foreach (QAction* action, m_gameActions) {
680		action->setDisabled(true);
681	}
682}
683
684void Window::attachWidget(QWidget* widget) {
685	m_screenWidget->layout()->addWidget(widget);
686	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
687}
688
689void Window::detachWidget(QWidget* widget) {
690	m_screenWidget->layout()->removeWidget(widget);
691}
692
693void Window::appendMRU(const QString& fname) {
694	int index = m_mruFiles.indexOf(fname);
695	if (index >= 0) {
696		m_mruFiles.removeAt(index);
697	}
698	m_mruFiles.prepend(fname);
699	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
700		m_mruFiles.removeLast();
701	}
702	updateMRU();
703}
704
705void Window::updateMRU() {
706	if (!m_mruMenu) {
707		return;
708	}
709	m_mruMenu->clear();
710	int i = 0;
711	for (const QString& file : m_mruFiles) {
712		QAction* item = new QAction(file, m_mruMenu);
713		item->setShortcut(QString("Ctrl+%1").arg(i));
714		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
715		m_mruMenu->addAction(item);
716		++i;
717	}
718	m_config->setMRU(m_mruFiles);
719	m_config->write();
720	m_mruMenu->setEnabled(i > 0);
721}
722
723QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
724	m_shortcutController->addAction(menu, action, name);
725	menu->addAction(action);
726	addAction(action);
727	return action;
728}
729
730WindowBackground::WindowBackground(QWidget* parent)
731	: QLabel(parent)
732{
733	setLayout(new QStackedLayout());
734	layout()->setContentsMargins(0, 0, 0, 0);
735	setAlignment(Qt::AlignCenter);
736	QPalette p = palette();
737	p.setColor(backgroundRole(), Qt::black);
738	setPalette(p);
739	setAutoFillBackground(true);
740}
741
742void WindowBackground::setSizeHint(const QSize& hint) {
743	m_sizeHint = hint;
744}
745
746QSize WindowBackground::sizeHint() const {
747	return m_sizeHint;
748}