all repos — mgba @ 908e61f4153702bce5a28616229f848168203b84

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(
 556	    QMessageBox::Warning, tr("Unimplemented BIOS call"),
 557	    tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
 558	    QMessageBox::Ok, this, Qt::Sheet);
 559	fail->setAttribute(Qt::WA_DeleteOnClose);
 560	fail->show();
 561}
 562
 563void Window::recordFrame() {
 564	m_frameList.append(QDateTime::currentDateTime());
 565	while (m_frameList.count() > FRAME_LIST_SIZE) {
 566		m_frameList.removeFirst();
 567	}
 568}
 569
 570void Window::showFPS() {
 571	if (m_frameList.isEmpty()) {
 572		updateTitle();
 573		return;
 574	}
 575	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
 576	float fps = (m_frameList.count() - 1) * 10000.f / interval;
 577	fps = round(fps) / 10.f;
 578	updateTitle(fps);
 579}
 580
 581void Window::updateTitle(float fps) {
 582	QString title;
 583
 584	m_controller->threadInterrupt();
 585	if (m_controller->isLoaded()) {
 586		char gameTitle[13] = { '\0' };
 587		GBAGetGameTitle(m_controller->thread()->gba, gameTitle);
 588
 589		title = (gameTitle);
 590	}
 591	MultiplayerController* multiplayer = m_controller->multiplayerController();
 592	if (multiplayer && multiplayer->attached() > 1) {
 593		title += tr(" -  Player %1 of %2").arg(m_playerId + 1).arg(multiplayer->attached());
 594	}
 595	m_controller->threadContinue();
 596	if (title.isNull()) {
 597		setWindowTitle(tr("%1 - %2").arg(projectName).arg(projectVersion));
 598	} else if (isnan(fps)) {
 599		setWindowTitle(tr("%1 - %2 - %3").arg(projectName).arg(title).arg(projectVersion));
 600	} else {
 601		setWindowTitle(tr("%1 - %2 (%3 fps) - %4").arg(projectName).arg(title).arg(fps).arg(projectVersion));
 602	}
 603}
 604
 605void Window::openStateWindow(LoadSave ls) {
 606	if (m_stateWindow) {
 607		return;
 608	}
 609	bool wasPaused = m_controller->isPaused();
 610	m_stateWindow = new LoadSaveState(m_controller);
 611	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
 612	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
 613	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
 614		m_screenWidget->layout()->removeWidget(m_stateWindow);
 615		m_stateWindow = nullptr;
 616		QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
 617	});
 618	if (!wasPaused) {
 619		m_controller->setPaused(true);
 620		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
 621	}
 622	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
 623	m_stateWindow->setMode(ls);
 624	attachWidget(m_stateWindow);
 625}
 626
 627void Window::setupMenu(QMenuBar* menubar) {
 628	menubar->clear();
 629	QMenu* fileMenu = menubar->addMenu(tr("&File"));
 630	m_shortcutController->addMenu(fileMenu);
 631	installEventFilter(m_shortcutController);
 632	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open),
 633	                    "loadROM");
 634	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
 635	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
 636	addControlledAction(fileMenu, fileMenu->addAction(tr("Boot BIOS"), m_controller, SLOT(bootBIOS())), "bootBIOS");
 637
 638	addControlledAction(fileMenu, fileMenu->addAction(tr("Replace ROM..."), this, SLOT(replaceROM())), "replaceROM");
 639
 640	m_mruMenu = fileMenu->addMenu(tr("Recent"));
 641
 642	fileMenu->addSeparator();
 643
 644	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
 645	loadState->setShortcut(tr("F10"));
 646	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
 647	m_gameActions.append(loadState);
 648	addControlledAction(fileMenu, loadState, "loadState");
 649
 650	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
 651	saveState->setShortcut(tr("Shift+F10"));
 652	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
 653	m_gameActions.append(saveState);
 654	addControlledAction(fileMenu, saveState, "saveState");
 655
 656	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
 657	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
 658	m_shortcutController->addMenu(quickLoadMenu);
 659	m_shortcutController->addMenu(quickSaveMenu);
 660
 661	QAction* quickLoad = new QAction(tr("Load recent"), quickLoadMenu);
 662	connect(quickLoad, SIGNAL(triggered()), m_controller, SLOT(loadState()));
 663	m_gameActions.append(quickLoad);
 664	addControlledAction(quickLoadMenu, quickLoad, "quickLoad");
 665
 666	QAction* quickSave = new QAction(tr("Save recent"), quickSaveMenu);
 667	connect(quickSave, SIGNAL(triggered()), m_controller, SLOT(saveState()));
 668	m_gameActions.append(quickSave);
 669	addControlledAction(quickSaveMenu, quickSave, "quickSave");
 670
 671	quickLoadMenu->addSeparator();
 672	quickSaveMenu->addSeparator();
 673
 674	int i;
 675	for (i = 1; i < 10; ++i) {
 676		quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
 677		quickLoad->setShortcut(tr("F%1").arg(i));
 678		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
 679		m_gameActions.append(quickLoad);
 680		addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
 681
 682		quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
 683		quickSave->setShortcut(tr("Shift+F%1").arg(i));
 684		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
 685		m_gameActions.append(quickSave);
 686		addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
 687	}
 688
 689	fileMenu->addSeparator();
 690	QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
 691	connect(importShark, SIGNAL(triggered()), this, SLOT(importSharkport()));
 692	m_gameActions.append(importShark);
 693	addControlledAction(fileMenu, importShark, "importShark");
 694
 695	QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
 696	connect(exportShark, SIGNAL(triggered()), this, SLOT(exportSharkport()));
 697	m_gameActions.append(exportShark);
 698	addControlledAction(fileMenu, exportShark, "exportShark");
 699
 700	fileMenu->addSeparator();
 701	QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
 702	connect(multiWindow, &QAction::triggered, [this]() {
 703		GBAApp::app()->newWindow();
 704	});
 705	addControlledAction(fileMenu, multiWindow, "multiWindow");
 706
 707#ifndef Q_OS_MAC
 708	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
 709#endif
 710
 711	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
 712	m_shortcutController->addMenu(emulationMenu);
 713	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
 714	reset->setShortcut(tr("Ctrl+R"));
 715	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
 716	m_gameActions.append(reset);
 717	addControlledAction(emulationMenu, reset, "reset");
 718
 719	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
 720	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
 721	m_gameActions.append(shutdown);
 722	addControlledAction(emulationMenu, shutdown, "shutdown");
 723
 724	QAction* yank = new QAction(tr("Yank game pak"), emulationMenu);
 725	connect(yank, SIGNAL(triggered()), m_controller, SLOT(yankPak()));
 726	m_gameActions.append(yank);
 727	addControlledAction(emulationMenu, yank, "yank");
 728	emulationMenu->addSeparator();
 729
 730	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
 731	pause->setChecked(false);
 732	pause->setCheckable(true);
 733	pause->setShortcut(tr("Ctrl+P"));
 734	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
 735	connect(m_controller, &GameController::gamePaused, [this, pause]() {
 736		pause->setChecked(true);
 737
 738		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS,
 739		                    VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
 740		QPixmap pixmap;
 741		pixmap.convertFromImage(currentImage.rgbSwapped());
 742		m_screenWidget->setPixmap(pixmap);
 743		m_screenWidget->setLockAspectRatio(3, 2);
 744	});
 745	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
 746	m_gameActions.append(pause);
 747	addControlledAction(emulationMenu, pause, "pause");
 748
 749	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
 750	frameAdvance->setShortcut(tr("Ctrl+N"));
 751	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
 752	m_gameActions.append(frameAdvance);
 753	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
 754
 755	emulationMenu->addSeparator();
 756
 757	m_shortcutController->addFunctions(emulationMenu, [this]() {
 758		m_controller->setTurbo(true, false);
 759	}, [this]() {
 760		m_controller->setTurbo(false, false);
 761	}, QKeySequence(Qt::Key_Tab), tr("Fast forward (held)"), "holdFastForward");
 762
 763	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
 764	turbo->setCheckable(true);
 765	turbo->setChecked(false);
 766	turbo->setShortcut(tr("Shift+Tab"));
 767	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
 768	addControlledAction(emulationMenu, turbo, "fastForward");
 769
 770	QMenu* ffspeedMenu = emulationMenu->addMenu(tr("Fast forward speed"));
 771	ConfigOption* ffspeed = m_config->addOption("fastForwardRatio");
 772	ffspeed->connect([this](const QVariant& value) {
 773		m_controller->setTurboSpeed(value.toFloat());
 774	}, this);
 775	ffspeed->addValue(tr("Unbounded"), -1.0f, ffspeedMenu);
 776	ffspeed->setValue(QVariant(-1.0f));
 777	ffspeedMenu->addSeparator();
 778	for (i = 2; i < 11; ++i) {
 779		ffspeed->addValue(tr("%0x").arg(i), i, ffspeedMenu);
 780	}
 781	m_config->updateOption("fastForwardRatio");
 782
 783	m_shortcutController->addFunctions(emulationMenu, [this]() {
 784		m_controller->startRewinding();
 785	}, [this]() {
 786		m_controller->stopRewinding();
 787	}, QKeySequence("~"), tr("Rewind (held)"), "holdRewind");
 788
 789	QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
 790	rewind->setShortcut(tr("`"));
 791	connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
 792	m_gameActions.append(rewind);
 793	addControlledAction(emulationMenu, rewind, "rewind");
 794
 795	QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
 796	frameRewind->setShortcut(tr("Ctrl+B"));
 797	connect(frameRewind, &QAction::triggered, [this] () {
 798		m_controller->rewind(1);
 799	});
 800	m_gameActions.append(frameRewind);
 801	addControlledAction(emulationMenu, frameRewind, "frameRewind");
 802
 803	ConfigOption* videoSync = m_config->addOption("videoSync");
 804	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
 805	videoSync->connect([this](const QVariant& value) {
 806		m_controller->setVideoSync(value.toBool());
 807	}, this);
 808	m_config->updateOption("videoSync");
 809
 810	ConfigOption* audioSync = m_config->addOption("audioSync");
 811	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
 812	audioSync->connect([this](const QVariant& value) {
 813		m_controller->setAudioSync(value.toBool());
 814	}, this);
 815	m_config->updateOption("audioSync");
 816
 817	emulationMenu->addSeparator();
 818
 819	QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
 820	m_shortcutController->addMenu(solarMenu);
 821	QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
 822	connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
 823	addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
 824
 825	QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
 826	connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
 827	addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
 828
 829	QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
 830	connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
 831	addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
 832
 833	QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
 834	connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
 835	addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
 836
 837	solarMenu->addSeparator();
 838	for (int i = 0; i <= 10; ++i) {
 839		QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
 840		connect(setSolar, &QAction::triggered, [this, i]() {
 841			m_controller->setLuminanceLevel(i);
 842		});
 843		addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
 844	}
 845
 846	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
 847	m_shortcutController->addMenu(avMenu);
 848	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
 849	m_shortcutController->addMenu(frameMenu, avMenu);
 850	for (int i = 1; i <= 6; ++i) {
 851		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
 852		connect(setSize, &QAction::triggered, [this, i]() {
 853			showNormal();
 854			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
 855		});
 856		addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
 857	}
 858	QKeySequence fullscreenKeys;
 859#ifdef Q_OS_WIN
 860	fullscreenKeys = QKeySequence("Alt+Enter");
 861#else
 862	fullscreenKeys = QKeySequence("Ctrl+F");
 863#endif
 864	addControlledAction(frameMenu, frameMenu->addAction(tr("Toggle fullscreen"), this, SLOT(toggleFullScreen()), fullscreenKeys), "fullscreen");
 865
 866	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
 867	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
 868	lockAspectRatio->connect([this](const QVariant& value) {
 869		m_display->lockAspectRatio(value.toBool());
 870	}, this);
 871	m_config->updateOption("lockAspectRatio");
 872
 873	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
 874	resampleVideo->addBoolean(tr("Resample video"), avMenu);
 875	resampleVideo->connect([this](const QVariant& value) {
 876		m_display->filter(value.toBool());
 877	}, this);
 878	m_config->updateOption("resampleVideo");
 879
 880	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
 881	ConfigOption* skip = m_config->addOption("frameskip");
 882	skip->connect([this](const QVariant& value) {
 883		m_controller->setFrameskip(value.toInt());
 884	}, this);
 885	for (int i = 0; i <= 10; ++i) {
 886		skip->addValue(QString::number(i), i, skipMenu);
 887	}
 888	m_config->updateOption("frameskip");
 889
 890	avMenu->addSeparator();
 891
 892	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
 893	ConfigOption* buffers = m_config->addOption("audioBuffers");
 894	buffers->connect([this](const QVariant& value) {
 895		emit audioBufferSamplesChanged(value.toInt());
 896	}, this);
 897	buffers->addValue(tr("512"), 512, buffersMenu);
 898	buffers->addValue(tr("768"), 768, buffersMenu);
 899	buffers->addValue(tr("1024"), 1024, buffersMenu);
 900	buffers->addValue(tr("2048"), 2048, buffersMenu);
 901	buffers->addValue(tr("4096"), 4096, buffersMenu);
 902	m_config->updateOption("audioBuffers");
 903
 904	avMenu->addSeparator();
 905
 906	QMenu* target = avMenu->addMenu(tr("FPS target"));
 907	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
 908	fpsTargetOption->connect([this](const QVariant& value) {
 909		emit fpsTargetChanged(value.toFloat());
 910	}, this);
 911	fpsTargetOption->addValue(tr("15"), 15, target);
 912	fpsTargetOption->addValue(tr("30"), 30, target);
 913	fpsTargetOption->addValue(tr("45"), 45, target);
 914	fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
 915	fpsTargetOption->addValue(tr("60"), 60, target);
 916	fpsTargetOption->addValue(tr("90"), 90, target);
 917	fpsTargetOption->addValue(tr("120"), 120, target);
 918	fpsTargetOption->addValue(tr("240"), 240, target);
 919	m_config->updateOption("fpsTarget");
 920
 921#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
 922	avMenu->addSeparator();
 923#endif
 924
 925#ifdef USE_PNG
 926	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
 927	screenshot->setShortcut(tr("F12"));
 928	connect(screenshot, SIGNAL(triggered()), m_controller, SLOT(screenshot()));
 929	m_gameActions.append(screenshot);
 930	addControlledAction(avMenu, screenshot, "screenshot");
 931#endif
 932
 933#ifdef USE_FFMPEG
 934	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
 935	recordOutput->setShortcut(tr("F11"));
 936	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
 937	addControlledAction(avMenu, recordOutput, "recordOutput");
 938#endif
 939
 940#ifdef USE_MAGICK
 941	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
 942	recordGIF->setShortcut(tr("Shift+F11"));
 943	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
 944	addControlledAction(avMenu, recordGIF, "recordGIF");
 945#endif
 946
 947	avMenu->addSeparator();
 948	QMenu* videoLayers = avMenu->addMenu(tr("Video layers"));
 949
 950	for (int i = 0; i < 4; ++i) {
 951		QAction* enableBg = new QAction(tr("Background %0").arg(i), videoLayers);
 952		enableBg->setCheckable(true);
 953		enableBg->setChecked(true);
 954		connect(enableBg, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->video.renderer->disableBG[i] = !enable; });
 955		m_gameActions.append(enableBg);
 956		addControlledAction(videoLayers, enableBg, QString("enableBG%0").arg(i));
 957	}
 958
 959	QAction* enableObj = new QAction(tr("OBJ (sprites)"), videoLayers);
 960	enableObj->setCheckable(true);
 961	enableObj->setChecked(true);
 962	connect(enableObj, &QAction::triggered, [this](bool enable) { m_controller->thread()->gba->video.renderer->disableOBJ = !enable; });
 963	m_gameActions.append(enableObj);
 964	addControlledAction(videoLayers, enableObj, "enableOBJ");
 965
 966	QMenu* audioChannels = avMenu->addMenu(tr("Audio channels"));
 967
 968	for (int i = 0; i < 4; ++i) {
 969		QAction* enableCh = new QAction(tr("Channel %0").arg(i + 1), audioChannels);
 970		enableCh->setCheckable(true);
 971		enableCh->setChecked(true);
 972		connect(enableCh, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableCh[i] = !enable; });
 973		m_gameActions.append(enableCh);
 974		addControlledAction(audioChannels, enableCh, QString("enableCh%0").arg(i + 1));
 975	}
 976
 977	QAction* enableChA = new QAction(tr("Channel A"), audioChannels);
 978	enableChA->setCheckable(true);
 979	enableChA->setChecked(true);
 980	connect(enableChA, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChA = !enable; });
 981	m_gameActions.append(enableChA);
 982	addControlledAction(audioChannels, enableChA, QString("enableChA"));
 983
 984	QAction* enableChB = new QAction(tr("Channel B"), audioChannels);
 985	enableChB->setCheckable(true);
 986	enableChB->setChecked(true);
 987	connect(enableChB, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChB = !enable; });
 988	m_gameActions.append(enableChB);
 989	addControlledAction(audioChannels, enableChB, QString("enableChB"));
 990
 991	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
 992	m_shortcutController->addMenu(toolsMenu);
 993	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
 994	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
 995	addControlledAction(toolsMenu, viewLogs, "viewLogs");
 996
 997	QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
 998	connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
 999	addControlledAction(toolsMenu, overrides, "overrideWindow");
