all repos — mgba @ b2cceffdae98d4b74590b19303dc68f3758f325b

mGBA Game Boy Advance Emulator

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

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