all repos — mgba @ 21f9c0107a1daef41d8257a7939e8f47f2af1ef5

mGBA Game Boy Advance Emulator

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

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