1000
1001	QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
1002	connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
1003	addControlledAction(toolsMenu, sensors, "sensorWindow");
1004
1005	QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
1006	connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
1007	addControlledAction(toolsMenu, cheats, "cheatsWindow");
1008
1009#ifdef USE_GDB_STUB
1010	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
1011	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
1012	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
1013#endif
1014
1015	toolsMenu->addSeparator();
1016	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())),
1017	                    "settings");
1018	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())),
1019	                    "shortcuts");
1020
1021	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
1022	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
1023	addControlledAction(toolsMenu, keymap, "remapKeyboard");
1024
1025#ifdef BUILD_SDL
1026	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
1027	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
1028	addControlledAction(toolsMenu, gamepad, "remapGamepad");
1029#endif
1030
1031	toolsMenu->addSeparator();
1032
1033	QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
1034	connect(paletteView, SIGNAL(triggered()), this, SLOT(openPaletteWindow()));
1035	m_gameActions.append(paletteView);
1036	addControlledAction(toolsMenu, paletteView, "paletteWindow");
1037
1038	QAction* memoryView = new QAction(tr("View memory..."), toolsMenu);
1039	connect(memoryView, SIGNAL(triggered()), this, SLOT(openMemoryWindow()));
1040	m_gameActions.append(memoryView);
1041	addControlledAction(toolsMenu, memoryView, "memoryView");
1042
1043	ConfigOption* skipBios = m_config->addOption("skipBios");
1044	skipBios->connect([this](const QVariant& value) {
1045		m_controller->setSkipBIOS(value.toBool());
1046	}, this);
1047
1048	ConfigOption* volume = m_config->addOption("volume");
1049	volume->connect([this](const QVariant& value) {
1050		m_controller->setVolume(value.toInt());
1051	}, this);
1052
1053	ConfigOption* mute = m_config->addOption("mute");
1054	mute->connect([this](const QVariant& value) {
1055		m_controller->setMute(value.toBool());
1056	}, this);
1057
1058	ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
1059	rewindEnable->connect([this](const QVariant& value) {
1060		m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt());
1061	}, this);
1062
1063	ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
1064	rewindBufferCapacity->connect([this](const QVariant& value) {
1065		m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt());
1066	}, this);
1067
1068	ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
1069	rewindBufferInterval->connect([this](const QVariant& value) {
1070		m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt());
1071	}, this);
1072
1073	ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
1074	allowOpposingDirections->connect([this](const QVariant& value) {
1075		m_inputController.setAllowOpposing(value.toBool());
1076	}, this);
1077
1078	QAction* exitFullScreen = new QAction(tr("Exit fullscreen"), frameMenu);
1079	connect(exitFullScreen, SIGNAL(triggered()), this, SLOT(exitFullScreen()));
1080	exitFullScreen->setShortcut(QKeySequence("Esc"));
1081	addHiddenAction(frameMenu, exitFullScreen, "exitFullScreen");
1082
1083	foreach (QAction* action, m_gameActions) {
1084		action->setDisabled(true);
1085	}
1086}
1087
1088void Window::attachWidget(QWidget* widget) {
1089	m_screenWidget->layout()->addWidget(widget);
1090	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1091}
1092
1093void Window::detachWidget(QWidget* widget) {
1094	m_screenWidget->layout()->removeWidget(widget);
1095}
1096
1097void Window::appendMRU(const QString& fname) {
1098	int index = m_mruFiles.indexOf(fname);
1099	if (index >= 0) {
1100		m_mruFiles.removeAt(index);
1101	}
1102	m_mruFiles.prepend(fname);
1103	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1104		m_mruFiles.removeLast();
1105	}
1106	updateMRU();
1107}
1108
1109void Window::updateMRU() {
1110	if (!m_mruMenu) {
1111		return;
1112	}
1113	m_mruMenu->clear();
1114	int i = 0;
1115	for (const QString& file : m_mruFiles) {
1116		QAction* item = new QAction(file, m_mruMenu);
1117		item->setShortcut(QString("Ctrl+%1").arg(i));
1118		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
1119		m_mruMenu->addAction(item);
1120		++i;
1121	}
1122	m_config->setMRU(m_mruFiles);
1123	m_config->write();
1124	m_mruMenu->setEnabled(i > 0);
1125}
1126
1127QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1128	addHiddenAction(menu, action, name);
1129	menu->addAction(action);
1130	return action;
1131}
1132
1133QAction* Window::addHiddenAction(QMenu* menu, QAction* action, const QString& name) {
1134	m_shortcutController->addAction(menu, action, name);
1135	action->setShortcutContext(Qt::WidgetShortcut);
1136	addAction(action);
1137	return action;
1138}
1139
1140WindowBackground::WindowBackground(QWidget* parent)
1141	: QLabel(parent)
1142{
1143	setLayout(new QStackedLayout());
1144	layout()->setContentsMargins(0, 0, 0, 0);
1145	setAlignment(Qt::AlignCenter);
1146}
1147
1148void WindowBackground::setSizeHint(const QSize& hint) {
1149	m_sizeHint = hint;
1150}
1151
1152QSize WindowBackground::sizeHint() const {
1153	return m_sizeHint;
1154}
1155
1156void WindowBackground::setLockAspectRatio(int width, int height) {
1157	m_aspectWidth = width;
1158	m_aspectHeight = height;
1159}
1160
1161void WindowBackground::paintEvent(QPaintEvent*) {
1162	const QPixmap* logo = pixmap();
1163	if (!logo) {
1164		return;
1165	}
1166	QPainter painter(this);
1167	painter.setRenderHint(QPainter::SmoothPixmapTransform);
1168	painter.fillRect(QRect(QPoint(), size()), Qt::black);
1169	QSize s = size();
1170	QSize ds = s;
1171	if (s.width() * m_aspectHeight > s.height() * m_aspectWidth) {
1172		ds.setWidth(s.height() * m_aspectWidth / m_aspectHeight);
1173	} else if (s.width() * m_aspectHeight < s.height() * m_aspectWidth) {
1174		ds.setHeight(s.width() * m_aspectHeight / m_aspectWidth);
1175	}
1176	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1177	QRect full(origin, ds);
1178	painter.drawPixmap(full, *logo);
1179}