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