all repos — mgba @ 53c586044d76775bb0226a6c92ca61f0c1844467

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 <QKeyEvent>
 10#include <QKeySequence>
 11#include <QMenuBar>
 12#include <QMessageBox>
 13#include <QStackedLayout>
 14
 15#include "ConfigController.h"
 16#include "GameController.h"
 17#include "GBAKeyEditor.h"
 18#include "GDBController.h"
 19#include "GDBWindow.h"
 20#include "GIFView.h"
 21#include "GamePakView.h"
 22#include "LoadSaveState.h"
 23#include "LogView.h"
 24#include "SettingsView.h"
 25#include "ShortcutController.h"
 26#include "ShortcutView.h"
 27#include "VideoView.h"
 28
 29extern "C" {
 30#include "platform/commandline.h"
 31}
 32
 33using namespace QGBA;
 34
 35Window::Window(ConfigController* config, QWidget* parent)
 36	: QMainWindow(parent)
 37	, m_logView(new LogView())
 38	, m_stateWindow(nullptr)
 39	, m_screenWidget(new WindowBackground())
 40	, m_logo(":/res/mgba-1024.png")
 41	, m_config(config)
 42#ifdef USE_FFMPEG
 43	, m_videoView(nullptr)
 44#endif
 45#ifdef USE_MAGICK
 46	, m_gifView(nullptr)
 47#endif
 48#ifdef USE_GDB_STUB
 49	, m_gdbController(nullptr)
 50#endif
 51	, m_mruMenu(nullptr)
 52	, m_shortcutController(new ShortcutController(this))
 53{
 54	setWindowTitle(PROJECT_NAME);
 55	setFocusPolicy(Qt::StrongFocus);
 56	m_controller = new GameController(this);
 57	m_controller->setInputController(&m_inputController);
 58
 59	QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
 60	format.setSwapInterval(1);
 61	m_display = new Display(format);
 62
 63	m_screenWidget->setMinimumSize(m_display->minimumSize());
 64	m_screenWidget->setSizePolicy(m_display->sizePolicy());
 65	m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
 66	setCentralWidget(m_screenWidget);
 67
 68	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
 69	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
 70	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
 71	connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
 72	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
 73#ifndef Q_OS_MAC
 74	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
 75	connect(m_controller, &GameController::gameUnpaused, [this]() {
 76		if(isFullScreen()) {
 77			menuBar()->hide();
 78		}
 79	});
 80#endif
 81	connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
 82	connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
 83	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
 84	connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
 85	connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
 86	connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
 87	connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
 88	connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
 89	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 90	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 91	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 92	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 93	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 94	connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
 95
 96	connect(&m_inputController, SIGNAL(buttonPressed(int)), m_shortcutController, SLOT(pressButton(int)));
 97
 98	m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL);
 99	m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
