all repos — mgba @ ac686e3942bd3ba02b233b31286db2cae4661ba3

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	setupMenu(menuBar());
100}
101
102Window::~Window() {
103	delete m_logView;
104
105#ifdef USE_FFMPEG
106	delete m_videoView;
107#endif
108
109#ifdef USE_MAGICK
110	delete m_gifView;
111#endif
112}
113
114void Window::argumentsPassed(GBAArguments* args) {
115	loadConfig();
116
117	if (args->patch) {
118		m_controller->loadPatch(args->patch);
119	}
120
121	if (args->fname) {
122		m_controller->loadGame(args->fname, args->dirmode);
123	}
124}
125
126void Window::resizeFrame(int width, int height) {
127	QSize newSize(width, height);
128	newSize -= m_screenWidget->size();
129	newSize += size();
130	resize(newSize);
131}
132
133void Window::setConfig(ConfigController* config) {
134	m_config = config;
135}
136
137void Window::loadConfig() {
138	const GBAOptions* opts = m_config->options();
139
140	m_logView->setLevels(opts->logLevel);
141
142	m_controller->setFrameskip(opts->frameskip);
143	m_controller->setAudioSync(opts->audioSync);
144	m_controller->setVideoSync(opts->videoSync);
145	m_controller->setSkipBIOS(opts->skipBios);
146	m_display->lockAspectRatio(opts->lockAspectRatio);
147	m_display->filter(opts->resampleVideo);
148
149	if (opts->bios) {
150		m_controller->loadBIOS(opts->bios);
151	}
152
153	if (opts->fpsTarget) {
154		emit fpsTargetChanged(opts->fpsTarget);
155	}
156
157	if (opts->audioBuffers) {
158		emit audioBufferSamplesChanged(opts->audioBuffers);
159	}
160
161	if (opts->width && opts->height) {
162		resizeFrame(opts->width, opts->height);
163	}
164
165	m_mruFiles = m_config->getMRU();
166	updateMRU();
167
168	m_inputController.setConfiguration(m_config);
169}
170
171void Window::saveConfig() {
172	m_config->write();
173}
174
175void Window::selectROM() {
176	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
177	if (!filename.isEmpty()) {
178		m_controller->loadGame(filename);
179	}
180}
181
182void Window::selectBIOS() {
183	QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
184	if (!filename.isEmpty()) {
185		m_config->setOption("bios", filename);
186		m_config->updateOption("bios");
187		m_controller->loadBIOS(filename);
188	}
189}
190
191void Window::selectPatch() {
192	QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), QString(), tr("Patches (*.ips *.ups)"));
193	if (!filename.isEmpty()) {
194		m_controller->loadPatch(filename);
195	}
196}
197
198void Window::openKeymapWindow() {
199	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
200	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
201	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
202	keyEditor->show();
203}
204
205void Window::openSettingsWindow() {
206	SettingsView* settingsWindow = new SettingsView(m_config);
207	connect(this, SIGNAL(shutdown()), settingsWindow, SLOT(close()));
208	connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
209	settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
210	settingsWindow->show();
211}
212
213void Window::openShortcutWindow() {
214	ShortcutView* shortcutView = new ShortcutView();
215	shortcutView->setController(m_shortcutController);
216	shortcutView->setInputController(&m_inputController);
217	connect(this, SIGNAL(shutdown()), shortcutView, SLOT(close()));
218	shortcutView->setAttribute(Qt::WA_DeleteOnClose);
219	shortcutView->show();
220}
221
222void Window::openGamePakWindow() {
223	GamePakView* gamePakWindow = new GamePakView(m_controller);
224	connect(this, SIGNAL(shutdown()), gamePakWindow, SLOT(close()));
225	gamePakWindow->setAttribute(Qt::WA_DeleteOnClose);
226	gamePakWindow->show();
227}
228
229#ifdef BUILD_SDL
230void Window::openGamepadWindow() {
231	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON);
232	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
233	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
234	keyEditor->show();
235}
236#endif
237
238#ifdef USE_FFMPEG
239void Window::openVideoWindow() {
240	if (!m_videoView) {
241		m_videoView = new VideoView();
242		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
243		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
244		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
245		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
246		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
247	}
248	m_videoView->show();
249}
250#endif
251
252#ifdef USE_MAGICK
253void Window::openGIFWindow() {
254	if (!m_gifView) {
255		m_gifView = new GIFView();
256		connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
257		connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
258		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
259		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
260		connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
261	}
262	m_gifView->show();
263}
264#endif
265
266#ifdef USE_GDB_STUB
267void Window::gdbOpen() {
268	if (!m_gdbController) {
269		m_gdbController = new GDBController(m_controller, this);
270	}
271	GDBWindow* window = new GDBWindow(m_gdbController);
272	window->show();
273}
274#endif
275
276void Window::keyPressEvent(QKeyEvent* event) {
277	if (event->isAutoRepeat()) {
278		QWidget::keyPressEvent(event);
279		return;
280	}
281	GBAKey key = m_inputController.mapKeyboard(event->key());
282	if (key == GBA_KEY_NONE) {
283		QWidget::keyPressEvent(event);
284		return;
285	}
286	m_controller->keyPressed(key);
287	event->accept();
288}
289
290void Window::keyReleaseEvent(QKeyEvent* event) {
291	if (event->isAutoRepeat()) {
292		QWidget::keyReleaseEvent(event);
293		return;
294	}
295	GBAKey key = m_inputController.mapKeyboard(event->key());
296	if (key == GBA_KEY_NONE) {
297		QWidget::keyPressEvent(event);
298		return;
299	}
300	m_controller->keyReleased(key);
301	event->accept();
302}
303
304void Window::resizeEvent(QResizeEvent*) {
305	redoLogo();
306	m_config->setOption("height", m_screenWidget->height());
307	m_config->setOption("width", m_screenWidget->width());
308}
309
310void Window::closeEvent(QCloseEvent* event) {
311	emit shutdown();
312	QMainWindow::closeEvent(event);
313}
314
315void Window::focusOutEvent(QFocusEvent*) {
316	m_controller->setTurbo(false, false);
317	m_controller->clearKeys();
318}
319
320void Window::toggleFullScreen() {
321	if (isFullScreen()) {
322		showNormal();
323		menuBar()->show();
324	} else {
325		showFullScreen();
326#ifndef Q_OS_MAC
327		if (m_controller->isLoaded() && !m_controller->isPaused()) {
328			menuBar()->hide();
329		}
330#endif
331	}
332}
333
334void Window::gameStarted(GBAThread* context) {
335	char title[13] = { '\0' };
336	MutexLock(&context->stateMutex);
337	if (context->state < THREAD_EXITING) {
338		emit startDrawing(m_controller->drawContext(), context);
339		GBAGetGameTitle(context->gba, title);
340	} else {
341		MutexUnlock(&context->stateMutex);
342		return;
343	}
344	MutexUnlock(&context->stateMutex);
345	foreach (QAction* action, m_gameActions) {
346		action->setDisabled(false);
347	}
348	appendMRU(context->fname);
349	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
350	attachWidget(m_display);
351	m_screenWidget->setScaledContents(true);
352
353#ifndef Q_OS_MAC
354	if(isFullScreen()) {
355		menuBar()->hide();
356	}
357#endif
358
359	m_fpsTimer.start();
360}
361
362void Window::gameStopped() {
363	foreach (QAction* action, m_gameActions) {
364		action->setDisabled(true);
365	}
366	setWindowTitle(tr(PROJECT_NAME));
367	detachWidget(m_display);
368	m_screenWidget->setScaledContents(false);
369	redoLogo();
370
371	m_fpsTimer.stop();
372}
373
374void Window::gameCrashed(const QString& errorMessage) {
375	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
376		tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
377		QMessageBox::Ok, this,  Qt::Sheet);
378	crash->setAttribute(Qt::WA_DeleteOnClose);
379	crash->show();
380}
381
382void Window::redoLogo() {
383	if (m_controller->isLoaded()) {
384		return;
385	}
386	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
387	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
388	m_screenWidget->setPixmap(logo);
389}
390
391void Window::recordFrame() {
392	m_frameList.append(QDateTime::currentDateTime());
393	while (m_frameList.count() > FRAME_LIST_SIZE) {
394		m_frameList.removeFirst();
395	}
396}
397
398void Window::showFPS() {
399	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
400	float fps = (m_frameList.count() - 1) * 10000.f / interval;
401	fps = round(fps) / 10.f;
402	char title[13] = { '\0' };
403	GBAGetGameTitle(m_controller->thread()->gba, title);
404	setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
405}
406
407void Window::openStateWindow(LoadSave ls) {
408	if (m_stateWindow) {
409		return;
410	}
411	bool wasPaused = m_controller->isPaused();
412	m_stateWindow = new LoadSaveState(m_controller);
413	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
414	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
415	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
416		m_screenWidget->layout()->removeWidget(m_stateWindow);
417		m_stateWindow = nullptr;
418		setFocus();
419	});
420	if (!wasPaused) {
421		m_controller->setPaused(true);
422		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
423	}
424	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
425	m_stateWindow->setMode(ls);
426	attachWidget(m_stateWindow);
427}
428
429void Window::setupMenu(QMenuBar* menubar) {
430	menubar->clear();
431	QMenu* fileMenu = menubar->addMenu(tr("&File"));
432	m_shortcutController->addMenu(fileMenu);
433	installEventFilter(m_shortcutController);
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	m_shortcutController->addMenu(frameMenu, avMenu);
538	for (int i = 1; i <= 6; ++i) {
539		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
540		connect(setSize, &QAction::triggered, [this, i]() {
541			showNormal();
542			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
543		});
544		addControlledAction(frameMenu, setSize, tr("frame%1x").arg(QString::number(i)));
545	}
546	addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
547
548	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
549	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
550	lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
551	m_config->updateOption("lockAspectRatio");
552
553	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
554	resampleVideo->addBoolean(tr("Resample video"), avMenu);
555	resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
556	m_config->updateOption("resampleVideo");
557
558	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
559	ConfigOption* skip = m_config->addOption("frameskip");
560	skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
561	for (int i = 0; i <= 10; ++i) {
562		skip->addValue(QString::number(i), i, skipMenu);
563	}
564	m_config->updateOption("frameskip");
565
566	avMenu->addSeparator();
567
568	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
569	ConfigOption* buffers = m_config->addOption("audioBuffers");
570	buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
571	buffers->addValue(tr("512"), 512, buffersMenu);
572	buffers->addValue(tr("768"), 768, buffersMenu);
573	buffers->addValue(tr("1024"), 1024, buffersMenu);
574	buffers->addValue(tr("2048"), 2048, buffersMenu);
575	buffers->addValue(tr("4096"), 4096, buffersMenu);
576	m_config->updateOption("audioBuffers");
577
578	avMenu->addSeparator();
579
580	QMenu* target = avMenu->addMenu("FPS target");
581	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
582	fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
583	fpsTargetOption->addValue(tr("15"), 15, target);
584	fpsTargetOption->addValue(tr("30"), 30, target);
585	fpsTargetOption->addValue(tr("45"), 45, target);
586	fpsTargetOption->addValue(tr("60"), 60, target);
587	fpsTargetOption->addValue(tr("90"), 90, target);
588	fpsTargetOption->addValue(tr("120"), 120, target);
589	fpsTargetOption->addValue(tr("240"), 240, target);
590	m_config->updateOption("fpsTarget");
591
592#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
593	avMenu->addSeparator();
594#endif
595
596#ifdef USE_PNG
597	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
598	screenshot->setShortcut(tr("F12"));
599	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
600	m_gameActions.append(screenshot);
601	addControlledAction(avMenu, screenshot, "screenshot");
602#endif
603
604#ifdef USE_FFMPEG
605	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
606	recordOutput->setShortcut(tr("F11"));
607	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
608	addControlledAction(avMenu, recordOutput, "recordOutput");
609#endif
610
611#ifdef USE_MAGICK
612	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
613	recordGIF->setShortcut(tr("Shift+F11"));
614	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
615	addControlledAction(avMenu, recordGIF, "recordGIF");
616#endif
617
618	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
619	m_shortcutController->addMenu(toolsMenu);
620	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
621	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
622	addControlledAction(toolsMenu, viewLogs, "viewLogs");
623
624	QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
625	connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
626	addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
627
628#ifdef USE_GDB_STUB
629	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
630	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
631	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
632#endif
633
634	toolsMenu->addSeparator();
635	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
636	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
637
638	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
639	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
640	addControlledAction(toolsMenu, keymap, "remapKeyboard");
641
642#ifdef BUILD_SDL
643	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
644	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
645	addControlledAction(toolsMenu, gamepad, "remapGamepad");
646#endif
647
648	ConfigOption* skipBios = m_config->addOption("skipBios");
649	skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
650
651	QMenu* other = new QMenu(tr("Other"), this);
652	m_shortcutController->addMenu(other);
653	m_shortcutController->addFunctions(other, [this]() {
654		m_controller->setTurbo(true, false);
655	}, [this]() {
656		m_controller->setTurbo(false, false);
657	}, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
658
659	foreach (QAction* action, m_gameActions) {
660		action->setDisabled(true);
661	}
662}
663
664void Window::attachWidget(QWidget* widget) {
665	m_screenWidget->layout()->addWidget(widget);
666	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
667}
668
669void Window::detachWidget(QWidget* widget) {
670	m_screenWidget->layout()->removeWidget(widget);
671}
672
673void Window::appendMRU(const QString& fname) {
674	int index = m_mruFiles.indexOf(fname);
675	if (index >= 0) {
676		m_mruFiles.removeAt(index);
677	}
678	m_mruFiles.prepend(fname);
679	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
680		m_mruFiles.removeLast();
681	}
682	updateMRU();
683}
684
685void Window::updateMRU() {
686	if (!m_mruMenu) {
687		return;
688	}
689	m_mruMenu->clear();
690	int i = 0;
691	for (const QString& file : m_mruFiles) {
692		QAction* item = new QAction(file, m_mruMenu);
693		item->setShortcut(QString("Ctrl+%1").arg(i));
694		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
695		m_mruMenu->addAction(item);
696		++i;
697	}
698	m_config->setMRU(m_mruFiles);
699	m_config->write();
700	m_mruMenu->setEnabled(i > 0);
701}
702
703QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
704	m_shortcutController->addAction(menu, action, name);
705	menu->addAction(action);
706	addAction(action);
707	return action;
708}
709
710WindowBackground::WindowBackground(QWidget* parent)
711	: QLabel(parent)
712{
713	setLayout(new QStackedLayout());
714	layout()->setContentsMargins(0, 0, 0, 0);
715	setAlignment(Qt::AlignCenter);
716	QPalette p = palette();
717	p.setColor(backgroundRole(), Qt::black);
718	setPalette(p);
719	setAutoFillBackground(true);
720}
721
722void WindowBackground::setSizeHint(const QSize& hint) {
723	m_sizeHint = hint;
724}
725
726QSize WindowBackground::sizeHint() const {
727	return m_sizeHint;
728}