all repos — mgba @ 38ec77b8038c826313f42149a8cb114dcabeed8e

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 <QKeyEvent>
   9#include <QKeySequence>
  10#include <QMenuBar>
  11#include <QMessageBox>
  12#include <QMimeData>
  13#include <QPainter>
  14#include <QStackedLayout>
  15
  16#include "CheatsView.h"
  17#include "ConfigController.h"
  18#include "Display.h"
  19#include "GameController.h"
  20#include "GBAApp.h"
  21#include "GBAKeyEditor.h"
  22#include "GDBController.h"
  23#include "GDBWindow.h"
  24#include "GIFView.h"
  25#include "LoadSaveState.h"
  26#include "LogView.h"
  27#include "MultiplayerController.h"
  28#include "MemoryView.h"
  29#include "OverrideView.h"
  30#include "PaletteView.h"
  31#include "SensorView.h"
  32#include "SettingsView.h"
  33#include "ShortcutController.h"
  34#include "ShortcutView.h"
  35#include "VideoView.h"
  36
  37extern "C" {
  38#include "platform/commandline.h"
  39}
  40
  41using namespace QGBA;
  42
  43#ifdef __WIN32
  44// This is a macro everywhere except MinGW, it seems
  45using std::isnan;
  46#endif
  47
  48Window::Window(ConfigController* config, int playerId, QWidget* parent)
  49	: QMainWindow(parent)
  50	, m_log(0)
  51	, m_logView(new LogView(&m_log))
  52	, m_stateWindow(nullptr)
  53	, m_screenWidget(new WindowBackground())
  54	, m_logo(":/res/mgba-1024.png")
  55	, m_config(config)
  56	, m_inputController(playerId)
  57#ifdef USE_FFMPEG
  58	, m_videoView(nullptr)
  59#endif
  60#ifdef USE_MAGICK
  61	, m_gifView(nullptr)
  62#endif
  63#ifdef USE_GDB_STUB
  64	, m_gdbController(nullptr)
  65#endif
  66	, m_mruMenu(nullptr)
  67	, m_shortcutController(new ShortcutController(this))
  68	, m_playerId(playerId)
  69{
  70	setFocusPolicy(Qt::StrongFocus);
  71	setAcceptDrops(true);
  72	setAttribute(Qt::WA_DeleteOnClose);
  73	m_controller = new GameController(this);
  74	m_controller->setInputController(&m_inputController);
  75	m_controller->setOverrides(m_config->overrides());
  76	updateTitle();
  77
  78	m_display = Display::create(this);
  79
  80	m_logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
  81	m_logo = m_logo; // Free memory left over in old pixmap
  82
  83	m_screenWidget->setMinimumSize(m_display->minimumSize());
  84	m_screenWidget->setSizePolicy(m_display->sizePolicy());
  85	m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
  86	m_screenWidget->setPixmap(m_logo);
  87	m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
  88	setCentralWidget(m_screenWidget);
  89
  90	QVariant windowPos = m_config->getQtOption("windowPos");
  91	if (!windowPos.isNull()) {
  92		move(windowPos.toPoint());
  93	}
  94
  95	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
  96	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), &m_inputController, SLOT(suspendScreensaver()));
  97	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
  98	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
  99	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), &m_inputController, SLOT(resumeScreensaver()));
 100	connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
 101	connect(m_controller, SIGNAL(rewound(GBAThread*)), m_display, SLOT(forceDraw()));
 102	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
 103#ifndef Q_OS_MAC
 104	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
 105	connect(m_controller, &GameController::gameUnpaused, [this]() {
 106		if(isFullScreen()) {
 107			menuBar()->hide();
 108		}
 109	});
 110#endif
 111	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), &m_inputController, SLOT(resumeScreensaver()));
 112	connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
 113	connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), &m_inputController, SLOT(suspendScreensaver()));
 114	connect(m_controller, SIGNAL(postLog(int, const QString&)), &m_log, SLOT(postLog(int, const QString&)));
 115	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
 116	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), m_display, SLOT(framePosted(const uint32_t*)));
 117	connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
 118	connect(m_controller, SIGNAL(gameFailed()), this, SLOT(gameFailed()));
 119	connect(m_controller, SIGNAL(unimplementedBiosCall(int)), this, SLOT(unimplementedBiosCall(int)));
 120	connect(m_controller, SIGNAL(statusPosted(const QString&)), m_display, SLOT(showMessage(const QString&)));
 121	connect(&m_log, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
 122	connect(&m_log, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
 123	connect(&m_log, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
 124	connect(this, SIGNAL(startDrawing(GBAThread*)), m_display, SLOT(startDrawing(GBAThread*)), Qt::QueuedConnection);
 125	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 126	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 127	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 128	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 129	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 130	connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
 131
 132	m_log.setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS);
 133	m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
 134
 135	m_shortcutController->setConfigController(m_config);
 136	setupMenu(menuBar());
 137}
 138
 139Window::~Window() {
 140	delete m_logView;
 141
 142#ifdef USE_FFMPEG
 143	delete m_videoView;
 144#endif
 145
 146#ifdef USE_MAGICK
 147	delete m_gifView;
 148#endif
 149}
 150
 151void Window::argumentsPassed(GBAArguments* args) {
 152	loadConfig();
 153
 154	if (args->patch) {
 155		m_controller->loadPatch(args->patch);
 156	}
 157
 158	if (args->fname) {
 159		m_controller->loadGame(args->fname, args->dirmode);
 160	}
 161}
 162
 163void Window::resizeFrame(int width, int height) {
 164	QSize newSize(width, height);
 165	newSize -= m_screenWidget->size();
 166	newSize += size();
 167	resize(newSize);
 168}
 169
 170void Window::setConfig(ConfigController* config) {
 171	m_config = config;
 172}
 173
 174void Window::loadConfig() {
 175	const GBAOptions* opts = m_config->options();
 176
 177	m_log.setLevels(opts->logLevel);
 178
 179	m_controller->setOptions(opts);
 180	m_display->lockAspectRatio(opts->lockAspectRatio);
 181	m_display->filter(opts->resampleVideo);
 182
 183	if (opts->bios) {
 184		m_controller->loadBIOS(opts->bios);
 185	}
 186
 187	if (opts->fpsTarget) {
 188		emit fpsTargetChanged(opts->fpsTarget);
 189	}
 190
 191	if (opts->audioBuffers) {
 192		emit audioBufferSamplesChanged(opts->audioBuffers);
 193	}
 194
 195	if (opts->width && opts->height) {
 196		resizeFrame(opts->width, opts->height);
 197	}
 198
 199	if (opts->fullscreen) {
 200		enterFullScreen();
 201	}
 202
 203	m_inputController.setScreensaverSuspendable(opts->suspendScreensaver);
 204
 205	m_mruFiles = m_config->getMRU();
 206	updateMRU();
 207
 208	m_inputController.setConfiguration(m_config);
 209}
 210
 211void Window::saveConfig() {
 212	m_inputController.saveConfiguration();
 213	m_config->write();
 214}
 215
 216void Window::selectROM() {
 217	QStringList formats{
 218		"*.gba",
 219#ifdef USE_LIBZIP
 220		"*.zip",
 221#endif
 222#ifdef USE_LZMA
 223		"*.7z",
 224#endif
 225		"*.rom",
 226		"*.bin"};
 227	QString filter = tr("Game Boy Advance ROMs (%1)").arg(formats.join(QChar(' ')));
 228	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), filter);
 229	if (!filename.isEmpty()) {
 230		m_controller->loadGame(filename);
 231	}
 232}
 233
 234void Window::replaceROM() {
 235	QStringList formats{
 236		"*.gba",
 237#ifdef USE_LIBZIP
 238		"*.zip",
 239#endif
 240#ifdef USE_LZMA
 241		"*.7z",
 242#endif
 243		"*.rom",
 244		"*.bin"};
 245	QString filter = tr("Game Boy Advance ROMs (%1)").arg(formats.join(QChar(' ')));
 246	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), filter);
 247	if (!filename.isEmpty()) {
 248		m_controller->replaceGame(filename);
 249	}
 250}
 251
 252void Window::selectBIOS() {
 253	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
 254	if (!filename.isEmpty()) {
 255		m_config->setOption("bios", filename);
 256		m_config->updateOption("bios");
 257		m_config->setOption("useBios", true);
 258		m_config->updateOption("useBios");
 259		m_controller->loadBIOS(filename);
 260	}
 261}
 262
 263void Window::selectPatch() {
 264	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select patch"), tr("Patches (*.ips *.ups *.bps)"));
 265	if (!filename.isEmpty()) {
 266		m_controller->loadPatch(filename);
 267	}
 268}
 269
 270void Window::openView(QWidget* widget) {
 271	connect(this, SIGNAL(shutdown()), widget, SLOT(close()));
 272	widget->setAttribute(Qt::WA_DeleteOnClose);
 273	widget->show();
 274}
 275
 276void Window::importSharkport() {
 277	QString filename = GBAApp::app()->getOpenFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
 278	if (!filename.isEmpty()) {
 279		m_controller->importSharkport(filename);
 280	}
 281}
 282
 283void Window::exportSharkport() {
 284	QString filename = GBAApp::app()->getSaveFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
 285	if (!filename.isEmpty()) {
 286		m_controller->exportSharkport(filename);
 287	}
 288}
 289
 290void Window::openKeymapWindow() {
 291	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
 292	openView(keyEditor);
 293}
 294
 295void Window::openSettingsWindow() {
 296	SettingsView* settingsWindow = new SettingsView(m_config);
 297	connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
 298	connect(settingsWindow, SIGNAL(audioDriverChanged()), m_controller, SLOT(reloadAudioDriver()));
 299	openView(settingsWindow);
 300}
 301
 302void Window::openShortcutWindow() {
 303#ifdef BUILD_SDL
 304	m_inputController.recalibrateAxes();
 305#endif
 306	ShortcutView* shortcutView = new ShortcutView();
 307	shortcutView->setController(m_shortcutController);
 308	openView(shortcutView);
 309}
 310
 311void Window::openOverrideWindow() {
 312	OverrideView* overrideWindow = new OverrideView(m_controller, m_config);
 313	openView(overrideWindow);
 314}
 315
 316void Window::openSensorWindow() {
 317	SensorView* sensorWindow = new SensorView(m_controller, &m_inputController);
 318	openView(sensorWindow);
 319}
 320
 321void Window::openCheatsWindow() {
 322	CheatsView* cheatsWindow = new CheatsView(m_controller);
 323	openView(cheatsWindow);
 324}
 325
 326void Window::openPaletteWindow() {
 327	PaletteView* paletteWindow = new PaletteView(m_controller);
 328	openView(paletteWindow);
 329}
 330
 331void Window::openMemoryWindow() {
 332	MemoryView* memoryWindow = new MemoryView(m_controller);
 333	openView(memoryWindow);
 334}
 335
 336#ifdef BUILD_SDL
 337void Window::openGamepadWindow() {
 338	const char* profile = m_inputController.profileForType(SDL_BINDING_BUTTON);
 339	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON, profile);
 340	openView(keyEditor);
 341}
 342#endif
 343
 344#ifdef USE_FFMPEG
 345void Window::openVideoWindow() {
 346	if (!m_videoView) {
 347		m_videoView = new VideoView();
 348		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
 349		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
 350		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
 351		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
 352		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
 353	}
 354	m_videoView->show();
 355}
 356#endif
 357
 358#ifdef USE_MAGICK
 359void Window::openGIFWindow() {
 360	if (!m_gifView) {
 361		m_gifView = new GIFView();
 362		connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
 363		connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
 364		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
 365		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
 366		connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
 367	}
 368	m_gifView->show();
 369}
 370#endif
 371
 372#ifdef USE_GDB_STUB
 373void Window::gdbOpen() {
 374	if (!m_gdbController) {
 375		m_gdbController = new GDBController(m_controller, this);
 376	}
 377	GDBWindow* window = new GDBWindow(m_gdbController);
 378	connect(this, SIGNAL(shutdown()), window, SLOT(close()));
 379	window->setAttribute(Qt::WA_DeleteOnClose);
 380	window->show();
 381}
 382#endif
 383
 384void Window::keyPressEvent(QKeyEvent* event) {
 385	if (event->isAutoRepeat()) {
 386		QWidget::keyPressEvent(event);
 387		return;
 388	}
 389	GBAKey key = m_inputController.mapKeyboard(event->key());
 390	if (key == GBA_KEY_NONE) {
 391		QWidget::keyPressEvent(event);
 392		return;
 393	}
 394	m_controller->keyPressed(key);
 395	event->accept();
 396}
 397
 398void Window::keyReleaseEvent(QKeyEvent* event) {
 399	if (event->isAutoRepeat()) {
 400		QWidget::keyReleaseEvent(event);
 401		return;
 402	}
 403	GBAKey key = m_inputController.mapKeyboard(event->key());
 404	if (key == GBA_KEY_NONE) {
 405		QWidget::keyPressEvent(event);
 406		return;
 407	}
 408	m_controller->keyReleased(key);
 409	event->accept();
 410}
 411
 412void Window::resizeEvent(QResizeEvent*) {
 413	if (!isFullScreen()) {
 414		m_config->setOption("height", m_screenWidget->height());
 415		m_config->setOption("width", m_screenWidget->width());
 416	}
 417	m_config->setOption("fullscreen", isFullScreen());
 418}
 419
 420void Window::closeEvent(QCloseEvent* event) {
 421	emit shutdown();
 422	m_config->setQtOption("windowPos", pos());
 423	saveConfig();
 424	QMainWindow::closeEvent(event);
 425}
 426
 427void Window::focusOutEvent(QFocusEvent*) {
 428	m_controller->setTurbo(false, false);
 429	m_controller->stopRewinding();
 430	m_controller->clearKeys();
 431}
 432
 433void Window::dragEnterEvent(QDragEnterEvent* event) {
 434	if (event->mimeData()->hasFormat("text/uri-list")) {
 435		event->acceptProposedAction();
 436	}
 437}
 438
 439void Window::dropEvent(QDropEvent* event) {
 440	QString uris = event->mimeData()->data("text/uri-list");
 441	uris = uris.trimmed();
 442	if (uris.contains("\n")) {
 443		// Only one file please
 444		return;
 445	}
 446	QUrl url(uris);
 447	if (!url.isLocalFile()) {
 448		// No remote loading
 449		return;
 450	}
 451	event->accept();
 452	m_controller->loadGame(url.path());
 453}
 454
 455void Window::mouseDoubleClickEvent(QMouseEvent* event) {
 456	if (event->button() != Qt::LeftButton) {
 457		return;
 458	}
 459	toggleFullScreen();
 460}
 461
 462void Window::enterFullScreen() {
 463	if (isFullScreen()) {
 464		return;
 465	}
 466	showFullScreen();
 467	setCursor(Qt::BlankCursor);
 468#ifndef Q_OS_MAC
 469	if (m_controller->isLoaded() && !m_controller->isPaused()) {
 470		menuBar()->hide();
 471	}
 472#endif
 473}
 474
 475void Window::exitFullScreen() {
 476	if (!isFullScreen()) {
 477		return;
 478	}
 479	unsetCursor();
 480	menuBar()->show();
 481	showNormal();
 482}
 483
 484void Window::toggleFullScreen() {
 485	if (isFullScreen()) {
 486		exitFullScreen();
 487	} else {
 488		enterFullScreen();
 489	}
 490}
 491
 492void Window::gameStarted(GBAThread* context) {
 493	char title[13] = { '\0' };
 494	MutexLock(&context->stateMutex);
 495	if (context->state < THREAD_EXITING) {
 496		emit startDrawing(context);
 497		GBAGetGameTitle(context->gba, title);
 498	} else {
 499		MutexUnlock(&context->stateMutex);
 500		return;
 501	}
 502	MutexUnlock(&context->stateMutex);
 503	foreach (QAction* action, m_gameActions) {
 504		action->setDisabled(false);
 505	}
 506	if (context->fname) {
 507		appendMRU(context->fname);
 508	}
 509	updateTitle();
 510	attachWidget(m_display);
 511
 512#ifndef Q_OS_MAC
 513	if (isFullScreen()) {
 514		menuBar()->hide();
 515	}
 516#endif
 517
 518	m_hitUnimplementedBiosCall = false;
 519	m_fpsTimer.start();
 520}
 521
 522void Window::gameStopped() {
 523	foreach (QAction* action, m_gameActions) {
 524		action->setDisabled(true);
 525	}
 526	updateTitle();
 527	detachWidget(m_display);
 528	m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
 529	m_screenWidget->setPixmap(m_logo);
 530
 531	m_fpsTimer.stop();
 532}
 533
 534void Window::gameCrashed(const QString& errorMessage) {
 535	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
 536	                                     tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
 537	                                     QMessageBox::Ok, this, Qt::Sheet);
 538	crash->setAttribute(Qt::WA_DeleteOnClose);
 539	crash->show();
 540}
 541
 542void Window::gameFailed() {
 543	QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
 544	                                    tr("Could not load game. Are you sure it's in the correct format?"),
 545	                                    QMessageBox::Ok, this, Qt::Sheet);
 546	fail->setAttribute(Qt::WA_DeleteOnClose);
 547	fail->show();
 548}
 549
 550void Window::unimplementedBiosCall(int call) {
 551	if (m_hitUnimplementedBiosCall) {
 552		return;
 553	}
 554	m_hitUnimplementedBiosCall = true;
 555
 556	QMessageBox* fail = new QMessageBox(
 557	    QMessageBox::Warning, tr("Unimplemented BIOS call"),
 558	    tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
 559	    QMessageBox::Ok, this, Qt::Sheet);
 560	fail->setAttribute(Qt::WA_DeleteOnClose);
 561	fail->show();
 562}
 563
 564void Window::recordFrame() {
 565	m_frameList.append(QDateTime::currentDateTime());
 566	while (m_frameList.count() > FRAME_LIST_SIZE) {
 567		m_frameList.removeFirst();
 568	}
 569}
 570
 571void Window::showFPS() {
 572	if (m_frameList.isEmpty()) {
 573		updateTitle();
 574		return;
 575	}
 576	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
 577	float fps = (m_frameList.count() - 1) * 10000.f / interval;
 578	fps = round(fps) / 10.f;
 579	updateTitle(fps);
 580}
 581
 582void Window::updateTitle(float fps) {
 583	QString title;
 584
 585	m_controller->threadInterrupt();
 586	if (m_controller->isLoaded()) {
 587		char gameTitle[13] = { '\0' };
 588		GBAGetGameTitle(m_controller->thread()->gba, gameTitle);
 589
 590		title = (gameTitle);
 591	}
 592	MultiplayerController* multiplayer = m_controller->multiplayerController();
 593	if (multiplayer && multiplayer->attached() > 1) {
 594		title += tr(" -  Player %1 of %2").arg(m_playerId + 1).arg(multiplayer->attached());
 595	}
 596	m_controller->threadContinue();
 597	if (title.isNull()) {
 598		setWindowTitle(tr("%1 - %2").arg(projectName).arg(projectVersion));
 599	} else if (isnan(fps)) {
 600		setWindowTitle(tr("%1 - %2 - %3").arg(projectName).arg(title).arg(projectVersion));
 601	} else {
 602		setWindowTitle(tr("%1 - %2 (%3 fps) - %4").arg(projectName).arg(title).arg(fps).arg(projectVersion));
 603	}
 604}
 605
 606void Window::openStateWindow(LoadSave ls) {
 607	if (m_stateWindow) {
 608		return;
 609	}
 610	bool wasPaused = m_controller->isPaused();
 611	m_stateWindow = new LoadSaveState(m_controller);
 612	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
 613	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
 614	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
 615		m_screenWidget->layout()->removeWidget(m_stateWindow);
 616		m_stateWindow = nullptr;
 617		QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
 618	});
 619	if (!wasPaused) {
 620		m_controller->setPaused(true);
 621		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
 622	}
 623	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
 624	m_stateWindow->setMode(ls);
 625	attachWidget(m_stateWindow);
 626}
 627
 628void Window::setupMenu(QMenuBar* menubar) {
 629	menubar->clear();
 630	QMenu* fileMenu = menubar->addMenu(tr("&File"));
 631	m_shortcutController->addMenu(fileMenu);
 632	installEventFilter(m_shortcutController);
 633	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open),
 634	                    "loadROM");
 635	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
 636	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
 637	addControlledAction(fileMenu, fileMenu->addAction(tr("Boot BIOS"), m_controller, SLOT(bootBIOS())), "bootBIOS");
 638
 639	addControlledAction(fileMenu, fileMenu->addAction(tr("Replace ROM..."), this, SLOT(replaceROM())), "replaceROM");
 640
 641	m_mruMenu = fileMenu->addMenu(tr("Recent"));
 642
 643	fileMenu->addSeparator();
 644
 645	addControlledAction(fileMenu, fileMenu->addAction(tr("Make portable"), m_config, SLOT(makePortable())), "makePortable");
 646
 647	fileMenu->addSeparator();
 648
 649	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
 650	loadState->setShortcut(tr("F10"));
 651	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
 652	m_gameActions.append(loadState);
 653	addControlledAction(fileMenu, loadState, "loadState");
 654
 655	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
 656	saveState->setShortcut(tr("Shift+F10"));
 657	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
 658	m_gameActions.append(saveState);
 659	addControlledAction(fileMenu, saveState, "saveState");
 660
 661	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
 662	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
 663	m_shortcutController->addMenu(quickLoadMenu);
 664	m_shortcutController->addMenu(quickSaveMenu);
 665
 666	QAction* quickLoad = new QAction(tr("Load recent"), quickLoadMenu);
 667	connect(quickLoad, SIGNAL(triggered()), m_controller, SLOT(loadState()));
 668	m_gameActions.append(quickLoad);
 669	addControlledAction(quickLoadMenu, quickLoad, "quickLoad");
 670
 671	QAction* quickSave = new QAction(tr("Save recent"), quickSaveMenu);
 672	connect(quickSave, SIGNAL(triggered()), m_controller, SLOT(saveState()));
 673	m_gameActions.append(quickSave);
 674	addControlledAction(quickSaveMenu, quickSave, "quickSave");
 675
 676	quickLoadMenu->addSeparator();
 677	quickSaveMenu->addSeparator();
 678
 679	int i;
 680	for (i = 1; i < 10; ++i) {
 681		quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
 682		quickLoad->setShortcut(tr("F%1").arg(i));
 683		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
 684		m_gameActions.append(quickLoad);
 685		addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
 686
 687		quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
 688		quickSave->setShortcut(tr("Shift+F%1").arg(i));
 689		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
 690		m_gameActions.append(quickSave);
 691		addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
 692	}
 693
 694	fileMenu->addSeparator();
 695	QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
 696	connect(importShark, SIGNAL(triggered()), this, SLOT(importSharkport()));
 697	m_gameActions.append(importShark);
 698	addControlledAction(fileMenu, importShark, "importShark");
 699
 700	QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
 701	connect(exportShark, SIGNAL(triggered()), this, SLOT(exportSharkport()));
 702	m_gameActions.append(exportShark);
 703	addControlledAction(fileMenu, exportShark, "exportShark");
 704
 705	fileMenu->addSeparator();
 706	QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
 707	connect(multiWindow, &QAction::triggered, [this]() {
 708		GBAApp::app()->newWindow();
 709	});
 710	addControlledAction(fileMenu, multiWindow, "multiWindow");
 711
 712#ifndef Q_OS_MAC
 713	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
 714#endif
 715
 716	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
 717	m_shortcutController->addMenu(emulationMenu);
 718	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
 719	reset->setShortcut(tr("Ctrl+R"));
 720	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
 721	m_gameActions.append(reset);
 722	addControlledAction(emulationMenu, reset, "reset");
 723
 724	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
 725	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
 726	m_gameActions.append(shutdown);
 727	addControlledAction(emulationMenu, shutdown, "shutdown");
 728
 729	QAction* yank = new QAction(tr("Yank game pak"), emulationMenu);
 730	connect(yank, SIGNAL(triggered()), m_controller, SLOT(yankPak()));
 731	m_gameActions.append(yank);
 732	addControlledAction(emulationMenu, yank, "yank");
 733	emulationMenu->addSeparator();
 734
 735	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
 736	pause->setChecked(false);
 737	pause->setCheckable(true);
 738	pause->setShortcut(tr("Ctrl+P"));
 739	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
 740	connect(m_controller, &GameController::gamePaused, [this, pause]() {
 741		pause->setChecked(true);
 742
 743		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS,
 744		                    VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
 745		QPixmap pixmap;
 746		pixmap.convertFromImage(currentImage.rgbSwapped());
 747		m_screenWidget->setPixmap(pixmap);
 748		m_screenWidget->setLockAspectRatio(3, 2);
 749	});
 750	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
 751	m_gameActions.append(pause);
 752	addControlledAction(emulationMenu, pause, "pause");
 753
 754	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
 755	frameAdvance->setShortcut(tr("Ctrl+N"));
 756	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
 757	m_gameActions.append(frameAdvance);
 758	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
 759
 760	emulationMenu->addSeparator();
 761
 762	m_shortcutController->addFunctions(emulationMenu, [this]() {
 763		m_controller->setTurbo(true, false);
 764	}, [this]() {
 765		m_controller->setTurbo(false, false);
 766	}, QKeySequence(Qt::Key_Tab), tr("Fast forward (held)"), "holdFastForward");
 767
 768	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
 769	turbo->setCheckable(true);
 770	turbo->setChecked(false);
 771	turbo->setShortcut(tr("Shift+Tab"));
 772	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
 773	addControlledAction(emulationMenu, turbo, "fastForward");
 774
 775	QMenu* ffspeedMenu = emulationMenu->addMenu(tr("Fast forward speed"));
 776	ConfigOption* ffspeed = m_config->addOption("fastForwardRatio");
 777	ffspeed->connect([this](const QVariant& value) {
 778		m_controller->setTurboSpeed(value.toFloat());
 779	}, this);
 780	ffspeed->addValue(tr("Unbounded"), -1.0f, ffspeedMenu);
 781	ffspeed->setValue(QVariant(-1.0f));
 782	ffspeedMenu->addSeparator();
 783	for (i = 2; i < 11; ++i) {
 784		ffspeed->addValue(tr("%0x").arg(i), i, ffspeedMenu);
 785	}
 786	m_config->updateOption("fastForwardRatio");
 787
 788	m_shortcutController->addFunctions(emulationMenu, [this]() {
 789		m_controller->startRewinding();
 790	}, [this]() {
 791		m_controller->stopRewinding();
 792	}, QKeySequence("~"), tr("Rewind (held)"), "holdRewind");
 793
 794	QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
 795	rewind->setShortcut(tr("`"));
 796	connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
 797	m_gameActions.append(rewind);
 798	addControlledAction(emulationMenu, rewind, "rewind");
 799
 800	QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
 801	frameRewind->setShortcut(tr("Ctrl+B"));
 802	connect(frameRewind, &QAction::triggered, [this] () {
 803		m_controller->rewind(1);
 804	});
 805	m_gameActions.append(frameRewind);
 806	addControlledAction(emulationMenu, frameRewind, "frameRewind");
 807
 808	ConfigOption* videoSync = m_config->addOption("videoSync");
 809	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
 810	videoSync->connect([this](const QVariant& value) {
 811		m_controller->setVideoSync(value.toBool());
 812	}, this);
 813	m_config->updateOption("videoSync");
 814
 815	ConfigOption* audioSync = m_config->addOption("audioSync");
 816	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
 817	audioSync->connect([this](const QVariant& value) {
 818		m_controller->setAudioSync(value.toBool());
 819	}, this);
 820	m_config->updateOption("audioSync");
 821
 822	emulationMenu->addSeparator();
 823
 824	QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
 825	m_shortcutController->addMenu(solarMenu);
 826	QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
 827	connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
 828	addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
 829
 830	QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
 831	connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
 832	addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
 833
 834	QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
 835	connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
 836	addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
 837
 838	QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
 839	connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
 840	addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
 841
 842	solarMenu->addSeparator();
 843	for (int i = 0; i <= 10; ++i) {
 844		QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
 845		connect(setSolar, &QAction::triggered, [this, i]() {
 846			m_controller->setLuminanceLevel(i);
 847		});
 848		addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
 849	}
 850
 851	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
 852	m_shortcutController->addMenu(avMenu);
 853	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
 854	m_shortcutController->addMenu(frameMenu, avMenu);
 855	for (int i = 1; i <= 6; ++i) {
 856		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
 857		connect(setSize, &QAction::triggered, [this, i]() {
 858			showNormal();
 859			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
 860		});
 861		addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
 862	}
 863	QKeySequence fullscreenKeys;
 864#ifdef Q_OS_WIN
 865	fullscreenKeys = QKeySequence("Alt+Return");
 866#else
 867	fullscreenKeys = QKeySequence("Ctrl+F");
 868#endif
 869	addControlledAction(frameMenu, frameMenu->addAction(tr("Toggle fullscreen"), this, SLOT(toggleFullScreen()), fullscreenKeys), "fullscreen");
 870
 871	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
 872	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
 873	lockAspectRatio->connect([this](const QVariant& value) {
 874		m_display->lockAspectRatio(value.toBool());
 875	}, this);
 876	m_config->updateOption("lockAspectRatio");
 877
 878	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
 879	resampleVideo->addBoolean(tr("Resample video"), avMenu);
 880	resampleVideo->connect([this](const QVariant& value) {
 881		m_display->filter(value.toBool());
 882	}, this);
 883	m_config->updateOption("resampleVideo");
 884
 885	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
 886	ConfigOption* skip = m_config->addOption("frameskip");
 887	skip->connect([this](const QVariant& value) {
 888		m_controller->setFrameskip(value.toInt());
 889	}, this);
 890	for (int i = 0; i <= 10; ++i) {
 891		skip->addValue(QString::number(i), i, skipMenu);
 892	}
 893	m_config->updateOption("frameskip");
 894
 895	avMenu->addSeparator();
 896
 897	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
 898	ConfigOption* buffers = m_config->addOption("audioBuffers");
 899	buffers->connect([this](const QVariant& value) {
 900		emit audioBufferSamplesChanged(value.toInt());
 901	}, this);
 902	buffers->addValue(tr("512"), 512, buffersMenu);
 903	buffers->addValue(tr("768"), 768, buffersMenu);
 904	buffers->addValue(tr("1024"), 1024, buffersMenu);
 905	buffers->addValue(tr("2048"), 2048, buffersMenu);
 906	buffers->addValue(tr("4096"), 4096, buffersMenu);
 907	m_config->updateOption("audioBuffers");
 908
 909	avMenu->addSeparator();
 910
 911	QMenu* target = avMenu->addMenu(tr("FPS target"));
 912	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
 913	fpsTargetOption->connect([this](const QVariant& value) {
 914		emit fpsTargetChanged(value.toFloat());
 915	}, this);
 916	fpsTargetOption->addValue(tr("15"), 15, target);
 917	fpsTargetOption->addValue(tr("30"), 30, target);
 918	fpsTargetOption->addValue(tr("45"), 45, target);
 919	fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
 920	fpsTargetOption->addValue(tr("60"), 60, target);
 921	fpsTargetOption->addValue(tr("90"), 90, target);
 922	fpsTargetOption->addValue(tr("120"), 120, target);
 923	fpsTargetOption->addValue(tr("240"), 240, target);
 924	m_config->updateOption("fpsTarget");
 925
 926#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
 927	avMenu->addSeparator();
 928#endif
 929
 930#ifdef USE_PNG
 931	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
 932	screenshot->setShortcut(tr("F12"));
 933	connect(screenshot, SIGNAL(triggered()), m_controller, SLOT(screenshot()));
 934	m_gameActions.append(screenshot);
 935	addControlledAction(avMenu, screenshot, "screenshot");
 936#endif
 937
 938#ifdef USE_FFMPEG
 939	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
 940	recordOutput->setShortcut(tr("F11"));
 941	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
 942	addControlledAction(avMenu, recordOutput, "recordOutput");
 943#endif
 944
 945#ifdef USE_MAGICK
 946	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
 947	recordGIF->setShortcut(tr("Shift+F11"));
 948	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
 949	addControlledAction(avMenu, recordGIF, "recordGIF");
 950#endif
 951
 952	avMenu->addSeparator();
 953	QMenu* videoLayers = avMenu->addMenu(tr("Video layers"));
 954
 955	for (int i = 0; i < 4; ++i) {
 956		QAction* enableBg = new QAction(tr("Background %0").arg(i), videoLayers);
 957		enableBg->setCheckable(true);
 958		enableBg->setChecked(true);
 959		connect(enableBg, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->video.renderer->disableBG[i] = !enable; });
 960		m_gameActions.append(enableBg);
 961		addControlledAction(videoLayers, enableBg, QString("enableBG%0").arg(i));
 962	}
 963
 964	QAction* enableObj = new QAction(tr("OBJ (sprites)"), videoLayers);
 965	enableObj->setCheckable(true);
 966	enableObj->setChecked(true);
 967	connect(enableObj, &QAction::triggered, [this](bool enable) { m_controller->thread()->gba->video.renderer->disableOBJ = !enable; });
 968	m_gameActions.append(enableObj);
 969	addControlledAction(videoLayers, enableObj, "enableOBJ");
 970
 971	QMenu* audioChannels = avMenu->addMenu(tr("Audio channels"));
 972
 973	for (int i = 0; i < 4; ++i) {
 974		QAction* enableCh = new QAction(tr("Channel %0").arg(i + 1), audioChannels);
 975		enableCh->setCheckable(true);
 976		enableCh->setChecked(true);
 977		connect(enableCh, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableCh[i] = !enable; });
 978		m_gameActions.append(enableCh);
 979		addControlledAction(audioChannels, enableCh, QString("enableCh%0").arg(i + 1));
 980	}
 981
 982	QAction* enableChA = new QAction(tr("Channel A"), audioChannels);
 983	enableChA->setCheckable(true);
 984	enableChA->setChecked(true);
 985	connect(enableChA, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChA = !enable; });
 986	m_gameActions.append(enableChA);
 987	addControlledAction(audioChannels, enableChA, QString("enableChA"));
 988
 989	QAction* enableChB = new QAction(tr("Channel B"), audioChannels);
 990	enableChB->setCheckable(true);
 991	enableChB->setChecked(true);
 992	connect(enableChB, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChB = !enable; });
 993	m_gameActions.append(enableChB);
 994	addControlledAction(audioChannels, enableChB, QString("enableChB"));
 995
 996	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
 997	m_shortcutController->addMenu(toolsMenu);
 998	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
 999	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
