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