all repos — mgba @ 38a4e9988f8c6c210b2fd70790ff1f0f7006515b

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