100
101	setupMenu(menuBar());
102}
103
104Window::~Window() {
105	delete m_logView;
106
107#ifdef USE_FFMPEG
108	delete m_videoView;
109#endif
110
111#ifdef USE_MAGICK
112	delete m_gifView;
113#endif
114}
115
116void Window::argumentsPassed(GBAArguments* args) {
117	loadConfig();
118
119	if (args->patch) {
120		m_controller->loadPatch(args->patch);
121	}
122
123	if (args->fname) {
124		m_controller->loadGame(args->fname, args->dirmode);
125	}
126}
127
128void Window::resizeFrame(int width, int height) {
129	QSize newSize(width, height);
130	newSize -= m_screenWidget->size();
131	newSize += size();
132	resize(newSize);
133}
134
135void Window::setConfig(ConfigController* config) {
136	m_config = config;
137}
138
139void Window::loadConfig() {
140	const GBAOptions* opts = m_config->options();
141
142	m_logView->setLevels(opts->logLevel);
143
144	m_controller->setFrameskip(opts->frameskip);
145	m_controller->setAudioSync(opts->audioSync);
146	m_controller->setVideoSync(opts->videoSync);
147	m_controller->setSkipBIOS(opts->skipBios);
148	m_display->lockAspectRatio(opts->lockAspectRatio);
149	m_display->filter(opts->resampleVideo);
150
151	if (opts->bios) {
152		m_controller->loadBIOS(opts->bios);
153	}
154
155	if (opts->fpsTarget) {
156		emit fpsTargetChanged(opts->fpsTarget);
157	}
158
159	if (opts->audioBuffers) {
160		emit audioBufferSamplesChanged(opts->audioBuffers);
161	}
162
163	if (opts->width && opts->height) {
164		resizeFrame(opts->width, opts->height);
165	}
166
167	m_mruFiles = m_config->getMRU();
168	updateMRU();
169
170	m_inputController.setConfiguration(m_config);
171}
172
173void Window::saveConfig() {
174	m_config->write();
175}
176
177void Window::selectROM() {
178	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
179	if (!filename.isEmpty()) {
180		m_controller->loadGame(filename);
181	}
182}
183
184void Window::selectBIOS() {
185	QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
186	if (!filename.isEmpty()) {
187		m_config->setOption("bios", filename);
188		m_config->updateOption("bios");
189		m_controller->loadBIOS(filename);
190	}
191}
192
193void Window::selectPatch() {
194	QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), QString(), tr("Patches (*.ips *.ups)"));
195	if (!filename.isEmpty()) {
196		m_controller->loadPatch(filename);
197	}
198}
199
200void Window::openKeymapWindow() {
201	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
202	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
203	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
204	keyEditor->show();
205}
206
207void Window::openSettingsWindow() {
208	SettingsView* settingsWindow = new SettingsView(m_config);
209	connect(this, SIGNAL(shutdown()), settingsWindow, SLOT(close()));
210	connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
211	settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
212	settingsWindow->show();
213}
214
215void Window::openShortcutWindow() {
216	ShortcutView* shortcutView = new ShortcutView();
217	shortcutView->setController(m_shortcutController);
218	shortcutView->setInputController(&m_inputController);
219	connect(this, SIGNAL(shutdown()), shortcutView, SLOT(close()));
220	shortcutView->setAttribute(Qt::WA_DeleteOnClose);
221	shortcutView->show();
222}
223
224void Window::openGamePakWindow() {
225	GamePakView* gamePakWindow = new GamePakView(m_controller);
226	connect(this, SIGNAL(shutdown()), gamePakWindow, SLOT(close()));
227	gamePakWindow->setAttribute(Qt::WA_DeleteOnClose);
228	gamePakWindow->show();
229}
230
231#ifdef BUILD_SDL
232void Window::openGamepadWindow() {
233	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON);
234	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
235	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
236	keyEditor->show();
237}
238#endif
239
240#ifdef USE_FFMPEG
241void Window::openVideoWindow() {
242	if (!m_videoView) {
243		m_videoView = new VideoView();
244		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
245		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
246		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
247		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
248		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
249	}
250	m_videoView->show();
251}
252#endif
253
254#ifdef USE_MAGICK
255void Window::openGIFWindow() {
256	if (!m_gifView) {
257		m_gifView = new GIFView();
258		connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
259		connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
260		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
261		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
262		connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
263	}
264	m_gifView->show();
265}
266#endif
267
268#ifdef USE_GDB_STUB
269void Window::gdbOpen() {
270	if (!m_gdbController) {
271		m_gdbController = new GDBController(m_controller, this);
272	}
273	GDBWindow* window = new GDBWindow(m_gdbController);
274	window->show();
275}
276#endif
277
278void Window::keyPressEvent(QKeyEvent* event) {
279	if (event->isAutoRepeat()) {
280		QWidget::keyPressEvent(event);
281		return;
282	}
283	if (event->key() == Qt::Key_Tab) {
284		m_controller->setTurbo(true, false);
285	}
286	GBAKey key = m_inputController.mapKeyboard(event->key());
287	if (key == GBA_KEY_NONE) {
288		QWidget::keyPressEvent(event);
289		return;
290	}
291	m_controller->keyPressed(key);
292	event->accept();
293}
294
295void Window::keyReleaseEvent(QKeyEvent* event) {
296	if (event->isAutoRepeat()) {
297		QWidget::keyReleaseEvent(event);
298		return;
299	}
300	if (event->key() == Qt::Key_Tab) {
301		m_controller->setTurbo(false, false);
302	}
303	GBAKey key = m_inputController.mapKeyboard(event->key());
304	if (key == GBA_KEY_NONE) {
305		QWidget::keyPressEvent(event);
306		return;
307	}
308	m_controller->keyReleased(key);
309	event->accept();
310}
311
312void Window::resizeEvent(QResizeEvent*) {
313	redoLogo();
314	m_config->setOption("height", m_screenWidget->height());
315	m_config->setOption("width", m_screenWidget->width());
316}
317
318void Window::closeEvent(QCloseEvent* event) {
319	emit shutdown();
320	QMainWindow::closeEvent(event);
321}
322
323void Window::focusOutEvent(QFocusEvent*) {
324	m_controller->setTurbo(false, false);
325	m_controller->clearKeys();
326}
327
328void Window::toggleFullScreen() {
329	if (isFullScreen()) {
330		showNormal();
331		menuBar()->show();
332	} else {
333		showFullScreen();
334#ifndef Q_OS_MAC
335		if (m_controller->isLoaded() && !m_controller->isPaused()) {
336			menuBar()->hide();
337		}
338#endif
339	}
340}
341
342void Window::gameStarted(GBAThread* context) {
343	emit startDrawing(m_controller->drawContext(), context);
344	foreach (QAction* action, m_gameActions) {
345		action->setDisabled(false);
346	}
347	appendMRU(context->fname);
348	char title[13] = { '\0' };
349	GBAGetGameTitle(context->gba, title);
350	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
351	attachWidget(m_display);
352	m_screenWidget->setScaledContents(true);
353
354#ifndef Q_OS_MAC
355	if(isFullScreen()) {
356		menuBar()->hide();
357	}
358#endif
359
360	m_fpsTimer.start();
361}
362
363void Window::gameStopped() {
364	foreach (QAction* action, m_gameActions) {
365		action->setDisabled(true);
366	}
367	setWindowTitle(tr(PROJECT_NAME));
368	detachWidget(m_display);
369	m_screenWidget->setScaledContents(false);
370	redoLogo();
371
372	m_fpsTimer.stop();
373}
374
375void Window::gameCrashed(const QString& errorMessage) {
376	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
377		tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
378		QMessageBox::Ok, this,  Qt::Sheet);
379	crash->setAttribute(Qt::WA_DeleteOnClose);
380	crash->show();
381}
382
383void Window::redoLogo() {
384	if (m_controller->isLoaded()) {
385		return;
386	}
387	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
388	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
389	m_screenWidget->setPixmap(logo);
390}
391
392void Window::recordFrame() {
393	m_frameList.append(QDateTime::currentDateTime());
394	while (m_frameList.count() > FRAME_LIST_SIZE) {
395		m_frameList.removeFirst();
396	}
397}
398
399void Window::showFPS() {
400	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
401	float fps = (m_frameList.count() - 1) * 10000.f / interval;
402	fps = round(fps) / 10.f;
403	char title[13] = { '\0' };
404	GBAGetGameTitle(m_controller->thread()->gba, title);
405	setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
406}
407
408void Window::openStateWindow(LoadSave ls) {
409	if (m_stateWindow) {
410		return;
411	}
412	bool wasPaused = m_controller->isPaused();
413	m_stateWindow = new LoadSaveState(m_controller);
414	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
415	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
416	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
417		m_screenWidget->layout()->removeWidget(m_stateWindow);
418		m_stateWindow = nullptr;
419		setFocus();
420	});
421	if (!wasPaused) {
422		m_controller->setPaused(true);
423		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
424	}
425	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
426	m_stateWindow->setMode(ls);
427	attachWidget(m_stateWindow);
428}
429
430void Window::setupMenu(QMenuBar* menubar) {
431	menubar->clear();
432	QMenu* fileMenu = menubar->addMenu(tr("&File"));
433	m_shortcutController->addMenu(fileMenu);
434	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
435	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
436	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
437
438	m_mruMenu = fileMenu->addMenu(tr("Recent"));
439
440	fileMenu->addSeparator();
441
442	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
443	loadState->setShortcut(tr("F10"));
444	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
445	m_gameActions.append(loadState);
446	addControlledAction(fileMenu, loadState, "loadState");
447
448	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
449	saveState->setShortcut(tr("Shift+F10"));
450	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
451	m_gameActions.append(saveState);
452	addControlledAction(fileMenu, saveState, "saveState");
453
454	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
455	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
456	int i;
457	for (i = 1; i < 10; ++i) {
458		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
459		quickLoad->setShortcut(tr("F%1").arg(i));
460		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
461		m_gameActions.append(quickLoad);
462		addAction(quickLoad);
463		quickLoadMenu->addAction(quickLoad);
464
465		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
466		quickSave->setShortcut(tr("Shift+F%1").arg(i));
467		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
468		m_gameActions.append(quickSave);
469		addAction(quickSave);
470		quickSaveMenu->addAction(quickSave);
471	}
472
473#ifndef Q_OS_MAC
474	fileMenu->addSeparator();
475	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
476#endif
477
478	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
479	m_shortcutController->addMenu(emulationMenu);
480	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
481	reset->setShortcut(tr("Ctrl+R"));
482	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
483	m_gameActions.append(reset);
484	addControlledAction(emulationMenu, reset, "reset");
485
486	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
487	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
488	m_gameActions.append(shutdown);
489	addControlledAction(emulationMenu, shutdown, "shutdown");
490	emulationMenu->addSeparator();
491
492	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
493	pause->setChecked(false);
494	pause->setCheckable(true);
495	pause->setShortcut(tr("Ctrl+P"));
496	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
497	connect(m_controller, &GameController::gamePaused, [this, pause]() {
498		pause->setChecked(true);
499
500		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
501		QPixmap pixmap;
502		pixmap.convertFromImage(currentImage.rgbSwapped());
503		m_screenWidget->setPixmap(pixmap);
504	});
505	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
506	m_gameActions.append(pause);
507	addControlledAction(emulationMenu, pause, "pause");
508
509	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
510	frameAdvance->setShortcut(tr("Ctrl+N"));
511	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
512	m_gameActions.append(frameAdvance);
513	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
514
515	emulationMenu->addSeparator();
516
517	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
518	turbo->setCheckable(true);
519	turbo->setChecked(false);
520	turbo->setShortcut(tr("Shift+Tab"));
521	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
522	addControlledAction(emulationMenu, turbo, "fastForward");
523
524	ConfigOption* videoSync = m_config->addOption("videoSync");
525	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
526	videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
527	m_config->updateOption("videoSync");
528
529	ConfigOption* audioSync = m_config->addOption("audioSync");
530	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
531	audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
532	m_config->updateOption("audioSync");
533
534	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
535	m_shortcutController->addMenu(avMenu);
536	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
537	for (int i = 1; i <= 6; ++i) {
538		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
539		connect(setSize, &QAction::triggered, [this, i]() {
540			showNormal();
541			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
542		});
543		frameMenu->addAction(setSize);
544	}
545	addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
546
547	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
548	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
549	lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
550	m_config->updateOption("lockAspectRatio");
551
552	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
553	resampleVideo->addBoolean(tr("Resample video"), avMenu);
554	resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
555	m_config->updateOption("resampleVideo");
556
557	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
558	ConfigOption* skip = m_config->addOption("frameskip");
559	skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
560	for (int i = 0; i <= 10; ++i) {
561		skip->addValue(QString::number(i), i, skipMenu);
562	}
563	m_config->updateOption("frameskip");
564
565	avMenu->addSeparator();
566
567	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
568	ConfigOption* buffers = m_config->addOption("audioBuffers");
569	buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
570	buffers->addValue(tr("512"), 512, buffersMenu);
571	buffers->addValue(tr("768"), 768, buffersMenu);
572	buffers->addValue(tr("1024"), 1024, buffersMenu);
573	buffers->addValue(tr("2048"), 2048, buffersMenu);
574	buffers->addValue(tr("4096"), 4096, buffersMenu);
575	m_config->updateOption("audioBuffers");
576
577	avMenu->addSeparator();
578
579	QMenu* target = avMenu->addMenu("FPS target");
580	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
581	fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
582	fpsTargetOption->addValue(tr("15"), 15, target);
583	fpsTargetOption->addValue(tr("30"), 30, target);
584	fpsTargetOption->addValue(tr("45"), 45, target);
585	fpsTargetOption->addValue(tr("60"), 60, target);
586	fpsTargetOption->addValue(tr("90"), 90, target);
587	fpsTargetOption->addValue(tr("120"), 120, target);
588	fpsTargetOption->addValue(tr("240"), 240, target);
589	m_config->updateOption("fpsTarget");
590
591#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
592	avMenu->addSeparator();
593#endif
594
595#ifdef USE_PNG
596	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
597	screenshot->setShortcut(tr("F12"));
598	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
599	m_gameActions.append(screenshot);
600	addControlledAction(avMenu, screenshot, "screenshot");
601#endif
602
603#ifdef USE_FFMPEG
604	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
605	recordOutput->setShortcut(tr("F11"));
606	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
607	addControlledAction(avMenu, recordOutput, "recordOutput");
608#endif
609
610#ifdef USE_MAGICK
611	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
612	recordGIF->setShortcut(tr("Shift+F11"));
613	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
614	addControlledAction(avMenu, recordGIF, "recordGIF");
615#endif
616
617	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
618	m_shortcutController->addMenu(toolsMenu);
619	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
620	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
621	addControlledAction(toolsMenu, viewLogs, "viewLogs");
622
623	QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
624	connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
625	addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
626
627#ifdef USE_GDB_STUB
628	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
629	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
630	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
631#endif
632
633	toolsMenu->addSeparator();
634	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
635	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
636
637	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
638	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
639	addControlledAction(toolsMenu, keymap, "remapKeyboard");
640
641#ifdef BUILD_SDL
642	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
643	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
644	addControlledAction(toolsMenu, gamepad, "remapGamepad");
645#endif
646
647	ConfigOption* skipBios = m_config->addOption("skipBios");
648	skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
649
650	foreach (QAction* action, m_gameActions) {
651		action->setDisabled(true);
652	}
653}
654
655void Window::attachWidget(QWidget* widget) {
656	m_screenWidget->layout()->addWidget(widget);
657	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
658}
659
660void Window::detachWidget(QWidget* widget) {
661	m_screenWidget->layout()->removeWidget(widget);
662}
663
664void Window::appendMRU(const QString& fname) {
665	int index = m_mruFiles.indexOf(fname);
666	if (index >= 0) {
667		m_mruFiles.removeAt(index);
668	}
669	m_mruFiles.prepend(fname);
670	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
671		m_mruFiles.removeLast();
672	}
673	updateMRU();
674}
675
676void Window::updateMRU() {
677	if (!m_mruMenu) {
678		return;
679	}
680	m_mruMenu->clear();
681	int i = 0;
682	for (const QString& file : m_mruFiles) {
683		QAction* item = new QAction(file, m_mruMenu);
684		item->setShortcut(QString("Ctrl+%1").arg(i));
685		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
686		m_mruMenu->addAction(item);
687		++i;
688	}
689	m_config->setMRU(m_mruFiles);
690	m_config->write();
691	m_mruMenu->setEnabled(i > 0);
692}
693
694QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
695	m_shortcutController->addAction(menu, action, name);
696	menu->addAction(action);
697	addAction(action);
698	return action;
699}
700
701WindowBackground::WindowBackground(QWidget* parent)
702	: QLabel(parent)
703{
704	setLayout(new QStackedLayout());
705	layout()->setContentsMargins(0, 0, 0, 0);
706	setAlignment(Qt::AlignCenter);
707	QPalette p = palette();
708	p.setColor(backgroundRole(), Qt::black);
709	setPalette(p);
710	setAutoFillBackground(true);
711}
712
713void WindowBackground::setSizeHint(const QSize& hint) {
714	m_sizeHint = hint;
715}
716
717QSize WindowBackground::sizeHint() const {
718	return m_sizeHint;
719}