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