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