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