1000	addControlledAction(toolsMenu, viewLogs, "viewLogs");
1001
1002	QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
1003	connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
1004	addControlledAction(toolsMenu, overrides, "overrideWindow");
1005
1006	QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
1007	connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
1008	addControlledAction(toolsMenu, sensors, "sensorWindow");
1009
1010	QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
1011	connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
1012	addControlledAction(toolsMenu, cheats, "cheatsWindow");
1013
1014#ifdef USE_GDB_STUB
1015	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
1016	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
1017	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
1018#endif
1019
1020	toolsMenu->addSeparator();
1021	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())),
1022	                    "settings");
1023	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())),
1024	                    "shortcuts");
1025
1026	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
1027	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
1028	addControlledAction(toolsMenu, keymap, "remapKeyboard");
1029
1030#ifdef BUILD_SDL
1031	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
1032	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
1033	addControlledAction(toolsMenu, gamepad, "remapGamepad");
1034#endif
1035
1036	toolsMenu->addSeparator();
1037
1038	QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
1039	connect(paletteView, SIGNAL(triggered()), this, SLOT(openPaletteWindow()));
1040	m_gameActions.append(paletteView);
1041	addControlledAction(toolsMenu, paletteView, "paletteWindow");
1042
1043	QAction* memoryView = new QAction(tr("View memory..."), toolsMenu);
1044	connect(memoryView, SIGNAL(triggered()), this, SLOT(openMemoryWindow()));
1045	m_gameActions.append(memoryView);
1046	addControlledAction(toolsMenu, memoryView, "memoryView");
1047
1048	ConfigOption* skipBios = m_config->addOption("skipBios");
1049	skipBios->connect([this](const QVariant& value) {
1050		m_controller->setSkipBIOS(value.toBool());
1051	}, this);
1052
1053	ConfigOption* volume = m_config->addOption("volume");
1054	volume->connect([this](const QVariant& value) {
1055		m_controller->setVolume(value.toInt());
1056	}, this);
1057
1058	ConfigOption* mute = m_config->addOption("mute");
1059	mute->connect([this](const QVariant& value) {
1060		m_controller->setMute(value.toBool());
1061	}, this);
1062
1063	ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
1064	rewindEnable->connect([this](const QVariant& value) {
1065		m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt());
1066	}, this);
1067
1068	ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
1069	rewindBufferCapacity->connect([this](const QVariant& value) {
1070		m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt());
1071	}, this);
1072
1073	ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
1074	rewindBufferInterval->connect([this](const QVariant& value) {
1075		m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt());
1076	}, this);
1077
1078	ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
1079	allowOpposingDirections->connect([this](const QVariant& value) {
1080		m_inputController.setAllowOpposing(value.toBool());
1081	}, this);
1082
1083	QAction* exitFullScreen = new QAction(tr("Exit fullscreen"), frameMenu);
1084	connect(exitFullScreen, SIGNAL(triggered()), this, SLOT(exitFullScreen()));
1085	exitFullScreen->setShortcut(QKeySequence("Esc"));
1086	addHiddenAction(frameMenu, exitFullScreen, "exitFullScreen");
1087
1088	foreach (QAction* action, m_gameActions) {
1089		action->setDisabled(true);
1090	}
1091}
1092
1093void Window::attachWidget(QWidget* widget) {
1094	m_screenWidget->layout()->addWidget(widget);
1095	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1096}
1097
1098void Window::detachWidget(QWidget* widget) {
1099	m_screenWidget->layout()->removeWidget(widget);
1100}
1101
1102void Window::appendMRU(const QString& fname) {
1103	int index = m_mruFiles.indexOf(fname);
1104	if (index >= 0) {
1105		m_mruFiles.removeAt(index);
1106	}
1107	m_mruFiles.prepend(fname);
1108	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1109		m_mruFiles.removeLast();
1110	}
1111	updateMRU();
1112}
1113
1114void Window::updateMRU() {
1115	if (!m_mruMenu) {
1116		return;
1117	}
1118	m_mruMenu->clear();
1119	int i = 0;
1120	for (const QString& file : m_mruFiles) {
1121		QAction* item = new QAction(file, m_mruMenu);
1122		item->setShortcut(QString("Ctrl+%1").arg(i));
1123		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
1124		m_mruMenu->addAction(item);
1125		++i;
1126	}
1127	m_config->setMRU(m_mruFiles);
1128	m_config->write();
1129	m_mruMenu->setEnabled(i > 0);
1130}
1131
1132QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1133	addHiddenAction(menu, action, name);
1134	menu->addAction(action);
1135	return action;
1136}
1137
1138QAction* Window::addHiddenAction(QMenu* menu, QAction* action, const QString& name) {
1139	m_shortcutController->addAction(menu, action, name);
1140	action->setShortcutContext(Qt::WidgetShortcut);
1141	addAction(action);
1142	return action;
1143}
1144
1145WindowBackground::WindowBackground(QWidget* parent)
1146	: QLabel(parent)
1147{
1148	setLayout(new QStackedLayout());
1149	layout()->setContentsMargins(0, 0, 0, 0);
1150	setAlignment(Qt::AlignCenter);
1151}
1152
1153void WindowBackground::setSizeHint(const QSize& hint) {
1154	m_sizeHint = hint;
1155}
1156
1157QSize WindowBackground::sizeHint() const {
1158	return m_sizeHint;
1159}
1160
1161void WindowBackground::setLockAspectRatio(int width, int height) {
1162	m_aspectWidth = width;
1163	m_aspectHeight = height;
1164}
1165
1166void WindowBackground::paintEvent(QPaintEvent*) {
1167	const QPixmap* logo = pixmap();
1168	if (!logo) {
1169		return;
1170	}
1171	QPainter painter(this);
1172	painter.setRenderHint(QPainter::SmoothPixmapTransform);
1173	painter.fillRect(QRect(QPoint(), size()), Qt::black);
1174	QSize s = size();
1175	QSize ds = s;
1176	if (s.width() * m_aspectHeight > s.height() * m_aspectWidth) {
1177		ds.setWidth(s.height() * m_aspectWidth / m_aspectHeight);
1178	} else if (s.width() * m_aspectHeight < s.height() * m_aspectWidth) {
1179		ds.setHeight(s.width() * m_aspectHeight / m_aspectWidth);
1180	}
1181	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1182	QRect full(origin, ds);
1183	painter.drawPixmap(full, *logo);
1184}