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