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