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