src/platform/qt/Window.cpp (view raw)
1/* Copyright (c) 2013-2017 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#ifdef USE_SQLITE3
18#include "ArchiveInspector.h"
19#include "library/LibraryController.h"
20#endif
21
22#include "AboutScreen.h"
23#include "AudioProcessor.h"
24#include "CheatsView.h"
25#include "ConfigController.h"
26#include "CoreController.h"
27#include "DebuggerConsole.h"
28#include "DebuggerConsoleController.h"
29#include "Display.h"
30#include "CoreController.h"
31#include "GBAApp.h"
32#include "GDBController.h"
33#include "GDBWindow.h"
34#include "GIFView.h"
35#include "IOViewer.h"
36#include "LoadSaveState.h"
37#include "LogView.h"
38#include "MultiplayerController.h"
39#include "MemorySearch.h"
40#include "MemoryView.h"
41#include "OverrideView.h"
42#include "ObjView.h"
43#include "PaletteView.h"
44#include "ROMInfo.h"
45#include "SensorView.h"
46#include "SettingsView.h"
47#include "ShaderSelector.h"
48#include "ShortcutController.h"
49#include "TileView.h"
50#include "VideoView.h"
51
52#include <mgba/core/version.h>
53#ifdef M_CORE_GB
54#include <mgba/internal/gb/gb.h>
55#include <mgba/internal/gb/video.h>
56#endif
57#ifdef M_CORE_GBA
58#include <mgba/internal/gba/gba.h>
59#include <mgba/internal/gba/video.h>
60#endif
61#include <mgba/feature/commandline.h>
62#include "feature/sqlite3/no-intro.h"
63#include <mgba-util/vfs.h>
64
65using namespace QGBA;
66
67Window::Window(CoreManager* manager, ConfigController* config, int playerId, QWidget* parent)
68 : QMainWindow(parent)
69 , m_manager(manager)
70 , m_logView(new LogView(&m_log))
71 , m_screenWidget(new WindowBackground())
72 , m_config(config)
73 , m_inputController(playerId, this)
74 , m_shortcutController(new ShortcutController(this))
75{
76 setFocusPolicy(Qt::StrongFocus);
77 setAcceptDrops(true);
78 setAttribute(Qt::WA_DeleteOnClose);
79 updateTitle();
80 reloadDisplayDriver();
81
82 m_logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
83 m_logo = m_logo; // Free memory left over in old pixmap
84
85#if defined(M_CORE_GBA)
86 float i = 2;
87#elif defined(M_CORE_GB)
88 float i = 3;
89#endif
90 QVariant multiplier = m_config->getOption("scaleMultiplier");
91 if (!multiplier.isNull()) {
92 m_savedScale = multiplier.toInt();
93 i = m_savedScale;
94 }
95#ifdef USE_SQLITE3
96 m_libraryView = new LibraryController(nullptr, ConfigController::configDir() + "/library.sqlite3", m_config);
97 ConfigOption* showLibrary = m_config->addOption("showLibrary");
98 showLibrary->connect([this](const QVariant& value) {
99 if (value.toBool()) {
100 if (m_controller) {
101 m_screenWidget->layout()->addWidget(m_libraryView);
102 } else {
103 attachWidget(m_libraryView);
104 }
105 } else {
106 detachWidget(m_libraryView);
107 }
108 }, this);
109 m_config->updateOption("showLibrary");
110 ConfigOption* libraryStyle = m_config->addOption("libraryStyle");
111 libraryStyle->connect([this](const QVariant& value) {
112 m_libraryView->setViewStyle(static_cast<LibraryStyle>(value.toInt()));
113 }, this);
114 m_config->updateOption("libraryStyle");
115
116 connect(m_libraryView, &LibraryController::startGame, [this]() {
117 VFile* output = m_libraryView->selectedVFile();
118 if (output) {
119 QPair<QString, QString> path = m_libraryView->selectedPath();
120 setController(m_manager->loadGame(output, path.second, path.first), path.first + "/" + path.second);
121 }
122 });
123#endif
124#if defined(M_CORE_GBA)
125 resizeFrame(QSize(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i));
126#elif defined(M_CORE_GB)
127 resizeFrame(QSize(GB_VIDEO_HORIZONTAL_PIXELS * i, GB_VIDEO_VERTICAL_PIXELS * i));
128#endif
129 m_screenWidget->setPixmap(m_logo);
130 m_screenWidget->setDimensions(m_logo.width(), m_logo.height());
131 m_screenWidget->setLockIntegerScaling(false);
132 m_screenWidget->setLockAspectRatio(true);
133 setCentralWidget(m_screenWidget);
134
135 connect(this, &Window::shutdown, m_logView, &QWidget::hide);
136 connect(&m_fpsTimer, &QTimer::timeout, this, &Window::showFPS);
137 connect(&m_focusCheck, &QTimer::timeout, this, &Window::focusCheck);
138 connect(&m_inputController, &InputController::profileLoaded, m_shortcutController, &ShortcutController::loadProfile);
139
140 m_log.setLevels(mLOG_WARN | mLOG_ERROR | mLOG_FATAL);
141 m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
142 m_focusCheck.setInterval(200);
143
144 m_shortcutController->setConfigController(m_config);
145 setupMenu(menuBar());
146}
147
148Window::~Window() {
149 delete m_logView;
150 delete m_overrideView;
151
152#ifdef USE_FFMPEG
153 delete m_videoView;
154#endif
155
156#ifdef USE_MAGICK
157 delete m_gifView;
158#endif
159
160#ifdef USE_SQLITE3
161 delete m_libraryView;
162#endif
163}
164
165void Window::argumentsPassed(mArguments* args) {
166 loadConfig();
167
168 if (args->fname) {
169 setController(m_manager->loadGame(args->fname), args->fname);
170 }
171
172#ifdef USE_GDB_STUB
173 if (args->debuggerType == DEBUGGER_GDB) {
174 if (!m_gdbController) {
175 m_gdbController = new GDBController(this);
176 m_gdbController->listen();
177 }
178 }
179#endif
180}
181
182void Window::resizeFrame(const QSize& size) {
183 QSize newSize(size);
184 m_screenWidget->setSizeHint(newSize);
185 newSize -= m_screenWidget->size();
186 newSize += this->size();
187 resize(newSize);
188}
189
190void Window::setConfig(ConfigController* config) {
191 m_config = config;
192}
193
194void Window::loadConfig() {
195 const mCoreOptions* opts = m_config->options();
196 reloadConfig();
197
198 if (opts->width && opts->height) {
199 resizeFrame(QSize(opts->width, opts->height));
200 }
201
202 if (opts->fullscreen) {
203 enterFullScreen();
204 }
205
206 m_mruFiles = m_config->getMRU();
207 updateMRU();
208
209 m_inputController.setConfiguration(m_config);
210}
211
212void Window::reloadConfig() {
213 const mCoreOptions* opts = m_config->options();
214
215 m_log.setLevels(opts->logLevel);
216
217 if (m_controller) {
218 m_controller->loadConfig(m_config);
219 if (m_audioProcessor) {
220 m_audioProcessor->setBufferSamples(opts->audioBuffers);
221 m_audioProcessor->requestSampleRate(opts->sampleRate);
222 }
223 }
224 m_display->lockAspectRatio(opts->lockAspectRatio);
225 m_display->filter(opts->resampleVideo);
226
227 m_inputController.setScreensaverSuspendable(opts->suspendScreensaver);
228}
229
230void Window::saveConfig() {
231 m_inputController.saveConfiguration();
232 m_config->write();
233}
234
235QString Window::getFilters() const {
236 QStringList filters;
237 QStringList formats;
238
239#ifdef M_CORE_GBA
240 QStringList gbaFormats{
241 "*.gba",
242#if defined(USE_LIBZIP) || defined(USE_ZLIB)
243 "*.zip",
244#endif
245#ifdef USE_LZMA
246 "*.7z",
247#endif
248 "*.agb",
249 "*.mb",
250 "*.rom",
251 "*.bin"};
252 formats.append(gbaFormats);
253 filters.append(tr("Game Boy Advance ROMs (%1)").arg(gbaFormats.join(QChar(' '))));
254#endif
255
256#ifdef M_CORE_GB
257 QStringList gbFormats{
258 "*.gb",
259 "*.gbc",
260#if defined(USE_LIBZIP) || defined(USE_ZLIB)
261 "*.zip",
262#endif
263#ifdef USE_LZMA
264 "*.7z",
265#endif
266 "*.rom",
267 "*.bin"};
268 formats.append(gbFormats);
269 filters.append(tr("Game Boy ROMs (%1)").arg(gbFormats.join(QChar(' '))));
270#endif
271
272 formats.removeDuplicates();
273 filters.prepend(tr("All ROMs (%1)").arg(formats.join(QChar(' '))));
274 filters.append(tr("%1 Video Logs (*.mvl)").arg(projectName));
275 return filters.join(";;");
276}
277
278QString Window::getFiltersArchive() const {
279 QStringList filters;
280
281 QStringList formats{
282#if defined(USE_LIBZIP) || defined(USE_ZLIB)
283 "*.zip",
284#endif
285#ifdef USE_LZMA
286 "*.7z",
287#endif
288 };
289 filters.append(tr("Archives (%1)").arg(formats.join(QChar(' '))));
290 return filters.join(";;");
291}
292
293void Window::selectROM() {
294 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), getFilters());
295 if (!filename.isEmpty()) {
296 setController(m_manager->loadGame(filename), filename);
297 }
298}
299
300#ifdef USE_SQLITE3
301void Window::selectROMInArchive() {
302 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), getFiltersArchive());
303 if (filename.isEmpty()) {
304 return;
305 }
306 ArchiveInspector* archiveInspector = new ArchiveInspector(filename);
307 connect(archiveInspector, &QDialog::accepted, [this, archiveInspector]() {
308 VFile* output = archiveInspector->selectedVFile();
309 QPair<QString, QString> path = archiveInspector->selectedPath();
310 if (output) {
311 setController(m_manager->loadGame(output, path.second, path.first), path.first + "/" + path.second);
312 }
313 archiveInspector->close();
314 });
315 archiveInspector->setAttribute(Qt::WA_DeleteOnClose);
316 archiveInspector->show();
317}
318
319void Window::addDirToLibrary() {
320 QString filename = GBAApp::app()->getOpenDirectoryName(this, tr("Select folder"));
321 if (filename.isEmpty()) {
322 return;
323 }
324 m_libraryView->addDirectory(filename);
325}
326#endif
327
328void Window::replaceROM() {
329 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), getFilters());
330 if (!filename.isEmpty()) {
331 m_controller->replaceGame(filename);
332 }
333}
334
335void Window::selectSave(bool temporary) {
336 QStringList formats{"*.sav"};
337 QString filter = tr("Game Boy Advance save files (%1)").arg(formats.join(QChar(' ')));
338 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select save"), filter);
339 if (!filename.isEmpty()) {
340 m_controller->loadSave(filename, temporary);
341 }
342}
343
344void Window::multiplayerChanged() {
345 if (!m_controller) {
346 return;
347 }
348 int attached = 1;
349 MultiplayerController* multiplayer = m_controller->multiplayerController();
350 if (multiplayer) {
351 attached = multiplayer->attached();
352 }
353 for (QAction* action : m_nonMpActions) {
354 action->setDisabled(attached > 1);
355 }
356}
357
358void Window::selectPatch() {
359 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select patch"), tr("Patches (*.ips *.ups *.bps)"));
360 if (!filename.isEmpty()) {
361 if (m_controller) {
362 m_controller->loadPatch(filename);
363 } else {
364 m_pendingPatch = filename;
365 }
366 }
367}
368
369void Window::openView(QWidget* widget) {
370 connect(this, &Window::shutdown, widget, &QWidget::close);
371 widget->setAttribute(Qt::WA_DeleteOnClose);
372 widget->show();
373}
374
375void Window::loadCamImage() {
376 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select image"), tr("Image file (*.png *.gif *.jpg *.jpeg);;All files (*)"));
377 if (!filename.isEmpty()) {
378 m_inputController.loadCamImage(filename);
379 }
380}
381
382void Window::importSharkport() {
383 QString filename = GBAApp::app()->getOpenFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
384 if (!filename.isEmpty()) {
385 m_controller->importSharkport(filename);
386 }
387}
388
389void Window::exportSharkport() {
390 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
391 if (!filename.isEmpty()) {
392 m_controller->exportSharkport(filename);
393 }
394}
395
396void Window::openSettingsWindow() {
397 SettingsView* settingsWindow = new SettingsView(m_config, &m_inputController, m_shortcutController);
398#if defined(BUILD_GL) || defined(BUILD_GLES)
399 if (m_display->supportsShaders()) {
400 settingsWindow->setShaderSelector(m_shaderView.get());
401 }
402#endif
403 connect(settingsWindow, &SettingsView::displayDriverChanged, this, &Window::reloadDisplayDriver);
404 connect(settingsWindow, &SettingsView::audioDriverChanged, this, &Window::reloadAudioDriver);
405 connect(settingsWindow, &SettingsView::languageChanged, this, &Window::mustRestart);
406 connect(settingsWindow, &SettingsView::pathsChanged, this, &Window::reloadConfig);
407 connect(settingsWindow, &SettingsView::libraryCleared, m_libraryView, &LibraryController::clear);
408 openView(settingsWindow);
409}
410
411void Window::startVideoLog() {
412 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select video log"), tr("Video logs (*.mvl)"));
413 if (!filename.isEmpty()) {
414 m_controller->startVideoLog(filename);
415 }
416}
417
418template <typename T, typename... A>
419std::function<void()> Window::openTView(A... arg) {
420 return [=]() {
421 T* view = new T(arg...);
422 openView(view);
423 };
424}
425
426
427template <typename T, typename... A>
428std::function<void()> Window::openControllerTView(A... arg) {
429 return [=]() {
430 T* view = new T(m_controller, arg...);
431 openView(view);
432 };
433}
434
435#ifdef USE_FFMPEG
436void Window::openVideoWindow() {
437 if (!m_videoView) {
438 m_videoView = new VideoView();
439 if (m_controller) {
440 m_videoView->setController(m_controller);
441 }
442 connect(this, &Window::shutdown, m_videoView, &QWidget::close);
443 }
444 m_videoView->show();
445}
446#endif
447
448#ifdef USE_MAGICK
449void Window::openGIFWindow() {
450 if (!m_gifView) {
451 m_gifView = new GIFView();
452 if (m_controller) {
453 m_gifView->setController(m_controller);
454 }
455 connect(this, &Window::shutdown, m_gifView, &QWidget::close);
456 }
457 m_gifView->show();
458}
459#endif
460
461#ifdef USE_GDB_STUB
462void Window::gdbOpen() {
463 if (!m_gdbController) {
464 m_gdbController = new GDBController(this);
465 }
466 GDBWindow* window = new GDBWindow(m_gdbController);
467 m_gdbController->setController(m_controller);
468 connect(m_controller.get(), &CoreController::stopping, window, &QWidget::close);
469 openView(window);
470}
471#endif
472
473#ifdef USE_DEBUGGERS
474void Window::consoleOpen() {
475 if (!m_console) {
476 m_console = new DebuggerConsoleController(this);
477 }
478 DebuggerConsole* window = new DebuggerConsole(m_console);
479 if (m_controller) {
480 m_console->setController(m_controller);
481 }
482 openView(window);
483}
484#endif
485
486void Window::keyPressEvent(QKeyEvent* event) {
487 if (event->isAutoRepeat()) {
488 QWidget::keyPressEvent(event);
489 return;
490 }
491 GBAKey key = m_inputController.mapKeyboard(event->key());
492 if (key == GBA_KEY_NONE) {
493 QWidget::keyPressEvent(event);
494 return;
495 }
496 if (m_controller) {
497 m_controller->addKey(key);
498 }
499 event->accept();
500}
501
502void Window::keyReleaseEvent(QKeyEvent* event) {
503 if (event->isAutoRepeat()) {
504 QWidget::keyReleaseEvent(event);
505 return;
506 }
507 GBAKey key = m_inputController.mapKeyboard(event->key());
508 if (key == GBA_KEY_NONE) {
509 QWidget::keyPressEvent(event);
510 return;
511 }
512 if (m_controller) {
513 m_controller->clearKey(key);
514 }
515 event->accept();
516}
517
518void Window::resizeEvent(QResizeEvent* event) {
519 if (!isFullScreen()) {
520 m_config->setOption("height", m_screenWidget->height());
521 m_config->setOption("width", m_screenWidget->width());
522 }
523
524 int factor = 0;
525 QSize size(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
526 if (m_controller) {
527 size = m_controller->screenDimensions();
528 }
529 if (m_screenWidget->width() % size.width() == 0 && m_screenWidget->height() % size.height() == 0 &&
530 m_screenWidget->width() / size.width() == m_screenWidget->height() / size.height()) {
531 factor = m_screenWidget->width() / size.width();
532 } else {
533 m_savedScale = 0;
534 }
535 for (QMap<int, QAction*>::iterator iter = m_frameSizes.begin(); iter != m_frameSizes.end(); ++iter) {
536 bool enableSignals = iter.value()->blockSignals(true);
537 iter.value()->setChecked(iter.key() == factor);
538 iter.value()->blockSignals(enableSignals);
539 }
540
541 m_config->setOption("fullscreen", isFullScreen());
542}
543
544void Window::showEvent(QShowEvent* event) {
545 if (m_wasOpened) {
546 return;
547 }
548 m_wasOpened = true;
549 resizeFrame(m_screenWidget->sizeHint());
550 QVariant windowPos = m_config->getQtOption("windowPos");
551 if (!windowPos.isNull()) {
552 move(windowPos.toPoint());
553 } else {
554 QRect rect = frameGeometry();
555 rect.moveCenter(QApplication::desktop()->availableGeometry().center());
556 move(rect.topLeft());
557 }
558 if (m_fullscreenOnStart) {
559 enterFullScreen();
560 m_fullscreenOnStart = false;
561 }
562}
563
564void Window::closeEvent(QCloseEvent* event) {
565 emit shutdown();
566 m_config->setQtOption("windowPos", pos());
567
568 if (m_savedScale > 0) {
569 m_config->setOption("height", VIDEO_VERTICAL_PIXELS * m_savedScale);
570 m_config->setOption("width", VIDEO_HORIZONTAL_PIXELS * m_savedScale);
571 }
572 saveConfig();
573 QMainWindow::closeEvent(event);
574}
575
576void Window::focusInEvent(QFocusEvent*) {
577 m_display->forceDraw();
578}
579
580void Window::focusOutEvent(QFocusEvent*) {
581}
582
583void Window::dragEnterEvent(QDragEnterEvent* event) {
584 if (event->mimeData()->hasFormat("text/uri-list")) {
585 event->acceptProposedAction();
586 }
587}
588
589void Window::dropEvent(QDropEvent* event) {
590 QString uris = event->mimeData()->data("text/uri-list");
591 uris = uris.trimmed();
592 if (uris.contains("\n")) {
593 // Only one file please
594 return;
595 }
596 QUrl url(uris);
597 if (!url.isLocalFile()) {
598 // No remote loading
599 return;
600 }
601 event->accept();
602 setController(m_manager->loadGame(url.toLocalFile()), url.toLocalFile());
603}
604
605void Window::mouseDoubleClickEvent(QMouseEvent* event) {
606 if (event->button() != Qt::LeftButton) {
607 return;
608 }
609 toggleFullScreen();
610}
611
612void Window::enterFullScreen() {
613 if (!isVisible()) {
614 m_fullscreenOnStart = true;
615 return;
616 }
617 if (isFullScreen()) {
618 return;
619 }
620 showFullScreen();
621#ifndef Q_OS_MAC
622 if (m_controller && !m_controller->isPaused()) {
623 menuBar()->hide();
624 }
625#endif
626}
627
628void Window::exitFullScreen() {
629 if (!isFullScreen()) {
630 return;
631 }
632 m_screenWidget->unsetCursor();
633 menuBar()->show();
634 showNormal();
635}
636
637void Window::toggleFullScreen() {
638 if (isFullScreen()) {
639 exitFullScreen();
640 } else {
641 enterFullScreen();
642 }
643}
644
645void Window::gameStarted() {
646 for (QAction* action : m_gameActions) {
647 action->setDisabled(false);
648 }
649#ifdef M_CORE_GBA
650 for (QAction* action : m_gbaActions) {
651 action->setDisabled(m_controller->platform() != PLATFORM_GBA);
652 }
653#endif
654 multiplayerChanged();
655 updateTitle();
656 QSize size = m_controller->screenDimensions();
657 m_display->setMinimumSize(size);
658 m_screenWidget->setMinimumSize(m_display->minimumSize());
659 m_screenWidget->setDimensions(size.width(), size.height());
660 m_config->updateOption("lockIntegerScaling");
661 m_config->updateOption("lockAspectRatio");
662 if (m_savedScale > 0) {
663 resizeFrame(size * m_savedScale);
664 }
665 attachWidget(m_display.get());
666
667#ifndef Q_OS_MAC
668 if (isFullScreen()) {
669 menuBar()->hide();
670 }
671#endif
672
673 m_hitUnimplementedBiosCall = false;
674 m_fpsTimer.start();
675 m_focusCheck.start();
676
677 CoreController::Interrupter interrupter(m_controller, true);
678 mCore* core = m_controller->thread()->core;
679 m_videoLayers->clear();
680 m_audioChannels->clear();
681 const mCoreChannelInfo* videoLayers;
682 const mCoreChannelInfo* audioChannels;
683 size_t nVideo = core->listVideoLayers(core, &videoLayers);
684 size_t nAudio = core->listAudioChannels(core, &audioChannels);
685
686 if (nVideo) {
687 for (size_t i = 0; i < nVideo; ++i) {
688 QAction* action = new QAction(videoLayers[i].visibleName, m_videoLayers);
689 action->setCheckable(true);
690 action->setChecked(true);
691 connect(action, &QAction::triggered, [this, videoLayers, i](bool enable) {
692 m_controller->thread()->core->enableVideoLayer(m_controller->thread()->core, videoLayers[i].id, enable);
693 });
694 m_videoLayers->addAction(action);
695 }
696 }
697 if (nAudio) {
698 for (size_t i = 0; i < nAudio; ++i) {
699 QAction* action = new QAction(audioChannels[i].visibleName, m_audioChannels);
700 action->setCheckable(true);
701 action->setChecked(true);
702 connect(action, &QAction::triggered, [this, audioChannels, i](bool enable) {
703 m_controller->thread()->core->enableAudioChannel(m_controller->thread()->core, audioChannels[i].id, enable);
704 });
705 m_audioChannels->addAction(action);
706 }
707 }
708 m_display->startDrawing(m_controller);
709
710 reloadAudioDriver();
711}
712
713void Window::gameStopped() {
714#ifdef M_CORE_GBA
715 for (QAction* action : m_gbaActions) {
716 action->setDisabled(false);
717 }
718#endif
719 for (QAction* action : m_gameActions) {
720 action->setDisabled(true);
721 }
722 setWindowFilePath(QString());
723 updateTitle();
724 detachWidget(m_display.get());
725 m_screenWidget->setDimensions(m_logo.width(), m_logo.height());
726 m_screenWidget->setLockIntegerScaling(false);
727 m_screenWidget->setLockAspectRatio(true);
728 m_screenWidget->setPixmap(m_logo);
729 m_screenWidget->unsetCursor();
730#ifdef M_CORE_GB
731 m_display->setMinimumSize(GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
732#elif defined(M_CORE_GBA)
733 m_display->setMinimumSize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
734#endif
735 m_screenWidget->setMinimumSize(m_display->minimumSize());
736
737 m_videoLayers->clear();
738 m_audioChannels->clear();
739
740 m_fpsTimer.stop();
741 m_focusCheck.stop();
742
743 emit paused(false);
744}
745
746void Window::gameCrashed(const QString& errorMessage) {
747 QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
748 tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
749 QMessageBox::Ok, this, Qt::Sheet);
750 crash->setAttribute(Qt::WA_DeleteOnClose);
751 crash->show();
752 m_controller->stop();
753}
754
755void Window::gameFailed() {
756 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
757 tr("Could not load game. Are you sure it's in the correct format?"),
758 QMessageBox::Ok, this, Qt::Sheet);
759 fail->setAttribute(Qt::WA_DeleteOnClose);
760 fail->show();
761}
762
763void Window::unimplementedBiosCall(int call) {
764 if (m_hitUnimplementedBiosCall) {
765 return;
766 }
767 m_hitUnimplementedBiosCall = true;
768
769 QMessageBox* fail = new QMessageBox(
770 QMessageBox::Warning, tr("Unimplemented BIOS call"),
771 tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
772 QMessageBox::Ok, this, Qt::Sheet);
773 fail->setAttribute(Qt::WA_DeleteOnClose);
774 fail->show();
775}
776
777void Window::reloadDisplayDriver() {
778 if (m_controller) {
779 m_display->stopDrawing();
780 detachWidget(m_display.get());
781 }
782 m_display = std::move(std::unique_ptr<Display>(Display::create(this)));
783#if defined(BUILD_GL) || defined(BUILD_GLES)
784 m_shaderView.reset();
785 m_shaderView = std::make_unique<ShaderSelector>(m_display.get(), m_config);
786#endif
787 m_screenWidget->setMinimumSize(m_display->minimumSize());
788 m_screenWidget->setSizePolicy(m_display->sizePolicy());
789 connect(this, &Window::shutdown, m_display.get(), &Display::stopDrawing);
790 connect(m_display.get(), &Display::hideCursor, [this]() {
791 if (static_cast<QStackedLayout*>(m_screenWidget->layout())->currentWidget() == m_display.get()) {
792 m_screenWidget->setCursor(Qt::BlankCursor);
793 }
794 });
795 connect(m_display.get(), &Display::showCursor, [this]() {
796 m_screenWidget->unsetCursor();
797 });
798
799 const mCoreOptions* opts = m_config->options();
800 m_display->lockAspectRatio(opts->lockAspectRatio);
801 m_display->filter(opts->resampleVideo);
802#if defined(BUILD_GL) || defined(BUILD_GLES)
803 if (opts->shader) {
804 struct VDir* shader = VDirOpen(opts->shader);
805 if (shader && m_display->supportsShaders()) {
806 m_display->setShaders(shader);
807 m_shaderView->refreshShaders();
808 shader->close(shader);
809 }
810 }
811#endif
812
813 if (m_controller) {
814 connect(m_controller.get(), &CoreController::stopping, m_display.get(), &Display::stopDrawing);
815 connect(m_controller.get(), &CoreController::stateLoaded, m_display.get(), &Display::forceDraw);
816 connect(m_controller.get(), &CoreController::rewound, m_display.get(), &Display::forceDraw);
817 connect(m_controller.get(), &CoreController::paused, m_display.get(), &Display::pauseDrawing);
818 connect(m_controller.get(), &CoreController::unpaused, m_display.get(), &Display::unpauseDrawing);
819 connect(m_controller.get(), &CoreController::frameAvailable, m_display.get(), &Display::framePosted);
820 connect(m_controller.get(), &CoreController::statusPosted, m_display.get(), &Display::showMessage);
821
822 attachWidget(m_display.get());
823 m_display->startDrawing(m_controller);
824 }
825}
826
827void Window::reloadAudioDriver() {
828 if (!m_controller) {
829 return;
830 }
831 if (m_audioProcessor) {
832 m_audioProcessor->stop();
833 m_audioProcessor.reset();
834 }
835
836 const mCoreOptions* opts = m_config->options();
837 m_audioProcessor = std::move(std::unique_ptr<AudioProcessor>(AudioProcessor::create()));
838 m_audioProcessor->setInput(m_controller);
839 m_audioProcessor->setBufferSamples(opts->audioBuffers);
840 m_audioProcessor->requestSampleRate(opts->sampleRate);
841 m_audioProcessor->start();
842 connect(m_controller.get(), &CoreController::stopping, m_audioProcessor.get(), &AudioProcessor::stop);
843}
844
845void Window::tryMakePortable() {
846 QMessageBox* confirm = new QMessageBox(QMessageBox::Question, tr("Really make portable?"),
847 tr("This will make the emulator load its configuration from the same directory as the executable. Do you want to continue?"),
848 QMessageBox::Yes | QMessageBox::Cancel, this, Qt::Sheet);
849 confirm->setAttribute(Qt::WA_DeleteOnClose);
850 connect(confirm->button(QMessageBox::Yes), &QAbstractButton::clicked, m_config, &ConfigController::makePortable);
851 confirm->show();
852}
853
854void Window::mustRestart() {
855 QMessageBox* dialog = new QMessageBox(QMessageBox::Warning, tr("Restart needed"),
856 tr("Some changes will not take effect until the emulator is restarted."),
857 QMessageBox::Ok, this, Qt::Sheet);
858 dialog->setAttribute(Qt::WA_DeleteOnClose);
859 dialog->show();
860}
861
862void Window::recordFrame() {
863 m_frameList.append(QDateTime::currentDateTime());
864 while (m_frameList.count() > FRAME_LIST_SIZE) {
865 m_frameList.removeFirst();
866 }
867}
868
869void Window::showFPS() {
870 if (m_frameList.isEmpty()) {
871 updateTitle();
872 return;
873 }
874 qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
875 float fps = (m_frameList.count() - 1) * 10000.f / interval;
876 fps = round(fps) / 10.f;
877 updateTitle(fps);
878}
879
880void Window::updateTitle(float fps) {
881 QString title;
882
883 if (m_controller) {
884 CoreController::Interrupter interrupter(m_controller);
885 const NoIntroDB* db = GBAApp::app()->gameDB();
886 NoIntroGame game{};
887 uint32_t crc32 = 0;
888 m_controller->thread()->core->checksum(m_controller->thread()->core, &crc32, CHECKSUM_CRC32);
889
890 char gameTitle[17] = { '\0' };
891 mCore* core = m_controller->thread()->core;
892 core->getGameTitle(core, gameTitle);
893 title = gameTitle;
894
895#ifdef USE_SQLITE3
896 if (db && crc32 && NoIntroDBLookupGameByCRC(db, crc32, &game)) {
897 title = QLatin1String(game.name);
898 }
899#endif
900 MultiplayerController* multiplayer = m_controller->multiplayerController();
901 if (multiplayer && multiplayer->attached() > 1) {
902 title += tr(" - Player %1 of %2").arg(multiplayer->playerId(m_controller.get()) + 1).arg(multiplayer->attached());
903 for (QAction* action : m_nonMpActions) {
904 action->setDisabled(true);
905 }
906 } else {
907 for (QAction* action : m_nonMpActions) {
908 action->setDisabled(false);
909 }
910 }
911 }
912 if (title.isNull()) {
913 setWindowTitle(tr("%1 - %2").arg(projectName).arg(projectVersion));
914 } else if (fps < 0) {
915 setWindowTitle(tr("%1 - %2 - %3").arg(projectName).arg(title).arg(projectVersion));
916 } else {
917 setWindowTitle(tr("%1 - %2 (%3 fps) - %4").arg(projectName).arg(title).arg(fps).arg(projectVersion));
918 }
919}
920
921void Window::openStateWindow(LoadSave ls) {
922 if (m_stateWindow) {
923 return;
924 }
925 MultiplayerController* multiplayer = m_controller->multiplayerController();
926 if (multiplayer && multiplayer->attached() > 1) {
927 return;
928 }
929 bool wasPaused = m_controller->isPaused();
930 m_stateWindow = new LoadSaveState(m_controller);
931 connect(this, &Window::shutdown, m_stateWindow, &QWidget::close);
932 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
933 detachWidget(m_stateWindow);
934 m_stateWindow = nullptr;
935 QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
936 });
937 if (!wasPaused) {
938 m_controller->setPaused(true);
939 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
940 if (m_controller) {
941 m_controller->setPaused(false);
942 }
943 });
944 }
945 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
946 m_stateWindow->setMode(ls);
947 attachWidget(m_stateWindow);
948}
949
950void Window::setupMenu(QMenuBar* menubar) {
951 menubar->clear();
952 QMenu* fileMenu = menubar->addMenu(tr("&File"));
953 m_shortcutController->addMenu(fileMenu);
954 installEventFilter(m_shortcutController);
955 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open),
956 "loadROM");
957#ifdef USE_SQLITE3
958 addControlledAction(fileMenu, fileMenu->addAction(tr("Load ROM in archive..."), this, SLOT(selectROMInArchive())),
959 "loadROMInArchive");
960 addControlledAction(fileMenu, fileMenu->addAction(tr("Add folder to library..."), this, SLOT(addDirToLibrary())),
961 "addDirToLibrary");
962#endif
963
964 QAction* loadTemporarySave = new QAction(tr("Load temporary save..."), fileMenu);
965 connect(loadTemporarySave, &QAction::triggered, [this]() { this->selectSave(true); });
966 m_gameActions.append(loadTemporarySave);
967 addControlledAction(fileMenu, loadTemporarySave, "loadTemporarySave");
968
969 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
970
971#ifdef M_CORE_GBA
972 QAction* bootBIOS = new QAction(tr("Boot BIOS"), fileMenu);
973 connect(bootBIOS, &QAction::triggered, [this]() {
974 setController(m_manager->loadBIOS(PLATFORM_GBA, m_config->getOption("gba.bios")), QString());
975 });
976 addControlledAction(fileMenu, bootBIOS, "bootBIOS");
977#endif
978
979 addControlledAction(fileMenu, fileMenu->addAction(tr("Replace ROM..."), this, SLOT(replaceROM())), "replaceROM");
980
981 QAction* romInfo = new QAction(tr("ROM &info..."), fileMenu);
982 connect(romInfo, &QAction::triggered, openControllerTView<ROMInfo>());
983 m_gameActions.append(romInfo);
984 addControlledAction(fileMenu, romInfo, "romInfo");
985
986 m_mruMenu = fileMenu->addMenu(tr("Recent"));
987
988 fileMenu->addSeparator();
989
990 addControlledAction(fileMenu, fileMenu->addAction(tr("Make portable"), this, SLOT(tryMakePortable())), "makePortable");
991
992 fileMenu->addSeparator();
993
994 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
995 loadState->setShortcut(tr("F10"));
996 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
997 m_gameActions.append(loadState);
998 m_nonMpActions.append(loadState);
999 addControlledAction(fileMenu, loadState, "loadState");
1000
1001 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
1002 saveState->setShortcut(tr("Shift+F10"));
1003 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
1004 m_gameActions.append(saveState);
1005 m_nonMpActions.append(saveState);
1006 addControlledAction(fileMenu, saveState, "saveState");
1007
1008 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
1009 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
1010 m_shortcutController->addMenu(quickLoadMenu);
1011 m_shortcutController->addMenu(quickSaveMenu);
1012
1013 QAction* quickLoad = new QAction(tr("Load recent"), quickLoadMenu);
1014 connect(quickLoad, &QAction::triggered, [this] {
1015 m_controller->loadState();
1016 });
1017 m_gameActions.append(quickLoad);
1018 m_nonMpActions.append(quickLoad);
1019 addControlledAction(quickLoadMenu, quickLoad, "quickLoad");
1020
1021 QAction* quickSave = new QAction(tr("Save recent"), quickSaveMenu);
1022 connect(quickLoad, &QAction::triggered, [this] {
1023 m_controller->saveState();
1024 });
1025 m_gameActions.append(quickSave);
1026 m_nonMpActions.append(quickSave);
1027 addControlledAction(quickSaveMenu, quickSave, "quickSave");
1028
1029 quickLoadMenu->addSeparator();
1030 quickSaveMenu->addSeparator();
1031
1032 QAction* undoLoadState = new QAction(tr("Undo load state"), quickLoadMenu);
1033 undoLoadState->setShortcut(tr("F11"));
1034 connect(undoLoadState, &QAction::triggered, [this]() {
1035 m_controller->loadBackupState();
1036 });
1037 m_gameActions.append(undoLoadState);
1038 m_nonMpActions.append(undoLoadState);
1039 addControlledAction(quickLoadMenu, undoLoadState, "undoLoadState");
1040
1041 QAction* undoSaveState = new QAction(tr("Undo save state"), quickSaveMenu);
1042 undoSaveState->setShortcut(tr("Shift+F11"));
1043 connect(undoSaveState, &QAction::triggered, [this]() {
1044 m_controller->saveBackupState();
1045 });
1046 m_gameActions.append(undoSaveState);
1047 m_nonMpActions.append(undoSaveState);
1048 addControlledAction(quickSaveMenu, undoSaveState, "undoSaveState");
1049
1050 quickLoadMenu->addSeparator();
1051 quickSaveMenu->addSeparator();
1052
1053 int i;
1054 for (i = 1; i < 10; ++i) {
1055 quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
1056 quickLoad->setShortcut(tr("F%1").arg(i));
1057 connect(quickLoad, &QAction::triggered, [this, i]() {
1058 m_controller->loadState(i);
1059 });
1060 m_gameActions.append(quickLoad);
1061 m_nonMpActions.append(quickLoad);
1062 addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
1063
1064 quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
1065 quickSave->setShortcut(tr("Shift+F%1").arg(i));
1066 connect(quickSave, &QAction::triggered, [this, i]() {
1067 m_controller->saveState(i);
1068 });
1069 m_gameActions.append(quickSave);
1070 m_nonMpActions.append(quickSave);
1071 addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
1072 }
1073
1074 fileMenu->addSeparator();
1075 QAction* camImage = new QAction(tr("Load camera image..."), fileMenu);
1076 connect(camImage, &QAction::triggered, this, &Window::loadCamImage);
1077 addControlledAction(fileMenu, camImage, "loadCamImage");
1078
1079#ifdef M_CORE_GBA
1080 fileMenu->addSeparator();
1081 QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
1082 connect(importShark, &QAction::triggered, this, &Window::importSharkport);
1083 m_gameActions.append(importShark);
1084 m_gbaActions.append(importShark);
1085 addControlledAction(fileMenu, importShark, "importShark");
1086
1087 QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
1088 connect(exportShark, &QAction::triggered, this, &Window::exportSharkport);
1089 m_gameActions.append(exportShark);
1090 m_gbaActions.append(exportShark);
1091 addControlledAction(fileMenu, exportShark, "exportShark");
1092#endif
1093
1094 fileMenu->addSeparator();
1095 m_multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
1096 connect(m_multiWindow, &QAction::triggered, [this]() {
1097 GBAApp::app()->newWindow();
1098 });
1099 addControlledAction(fileMenu, m_multiWindow, "multiWindow");
1100
1101#ifndef Q_OS_MAC
1102 fileMenu->addSeparator();
1103#endif
1104
1105 QAction* about = new QAction(tr("About"), fileMenu);
1106 connect(about, &QAction::triggered, openTView<AboutScreen>());
1107 fileMenu->addAction(about);
1108
1109#ifndef Q_OS_MAC
1110 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
1111#endif
1112
1113 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
1114 m_shortcutController->addMenu(emulationMenu);
1115 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
1116 reset->setShortcut(tr("Ctrl+R"));
1117 connect(reset, &QAction::triggered, [this]() {
1118 m_controller->reset();
1119 });
1120 m_gameActions.append(reset);
1121 addControlledAction(emulationMenu, reset, "reset");
1122
1123 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
1124 connect(shutdown, &QAction::triggered, [this]() {
1125 m_controller->stop();
1126 });
1127 m_gameActions.append(shutdown);
1128 addControlledAction(emulationMenu, shutdown, "shutdown");
1129
1130#ifdef M_CORE_GBA
1131 QAction* yank = new QAction(tr("Yank game pak"), emulationMenu);
1132 connect(yank, &QAction::triggered, [this]() {
1133 m_controller->yankPak();
1134 });
1135 m_gameActions.append(yank);
1136 m_gbaActions.append(yank);
1137 addControlledAction(emulationMenu, yank, "yank");
1138#endif
1139 emulationMenu->addSeparator();
1140
1141 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
1142 pause->setChecked(false);
1143 pause->setCheckable(true);
1144 pause->setShortcut(tr("Ctrl+P"));
1145 connect(pause, &QAction::triggered, [this](bool paused) {
1146 m_controller->setPaused(paused);
1147 });
1148 connect(this, &Window::paused, [pause](bool paused) {
1149 pause->setChecked(paused);
1150 });
1151 m_gameActions.append(pause);
1152 addControlledAction(emulationMenu, pause, "pause");
1153
1154 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
1155 frameAdvance->setShortcut(tr("Ctrl+N"));
1156 connect(frameAdvance, &QAction::triggered, [this]() {
1157 m_controller->frameAdvance();
1158 });
1159 m_gameActions.append(frameAdvance);
1160 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
1161
1162 emulationMenu->addSeparator();
1163
1164 m_shortcutController->addFunctions(emulationMenu, [this]() {
1165 m_controller->setFastForward(true);
1166 }, [this]() {
1167 m_controller->setFastForward(false);
1168 }, QKeySequence(Qt::Key_Tab), tr("Fast forward (held)"), "holdFastForward");
1169
1170 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
1171 turbo->setCheckable(true);
1172 turbo->setChecked(false);
1173 turbo->setShortcut(tr("Shift+Tab"));
1174 connect(turbo, &QAction::triggered, [this](bool value) {
1175 m_controller->forceFastForward(value);
1176 });
1177 addControlledAction(emulationMenu, turbo, "fastForward");
1178 m_gameActions.append(turbo);
1179
1180 QMenu* ffspeedMenu = emulationMenu->addMenu(tr("Fast forward speed"));
1181 ConfigOption* ffspeed = m_config->addOption("fastForwardRatio");
1182 ffspeed->connect([this](const QVariant& value) {
1183 reloadConfig();
1184 }, this);
1185 ffspeed->addValue(tr("Unbounded"), -1.0f, ffspeedMenu);
1186 ffspeed->setValue(QVariant(-1.0f));
1187 ffspeedMenu->addSeparator();
1188 for (i = 2; i < 11; ++i) {
1189 ffspeed->addValue(tr("%0x").arg(i), i, ffspeedMenu);
1190 }
1191 m_config->updateOption("fastForwardRatio");
1192
1193 m_shortcutController->addFunctions(emulationMenu, [this]() {
1194 if (m_controller) {
1195 m_controller->setRewinding(true);
1196 }
1197 }, [this]() {
1198 if (m_controller) {
1199 m_controller->setRewinding(false);
1200 }
1201 }, QKeySequence("`"), tr("Rewind (held)"), "holdRewind");
1202
1203 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
1204 rewind->setShortcut(tr("~"));
1205 connect(rewind, &QAction::triggered, [this]() {
1206 m_controller->rewind();
1207 });
1208 m_gameActions.append(rewind);
1209 m_nonMpActions.append(rewind);
1210 addControlledAction(emulationMenu, rewind, "rewind");
1211
1212 QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
1213 frameRewind->setShortcut(tr("Ctrl+B"));
1214 connect(frameRewind, &QAction::triggered, [this] () {
1215 m_controller->rewind(1);
1216 });
1217 m_gameActions.append(frameRewind);
1218 m_nonMpActions.append(frameRewind);
1219 addControlledAction(emulationMenu, frameRewind, "frameRewind");
1220
1221 ConfigOption* videoSync = m_config->addOption("videoSync");
1222 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
1223 videoSync->connect([this](const QVariant& value) {
1224 reloadConfig();
1225 }, this);
1226 m_config->updateOption("videoSync");
1227
1228 ConfigOption* audioSync = m_config->addOption("audioSync");
1229 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
1230 audioSync->connect([this](const QVariant& value) {
1231 reloadConfig();
1232 }, this);
1233 m_config->updateOption("audioSync");
1234
1235 emulationMenu->addSeparator();
1236
1237 QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
1238 m_shortcutController->addMenu(solarMenu);
1239 QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
1240 connect(solarIncrease, &QAction::triggered, &m_inputController, &InputController::increaseLuminanceLevel);
1241 addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
1242
1243 QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
1244 connect(solarDecrease, &QAction::triggered, &m_inputController, &InputController::decreaseLuminanceLevel);
1245 addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
1246
1247 QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
1248 connect(maxSolar, &QAction::triggered, [this]() { m_inputController.setLuminanceLevel(10); });
1249 addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
1250
1251 QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
1252 connect(minSolar, &QAction::triggered, [this]() { m_inputController.setLuminanceLevel(0); });
1253 addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
1254
1255 solarMenu->addSeparator();
1256 for (int i = 0; i <= 10; ++i) {
1257 QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
1258 connect(setSolar, &QAction::triggered, [this, i]() {
1259 m_inputController.setLuminanceLevel(i);
1260 });
1261 addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
1262 }
1263
1264 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
1265 m_shortcutController->addMenu(avMenu);
1266 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
1267 m_shortcutController->addMenu(frameMenu, avMenu);
1268 for (int i = 1; i <= 6; ++i) {
1269 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
1270 setSize->setCheckable(true);
1271 if (m_savedScale == i) {
1272 setSize->setChecked(true);
1273 }
1274 connect(setSize, &QAction::triggered, [this, i, setSize]() {
1275 showNormal();
1276 QSize size(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
1277 if (m_controller) {
1278 size = m_controller->screenDimensions();
1279 }
1280 size *= i;
1281 m_savedScale = i;
1282 m_config->setOption("scaleMultiplier", i); // TODO: Port to other
1283 resizeFrame(size);
1284 bool enableSignals = setSize->blockSignals(true);
1285 setSize->setChecked(true);
1286 setSize->blockSignals(enableSignals);
1287 });
1288 m_frameSizes[i] = setSize;
1289 addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
1290 }
1291 QKeySequence fullscreenKeys;
1292#ifdef Q_OS_WIN
1293 fullscreenKeys = QKeySequence("Alt+Return");
1294#else
1295 fullscreenKeys = QKeySequence("Ctrl+F");
1296#endif
1297 addControlledAction(frameMenu, frameMenu->addAction(tr("Toggle fullscreen"), this, SLOT(toggleFullScreen()), fullscreenKeys), "fullscreen");
1298
1299 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
1300 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
1301 lockAspectRatio->connect([this](const QVariant& value) {
1302 m_display->lockAspectRatio(value.toBool());
1303 if (m_controller) {
1304 m_screenWidget->setLockAspectRatio(value.toBool());
1305 }
1306 }, this);
1307 m_config->updateOption("lockAspectRatio");
1308
1309 ConfigOption* lockIntegerScaling = m_config->addOption("lockIntegerScaling");
1310 lockIntegerScaling->addBoolean(tr("Force integer scaling"), avMenu);
1311 lockIntegerScaling->connect([this](const QVariant& value) {
1312 m_display->lockIntegerScaling(value.toBool());
1313 if (m_controller) {
1314 m_screenWidget->setLockIntegerScaling(value.toBool());
1315 }
1316 }, this);
1317 m_config->updateOption("lockIntegerScaling");
1318
1319 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
1320 resampleVideo->addBoolean(tr("Bilinear filtering"), avMenu);
1321 resampleVideo->connect([this](const QVariant& value) {
1322 m_display->filter(value.toBool());
1323 }, this);
1324 m_config->updateOption("resampleVideo");
1325
1326 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
1327 ConfigOption* skip = m_config->addOption("frameskip");
1328 skip->connect([this](const QVariant& value) {
1329 reloadConfig();
1330 }, this);
1331 for (int i = 0; i <= 10; ++i) {
1332 skip->addValue(QString::number(i), i, skipMenu);
1333 }
1334 m_config->updateOption("frameskip");
1335
1336 avMenu->addSeparator();
1337
1338 ConfigOption* mute = m_config->addOption("mute");
1339 QAction* muteAction = mute->addBoolean(tr("Mute"), avMenu);
1340 mute->connect([this](const QVariant& value) {
1341 reloadConfig();
1342 }, this);
1343 m_config->updateOption("mute");
1344 addControlledAction(avMenu, muteAction, "mute");
1345
1346 QMenu* target = avMenu->addMenu(tr("FPS target"));
1347 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
1348 fpsTargetOption->connect([this](const QVariant& value) {
1349 reloadConfig();
1350 }, this);
1351 fpsTargetOption->addValue(tr("15"), 15, target);
1352 fpsTargetOption->addValue(tr("30"), 30, target);
1353 fpsTargetOption->addValue(tr("45"), 45, target);
1354 fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
1355 fpsTargetOption->addValue(tr("60"), 60, target);
1356 fpsTargetOption->addValue(tr("90"), 90, target);
1357 fpsTargetOption->addValue(tr("120"), 120, target);
1358 fpsTargetOption->addValue(tr("240"), 240, target);
1359 m_config->updateOption("fpsTarget");
1360
1361#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
1362 avMenu->addSeparator();
1363#endif
1364
1365#ifdef USE_PNG
1366 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
1367 screenshot->setShortcut(tr("F12"));
1368 connect(screenshot, &QAction::triggered, [this]() {
1369 m_controller->screenshot();
1370 });
1371 m_gameActions.append(screenshot);
1372 addControlledAction(avMenu, screenshot, "screenshot");
1373#endif
1374
1375#ifdef USE_FFMPEG
1376 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
1377 connect(recordOutput, &QAction::triggered, this, &Window::openVideoWindow);
1378 addControlledAction(avMenu, recordOutput, "recordOutput");
1379 m_gameActions.append(recordOutput);
1380#endif
1381
1382#ifdef USE_MAGICK
1383 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
1384 connect(recordGIF, &QAction::triggered, this, &Window::openGIFWindow);
1385 addControlledAction(avMenu, recordGIF, "recordGIF");
1386#endif
1387
1388 QAction* recordVL = new QAction(tr("Record video log..."), avMenu);
1389 connect(recordVL, &QAction::triggered, this, &Window::startVideoLog);
1390 addControlledAction(avMenu, recordVL, "recordVL");
1391 m_gameActions.append(recordVL);
1392
1393 QAction* stopVL = new QAction(tr("Stop video log"), avMenu);
1394 connect(stopVL, &QAction::triggered, [this]() {
1395 m_controller->endVideoLog();
1396 });
1397 addControlledAction(avMenu, stopVL, "stopVL");
1398 m_gameActions.append(stopVL);
1399
1400 avMenu->addSeparator();
1401 m_videoLayers = avMenu->addMenu(tr("Video layers"));
1402 m_shortcutController->addMenu(m_videoLayers, avMenu);
1403
1404 m_audioChannels = avMenu->addMenu(tr("Audio channels"));
1405 m_shortcutController->addMenu(m_audioChannels, avMenu);
1406
1407 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
1408 m_shortcutController->addMenu(toolsMenu);
1409 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
1410 connect(viewLogs, &QAction::triggered, m_logView, &QWidget::show);
1411 addControlledAction(toolsMenu, viewLogs, "viewLogs");
1412
1413 QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
1414 connect(overrides, &QAction::triggered, [this]() {
1415 if (!m_overrideView) {
1416 m_overrideView = new OverrideView(m_config);
1417 if (m_controller) {
1418 m_overrideView->setController(m_controller);
1419 }
1420 connect(this, &Window::shutdown, m_overrideView, &QWidget::close);
1421 }
1422 m_overrideView->show();
1423 });
1424 addControlledAction(toolsMenu, overrides, "overrideWindow");
1425
1426 QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
1427 connect(sensors, &QAction::triggered, openTView<SensorView, InputController*>(&m_inputController));
1428 addControlledAction(toolsMenu, sensors, "sensorWindow");
1429
1430 QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
1431 connect(cheats, &QAction::triggered, openControllerTView<CheatsView>());
1432 m_gameActions.append(cheats);
1433 addControlledAction(toolsMenu, cheats, "cheatsWindow");
1434
1435 toolsMenu->addSeparator();
1436 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())),
1437 "settings");
1438
1439 toolsMenu->addSeparator();
1440
1441#ifdef USE_DEBUGGERS
1442 QAction* consoleWindow = new QAction(tr("Open debugger console..."), toolsMenu);
1443 connect(consoleWindow, &QAction::triggered, this, &Window::consoleOpen);
1444 addControlledAction(toolsMenu, consoleWindow, "debuggerWindow");
1445#endif
1446
1447#ifdef USE_GDB_STUB
1448 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
1449 connect(gdbWindow, &QAction::triggered, this, &Window::gdbOpen);
1450 m_gbaActions.append(gdbWindow);
1451 m_gameActions.append(gdbWindow);
1452 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
1453#endif
1454 toolsMenu->addSeparator();
1455
1456 QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
1457 connect(paletteView, &QAction::triggered, openControllerTView<PaletteView>());
1458 m_gameActions.append(paletteView);
1459 addControlledAction(toolsMenu, paletteView, "paletteWindow");
1460
1461 QAction* objView = new QAction(tr("View &sprites..."), toolsMenu);
1462 connect(objView, &QAction::triggered, openControllerTView<ObjView>());
1463 m_gameActions.append(objView);
1464 addControlledAction(toolsMenu, objView, "spriteWindow");
1465
1466 QAction* tileView = new QAction(tr("View &tiles..."), toolsMenu);
1467 connect(tileView, &QAction::triggered, openControllerTView<TileView>());
1468 m_gameActions.append(tileView);
1469 addControlledAction(toolsMenu, tileView, "tileWindow");
1470
1471 QAction* memoryView = new QAction(tr("View memory..."), toolsMenu);
1472 connect(memoryView, &QAction::triggered, openControllerTView<MemoryView>());
1473 m_gameActions.append(memoryView);
1474 addControlledAction(toolsMenu, memoryView, "memoryView");
1475
1476 QAction* memorySearch = new QAction(tr("Search memory..."), toolsMenu);
1477 connect(memorySearch, &QAction::triggered, openControllerTView<MemorySearch>());
1478 m_gameActions.append(memorySearch);
1479 addControlledAction(toolsMenu, memorySearch, "memorySearch");
1480
1481#ifdef M_CORE_GBA
1482 QAction* ioViewer = new QAction(tr("View &I/O registers..."), toolsMenu);
1483 connect(ioViewer, &QAction::triggered, openControllerTView<IOViewer>());
1484 m_gameActions.append(ioViewer);
1485 m_gbaActions.append(ioViewer);
1486 addControlledAction(toolsMenu, ioViewer, "ioViewer");
1487#endif
1488
1489 ConfigOption* skipBios = m_config->addOption("skipBios");
1490 skipBios->connect([this](const QVariant& value) {
1491 reloadConfig();
1492 }, this);
1493
1494 ConfigOption* useBios = m_config->addOption("useBios");
1495 useBios->connect([this](const QVariant& value) {
1496 reloadConfig();
1497 }, this);
1498
1499 ConfigOption* buffers = m_config->addOption("audioBuffers");
1500 buffers->connect([this](const QVariant& value) {
1501 reloadConfig();
1502 }, this);
1503
1504 ConfigOption* sampleRate = m_config->addOption("sampleRate");
1505 sampleRate->connect([this](const QVariant& value) {
1506 reloadConfig();
1507 }, this);
1508
1509 ConfigOption* volume = m_config->addOption("volume");
1510 volume->connect([this](const QVariant& value) {
1511 reloadConfig();
1512 }, this);
1513
1514 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
1515 rewindEnable->connect([this](const QVariant& value) {
1516 reloadConfig();
1517 }, this);
1518
1519 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
1520 rewindBufferCapacity->connect([this](const QVariant& value) {
1521 reloadConfig();
1522 }, this);
1523
1524 ConfigOption* rewindSave = m_config->addOption("rewindSave");
1525 rewindBufferCapacity->connect([this](const QVariant& value) {
1526 reloadConfig();
1527 }, this);
1528
1529 ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
1530 allowOpposingDirections->connect([this](const QVariant& value) {
1531 reloadConfig();
1532 }, this);
1533
1534 ConfigOption* saveStateExtdata = m_config->addOption("saveStateExtdata");
1535 saveStateExtdata->connect([this](const QVariant& value) {
1536 reloadConfig();
1537 }, this);
1538
1539 ConfigOption* loadStateExtdata = m_config->addOption("loadStateExtdata");
1540 loadStateExtdata->connect([this](const QVariant& value) {
1541 reloadConfig();
1542 }, this);
1543
1544 ConfigOption* preload = m_config->addOption("preload");
1545 preload->connect([this](const QVariant& value) {
1546 m_manager->setPreload(value.toBool());
1547 }, this);
1548 m_config->updateOption("preload");
1549
1550 QAction* exitFullScreen = new QAction(tr("Exit fullscreen"), frameMenu);
1551 connect(exitFullScreen, &QAction::triggered, this, &Window::exitFullScreen);
1552 exitFullScreen->setShortcut(QKeySequence("Esc"));
1553 addHiddenAction(frameMenu, exitFullScreen, "exitFullScreen");
1554
1555 QMenu* autofireMenu = new QMenu(tr("Autofire"), this);
1556 m_shortcutController->addMenu(autofireMenu);
1557
1558 m_shortcutController->addFunctions(autofireMenu, [this]() {
1559 m_controller->setAutofire(GBA_KEY_A, true);
1560 }, [this]() {
1561 m_controller->setAutofire(GBA_KEY_A, false);
1562 }, QKeySequence(), tr("Autofire A"), "autofireA");
1563
1564 m_shortcutController->addFunctions(autofireMenu, [this]() {
1565 m_controller->setAutofire(GBA_KEY_B, true);
1566 }, [this]() {
1567 m_controller->setAutofire(GBA_KEY_B, false);
1568 }, QKeySequence(), tr("Autofire B"), "autofireB");
1569
1570 m_shortcutController->addFunctions(autofireMenu, [this]() {
1571 m_controller->setAutofire(GBA_KEY_L, true);
1572 }, [this]() {
1573 m_controller->setAutofire(GBA_KEY_L, false);
1574 }, QKeySequence(), tr("Autofire L"), "autofireL");
1575
1576 m_shortcutController->addFunctions(autofireMenu, [this]() {
1577 m_controller->setAutofire(GBA_KEY_R, true);
1578 }, [this]() {
1579 m_controller->setAutofire(GBA_KEY_R, false);
1580 }, QKeySequence(), tr("Autofire R"), "autofireR");
1581
1582 m_shortcutController->addFunctions(autofireMenu, [this]() {
1583 m_controller->setAutofire(GBA_KEY_START, true);
1584 }, [this]() {
1585 m_controller->setAutofire(GBA_KEY_START, false);
1586 }, QKeySequence(), tr("Autofire Start"), "autofireStart");
1587
1588 m_shortcutController->addFunctions(autofireMenu, [this]() {
1589 m_controller->setAutofire(GBA_KEY_SELECT, true);
1590 }, [this]() {
1591 m_controller->setAutofire(GBA_KEY_SELECT, false);
1592 }, QKeySequence(), tr("Autofire Select"), "autofireSelect");
1593
1594 m_shortcutController->addFunctions(autofireMenu, [this]() {
1595 m_controller->setAutofire(GBA_KEY_UP, true);
1596 }, [this]() {
1597 m_controller->setAutofire(GBA_KEY_UP, false);
1598 }, QKeySequence(), tr("Autofire Up"), "autofireUp");
1599
1600 m_shortcutController->addFunctions(autofireMenu, [this]() {
1601 m_controller->setAutofire(GBA_KEY_RIGHT, true);
1602 }, [this]() {
1603 m_controller->setAutofire(GBA_KEY_RIGHT, false);
1604 }, QKeySequence(), tr("Autofire Right"), "autofireRight");
1605
1606 m_shortcutController->addFunctions(autofireMenu, [this]() {
1607 m_controller->setAutofire(GBA_KEY_DOWN, true);
1608 }, [this]() {
1609 m_controller->setAutofire(GBA_KEY_DOWN, false);
1610 }, QKeySequence(), tr("Autofire Down"), "autofireDown");
1611
1612 m_shortcutController->addFunctions(autofireMenu, [this]() {
1613 m_controller->setAutofire(GBA_KEY_LEFT, true);
1614 }, [this]() {
1615 m_controller->setAutofire(GBA_KEY_LEFT, false);
1616 }, QKeySequence(), tr("Autofire Left"), "autofireLeft");
1617
1618 for (QAction* action : m_gameActions) {
1619 action->setDisabled(true);
1620 }
1621}
1622
1623void Window::attachWidget(QWidget* widget) {
1624 m_screenWidget->layout()->addWidget(widget);
1625 m_screenWidget->unsetCursor();
1626 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1627}
1628
1629void Window::detachWidget(QWidget* widget) {
1630 m_screenWidget->layout()->removeWidget(widget);
1631}
1632
1633void Window::appendMRU(const QString& fname) {
1634 int index = m_mruFiles.indexOf(fname);
1635 if (index >= 0) {
1636 m_mruFiles.removeAt(index);
1637 }
1638 m_mruFiles.prepend(fname);
1639 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1640 m_mruFiles.removeLast();
1641 }
1642 updateMRU();
1643}
1644
1645void Window::updateMRU() {
1646 if (!m_mruMenu) {
1647 return;
1648 }
1649 for (QAction* action : m_mruMenu->actions()) {
1650 delete action;
1651 }
1652 m_mruMenu->clear();
1653 int i = 0;
1654 for (const QString& file : m_mruFiles) {
1655 QAction* item = new QAction(QDir::toNativeSeparators(file).replace("&", "&&"), m_mruMenu);
1656 item->setShortcut(QString("Ctrl+%1").arg(i));
1657 connect(item, &QAction::triggered, [this, file]() {
1658 setController(m_manager->loadGame(file), file);
1659 });
1660 m_mruMenu->addAction(item);
1661 ++i;
1662 }
1663 m_config->setMRU(m_mruFiles);
1664 m_config->write();
1665 m_mruMenu->setEnabled(i > 0);
1666}
1667
1668QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1669 addHiddenAction(menu, action, name);
1670 menu->addAction(action);
1671 return action;
1672}
1673
1674QAction* Window::addHiddenAction(QMenu* menu, QAction* action, const QString& name) {
1675 m_shortcutController->addAction(menu, action, name);
1676 action->setShortcutContext(Qt::WidgetShortcut);
1677 addAction(action);
1678 return action;
1679}
1680
1681void Window::focusCheck() {
1682 if (!m_config->getOption("pauseOnFocusLost").toInt() || !m_controller) {
1683 return;
1684 }
1685 if (QGuiApplication::focusWindow() && m_autoresume) {
1686 m_controller->setPaused(false);
1687 m_autoresume = false;
1688 } else if (!QGuiApplication::focusWindow() && !m_controller->isPaused()) {
1689 m_autoresume = true;
1690 m_controller->setPaused(true);
1691 }
1692}
1693
1694void Window::setController(CoreController* controller, const QString& fname) {
1695 if (!controller) {
1696 return;
1697 }
1698 if (!fname.isEmpty()) {
1699 setWindowFilePath(fname);
1700 appendMRU(fname);
1701 }
1702
1703 if (m_controller) {
1704 m_controller->disconnect(this);
1705 m_controller->stop();
1706 m_controller.reset();
1707 }
1708
1709 m_controller = std::shared_ptr<CoreController>(controller);
1710 m_inputController.recalibrateAxes();
1711 m_controller->setInputController(&m_inputController);
1712 m_controller->setLogger(&m_log);
1713
1714 connect(this, &Window::shutdown, [this]() {
1715 if (!m_controller) {
1716 return;
1717 }
1718 m_controller->stop();
1719 });
1720
1721 connect(m_controller.get(), &CoreController::started, this, &Window::gameStarted);
1722 connect(m_controller.get(), &CoreController::started, &m_inputController, &InputController::suspendScreensaver);
1723 connect(m_controller.get(), &CoreController::stopping, this, &Window::gameStopped);
1724 {
1725 std::shared_ptr<CoreController> controller(m_controller);
1726 connect(m_controller.get(), &CoreController::stopping, [this, controller]() {
1727 if (m_controller == controller) {
1728 m_controller.reset();
1729 }
1730 });
1731 }
1732 connect(m_controller.get(), &CoreController::stopping, &m_inputController, &InputController::resumeScreensaver);
1733 connect(m_controller.get(), &CoreController::paused, [this]() {
1734 QSize size = m_controller->screenDimensions();
1735 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), size.width(), size.height(),
1736 size.width() * BYTES_PER_PIXEL, QImage::Format_RGBX8888);
1737 QPixmap pixmap;
1738 pixmap.convertFromImage(currentImage);
1739 m_screenWidget->setPixmap(pixmap);
1740 emit paused(true);
1741 });
1742#ifndef Q_OS_MAC
1743 connect(m_controller.get(), &CoreController::paused, menuBar(), &QWidget::show);
1744 connect(m_controller.get(), &CoreController::unpaused, [this]() {
1745 if(isFullScreen()) {
1746 menuBar()->hide();
1747 }
1748 });
1749#endif
1750
1751 connect(m_controller.get(), &CoreController::paused, &m_inputController, &InputController::resumeScreensaver);
1752 connect(m_controller.get(), &CoreController::unpaused, [this]() {
1753 emit paused(false);
1754 });
1755
1756 connect(m_controller.get(), &CoreController::stopping, m_display.get(), &Display::stopDrawing);
1757 connect(m_controller.get(), &CoreController::stateLoaded, m_display.get(), &Display::forceDraw);
1758 connect(m_controller.get(), &CoreController::rewound, m_display.get(), &Display::forceDraw);
1759 connect(m_controller.get(), &CoreController::paused, m_display.get(), &Display::pauseDrawing);
1760 connect(m_controller.get(), &CoreController::unpaused, m_display.get(), &Display::unpauseDrawing);
1761 connect(m_controller.get(), &CoreController::frameAvailable, m_display.get(), &Display::framePosted);
1762 connect(m_controller.get(), &CoreController::statusPosted, m_display.get(), &Display::showMessage);
1763
1764 connect(m_controller.get(), &CoreController::unpaused, &m_inputController, &InputController::suspendScreensaver);
1765 connect(m_controller.get(), &CoreController::frameAvailable, this, &Window::recordFrame);
1766 connect(m_controller.get(), &CoreController::crashed, this, &Window::gameCrashed);
1767 connect(m_controller.get(), &CoreController::failed, this, &Window::gameFailed);
1768 connect(m_controller.get(), &CoreController::unimplementedBiosCall, this, &Window::unimplementedBiosCall);
1769
1770 if (m_gdbController) {
1771 m_gdbController->setController(m_controller);
1772 }
1773
1774 if (m_console) {
1775 m_console->setController(m_controller);
1776 }
1777
1778#ifdef USE_MAGICK
1779 if (m_gifView) {
1780 m_gifView->setController(m_controller);
1781 }
1782#endif
1783
1784#ifdef USE_FFMPEG
1785 if (m_videoView) {
1786 m_videoView->setController(m_controller);
1787 }
1788#endif
1789
1790 if (m_overrideView) {
1791 m_overrideView->setController(m_controller);
1792 }
1793
1794 if (!m_pendingPatch.isEmpty()) {
1795 m_controller->loadPatch(m_pendingPatch);
1796 m_pendingPatch = QString();
1797 }
1798
1799 m_controller->start();
1800}
1801
1802WindowBackground::WindowBackground(QWidget* parent)
1803 : QLabel(parent)
1804{
1805 setLayout(new QStackedLayout());
1806 layout()->setContentsMargins(0, 0, 0, 0);
1807 setAlignment(Qt::AlignCenter);
1808}
1809
1810void WindowBackground::setSizeHint(const QSize& hint) {
1811 m_sizeHint = hint;
1812}
1813
1814QSize WindowBackground::sizeHint() const {
1815 return m_sizeHint;
1816}
1817
1818void WindowBackground::setDimensions(int width, int height) {
1819 m_aspectWidth = width;
1820 m_aspectHeight = height;
1821}
1822
1823void WindowBackground::setLockIntegerScaling(bool lock) {
1824 m_lockIntegerScaling = lock;
1825}
1826
1827void WindowBackground::setLockAspectRatio(bool lock) {
1828 m_lockAspectRatio = lock;
1829}
1830
1831void WindowBackground::paintEvent(QPaintEvent*) {
1832 const QPixmap* logo = pixmap();
1833 if (!logo) {
1834 return;
1835 }
1836 QPainter painter(this);
1837 painter.setRenderHint(QPainter::SmoothPixmapTransform);
1838 painter.fillRect(QRect(QPoint(), size()), Qt::black);
1839 QSize s = size();
1840 QSize ds = s;
1841 if (m_lockAspectRatio) {
1842 if (ds.width() * m_aspectHeight > ds.height() * m_aspectWidth) {
1843 ds.setWidth(ds.height() * m_aspectWidth / m_aspectHeight);
1844 } else if (ds.width() * m_aspectHeight < ds.height() * m_aspectWidth) {
1845 ds.setHeight(ds.width() * m_aspectHeight / m_aspectWidth);
1846 }
1847 }
1848 if (m_lockIntegerScaling) {
1849 ds.setWidth(ds.width() - ds.width() % m_aspectWidth);
1850 ds.setHeight(ds.height() - ds.height() % m_aspectHeight);
1851 }
1852 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1853 QRect full(origin, ds);
1854 painter.drawPixmap(full, *logo);
1855}