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