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