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