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