all repos — mgba @ 30c28f225916338c75c7fa516a0bb4edbde81392

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	m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL);
 97	m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
 98
 99	m_shortcutController->setConfigController(m_config);
100	setupMenu(menuBar());
101}
102
103Window::~Window() {
104	delete m_logView;
105
106#ifdef USE_FFMPEG
107	delete m_videoView;
108#endif
109
110#ifdef USE_MAGICK
111	delete m_gifView;
112#endif
113}
114
115void Window::argumentsPassed(GBAArguments* args) {
116	loadConfig();
117
118	if (args->patch) {
119		m_controller->loadPatch(args->patch);
120	}
121
122	if (args->fname) {
123		m_controller->loadGame(args->fname, args->dirmode);
124	}
125}
126
127void Window::resizeFrame(int width, int height) {
128	QSize newSize(width, height);
129	newSize -= m_screenWidget->size();
130	newSize += size();
131	resize(newSize);
132}
133
134void Window::setConfig(ConfigController* config) {
135	m_config = config;
136}
137
138void Window::loadConfig() {
139	const GBAOptions* opts = m_config->options();
140
141	m_logView->setLevels(opts->logLevel);
142
143	m_controller->setFrameskip(opts->frameskip);
144	m_controller->setAudioSync(opts->audioSync);
145	m_controller->setVideoSync(opts->videoSync);
146	m_controller->setSkipBIOS(opts->skipBios);
147	m_controller->setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
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	GBAKey key = m_inputController.mapKeyboard(event->key());
284	if (key == GBA_KEY_NONE) {
285		QWidget::keyPressEvent(event);
286		return;
287	}
288	m_controller->keyPressed(key);
289	event->accept();
290}
291
292void Window::keyReleaseEvent(QKeyEvent* event) {
293	if (event->isAutoRepeat()) {
294		QWidget::keyReleaseEvent(event);
295		return;
296	}
297	GBAKey key = m_inputController.mapKeyboard(event->key());
298	if (key == GBA_KEY_NONE) {
299		QWidget::keyPressEvent(event);
300		return;
301	}
302	m_controller->keyReleased(key);
303	event->accept();
304}
305
306void Window::resizeEvent(QResizeEvent*) {
307	redoLogo();
308	m_config->setOption("height", m_screenWidget->height());
309	m_config->setOption("width", m_screenWidget->width());
310}
311
312void Window::closeEvent(QCloseEvent* event) {
313	emit shutdown();
314	QMainWindow::closeEvent(event);
315}
316
317void Window::focusOutEvent(QFocusEvent*) {
318	m_controller->setTurbo(false, false);
319	m_controller->clearKeys();
320}
321
322void Window::toggleFullScreen() {
323	if (isFullScreen()) {
324		showNormal();
325		menuBar()->show();
326	} else {
327		showFullScreen();
328#ifndef Q_OS_MAC
329		if (m_controller->isLoaded() && !m_controller->isPaused()) {
330			menuBar()->hide();
331		}
332#endif
333	}
334}
335
336void Window::gameStarted(GBAThread* context) {
337	char title[13] = { '\0' };
338	MutexLock(&context->stateMutex);
339	if (context->state < THREAD_EXITING) {
340		emit startDrawing(m_controller->drawContext(), context);
341		GBAGetGameTitle(context->gba, title);
342	} else {
343		MutexUnlock(&context->stateMutex);
344		return;
345	}
346	MutexUnlock(&context->stateMutex);
347	foreach (QAction* action, m_gameActions) {
348		action->setDisabled(false);
349	}
350	appendMRU(context->fname);
351	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
352	attachWidget(m_display);
353	m_screenWidget->setScaledContents(true);
354
355#ifndef Q_OS_MAC
356	if(isFullScreen()) {
357		menuBar()->hide();
358	}
359#endif
360
361	m_fpsTimer.start();
362}
363
364void Window::gameStopped() {
365	foreach (QAction* action, m_gameActions) {
366		action->setDisabled(true);
367	}
368	setWindowTitle(tr(PROJECT_NAME));
369	detachWidget(m_display);
370	m_screenWidget->setScaledContents(false);
371	redoLogo();
372
373	m_fpsTimer.stop();
374}
375
376void Window::gameCrashed(const QString& errorMessage) {
377	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
378		tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
379		QMessageBox::Ok, this,  Qt::Sheet);
380	crash->setAttribute(Qt::WA_DeleteOnClose);
381	crash->show();
382}
383
384void Window::redoLogo() {
385	if (m_controller->isLoaded()) {
386		return;
387	}
388	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
389	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
390	m_screenWidget->setPixmap(logo);
391}
392
393void Window::recordFrame() {
394	m_frameList.append(QDateTime::currentDateTime());
395	while (m_frameList.count() > FRAME_LIST_SIZE) {
396		m_frameList.removeFirst();
397	}
398}
399
400void Window::showFPS() {
401	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
402	float fps = (m_frameList.count() - 1) * 10000.f / interval;
403	fps = round(fps) / 10.f;
404	char title[13] = { '\0' };
405	GBAGetGameTitle(m_controller->thread()->gba, title);
406	setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
407}
408
409void Window::openStateWindow(LoadSave ls) {
410	if (m_stateWindow) {
411		return;
412	}
413	bool wasPaused = m_controller->isPaused();
414	m_stateWindow = new LoadSaveState(m_controller);
415	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
416	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
417	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
418		m_screenWidget->layout()->removeWidget(m_stateWindow);
419		m_stateWindow = nullptr;
420		setFocus();
421	});
422	if (!wasPaused) {
423		m_controller->setPaused(true);
424		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
425	}
426	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
427	m_stateWindow->setMode(ls);
428	attachWidget(m_stateWindow);
429}
430
431void Window::setupMenu(QMenuBar* menubar) {
432	menubar->clear();
433	QMenu* fileMenu = menubar->addMenu(tr("&File"));
434	m_shortcutController->addMenu(fileMenu);
435	installEventFilter(m_shortcutController);
436	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
437	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
438	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
439
440	m_mruMenu = fileMenu->addMenu(tr("Recent"));
441
442	fileMenu->addSeparator();
443
444	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
445	loadState->setShortcut(tr("F10"));
446	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
447	m_gameActions.append(loadState);
448	addControlledAction(fileMenu, loadState, "loadState");
449
450	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
451	saveState->setShortcut(tr("Shift+F10"));
452	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
453	m_gameActions.append(saveState);
454	addControlledAction(fileMenu, saveState, "saveState");
455
456	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
457	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
458	int i;
459	for (i = 1; i < 10; ++i) {
460		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
461		quickLoad->setShortcut(tr("F%1").arg(i));
462		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
463		m_gameActions.append(quickLoad);
464		addAction(quickLoad);
465		quickLoadMenu->addAction(quickLoad);
466
467		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
468		quickSave->setShortcut(tr("Shift+F%1").arg(i));
469		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
470		m_gameActions.append(quickSave);
471		addAction(quickSave);
472		quickSaveMenu->addAction(quickSave);
473	}
474
475#ifndef Q_OS_MAC
476	fileMenu->addSeparator();
477	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
478#endif
479
480	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
481	m_shortcutController->addMenu(emulationMenu);
482	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
483	reset->setShortcut(tr("Ctrl+R"));
484	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
485	m_gameActions.append(reset);
486	addControlledAction(emulationMenu, reset, "reset");
487
488	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
489	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
490	m_gameActions.append(shutdown);
491	addControlledAction(emulationMenu, shutdown, "shutdown");
492	emulationMenu->addSeparator();
493
494	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
495	pause->setChecked(false);
496	pause->setCheckable(true);
497	pause->setShortcut(tr("Ctrl+P"));
498	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
499	connect(m_controller, &GameController::gamePaused, [this, pause]() {
500		pause->setChecked(true);
501
502		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
503		QPixmap pixmap;
504		pixmap.convertFromImage(currentImage.rgbSwapped());
505		m_screenWidget->setPixmap(pixmap);
506	});
507	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
508	m_gameActions.append(pause);
509	addControlledAction(emulationMenu, pause, "pause");
510
511	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
512	frameAdvance->setShortcut(tr("Ctrl+N"));
513	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
514	m_gameActions.append(frameAdvance);
515	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
516
517	emulationMenu->addSeparator();
518
519	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
520	turbo->setCheckable(true);
521	turbo->setChecked(false);
522	turbo->setShortcut(tr("Shift+Tab"));
523	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
524	addControlledAction(emulationMenu, turbo, "fastForward");
525
526	QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
527	rewind->setShortcut(tr("`"));
528	connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
529	m_gameActions.append(rewind);
530	addControlledAction(emulationMenu, rewind, "rewind");
531
532	ConfigOption* videoSync = m_config->addOption("videoSync");
533	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
534	videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
535	m_config->updateOption("videoSync");
536
537	ConfigOption* audioSync = m_config->addOption("audioSync");
538	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
539	audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
540	m_config->updateOption("audioSync");
541
542	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
543	m_shortcutController->addMenu(avMenu);
544	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
545	m_shortcutController->addMenu(frameMenu, avMenu);
546	for (int i = 1; i <= 6; ++i) {
547		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
548		connect(setSize, &QAction::triggered, [this, i]() {
549			showNormal();
550			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
551		});
552		addControlledAction(frameMenu, setSize, tr("frame%1x").arg(QString::number(i)));
553	}
554	addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
555
556	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
557	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
558	lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
559	m_config->updateOption("lockAspectRatio");
560
561	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
562	resampleVideo->addBoolean(tr("Resample video"), avMenu);
563	resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
564	m_config->updateOption("resampleVideo");
565
566	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
567	ConfigOption* skip = m_config->addOption("frameskip");
568	skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
569	for (int i = 0; i <= 10; ++i) {
570		skip->addValue(QString::number(i), i, skipMenu);
571	}
572	m_config->updateOption("frameskip");
573
574	avMenu->addSeparator();
575
576	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
577	ConfigOption* buffers = m_config->addOption("audioBuffers");
578	buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
579	buffers->addValue(tr("512"), 512, buffersMenu);
580	buffers->addValue(tr("768"), 768, buffersMenu);
581	buffers->addValue(tr("1024"), 1024, buffersMenu);
582	buffers->addValue(tr("2048"), 2048, buffersMenu);
583	buffers->addValue(tr("4096"), 4096, buffersMenu);
584	m_config->updateOption("audioBuffers");
585
586	avMenu->addSeparator();
587
588	QMenu* target = avMenu->addMenu("FPS target");
589	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
590	fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
591	fpsTargetOption->addValue(tr("15"), 15, target);
592	fpsTargetOption->addValue(tr("30"), 30, target);
593	fpsTargetOption->addValue(tr("45"), 45, target);
594	fpsTargetOption->addValue(tr("60"), 60, target);
595	fpsTargetOption->addValue(tr("90"), 90, target);
596	fpsTargetOption->addValue(tr("120"), 120, target);
597	fpsTargetOption->addValue(tr("240"), 240, target);
598	m_config->updateOption("fpsTarget");
599
600#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
601	avMenu->addSeparator();
602#endif
603
604#ifdef USE_PNG
605	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
606	screenshot->setShortcut(tr("F12"));
607	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
608	m_gameActions.append(screenshot);
609	addControlledAction(avMenu, screenshot, "screenshot");
610#endif
611
612#ifdef USE_FFMPEG
613	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
614	recordOutput->setShortcut(tr("F11"));
615	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
616	addControlledAction(avMenu, recordOutput, "recordOutput");
617#endif
618
619#ifdef USE_MAGICK
620	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
621	recordGIF->setShortcut(tr("Shift+F11"));
622	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
623	addControlledAction(avMenu, recordGIF, "recordGIF");
624#endif
625
626	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
627	m_shortcutController->addMenu(toolsMenu);
628	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
629	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
630	addControlledAction(toolsMenu, viewLogs, "viewLogs");
631
632	QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
633	connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
634	addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
635
636#ifdef USE_GDB_STUB
637	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
638	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
639	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
640#endif
641
642	toolsMenu->addSeparator();
643	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
644	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
645
646	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
647	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
648	addControlledAction(toolsMenu, keymap, "remapKeyboard");
649
650#ifdef BUILD_SDL
651	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
652	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
653	addControlledAction(toolsMenu, gamepad, "remapGamepad");
654#endif
655
656	ConfigOption* skipBios = m_config->addOption("skipBios");
657	skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
658
659	ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
660	rewindEnable->connect([this](const QVariant& value) { m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
661
662	ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
663	rewindBufferCapacity->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
664
665	ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
666	rewindBufferInterval->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); });
667
668	QMenu* other = new QMenu(tr("Other"), this);
669	m_shortcutController->addMenu(other);
670	m_shortcutController->addFunctions(other, [this]() {
671		m_controller->setTurbo(true, false);
672	}, [this]() {
673		m_controller->setTurbo(false, false);
674	}, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
675
676	foreach (QAction* action, m_gameActions) {
677		action->setDisabled(true);
678	}
679}
680
681void Window::attachWidget(QWidget* widget) {
682	m_screenWidget->layout()->addWidget(widget);
683	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
684}
685
686void Window::detachWidget(QWidget* widget) {
687	m_screenWidget->layout()->removeWidget(widget);
688}
689
690void Window::appendMRU(const QString& fname) {
691	int index = m_mruFiles.indexOf(fname);
692	if (index >= 0) {
693		m_mruFiles.removeAt(index);
694	}
695	m_mruFiles.prepend(fname);
696	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
697		m_mruFiles.removeLast();
698	}
699	updateMRU();
700}
701
702void Window::updateMRU() {
703	if (!m_mruMenu) {
704		return;
705	}
706	m_mruMenu->clear();
707	int i = 0;
708	for (const QString& file : m_mruFiles) {
709		QAction* item = new QAction(file, m_mruMenu);
710		item->setShortcut(QString("Ctrl+%1").arg(i));
711		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
712		m_mruMenu->addAction(item);
713		++i;
714	}
715	m_config->setMRU(m_mruFiles);
716	m_config->write();
717	m_mruMenu->setEnabled(i > 0);
718}
719
720QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
721	m_shortcutController->addAction(menu, action, name);
722	menu->addAction(action);
723	addAction(action);
724	return action;
725}
726
727WindowBackground::WindowBackground(QWidget* parent)
728	: QLabel(parent)
729{
730	setLayout(new QStackedLayout());
731	layout()->setContentsMargins(0, 0, 0, 0);
732	setAlignment(Qt::AlignCenter);
733	QPalette p = palette();
734	p.setColor(backgroundRole(), Qt::black);
735	setPalette(p);
736	setAutoFillBackground(true);
737}
738
739void WindowBackground::setSizeHint(const QSize& hint) {
740	m_sizeHint = hint;
741}
742
743QSize WindowBackground::sizeHint() const {
744	return m_sizeHint;
745}