all repos — mgba @ 8b19ad78098d5fd6b081c1c23058479325c63826

mGBA Game Boy Advance Emulator

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

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