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 <QDesktopWidget>
9#include <QKeyEvent>
10#include <QKeySequence>
11#include <QMenuBar>
12#include <QMessageBox>
13#include <QMimeData>
14#include <QPainter>
15#include <QStackedLayout>
16
17#include "AboutScreen.h"
18#include "CheatsView.h"
19#include "ConfigController.h"
20#include "Display.h"
21#include "GameController.h"
22#include "GBAApp.h"
23#include "GDBController.h"
24#include "GDBWindow.h"
25#include "GIFView.h"
26#include "IOViewer.h"
27#include "LoadSaveState.h"
28#include "LogView.h"
29#include "MultiplayerController.h"
30#include "MemoryView.h"
31#include "OverrideView.h"
32#include "PaletteView.h"
33#include "ROMInfo.h"
34#include "SensorView.h"
35#include "SettingsView.h"
36#include "ShaderSelector.h"
37#include "ShortcutController.h"
38#include "VideoView.h"
39
40extern "C" {
41#include "core/version.h"
42#ifdef M_CORE_GB
43#include "gb/gb.h"
44#endif
45#include "feature/commandline.h"
46#include "util/nointro.h"
47#include "util/vfs.h"
48}
49
50using namespace QGBA;
51
52Window::Window(ConfigController* config, int playerId, QWidget* parent)
53 : QMainWindow(parent)
54 , m_log(0)
55 , m_logView(new LogView(&m_log))
56 , m_stateWindow(nullptr)
57 , m_screenWidget(new WindowBackground())
58 , m_logo(":/res/mgba-1024.png")
59 , m_config(config)
60 , m_inputController(playerId, this)
61#ifdef USE_FFMPEG
62 , m_videoView(nullptr)
63#endif
64#ifdef USE_MAGICK
65 , m_gifView(nullptr)
66#endif
67#ifdef USE_GDB_STUB
68 , m_gdbController(nullptr)
69#endif
70 , m_mruMenu(nullptr)
71 , m_shortcutController(new ShortcutController(this))
72 , m_playerId(playerId)
73 , m_fullscreenOnStart(false)
74 , m_autoresume(false)
75{
76 setFocusPolicy(Qt::StrongFocus);
77 setAcceptDrops(true);
78 setAttribute(Qt::WA_DeleteOnClose);
79 m_controller = new GameController(this);
80 m_controller->setInputController(&m_inputController);
81 updateTitle();
82
83 m_display = Display::create(this);
84 m_shaderView = new ShaderSelector(m_display, m_config);
85
86 m_logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
87 m_logo = m_logo; // Free memory left over in old pixmap
88
89 m_screenWidget->setMinimumSize(m_display->minimumSize());
90 m_screenWidget->setSizePolicy(m_display->sizePolicy());
91 m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
92 m_screenWidget->setPixmap(m_logo);
93 m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
94 setCentralWidget(m_screenWidget);
95
96 connect(m_controller, SIGNAL(gameStarted(mCoreThread*, const QString&)), this, SLOT(gameStarted(mCoreThread*, const QString&)));
97 connect(m_controller, SIGNAL(gameStarted(mCoreThread*, const QString&)), &m_inputController, SLOT(suspendScreensaver()));
98 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), m_display, SLOT(stopDrawing()));
99 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(gameStopped()));
100 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), &m_inputController, SLOT(resumeScreensaver()));
101 connect(m_controller, SIGNAL(stateLoaded(mCoreThread*)), m_display, SLOT(forceDraw()));
102 connect(m_controller, SIGNAL(rewound(mCoreThread*)), m_display, SLOT(forceDraw()));
103 connect(m_controller, &GameController::gamePaused, [this](mCoreThread* context) {
104 unsigned width, height;
105 context->core->desiredVideoDimensions(context->core, &width, &height);
106 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), width, height,
107 width * BYTES_PER_PIXEL, QImage::Format_RGBX8888);
108 QPixmap pixmap;
109 pixmap.convertFromImage(currentImage);
110 m_screenWidget->setPixmap(pixmap);
111 m_screenWidget->setLockAspectRatio(width, height);
112 });
113 connect(m_controller, SIGNAL(gamePaused(mCoreThread*)), m_display, SLOT(pauseDrawing()));
114#ifndef Q_OS_MAC
115 connect(m_controller, SIGNAL(gamePaused(mCoreThread*)), menuBar(), SLOT(show()));
116 connect(m_controller, &GameController::gameUnpaused, [this]() {
117 if(isFullScreen()) {
118 menuBar()->hide();
119 }
120 });
121#endif
122 connect(m_controller, SIGNAL(gamePaused(mCoreThread*)), &m_inputController, SLOT(resumeScreensaver()));
123 connect(m_controller, SIGNAL(gameUnpaused(mCoreThread*)), m_display, SLOT(unpauseDrawing()));
124 connect(m_controller, SIGNAL(gameUnpaused(mCoreThread*)), &m_inputController, SLOT(suspendScreensaver()));
125 connect(m_controller, SIGNAL(postLog(int, int, const QString&)), &m_log, SLOT(postLog(int, int, const QString&)));
126 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
127 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), m_display, SLOT(framePosted(const uint32_t*)));
128 connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
129 connect(m_controller, SIGNAL(gameFailed()), this, SLOT(gameFailed()));
130 connect(m_controller, SIGNAL(unimplementedBiosCall(int)), this, SLOT(unimplementedBiosCall(int)));
131 connect(m_controller, SIGNAL(statusPosted(const QString&)), m_display, SLOT(showMessage(const QString&)));
132 connect(&m_log, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
133 connect(&m_log, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
134 connect(&m_log, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
135 connect(this, SIGNAL(startDrawing(mCoreThread*)), m_display, SLOT(startDrawing(mCoreThread*)), Qt::QueuedConnection);
136 connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
137 connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
138 connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
139 connect(this, SIGNAL(shutdown()), m_shaderView, SLOT(hide()));
140 connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
141 connect(this, SIGNAL(sampleRateChanged(unsigned)), m_controller, SLOT(setAudioSampleRate(unsigned)));
142 connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
143 connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
144 connect(&m_focusCheck, SIGNAL(timeout()), this, SLOT(focusCheck()));
145 connect(m_display, &Display::hideCursor, [this]() {
146 if (static_cast<QStackedLayout*>(m_screenWidget->layout())->currentWidget() == m_display) {
147 m_screenWidget->setCursor(Qt::BlankCursor);
148 }
149 });
150 connect(m_display, &Display::showCursor, [this]() {
151 m_screenWidget->unsetCursor();
152 });
153 connect(&m_inputController, SIGNAL(profileLoaded(const QString&)), m_shortcutController, SLOT(loadProfile(const QString&)));
154
155 m_log.setLevels(mLOG_WARN | mLOG_ERROR | mLOG_FATAL);
156 m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
157 m_focusCheck.setInterval(200);
158
159 m_shortcutController->setConfigController(m_config);
160 setupMenu(menuBar());
161}
162
163Window::~Window() {
164 delete m_logView;
165
166#ifdef USE_FFMPEG
167 delete m_videoView;
168#endif
169
170#ifdef USE_MAGICK
171 delete m_gifView;
172#endif
173}
174
175void Window::argumentsPassed(mArguments* args) {
176 loadConfig();
177
178 if (args->patch) {
179 m_controller->loadPatch(args->patch);
180 }
181
182 if (args->fname) {
183 m_controller->loadGame(args->fname);
184 }
185}
186
187void Window::resizeFrame(const QSize& size) {
188 QSize newSize(size);
189 m_screenWidget->setSizeHint(newSize);
190 newSize -= m_screenWidget->size();
191 newSize += this->size();
192 resize(newSize);
193}
194
195void Window::setConfig(ConfigController* config) {
196 m_config = config;
197}
198
199void Window::loadConfig() {
200 const mCoreOptions* opts = m_config->options();
201 reloadConfig();
202
203 // TODO: Move these to ConfigController
204 if (opts->fpsTarget) {
205 emit fpsTargetChanged(opts->fpsTarget);
206 }
207
208 if (opts->audioBuffers) {
209 emit audioBufferSamplesChanged(opts->audioBuffers);
210 }
211
212 if (opts->sampleRate) {
213 emit sampleRateChanged(opts->sampleRate);
214 }
215
216 if (opts->width && opts->height) {
217 resizeFrame(QSize(opts->width, opts->height));
218 }
219
220 if (opts->fullscreen) {
221 enterFullScreen();
222 }
223
224 if (opts->shader) {
225 struct VDir* shader = VDirOpen(opts->shader);
226 if (shader) {
227 m_display->setShaders(shader);
228 m_shaderView->refreshShaders();
229 shader->close(shader);
230 }
231 }
232
233 m_mruFiles = m_config->getMRU();
234 updateMRU();
235
236 m_inputController.setConfiguration(m_config);
237 m_controller->setUseBIOS(opts->useBios);
238}
239
240void Window::reloadConfig() {
241 const mCoreOptions* opts = m_config->options();
242
243 m_log.setLevels(opts->logLevel);
244
245 QString saveStateExtdata = m_config->getOption("saveStateExtdata");
246 bool ok;
247 int flags = saveStateExtdata.toInt(&ok);
248 if (ok) {
249 m_controller->setSaveStateExtdata(flags);
250 }
251
252 QString loadStateExtdata = m_config->getOption("loadStateExtdata");
253 flags = loadStateExtdata.toInt(&ok);
254 if (ok) {
255 m_controller->setLoadStateExtdata(flags);
256 }
257
258 m_controller->setConfig(m_config->config());
259 m_display->lockAspectRatio(opts->lockAspectRatio);
260 m_display->filter(opts->resampleVideo);
261
262 if (opts->bios) {
263 m_controller->loadBIOS(opts->bios);
264 }
265
266 m_inputController.setScreensaverSuspendable(opts->suspendScreensaver);
267}
268
269void Window::saveConfig() {
270 m_inputController.saveConfiguration();
271 m_config->write();
272}
273
274void Window::selectROM() {
275 QStringList formats{
276 "*.gba",
277 "*.gb",
278 "*.gbc",
279#if defined(USE_LIBZIP) || defined(USE_ZLIB)
280 "*.zip",
281#endif
282#ifdef USE_LZMA
283 "*.7z",
284#endif
285 "*.agb",
286 "*.mb",
287 "*.rom",
288 "*.bin"};
289 QString filter = tr("Game Boy Advance ROMs (%1)").arg(formats.join(QChar(' ')));
290 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), filter);
291 if (!filename.isEmpty()) {
292 m_controller->loadGame(filename);
293 }
294}
295
296void Window::replaceROM() {
297 QStringList formats{
298 "*.gba",
299 "*.gb",
300 "*.gbc",
301#if defined(USE_LIBZIP) || defined(USE_ZLIB)
302 "*.zip",
303#endif
304#ifdef USE_LZMA
305 "*.7z",
306#endif
307 "*.rom",
308 "*.bin"};
309 QString filter = tr("Game Boy Advance ROMs (%1)").arg(formats.join(QChar(' ')));
310 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), filter);
311 if (!filename.isEmpty()) {
312 m_controller->replaceGame(filename);
313 }
314}
315
316void Window::multiplayerChanged() {
317 disconnect(nullptr, this, SLOT(multiplayerChanged()));
318 int attached = 1;
319 MultiplayerController* multiplayer = m_controller->multiplayerController();
320 if (multiplayer) {
321 attached = multiplayer->attached();
322 connect(multiplayer, SIGNAL(gameAttached()), this, SLOT(multiplayerChanged()));
323 connect(multiplayer, SIGNAL(gameDetached()), this, SLOT(multiplayerChanged()));
324 m_playerId = multiplayer->playerId(m_controller);
325 }
326 if (m_controller->isLoaded()) {
327 for (QAction* action : m_nonMpActions) {
328 action->setDisabled(attached > 1);
329 }
330 }
331}
332
333void Window::selectBIOS() {
334 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
335 if (!filename.isEmpty()) {
336 m_config->setOption("bios", filename);
337 m_config->updateOption("bios");
338 m_config->setOption("useBios", true);
339 m_config->updateOption("useBios");
340 m_controller->loadBIOS(filename);
341 }
342}
343
344void Window::selectPatch() {
345 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select patch"), tr("Patches (*.ips *.ups *.bps)"));
346 if (!filename.isEmpty()) {
347 m_controller->loadPatch(filename);
348 }
349}
350
351void Window::openView(QWidget* widget) {
352 connect(this, SIGNAL(shutdown()), widget, SLOT(close()));
353 widget->setAttribute(Qt::WA_DeleteOnClose);
354 widget->show();
355}
356
357void Window::importSharkport() {
358 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
359 if (!filename.isEmpty()) {
360 m_controller->importSharkport(filename);
361 }
362}
363
364void Window::exportSharkport() {
365 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
366 if (!filename.isEmpty()) {
367 m_controller->exportSharkport(filename);
368 }
369}
370
371void Window::openSettingsWindow() {
372 SettingsView* settingsWindow = new SettingsView(m_config, &m_inputController, m_shortcutController);
373 connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
374 connect(settingsWindow, SIGNAL(audioDriverChanged()), m_controller, SLOT(reloadAudioDriver()));
375 connect(settingsWindow, SIGNAL(displayDriverChanged()), this, SLOT(mustRestart()));
376 connect(settingsWindow, SIGNAL(pathsChanged()), this, SLOT(reloadConfig()));
377 openView(settingsWindow);
378}
379
380void Window::openOverrideWindow() {
381 OverrideView* overrideWindow = new OverrideView(m_controller, m_config);
382 openView(overrideWindow);
383}
384
385void Window::openSensorWindow() {
386 SensorView* sensorWindow = new SensorView(m_controller, &m_inputController);
387 openView(sensorWindow);
388}
389
390void Window::openCheatsWindow() {
391 CheatsView* cheatsWindow = new CheatsView(m_controller);
392 openView(cheatsWindow);
393}
394
395void Window::openPaletteWindow() {
396 PaletteView* paletteWindow = new PaletteView(m_controller);
397 openView(paletteWindow);
398}
399
400void Window::openMemoryWindow() {
401 MemoryView* memoryWindow = new MemoryView(m_controller);
402 openView(memoryWindow);
403}
404
405void Window::openIOViewer() {
406 IOViewer* ioViewer = new IOViewer(m_controller);
407 openView(ioViewer);
408}
409
410void Window::openAboutScreen() {
411 AboutScreen* about = new AboutScreen();
412 openView(about);
413}
414
415void Window::openROMInfo() {
416 ROMInfo* romInfo = new ROMInfo(m_controller);
417 openView(romInfo);
418}
419
420#ifdef USE_FFMPEG
421void Window::openVideoWindow() {
422 if (!m_videoView) {
423 m_videoView = new VideoView();
424 connect(m_videoView, SIGNAL(recordingStarted(mAVStream*)), m_controller, SLOT(setAVStream(mAVStream*)));
425 connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
426 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), m_videoView, SLOT(stopRecording()));
427 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), m_videoView, SLOT(close()));
428 connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
429 }
430 m_videoView->show();
431}
432#endif
433
434#ifdef USE_MAGICK
435void Window::openGIFWindow() {
436 if (!m_gifView) {
437 m_gifView = new GIFView();
438 connect(m_gifView, SIGNAL(recordingStarted(mAVStream*)), m_controller, SLOT(setAVStream(mAVStream*)));
439 connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
440 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), m_gifView, SLOT(stopRecording()));
441 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), m_gifView, SLOT(close()));
442 connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
443 }
444 m_gifView->show();
445}
446#endif
447
448#ifdef USE_GDB_STUB
449void Window::gdbOpen() {
450 if (!m_gdbController) {
451 m_gdbController = new GDBController(m_controller, this);
452 }
453 GDBWindow* window = new GDBWindow(m_gdbController);
454 openView(window);
455}
456#endif
457
458void Window::keyPressEvent(QKeyEvent* event) {
459 if (event->isAutoRepeat()) {
460 QWidget::keyPressEvent(event);
461 return;
462 }
463 GBAKey key = m_inputController.mapKeyboard(event->key());
464 if (key == GBA_KEY_NONE) {
465 QWidget::keyPressEvent(event);
466 return;
467 }
468 m_controller->keyPressed(key);
469 event->accept();
470}
471
472void Window::keyReleaseEvent(QKeyEvent* event) {
473 if (event->isAutoRepeat()) {
474 QWidget::keyReleaseEvent(event);
475 return;
476 }
477 GBAKey key = m_inputController.mapKeyboard(event->key());
478 if (key == GBA_KEY_NONE) {
479 QWidget::keyPressEvent(event);
480 return;
481 }
482 m_controller->keyReleased(key);
483 event->accept();
484}
485
486void Window::resizeEvent(QResizeEvent* event) {
487 if (!isFullScreen()) {
488 m_config->setOption("height", m_screenWidget->height());
489 m_config->setOption("width", m_screenWidget->width());
490 }
491
492 int factor = 0;
493 QSize size(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
494 if (m_controller->isLoaded()) {
495 size = m_controller->screenDimensions();
496 }
497 if (event->size().width() % size.width() == 0 && event->size().height() % size.height() == 0 &&
498 event->size().width() / size.width() == event->size().height() / size.height()) {
499 factor = event->size().width() / size.width();
500 }
501 for (QMap<int, QAction*>::iterator iter = m_frameSizes.begin(); iter != m_frameSizes.end(); ++iter) {
502 bool enableSignals = iter.value()->blockSignals(true);
503 if (iter.key() == factor) {
504 iter.value()->setChecked(true);
505 } else {
506 iter.value()->setChecked(false);
507 }
508 iter.value()->blockSignals(enableSignals);
509 }
510
511 m_config->setOption("fullscreen", isFullScreen());
512}
513
514void Window::showEvent(QShowEvent* event) {
515 resizeFrame(m_screenWidget->sizeHint());
516 QVariant windowPos = m_config->getQtOption("windowPos");
517 if (!windowPos.isNull()) {
518 move(windowPos.toPoint());
519 } else {
520 QRect rect = frameGeometry();
521 rect.moveCenter(QApplication::desktop()->availableGeometry().center());
522 move(rect.topLeft());
523 }
524 if (m_fullscreenOnStart) {
525 enterFullScreen();
526 m_fullscreenOnStart = false;
527 }
528}
529
530void Window::closeEvent(QCloseEvent* event) {
531 emit shutdown();
532 m_config->setQtOption("windowPos", pos());
533 saveConfig();
534 QMainWindow::closeEvent(event);
535}
536
537void Window::focusInEvent(QFocusEvent*) {
538 m_display->forceDraw();
539}
540
541void Window::focusOutEvent(QFocusEvent*) {
542 m_controller->setTurbo(false, false);
543 m_controller->stopRewinding();
544 m_controller->clearKeys();
545}
546
547void Window::dragEnterEvent(QDragEnterEvent* event) {
548 if (event->mimeData()->hasFormat("text/uri-list")) {
549 event->acceptProposedAction();
550 }
551}
552
553void Window::dropEvent(QDropEvent* event) {
554 QString uris = event->mimeData()->data("text/uri-list");
555 uris = uris.trimmed();
556 if (uris.contains("\n")) {
557 // Only one file please
558 return;
559 }
560 QUrl url(uris);
561 if (!url.isLocalFile()) {
562 // No remote loading
563 return;
564 }
565 event->accept();
566 m_controller->loadGame(url.toLocalFile());
567}
568
569void Window::mouseDoubleClickEvent(QMouseEvent* event) {
570 if (event->button() != Qt::LeftButton) {
571 return;
572 }
573 toggleFullScreen();
574}
575
576void Window::enterFullScreen() {
577 if (!isVisible()) {
578 m_fullscreenOnStart = true;
579 return;
580 }
581 if (isFullScreen()) {
582 return;
583 }
584 showFullScreen();
585#ifndef Q_OS_MAC
586 if (m_controller->isLoaded() && !m_controller->isPaused()) {
587 menuBar()->hide();
588 }
589#endif
590}
591
592void Window::exitFullScreen() {
593 if (!isFullScreen()) {
594 return;
595 }
596 m_screenWidget->unsetCursor();
597 menuBar()->show();
598 showNormal();
599}
600
601void Window::toggleFullScreen() {
602 if (isFullScreen()) {
603 exitFullScreen();
604 } else {
605 enterFullScreen();
606 }
607}
608
609void Window::gameStarted(mCoreThread* context, const QString& fname) {
610 MutexLock(&context->stateMutex);
611 if (context->state < THREAD_EXITING) {
612 emit startDrawing(context);
613 } else {
614 MutexUnlock(&context->stateMutex);
615 return;
616 }
617 MutexUnlock(&context->stateMutex);
618 foreach (QAction* action, m_gameActions) {
619 action->setDisabled(false);
620 }
621 foreach (QAction* action, m_gbaActions) {
622 action->setDisabled(context->core->platform(context->core) != PLATFORM_GBA);
623 }
624 multiplayerChanged();
625 if (!fname.isEmpty()) {
626 setWindowFilePath(fname);
627 appendMRU(fname);
628 }
629 updateTitle();
630 unsigned width, height;
631 context->core->desiredVideoDimensions(context->core, &width, &height);
632 m_display->setMinimumSize(width, height);
633 attachWidget(m_display);
634
635#ifndef Q_OS_MAC
636 if (isFullScreen()) {
637 menuBar()->hide();
638 }
639#endif
640
641 m_hitUnimplementedBiosCall = false;
642 m_fpsTimer.start();
643 m_focusCheck.start();
644}
645
646void Window::gameStopped() {
647 foreach (QAction* action, m_gbaActions) {
648 action->setDisabled(false);
649 }
650 foreach (QAction* action, m_gameActions) {
651 action->setDisabled(true);
652 }
653 setWindowFilePath(QString());
654 updateTitle();
655 detachWidget(m_display);
656 m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
657 m_screenWidget->setPixmap(m_logo);
658 m_screenWidget->unsetCursor();
659
660 m_fpsTimer.stop();
661 m_focusCheck.stop();
662}
663
664void Window::gameCrashed(const QString& errorMessage) {
665 QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
666 tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
667 QMessageBox::Ok, this, Qt::Sheet);
668 crash->setAttribute(Qt::WA_DeleteOnClose);
669 crash->show();
670}
671
672void Window::gameFailed() {
673 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
674 tr("Could not load game. Are you sure it's in the correct format?"),
675 QMessageBox::Ok, this, Qt::Sheet);
676 fail->setAttribute(Qt::WA_DeleteOnClose);
677 fail->show();
678}
679
680void Window::unimplementedBiosCall(int call) {
681 if (m_hitUnimplementedBiosCall) {
682 return;
683 }
684 m_hitUnimplementedBiosCall = true;
685
686 QMessageBox* fail = new QMessageBox(
687 QMessageBox::Warning, tr("Unimplemented BIOS call"),
688 tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
689 QMessageBox::Ok, this, Qt::Sheet);
690 fail->setAttribute(Qt::WA_DeleteOnClose);
691 fail->show();
692}
693
694void Window::tryMakePortable() {
695 QMessageBox* confirm = new QMessageBox(QMessageBox::Question, tr("Really make portable?"),
696 tr("This will make the emulator load its configuration from the same directory as the executable. Do you want to continue?"),
697 QMessageBox::Yes | QMessageBox::Cancel, this, Qt::Sheet);
698 confirm->setAttribute(Qt::WA_DeleteOnClose);
699 connect(confirm->button(QMessageBox::Yes), SIGNAL(clicked()), m_config, SLOT(makePortable()));
700 confirm->show();
701}
702
703void Window::mustRestart() {
704 QMessageBox* dialog = new QMessageBox(QMessageBox::Warning, tr("Restart needed"),
705 tr("Some changes will not take effect until the emulator is restarted."),
706 QMessageBox::Ok, this, Qt::Sheet);
707 dialog->setAttribute(Qt::WA_DeleteOnClose);
708 dialog->show();
709}
710
711void Window::recordFrame() {
712 m_frameList.append(QDateTime::currentDateTime());
713 while (m_frameList.count() > FRAME_LIST_SIZE) {
714 m_frameList.removeFirst();
715 }
716}
717
718void Window::showFPS() {
719 if (m_frameList.isEmpty()) {
720 updateTitle();
721 return;
722 }
723 qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
724 float fps = (m_frameList.count() - 1) * 10000.f / interval;
725 fps = round(fps) / 10.f;
726 updateTitle(fps);
727}
728
729void Window::updateTitle(float fps) {
730 QString title;
731
732 m_controller->threadInterrupt();
733 if (m_controller->isLoaded()) {
734 const NoIntroDB* db = GBAApp::app()->gameDB();
735 NoIntroGame game{};
736 uint32_t crc32 = 0;
737
738 switch (m_controller->thread()->core->platform(m_controller->thread()->core)) {
739 #ifdef M_CORE_GBA
740 case PLATFORM_GBA: {
741 GBA* gba = static_cast<GBA*>(m_controller->thread()->core->board);
742 crc32 = gba->romCrc32;
743 break;
744 }
745 #endif
746 #ifdef M_CORE_GB
747 case PLATFORM_GB: {
748 GB* gb = static_cast<GB*>(m_controller->thread()->core->board);
749 crc32 = gb->romCrc32;
750 break;
751 }
752 #endif
753 default:
754 break;
755 }
756
757 if (db && crc32) {
758 NoIntroDBLookupGameByCRC(db, crc32, &game);
759 title = QLatin1String(game.name);
760 } else {
761 char gameTitle[17] = { '\0' };
762 mCore* core = m_controller->thread()->core;
763 core->getGameTitle(core, gameTitle);
764 title = gameTitle;
765 }
766 }
767 MultiplayerController* multiplayer = m_controller->multiplayerController();
768 if (multiplayer && multiplayer->attached() > 1) {
769 title += tr(" - Player %1 of %2").arg(m_playerId + 1).arg(multiplayer->attached());
770 }
771 m_controller->threadContinue();
772 if (title.isNull()) {
773 setWindowTitle(tr("%1 - %2").arg(projectName).arg(projectVersion));
774 } else if (fps < 0) {
775 setWindowTitle(tr("%1 - %2 - %3").arg(projectName).arg(title).arg(projectVersion));
776 } else {
777 setWindowTitle(tr("%1 - %2 (%3 fps) - %4").arg(projectName).arg(title).arg(fps).arg(projectVersion));
778 }
779}
780
781void Window::openStateWindow(LoadSave ls) {
782 if (m_stateWindow) {
783 return;
784 }
785 MultiplayerController* multiplayer = m_controller->multiplayerController();
786 if (multiplayer && multiplayer->attached() > 1) {
787 return;
788 }
789 bool wasPaused = m_controller->isPaused();
790 m_stateWindow = new LoadSaveState(m_controller);
791 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
792 connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), m_stateWindow, SLOT(close()));
793 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
794 detachWidget(m_stateWindow);
795 m_stateWindow = nullptr;
796 QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
797 });
798 if (!wasPaused) {
799 m_controller->setPaused(true);
800 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
801 }
802 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
803 m_stateWindow->setMode(ls);
804 attachWidget(m_stateWindow);
805}
806
807void Window::setupMenu(QMenuBar* menubar) {
808 menubar->clear();
809 QMenu* fileMenu = menubar->addMenu(tr("&File"));
810 m_shortcutController->addMenu(fileMenu);
811 installEventFilter(m_shortcutController);
812 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open),
813 "loadROM");
814 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
815 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
816 addControlledAction(fileMenu, fileMenu->addAction(tr("Boot BIOS"), m_controller, SLOT(bootBIOS())), "bootBIOS");
817
818 addControlledAction(fileMenu, fileMenu->addAction(tr("Replace ROM..."), this, SLOT(replaceROM())), "replaceROM");
819
820 QAction* romInfo = new QAction(tr("ROM &info..."), fileMenu);
821 connect(romInfo, SIGNAL(triggered()), this, SLOT(openROMInfo()));
822 m_gameActions.append(romInfo);
823 addControlledAction(fileMenu, romInfo, "romInfo");
824
825 m_mruMenu = fileMenu->addMenu(tr("Recent"));
826
827 fileMenu->addSeparator();
828
829 addControlledAction(fileMenu, fileMenu->addAction(tr("Make portable"), this, SLOT(tryMakePortable())), "makePortable");
830
831 fileMenu->addSeparator();
832
833 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
834 loadState->setShortcut(tr("F10"));
835 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
836 m_gameActions.append(loadState);
837 m_nonMpActions.append(loadState);
838 m_gbaActions.append(loadState);
839 addControlledAction(fileMenu, loadState, "loadState");
840
841 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
842 saveState->setShortcut(tr("Shift+F10"));
843 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
844 m_gameActions.append(saveState);
845 m_nonMpActions.append(saveState);
846 m_gbaActions.append(saveState);
847 addControlledAction(fileMenu, saveState, "saveState");
848
849 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
850 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
851 m_shortcutController->addMenu(quickLoadMenu);
852 m_shortcutController->addMenu(quickSaveMenu);
853
854 QAction* quickLoad = new QAction(tr("Load recent"), quickLoadMenu);
855 connect(quickLoad, SIGNAL(triggered()), m_controller, SLOT(loadState()));
856 m_gameActions.append(quickLoad);
857 m_nonMpActions.append(quickLoad);
858 m_gbaActions.append(quickLoad);
859 addControlledAction(quickLoadMenu, quickLoad, "quickLoad");
860
861 QAction* quickSave = new QAction(tr("Save recent"), quickSaveMenu);
862 connect(quickSave, SIGNAL(triggered()), m_controller, SLOT(saveState()));
863 m_gameActions.append(quickSave);
864 m_nonMpActions.append(quickSave);
865 m_gbaActions.append(quickSave);
866 addControlledAction(quickSaveMenu, quickSave, "quickSave");
867
868 quickLoadMenu->addSeparator();
869 quickSaveMenu->addSeparator();
870
871 QAction* undoLoadState = new QAction(tr("Undo load state"), quickLoadMenu);
872 undoLoadState->setShortcut(tr("F11"));
873 connect(undoLoadState, SIGNAL(triggered()), m_controller, SLOT(loadBackupState()));
874 m_gameActions.append(undoLoadState);
875 m_nonMpActions.append(undoLoadState);
876 m_gbaActions.append(undoLoadState);
877 addControlledAction(quickLoadMenu, undoLoadState, "undoLoadState");
878
879 QAction* undoSaveState = new QAction(tr("Undo save state"), quickSaveMenu);
880 undoSaveState->setShortcut(tr("Shift+F11"));
881 connect(undoSaveState, SIGNAL(triggered()), m_controller, SLOT(saveBackupState()));
882 m_gameActions.append(undoSaveState);
883 m_nonMpActions.append(undoSaveState);
884 m_gbaActions.append(undoSaveState);
885 addControlledAction(quickSaveMenu, undoSaveState, "undoSaveState");
886
887 quickLoadMenu->addSeparator();
888 quickSaveMenu->addSeparator();
889
890 int i;
891 for (i = 1; i < 10; ++i) {
892 quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
893 quickLoad->setShortcut(tr("F%1").arg(i));
894 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
895 m_gameActions.append(quickLoad);
896 m_nonMpActions.append(quickLoad);
897 m_gbaActions.append(quickLoad);
898 addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
899
900 quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
901 quickSave->setShortcut(tr("Shift+F%1").arg(i));
902 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
903 m_gameActions.append(quickSave);
904 m_nonMpActions.append(quickSave);
905 m_gbaActions.append(quickSave);
906 addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
907 }
908
909 fileMenu->addSeparator();
910 QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
911 connect(importShark, SIGNAL(triggered()), this, SLOT(importSharkport()));
912 m_gameActions.append(importShark);
913 m_gbaActions.append(importShark);
914 addControlledAction(fileMenu, importShark, "importShark");
915
916 QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
917 connect(exportShark, SIGNAL(triggered()), this, SLOT(exportSharkport()));
918 m_gameActions.append(exportShark);
919 m_gbaActions.append(exportShark);
920 addControlledAction(fileMenu, exportShark, "exportShark");
921
922 fileMenu->addSeparator();
923 QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
924 connect(multiWindow, &QAction::triggered, [this]() {
925 GBAApp::app()->newWindow();
926 });
927 addControlledAction(fileMenu, multiWindow, "multiWindow");
928
929#ifndef Q_OS_MAC
930 fileMenu->addSeparator();
931#endif
932
933 QAction* about = new QAction(tr("About"), fileMenu);
934 connect(about, SIGNAL(triggered()), this, SLOT(openAboutScreen()));
935 fileMenu->addAction(about);
936
937#ifndef Q_OS_MAC
938 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
939#endif
940
941 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
942 m_shortcutController->addMenu(emulationMenu);
943 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
944 reset->setShortcut(tr("Ctrl+R"));
945 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
946 m_gameActions.append(reset);
947 addControlledAction(emulationMenu, reset, "reset");
948
949 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
950 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
951 m_gameActions.append(shutdown);
952 addControlledAction(emulationMenu, shutdown, "shutdown");
953
954 QAction* yank = new QAction(tr("Yank game pak"), emulationMenu);
955 connect(yank, SIGNAL(triggered()), m_controller, SLOT(yankPak()));
956 m_gameActions.append(yank);
957 m_gbaActions.append(yank);
958 addControlledAction(emulationMenu, yank, "yank");
959 emulationMenu->addSeparator();
960
961 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
962 pause->setChecked(false);
963 pause->setCheckable(true);
964 pause->setShortcut(tr("Ctrl+P"));
965 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
966 connect(m_controller, &GameController::gamePaused, [this, pause]() {
967 pause->setChecked(true);
968 });
969 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
970 m_gameActions.append(pause);
971 addControlledAction(emulationMenu, pause, "pause");
972
973 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
974 frameAdvance->setShortcut(tr("Ctrl+N"));
975 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
976 m_gameActions.append(frameAdvance);
977 m_nonMpActions.append(frameAdvance);
978 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
979
980 emulationMenu->addSeparator();
981
982 m_shortcutController->addFunctions(emulationMenu, [this]() {
983 m_controller->setTurbo(true, false);
984 }, [this]() {
985 m_controller->setTurbo(false, false);
986 }, QKeySequence(Qt::Key_Tab), tr("Fast forward (held)"), "holdFastForward");
987
988 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
989 turbo->setCheckable(true);
990 turbo->setChecked(false);
991 turbo->setShortcut(tr("Shift+Tab"));
992 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
993 addControlledAction(emulationMenu, turbo, "fastForward");
994
995 QMenu* ffspeedMenu = emulationMenu->addMenu(tr("Fast forward speed"));
996 ConfigOption* ffspeed = m_config->addOption("fastForwardRatio");
997 ffspeed->connect([this](const QVariant& value) {
998 m_controller->setTurboSpeed(value.toFloat());
999 }, this);
1000 ffspeed->addValue(tr("Unbounded"), -1.0f, ffspeedMenu);
1001 ffspeed->setValue(QVariant(-1.0f));
1002 ffspeedMenu->addSeparator();
1003 for (i = 2; i < 11; ++i) {
1004 ffspeed->addValue(tr("%0x").arg(i), i, ffspeedMenu);
1005 }
1006 m_config->updateOption("fastForwardRatio");
1007
1008 m_shortcutController->addFunctions(emulationMenu, [this]() {
1009 m_controller->startRewinding();
1010 }, [this]() {
1011 m_controller->stopRewinding();
1012 }, QKeySequence("~"), tr("Rewind (held)"), "holdRewind");
1013
1014 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
1015 rewind->setShortcut(tr("`"));
1016 connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
1017 m_gameActions.append(rewind);
1018 m_nonMpActions.append(rewind);
1019 addControlledAction(emulationMenu, rewind, "rewind");
1020
1021 QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
1022 frameRewind->setShortcut(tr("Ctrl+B"));
1023 connect(frameRewind, &QAction::triggered, [this] () {
1024 m_controller->rewind(1);
1025 });
1026 m_gameActions.append(frameRewind);
1027 m_nonMpActions.append(frameRewind);
1028 addControlledAction(emulationMenu, frameRewind, "frameRewind");
1029
1030 ConfigOption* videoSync = m_config->addOption("videoSync");
1031 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
1032 videoSync->connect([this](const QVariant& value) {
1033 reloadConfig();
1034 }, this);
1035 m_config->updateOption("videoSync");
1036
1037 ConfigOption* audioSync = m_config->addOption("audioSync");
1038 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
1039 audioSync->connect([this](const QVariant& value) {
1040 reloadConfig();
1041 }, this);
1042 m_config->updateOption("audioSync");
1043
1044 emulationMenu->addSeparator();
1045
1046 QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
1047 m_shortcutController->addMenu(solarMenu);
1048 QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
1049 connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
1050 addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
1051
1052 QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
1053 connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
1054 addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
1055
1056 QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
1057 connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
1058 addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
1059
1060 QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
1061 connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
1062 addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
1063
1064 solarMenu->addSeparator();
1065 for (int i = 0; i <= 10; ++i) {
1066 QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
1067 connect(setSolar, &QAction::triggered, [this, i]() {
1068 m_controller->setLuminanceLevel(i);
1069 });
1070 addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
1071 }
1072
1073 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
1074 m_shortcutController->addMenu(avMenu);
1075 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
1076 m_shortcutController->addMenu(frameMenu, avMenu);
1077 for (int i = 1; i <= 6; ++i) {
1078 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
1079 setSize->setCheckable(true);
1080 connect(setSize, &QAction::triggered, [this, i, setSize]() {
1081 showNormal();
1082 QSize size(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1083 if (m_controller->isLoaded()) {
1084 size = m_controller->screenDimensions();
1085 }
1086 size *= i;
1087 resizeFrame(size);
1088 bool enableSignals = setSize->blockSignals(true);
1089 setSize->setChecked(true);
1090 setSize->blockSignals(enableSignals);
1091 });
1092 m_frameSizes[i] = setSize;
1093 addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
1094 }
1095 QKeySequence fullscreenKeys;
1096#ifdef Q_OS_WIN
1097 fullscreenKeys = QKeySequence("Alt+Return");
1098#else
1099 fullscreenKeys = QKeySequence("Ctrl+F");
1100#endif
1101 addControlledAction(frameMenu, frameMenu->addAction(tr("Toggle fullscreen"), this, SLOT(toggleFullScreen()), fullscreenKeys), "fullscreen");
1102
1103 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
1104 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
1105 lockAspectRatio->connect([this](const QVariant& value) {
1106 m_display->lockAspectRatio(value.toBool());
1107 }, this);
1108 m_config->updateOption("lockAspectRatio");
1109
1110 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
1111 resampleVideo->addBoolean(tr("Resample video"), avMenu);
1112 resampleVideo->connect([this](const QVariant& value) {
1113 m_display->filter(value.toBool());
1114 }, this);
1115 m_config->updateOption("resampleVideo");
1116
1117 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
1118 ConfigOption* skip = m_config->addOption("frameskip");
1119 skip->connect([this](const QVariant& value) {
1120 reloadConfig();
1121 }, this);
1122 for (int i = 0; i <= 10; ++i) {
1123 skip->addValue(QString::number(i), i, skipMenu);
1124 }
1125 m_config->updateOption("frameskip");
1126
1127 QAction* shaderView = new QAction(tr("Shader options..."), avMenu);
1128 connect(shaderView, SIGNAL(triggered()), m_shaderView, SLOT(show()));
1129 if (!m_display->supportsShaders()) {
1130 shaderView->setEnabled(false);
1131 }
1132 addControlledAction(avMenu, shaderView, "shaderSelector");
1133
1134 avMenu->addSeparator();
1135
1136 ConfigOption* mute = m_config->addOption("mute");
1137 mute->addBoolean(tr("Mute"), avMenu);
1138 mute->connect([this](const QVariant& value) {
1139 reloadConfig();
1140 }, this);
1141 m_config->updateOption("mute");
1142
1143 QMenu* target = avMenu->addMenu(tr("FPS target"));
1144 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
1145 fpsTargetOption->connect([this](const QVariant& value) {
1146 emit fpsTargetChanged(value.toFloat());
1147 }, this);
1148 fpsTargetOption->addValue(tr("15"), 15, target);
1149 fpsTargetOption->addValue(tr("30"), 30, target);
1150 fpsTargetOption->addValue(tr("45"), 45, target);
1151 fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
1152 fpsTargetOption->addValue(tr("60"), 60, target);
1153 fpsTargetOption->addValue(tr("90"), 90, target);
1154 fpsTargetOption->addValue(tr("120"), 120, target);
1155 fpsTargetOption->addValue(tr("240"), 240, target);
1156 m_config->updateOption("fpsTarget");
1157
1158#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
1159 avMenu->addSeparator();
1160#endif
1161
1162#ifdef USE_PNG
1163 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
1164 screenshot->setShortcut(tr("F12"));
1165 connect(screenshot, SIGNAL(triggered()), m_controller, SLOT(screenshot()));
1166 m_gameActions.append(screenshot);
1167 addControlledAction(avMenu, screenshot, "screenshot");
1168#endif
1169
1170#ifdef USE_FFMPEG
1171 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
1172 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
1173 addControlledAction(avMenu, recordOutput, "recordOutput");
1174#endif
1175
1176#ifdef USE_MAGICK
1177 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
1178 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
1179 addControlledAction(avMenu, recordGIF, "recordGIF");
1180#endif
1181
1182 avMenu->addSeparator();
1183 QMenu* videoLayers = avMenu->addMenu(tr("Video layers"));
1184
1185 for (int i = 0; i < 4; ++i) {
1186 QAction* enableBg = new QAction(tr("Background %0").arg(i), videoLayers);
1187 enableBg->setCheckable(true);
1188 enableBg->setChecked(true);
1189 connect(enableBg, &QAction::triggered, [this, i](bool enable) { m_controller->setVideoLayerEnabled(i, enable); });
1190 addControlledAction(videoLayers, enableBg, QString("enableBG%0").arg(i));
1191 }
1192
1193 QAction* enableObj = new QAction(tr("OBJ (sprites)"), videoLayers);
1194 enableObj->setCheckable(true);
1195 enableObj->setChecked(true);
1196 connect(enableObj, &QAction::triggered, [this](bool enable) { m_controller->setVideoLayerEnabled(4, enable); });
1197 addControlledAction(videoLayers, enableObj, "enableOBJ");
1198
1199 QMenu* audioChannels = avMenu->addMenu(tr("Audio channels"));
1200
1201 for (int i = 0; i < 4; ++i) {
1202 QAction* enableCh = new QAction(tr("Channel %0").arg(i + 1), audioChannels);
1203 enableCh->setCheckable(true);
1204 enableCh->setChecked(true);
1205 connect(enableCh, &QAction::triggered, [this, i](bool enable) { m_controller->setAudioChannelEnabled(i, enable); });
1206 addControlledAction(audioChannels, enableCh, QString("enableCh%0").arg(i + 1));
1207 }
1208
1209 QAction* enableChA = new QAction(tr("Channel A"), audioChannels);
1210 enableChA->setCheckable(true);
1211 enableChA->setChecked(true);
1212 connect(enableChA, &QAction::triggered, [this, i](bool enable) { m_controller->setAudioChannelEnabled(4, enable); });
1213 addControlledAction(audioChannels, enableChA, QString("enableChA"));
1214
1215 QAction* enableChB = new QAction(tr("Channel B"), audioChannels);
1216 enableChB->setCheckable(true);
1217 enableChB->setChecked(true);
1218 connect(enableChB, &QAction::triggered, [this, i](bool enable) { m_controller->setAudioChannelEnabled(5, enable); });
1219 addControlledAction(audioChannels, enableChB, QString("enableChB"));
1220
1221 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
1222 m_shortcutController->addMenu(toolsMenu);
1223 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
1224 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
1225 addControlledAction(toolsMenu, viewLogs, "viewLogs");
1226
1227 QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
1228 connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
1229 m_gbaActions.append(overrides);
1230 addControlledAction(toolsMenu, overrides, "overrideWindow");
1231
1232 QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
1233 connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
1234 addControlledAction(toolsMenu, sensors, "sensorWindow");
1235
1236 QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
1237 connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
1238 m_gbaActions.append(cheats);
1239 addControlledAction(toolsMenu, cheats, "cheatsWindow");
1240
1241#ifdef USE_GDB_STUB
1242 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
1243 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
1244 m_gbaActions.append(gdbWindow);
1245 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
1246#endif
1247
1248 toolsMenu->addSeparator();
1249 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())),
1250 "settings");
1251
1252 toolsMenu->addSeparator();
1253
1254 QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
1255 connect(paletteView, SIGNAL(triggered()), this, SLOT(openPaletteWindow()));
1256 m_gameActions.append(paletteView);
1257 m_gbaActions.append(paletteView);
1258 addControlledAction(toolsMenu, paletteView, "paletteWindow");
1259
1260 QAction* memoryView = new QAction(tr("View memory..."), toolsMenu);
1261 connect(memoryView, SIGNAL(triggered()), this, SLOT(openMemoryWindow()));
1262 m_gameActions.append(memoryView);
1263 addControlledAction(toolsMenu, memoryView, "memoryView");
1264
1265 QAction* ioViewer = new QAction(tr("View &I/O registers..."), toolsMenu);
1266 connect(ioViewer, SIGNAL(triggered()), this, SLOT(openIOViewer()));
1267 m_gameActions.append(ioViewer);
1268 m_gbaActions.append(ioViewer);
1269 addControlledAction(toolsMenu, ioViewer, "ioViewer");
1270
1271 ConfigOption* skipBios = m_config->addOption("skipBios");
1272 skipBios->connect([this](const QVariant& value) {
1273 reloadConfig();
1274 }, this);
1275
1276 ConfigOption* useBios = m_config->addOption("useBios");
1277 useBios->connect([this](const QVariant& value) {
1278 m_controller->setUseBIOS(value.toBool());
1279 }, this);
1280
1281 ConfigOption* buffers = m_config->addOption("audioBuffers");
1282 buffers->connect([this](const QVariant& value) {
1283 emit audioBufferSamplesChanged(value.toInt());
1284 }, this);
1285
1286 ConfigOption* sampleRate = m_config->addOption("sampleRate");
1287 sampleRate->connect([this](const QVariant& value) {
1288 emit sampleRateChanged(value.toUInt());
1289 }, this);
1290
1291 ConfigOption* volume = m_config->addOption("volume");
1292 volume->connect([this](const QVariant& value) {
1293 reloadConfig();
1294 }, this);
1295
1296 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
1297 rewindEnable->connect([this](const QVariant& value) {
1298 m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt());
1299 }, this);
1300
1301 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
1302 rewindBufferCapacity->connect([this](const QVariant& value) {
1303 m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt());
1304 }, this);
1305
1306 ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
1307 rewindBufferInterval->connect([this](const QVariant& value) {
1308 m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt());
1309 }, this);
1310
1311 ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
1312 allowOpposingDirections->connect([this](const QVariant& value) {
1313 m_inputController.setAllowOpposing(value.toBool());
1314 }, this);
1315
1316 ConfigOption* saveStateExtdata = m_config->addOption("saveStateExtdata");
1317 saveStateExtdata->connect([this](const QVariant& value) {
1318 m_controller->setSaveStateExtdata(value.toInt());
1319 }, this);
1320
1321 ConfigOption* loadStateExtdata = m_config->addOption("loadStateExtdata");
1322 loadStateExtdata->connect([this](const QVariant& value) {
1323 m_controller->setLoadStateExtdata(value.toInt());
1324 }, this);
1325
1326 QAction* exitFullScreen = new QAction(tr("Exit fullscreen"), frameMenu);
1327 connect(exitFullScreen, SIGNAL(triggered()), this, SLOT(exitFullScreen()));
1328 exitFullScreen->setShortcut(QKeySequence("Esc"));
1329 addHiddenAction(frameMenu, exitFullScreen, "exitFullScreen");
1330
1331 QMenu* autofireMenu = new QMenu(tr("Autofire"), this);
1332 m_shortcutController->addMenu(autofireMenu);
1333
1334 m_shortcutController->addFunctions(autofireMenu, [this]() {
1335 m_controller->setAutofire(GBA_KEY_A, true);
1336 }, [this]() {
1337 m_controller->setAutofire(GBA_KEY_A, false);
1338 }, QKeySequence("W"), tr("Autofire A"), "autofireA");
1339
1340 m_shortcutController->addFunctions(autofireMenu, [this]() {
1341 m_controller->setAutofire(GBA_KEY_B, true);
1342 }, [this]() {
1343 m_controller->setAutofire(GBA_KEY_B, false);
1344 }, QKeySequence("Q"), tr("Autofire B"), "autofireB");
1345
1346 m_shortcutController->addFunctions(autofireMenu, [this]() {
1347 m_controller->setAutofire(GBA_KEY_L, true);
1348 }, [this]() {
1349 m_controller->setAutofire(GBA_KEY_L, false);
1350 }, QKeySequence(), tr("Autofire L"), "autofireL");
1351
1352 m_shortcutController->addFunctions(autofireMenu, [this]() {
1353 m_controller->setAutofire(GBA_KEY_R, true);
1354 }, [this]() {
1355 m_controller->setAutofire(GBA_KEY_R, false);
1356 }, QKeySequence(), tr("Autofire R"), "autofireR");
1357
1358 m_shortcutController->addFunctions(autofireMenu, [this]() {
1359 m_controller->setAutofire(GBA_KEY_START, true);
1360 }, [this]() {
1361 m_controller->setAutofire(GBA_KEY_START, false);
1362 }, QKeySequence(), tr("Autofire Start"), "autofireStart");
1363
1364 m_shortcutController->addFunctions(autofireMenu, [this]() {
1365 m_controller->setAutofire(GBA_KEY_SELECT, true);
1366 }, [this]() {
1367 m_controller->setAutofire(GBA_KEY_SELECT, false);
1368 }, QKeySequence(), tr("Autofire Select"), "autofireSelect");
1369
1370 m_shortcutController->addFunctions(autofireMenu, [this]() {
1371 m_controller->setAutofire(GBA_KEY_UP, true);
1372 }, [this]() {
1373 m_controller->setAutofire(GBA_KEY_UP, false);
1374 }, QKeySequence(), tr("Autofire Up"), "autofireUp");
1375
1376 m_shortcutController->addFunctions(autofireMenu, [this]() {
1377 m_controller->setAutofire(GBA_KEY_RIGHT, true);
1378 }, [this]() {
1379 m_controller->setAutofire(GBA_KEY_RIGHT, false);
1380 }, QKeySequence(), tr("Autofire Right"), "autofireRight");
1381
1382 m_shortcutController->addFunctions(autofireMenu, [this]() {
1383 m_controller->setAutofire(GBA_KEY_DOWN, true);
1384 }, [this]() {
1385 m_controller->setAutofire(GBA_KEY_DOWN, false);
1386 }, QKeySequence(), tr("Autofire Down"), "autofireDown");
1387
1388 m_shortcutController->addFunctions(autofireMenu, [this]() {
1389 m_controller->setAutofire(GBA_KEY_LEFT, true);
1390 }, [this]() {
1391 m_controller->setAutofire(GBA_KEY_LEFT, false);
1392 }, QKeySequence(), tr("Autofire Left"), "autofireLeft");
1393
1394 foreach (QAction* action, m_gameActions) {
1395 action->setDisabled(true);
1396 }
1397}
1398
1399void Window::attachWidget(QWidget* widget) {
1400 m_screenWidget->layout()->addWidget(widget);
1401 m_screenWidget->unsetCursor();
1402 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1403}
1404
1405void Window::detachWidget(QWidget* widget) {
1406 m_screenWidget->layout()->removeWidget(widget);
1407}
1408
1409void Window::appendMRU(const QString& fname) {
1410 int index = m_mruFiles.indexOf(fname);
1411 if (index >= 0) {
1412 m_mruFiles.removeAt(index);
1413 }
1414 m_mruFiles.prepend(fname);
1415 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1416 m_mruFiles.removeLast();
1417 }
1418 updateMRU();
1419}
1420
1421void Window::updateMRU() {
1422 if (!m_mruMenu) {
1423 return;
1424 }
1425 m_mruMenu->clear();
1426 int i = 0;
1427 for (const QString& file : m_mruFiles) {
1428 QAction* item = new QAction(file, m_mruMenu);
1429 item->setShortcut(QString("Ctrl+%1").arg(i));
1430 connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
1431 m_mruMenu->addAction(item);
1432 ++i;
1433 }
1434 m_config->setMRU(m_mruFiles);
1435 m_config->write();
1436 m_mruMenu->setEnabled(i > 0);
1437}
1438
1439QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1440 addHiddenAction(menu, action, name);
1441 menu->addAction(action);
1442 return action;
1443}
1444
1445QAction* Window::addHiddenAction(QMenu* menu, QAction* action, const QString& name) {
1446 m_shortcutController->addAction(menu, action, name);
1447 action->setShortcutContext(Qt::WidgetShortcut);
1448 addAction(action);
1449 return action;
1450}
1451
1452void Window::focusCheck() {
1453 if (!m_config->getOption("pauseOnFocusLost").toInt()) {
1454 return;
1455 }
1456 if (QGuiApplication::focusWindow() && m_autoresume) {
1457 m_controller->setPaused(false);
1458 } else if (!QGuiApplication::focusWindow() && !m_controller->isPaused()) {
1459 m_autoresume = true;
1460 m_controller->setPaused(true);
1461 }
1462}
1463
1464WindowBackground::WindowBackground(QWidget* parent)
1465 : QLabel(parent)
1466{
1467 setLayout(new QStackedLayout());
1468 layout()->setContentsMargins(0, 0, 0, 0);
1469 setAlignment(Qt::AlignCenter);
1470}
1471
1472void WindowBackground::setSizeHint(const QSize& hint) {
1473 m_sizeHint = hint;
1474}
1475
1476QSize WindowBackground::sizeHint() const {
1477 return m_sizeHint;
1478}
1479
1480void WindowBackground::setLockAspectRatio(int width, int height) {
1481 m_aspectWidth = width;
1482 m_aspectHeight = height;
1483}
1484
1485void WindowBackground::paintEvent(QPaintEvent*) {
1486 const QPixmap* logo = pixmap();
1487 if (!logo) {
1488 return;
1489 }
1490 QPainter painter(this);
1491 painter.setRenderHint(QPainter::SmoothPixmapTransform);
1492 painter.fillRect(QRect(QPoint(), size()), Qt::black);
1493 QSize s = size();
1494 QSize ds = s;
1495 if (ds.width() * m_aspectHeight > ds.height() * m_aspectWidth) {
1496 ds.setWidth(ds.height() * m_aspectWidth / m_aspectHeight);
1497 } else if (ds.width() * m_aspectHeight < ds.height() * m_aspectWidth) {
1498 ds.setHeight(ds.width() * m_aspectHeight / m_aspectWidth);
1499 }
1500 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1501 QRect full(origin, ds);
1502 painter.drawPixmap(full, *logo);
1503}