all repos — mgba @ 52c66de6945adefa72d87cf5c461af4b30fd1d68

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