all repos — mgba @ 3c18fe162c2c76eca80692d37083f6809bae4c8d

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