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