all repos — mgba @ d15c4f4bfb2ee7fcb920a391812b72e746353208

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	connect(this, SIGNAL(shutdown()), shortcutView, SLOT(close()));
217	shortcutView->setAttribute(Qt::WA_DeleteOnClose);
218	shortcutView->show();
219}
220
221void Window::openGamePakWindow() {
222	GamePakView* gamePakWindow = new GamePakView(m_controller);
223	connect(this, SIGNAL(shutdown()), gamePakWindow, SLOT(close()));
224	gamePakWindow->setAttribute(Qt::WA_DeleteOnClose);
225	gamePakWindow->show();
226}
227
228#ifdef BUILD_SDL
229void Window::openGamepadWindow() {
230	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON);
231	connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
232	keyEditor->setAttribute(Qt::WA_DeleteOnClose);
233	keyEditor->show();
234}
235#endif
236
237#ifdef USE_FFMPEG
238void Window::openVideoWindow() {
239	if (!m_videoView) {
240		m_videoView = new VideoView();
241		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
242		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
243		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
244		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
245		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
246	}
247	m_videoView->show();
248}
249#endif
250
251#ifdef USE_MAGICK
252void Window::openGIFWindow() {
253	if (!m_gifView) {
254		m_gifView = new GIFView();
255		connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
256		connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
257		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
258		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
259		connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
260	}
261	m_gifView->show();
262}
263#endif
264
265#ifdef USE_GDB_STUB
266void Window::gdbOpen() {
267	if (!m_gdbController) {
268		m_gdbController = new GDBController(m_controller, this);
269	}
270	GDBWindow* window = new GDBWindow(m_gdbController);
271	window->show();
272}
273#endif
274
275void Window::keyPressEvent(QKeyEvent* event) {
276	if (event->isAutoRepeat()) {
277		QWidget::keyPressEvent(event);
278		return;
279	}
280	if (event->key() == Qt::Key_Tab) {
281		m_controller->setTurbo(true, false);
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	if (event->key() == Qt::Key_Tab) {
298		m_controller->setTurbo(false, false);
299	}
300	GBAKey key = m_inputController.mapKeyboard(event->key());
301	if (key == GBA_KEY_NONE) {
302		QWidget::keyPressEvent(event);
303		return;
304	}
305	m_controller->keyReleased(key);
306	event->accept();
307}
308
309void Window::resizeEvent(QResizeEvent*) {
310	redoLogo();
311	m_config->setOption("height", m_screenWidget->height());
312	m_config->setOption("width", m_screenWidget->width());
313}
314
315void Window::closeEvent(QCloseEvent* event) {
316	emit shutdown();
317	QMainWindow::closeEvent(event);
318}
319
320void Window::focusOutEvent(QFocusEvent*) {
321	m_controller->setTurbo(false, false);
322	m_controller->clearKeys();
323}
324
325void Window::toggleFullScreen() {
326	if (isFullScreen()) {
327		showNormal();
328		menuBar()->show();
329	} else {
330		showFullScreen();
331#ifndef Q_OS_MAC
332		if (m_controller->isLoaded() && !m_controller->isPaused()) {
333			menuBar()->hide();
334		}
335#endif
336	}
337}
338
339void Window::gameStarted(GBAThread* context) {
340	emit startDrawing(m_controller->drawContext(), context);
341	foreach (QAction* action, m_gameActions) {
342		action->setDisabled(false);
343	}
344	appendMRU(context->fname);
345	char title[13] = { '\0' };
346	GBAGetGameTitle(context->gba, title);
347	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
348	attachWidget(m_display);
349	m_screenWidget->setScaledContents(true);
350
351#ifndef Q_OS_MAC
352	if(isFullScreen()) {
353		menuBar()->hide();
354	}
355#endif
356
357	m_fpsTimer.start();
358}
359
360void Window::gameStopped() {
361	foreach (QAction* action, m_gameActions) {
362		action->setDisabled(true);
363	}
364	setWindowTitle(tr(PROJECT_NAME));
365	detachWidget(m_display);
366	m_screenWidget->setScaledContents(false);
367	redoLogo();
368
369	m_fpsTimer.stop();
370}
371
372void Window::gameCrashed(const QString& errorMessage) {
373	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
374		tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
375		QMessageBox::Ok, this,  Qt::Sheet);
376	crash->setAttribute(Qt::WA_DeleteOnClose);
377	crash->show();
378}
379
380void Window::redoLogo() {
381	if (m_controller->isLoaded()) {
382		return;
383	}
384	QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
385	logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
386	m_screenWidget->setPixmap(logo);
387}
388
389void Window::recordFrame() {
390	m_frameList.append(QDateTime::currentDateTime());
391	while (m_frameList.count() > FRAME_LIST_SIZE) {
392		m_frameList.removeFirst();
393	}
394}
395
396void Window::showFPS() {
397	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
398	float fps = (m_frameList.count() - 1) * 10000.f / interval;
399	fps = round(fps) / 10.f;
400	char title[13] = { '\0' };
401	GBAGetGameTitle(m_controller->thread()->gba, title);
402	setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
403}
404
405void Window::openStateWindow(LoadSave ls) {
406	if (m_stateWindow) {
407		return;
408	}
409	bool wasPaused = m_controller->isPaused();
410	m_stateWindow = new LoadSaveState(m_controller);
411	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
412	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
413	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
414		m_screenWidget->layout()->removeWidget(m_stateWindow);
415		m_stateWindow = nullptr;
416		setFocus();
417	});
418	if (!wasPaused) {
419		m_controller->setPaused(true);
420		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
421	}
422	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
423	m_stateWindow->setMode(ls);
424	attachWidget(m_stateWindow);
425}
426
427void Window::setupMenu(QMenuBar* menubar) {
428	menubar->clear();
429	QMenu* fileMenu = menubar->addMenu(tr("&File"));
430	m_shortcutController->addMenu(fileMenu);
431	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
432	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
433	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
434
435	m_mruMenu = fileMenu->addMenu(tr("Recent"));
436
437	fileMenu->addSeparator();
438
439	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
440	loadState->setShortcut(tr("F10"));
441	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
442	m_gameActions.append(loadState);
443	addControlledAction(fileMenu, loadState, "loadState");
444
445	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
446	saveState->setShortcut(tr("Shift+F10"));
447	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
448	m_gameActions.append(saveState);
449	addControlledAction(fileMenu, saveState, "saveState");
450
451	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
452	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
453	int i;
454	for (i = 1; i < 10; ++i) {
455		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
456		quickLoad->setShortcut(tr("F%1").arg(i));
457		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
458		m_gameActions.append(quickLoad);
459		addAction(quickLoad);
460		quickLoadMenu->addAction(quickLoad);
461
462		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
463		quickSave->setShortcut(tr("Shift+F%1").arg(i));
464		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
465		m_gameActions.append(quickSave);
466		addAction(quickSave);
467		quickSaveMenu->addAction(quickSave);
468	}
469
470#ifndef Q_OS_MAC
471	fileMenu->addSeparator();
472	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
473#endif
474
475	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
476	m_shortcutController->addMenu(emulationMenu);
477	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
478	reset->setShortcut(tr("Ctrl+R"));
479	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
480	m_gameActions.append(reset);
481	addControlledAction(emulationMenu, reset, "reset");
482
483	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
484	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
485	m_gameActions.append(shutdown);
486	addControlledAction(emulationMenu, shutdown, "shutdown");
487	emulationMenu->addSeparator();
488
489	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
490	pause->setChecked(false);
491	pause->setCheckable(true);
492	pause->setShortcut(tr("Ctrl+P"));
493	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
494	connect(m_controller, &GameController::gamePaused, [this, pause]() {
495		pause->setChecked(true);
496
497		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
498		QPixmap pixmap;
499		pixmap.convertFromImage(currentImage.rgbSwapped());
500		m_screenWidget->setPixmap(pixmap);
501	});
502	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
503	m_gameActions.append(pause);
504	addControlledAction(emulationMenu, pause, "pause");
505
506	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
507	frameAdvance->setShortcut(tr("Ctrl+N"));
508	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
509	m_gameActions.append(frameAdvance);
510	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
511
512	emulationMenu->addSeparator();
513
514	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
515	turbo->setCheckable(true);
516	turbo->setChecked(false);
517	turbo->setShortcut(tr("Shift+Tab"));
518	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
519	addControlledAction(emulationMenu, turbo, "fastForward");
520
521	ConfigOption* videoSync = m_config->addOption("videoSync");
522	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
523	videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
524	m_config->updateOption("videoSync");
525
526	ConfigOption* audioSync = m_config->addOption("audioSync");
527	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
528	audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
529	m_config->updateOption("audioSync");
530
531	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
532	m_shortcutController->addMenu(avMenu);
533	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
534	for (int i = 1; i <= 6; ++i) {
535		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
536		connect(setSize, &QAction::triggered, [this, i]() {
537			showNormal();
538			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
539		});
540		frameMenu->addAction(setSize);
541	}
542	addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
543
544	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
545	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
546	lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
547	m_config->updateOption("lockAspectRatio");
548
549	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
550	resampleVideo->addBoolean(tr("Resample video"), avMenu);
551	resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
552	m_config->updateOption("resampleVideo");
553
554	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
555	ConfigOption* skip = m_config->addOption("frameskip");
556	skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
557	for (int i = 0; i <= 10; ++i) {
558		skip->addValue(QString::number(i), i, skipMenu);
559	}
560	m_config->updateOption("frameskip");
561
562	avMenu->addSeparator();
563
564	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
565	ConfigOption* buffers = m_config->addOption("audioBuffers");
566	buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
567	buffers->addValue(tr("512"), 512, buffersMenu);
568	buffers->addValue(tr("768"), 768, buffersMenu);
569	buffers->addValue(tr("1024"), 1024, buffersMenu);
570	buffers->addValue(tr("2048"), 2048, buffersMenu);
571	buffers->addValue(tr("4096"), 4096, buffersMenu);
572	m_config->updateOption("audioBuffers");
573
574	avMenu->addSeparator();
575
576	QMenu* target = avMenu->addMenu("FPS target");
577	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
578	fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
579	fpsTargetOption->addValue(tr("15"), 15, target);
580	fpsTargetOption->addValue(tr("30"), 30, target);
581	fpsTargetOption->addValue(tr("45"), 45, target);
582	fpsTargetOption->addValue(tr("60"), 60, target);
583	fpsTargetOption->addValue(tr("90"), 90, target);
584	fpsTargetOption->addValue(tr("120"), 120, target);
585	fpsTargetOption->addValue(tr("240"), 240, target);
586	m_config->updateOption("fpsTarget");
587
588#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
589	avMenu->addSeparator();
590#endif
591
592#ifdef USE_PNG
593	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
594	screenshot->setShortcut(tr("F12"));
595	connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
596	m_gameActions.append(screenshot);
597	addControlledAction(avMenu, screenshot, "screenshot");
598#endif
599
600#ifdef USE_FFMPEG
601	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
602	recordOutput->setShortcut(tr("F11"));
603	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
604	addControlledAction(avMenu, recordOutput, "recordOutput");
605#endif
606
607#ifdef USE_MAGICK
608	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
609	recordGIF->setShortcut(tr("Shift+F11"));
610	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
611	addControlledAction(avMenu, recordGIF, "recordGIF");
612#endif
613
614	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
615	m_shortcutController->addMenu(toolsMenu);
616	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
617	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
618	addControlledAction(toolsMenu, viewLogs, "viewLogs");
619
620	QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
621	connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
622	addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
623
624#ifdef USE_GDB_STUB
625	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
626	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
627	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
628#endif
629
630	toolsMenu->addSeparator();
631	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
632	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
633
634	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
635	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
636	addControlledAction(toolsMenu, keymap, "remapKeyboard");
637
638#ifdef BUILD_SDL
639	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
640	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
641	addControlledAction(toolsMenu, gamepad, "remapGamepad");
642#endif
643
644	ConfigOption* skipBios = m_config->addOption("skipBios");
645	skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
646
647	foreach (QAction* action, m_gameActions) {
648		action->setDisabled(true);
649	}
650}
651
652void Window::attachWidget(QWidget* widget) {
653	m_screenWidget->layout()->addWidget(widget);
654	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
655}
656
657void Window::detachWidget(QWidget* widget) {
658	m_screenWidget->layout()->removeWidget(widget);
659}
660
661void Window::appendMRU(const QString& fname) {
662	int index = m_mruFiles.indexOf(fname);
663	if (index >= 0) {
664		m_mruFiles.removeAt(index);
665	}
666	m_mruFiles.prepend(fname);
667	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
668		m_mruFiles.removeLast();
669	}
670	updateMRU();
671}
672
673void Window::updateMRU() {
674	if (!m_mruMenu) {
675		return;
676	}
677	m_mruMenu->clear();
678	int i = 0;
679	for (const QString& file : m_mruFiles) {
680		QAction* item = new QAction(file, m_mruMenu);
681		item->setShortcut(QString("Ctrl+%1").arg(i));
682		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
683		m_mruMenu->addAction(item);
684		++i;
685	}
686	m_config->setMRU(m_mruFiles);
687	m_config->write();
688	m_mruMenu->setEnabled(i > 0);
689}
690
691QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
692	m_shortcutController->addAction(menu, action, name);
693	menu->addAction(action);
694	addAction(action);
695	return action;
696}
697
698WindowBackground::WindowBackground(QWidget* parent)
699	: QLabel(parent)
700{
701	setLayout(new QStackedLayout());
702	layout()->setContentsMargins(0, 0, 0, 0);
703	setAlignment(Qt::AlignCenter);
704	QPalette p = palette();
705	p.setColor(backgroundRole(), Qt::black);
706	setPalette(p);
707	setAutoFillBackground(true);
708}
709
710void WindowBackground::setSizeHint(const QSize& hint) {
711	m_sizeHint = hint;
712}
713
714QSize WindowBackground::sizeHint() const {
715	return m_sizeHint;
716}