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 <QFileDialog>
9#include <QFileInfo>
10#include <QKeyEvent>
11#include <QKeySequence>
12#include <QMenuBar>
13#include <QMessageBox>
14#include <QMimeData>
15#include <QStackedLayout>
16
17#include "ConfigController.h"
18#include "GameController.h"
19#include "GBAKeyEditor.h"
20#include "GDBController.h"
21#include "GDBWindow.h"
22#include "GIFView.h"
23#include "GamePakView.h"
24#include "LoadSaveState.h"
25#include "LogView.h"
26#include "SensorView.h"
27#include "SettingsView.h"
28#include "ShortcutController.h"
29#include "ShortcutView.h"
30#include "VideoView.h"
31
32extern "C" {
33#include "platform/commandline.h"
34}
35
36using namespace QGBA;
37
38Window::Window(ConfigController* config, QWidget* parent)
39 : QMainWindow(parent)
40 , m_logView(new LogView())
41 , m_stateWindow(nullptr)
42 , m_screenWidget(new WindowBackground())
43 , m_logo(":/res/mgba-1024.png")
44 , m_config(config)
45#ifdef USE_FFMPEG
46 , m_videoView(nullptr)
47#endif
48#ifdef USE_MAGICK
49 , m_gifView(nullptr)
50#endif
51#ifdef USE_GDB_STUB
52 , m_gdbController(nullptr)
53#endif
54 , m_mruMenu(nullptr)
55 , m_shortcutController(new ShortcutController(this))
56{
57 setWindowTitle(PROJECT_NAME);
58 setFocusPolicy(Qt::StrongFocus);
59 setAcceptDrops(true);
60 m_controller = new GameController(this);
61 m_controller->setInputController(&m_inputController);
62 m_controller->setOverrides(m_config->overrides());
63
64 QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
65 format.setSwapInterval(1);
66 m_display = new Display(format);
67
68 m_screenWidget->setMinimumSize(m_display->minimumSize());
69 m_screenWidget->setSizePolicy(m_display->sizePolicy());
70 m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
71 setCentralWidget(m_screenWidget);
72
73 connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
74 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
75 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
76 connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
77 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
78#ifndef Q_OS_MAC
79 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
80 connect(m_controller, &GameController::gameUnpaused, [this]() {
81 if(isFullScreen()) {
82 menuBar()->hide();
83 }
84 });
85#endif
86 connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
87 connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
88 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
89 connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
90 connect(m_controller, SIGNAL(gameFailed()), this, SLOT(gameFailed()));
91 connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
92 connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
93 connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
94 connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
95 connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
96 connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
97 connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
98 connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
99 connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
100 connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
101
102 m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL);
103 m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
104
105 m_shortcutController->setConfigController(m_config);
106 setupMenu(menuBar());
107}
108
109Window::~Window() {
110 delete m_logView;
111
112#ifdef USE_FFMPEG
113 delete m_videoView;
114#endif
115
116#ifdef USE_MAGICK
117 delete m_gifView;
118#endif
119}
120
121void Window::argumentsPassed(GBAArguments* args) {
122 loadConfig();
123
124 if (args->patch) {
125 m_controller->loadPatch(args->patch);
126 }
127
128 if (args->fname) {
129 m_controller->loadGame(args->fname, args->dirmode);
130 }
131}
132
133void Window::resizeFrame(int width, int height) {
134 QSize newSize(width, height);
135 newSize -= m_screenWidget->size();
136 newSize += size();
137 resize(newSize);
138}
139
140void Window::setConfig(ConfigController* config) {
141 m_config = config;
142}
143
144void Window::loadConfig() {
145 const GBAOptions* opts = m_config->options();
146
147 m_logView->setLevels(opts->logLevel);
148
149 m_controller->setFrameskip(opts->frameskip);
150 m_controller->setAudioSync(opts->audioSync);
151 m_controller->setVideoSync(opts->videoSync);
152 m_controller->setSkipBIOS(opts->skipBios);
153 m_controller->setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
154 m_display->lockAspectRatio(opts->lockAspectRatio);
155 m_display->filter(opts->resampleVideo);
156
157 if (opts->bios) {
158 m_controller->loadBIOS(opts->bios);
159 }
160
161 if (opts->fpsTarget) {
162 emit fpsTargetChanged(opts->fpsTarget);
163 }
164
165 if (opts->audioBuffers) {
166 emit audioBufferSamplesChanged(opts->audioBuffers);
167 }
168
169 if (opts->width && opts->height) {
170 resizeFrame(opts->width, opts->height);
171 }
172
173 m_mruFiles = m_config->getMRU();
174 updateMRU();
175
176 m_inputController.setConfiguration(m_config);
177}
178
179void Window::saveConfig() {
180 m_config->write();
181}
182
183void Window::selectROM() {
184 QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"), m_config->getQtOption("lastDirectory").toString(), tr("Game Boy Advance ROMs (*.gba *.zip *.rom *.bin)"));
185 if (!filename.isEmpty()) {
186 m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
187 m_controller->loadGame(filename);
188 }
189}
190
191void Window::selectBIOS() {
192 QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"), m_config->getQtOption("lastDirectory").toString());
193 if (!filename.isEmpty()) {
194 m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
195 m_config->setOption("bios", filename);
196 m_config->updateOption("bios");
197 m_controller->loadBIOS(filename);
198 }
199}
200
201void Window::selectPatch() {
202 QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), m_config->getQtOption("lastDirectory").toString(), tr("Patches (*.ips *.ups)"));
203 if (!filename.isEmpty()) {
204 m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
205 m_controller->loadPatch(filename);
206 }
207}
208
209void Window::openKeymapWindow() {
210 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
211 connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
212 keyEditor->setAttribute(Qt::WA_DeleteOnClose);
213 keyEditor->show();
214}
215
216void Window::openSettingsWindow() {
217 SettingsView* settingsWindow = new SettingsView(m_config);
218 connect(this, SIGNAL(shutdown()), settingsWindow, SLOT(close()));
219 connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
220 settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
221 settingsWindow->show();
222}
223
224void Window::openShortcutWindow() {
225 ShortcutView* shortcutView = new ShortcutView();
226 shortcutView->setController(m_shortcutController);
227 connect(this, SIGNAL(shutdown()), shortcutView, SLOT(close()));
228 shortcutView->setAttribute(Qt::WA_DeleteOnClose);
229 shortcutView->show();
230}
231
232void Window::openGamePakWindow() {
233 GamePakView* gamePakWindow = new GamePakView(m_controller, m_config);
234 connect(this, SIGNAL(shutdown()), gamePakWindow, SLOT(close()));
235 gamePakWindow->setAttribute(Qt::WA_DeleteOnClose);
236 gamePakWindow->show();
237}
238void Window::openSensorWindow() {
239 SensorView* sensorWindow = new SensorView(m_controller);
240 connect(this, SIGNAL(shutdown()), sensorWindow, SLOT(close()));
241 sensorWindow->setAttribute(Qt::WA_DeleteOnClose);
242 sensorWindow->show();
243}
244
245#ifdef BUILD_SDL
246void Window::openGamepadWindow() {
247 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON);
248 connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
249 keyEditor->setAttribute(Qt::WA_DeleteOnClose);
250 keyEditor->show();
251}
252#endif
253
254#ifdef USE_FFMPEG
255void Window::openVideoWindow() {
256 if (!m_videoView) {
257 m_videoView = new VideoView();
258 connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
259 connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
260 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
261 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
262 connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
263 }
264 m_videoView->show();
265}
266#endif
267
268#ifdef USE_MAGICK
269void Window::openGIFWindow() {
270 if (!m_gifView) {
271 m_gifView = new GIFView();
272 connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
273 connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
274 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
275 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
276 connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
277 }
278 m_gifView->show();
279}
280#endif
281
282#ifdef USE_GDB_STUB
283void Window::gdbOpen() {
284 if (!m_gdbController) {
285 m_gdbController = new GDBController(m_controller, this);
286 }
287 GDBWindow* window = new GDBWindow(m_gdbController);
288 connect(this, SIGNAL(shutdown()), window, SLOT(close()));
289 window->setAttribute(Qt::WA_DeleteOnClose);
290 window->show();
291}
292#endif
293
294void Window::keyPressEvent(QKeyEvent* event) {
295 if (event->isAutoRepeat()) {
296 QWidget::keyPressEvent(event);
297 return;
298 }
299 GBAKey key = m_inputController.mapKeyboard(event->key());
300 if (key == GBA_KEY_NONE) {
301 QWidget::keyPressEvent(event);
302 return;
303 }
304 m_controller->keyPressed(key);
305 event->accept();
306}
307
308void Window::keyReleaseEvent(QKeyEvent* event) {
309 if (event->isAutoRepeat()) {
310 QWidget::keyReleaseEvent(event);
311 return;
312 }
313 GBAKey key = m_inputController.mapKeyboard(event->key());
314 if (key == GBA_KEY_NONE) {
315 QWidget::keyPressEvent(event);
316 return;
317 }
318 m_controller->keyReleased(key);
319 event->accept();
320}
321
322void Window::resizeEvent(QResizeEvent*) {
323 redoLogo();
324 m_config->setOption("height", m_screenWidget->height());
325 m_config->setOption("width", m_screenWidget->width());
326}
327
328void Window::closeEvent(QCloseEvent* event) {
329 emit shutdown();
330 QMainWindow::closeEvent(event);
331}
332
333void Window::focusOutEvent(QFocusEvent*) {
334 m_controller->setTurbo(false, false);
335 m_controller->clearKeys();
336}
337
338void Window::dragEnterEvent(QDragEnterEvent* event) {
339 if (event->mimeData()->hasFormat("text/uri-list")) {
340 event->acceptProposedAction();
341 }
342}
343
344void Window::dropEvent(QDropEvent* event) {
345 QString uris = event->mimeData()->data("text/uri-list");
346 uris = uris.trimmed();
347 if (uris.contains("\n")) {
348 // Only one file please
349 return;
350 }
351 QUrl url(uris);
352 if (!url.isLocalFile()) {
353 // No remote loading
354 return;
355 }
356 event->accept();
357 m_controller->loadGame(url.path());
358}
359
360void Window::toggleFullScreen() {
361 if (isFullScreen()) {
362 showNormal();
363 menuBar()->show();
364 } else {
365 showFullScreen();
366#ifndef Q_OS_MAC
367 if (m_controller->isLoaded() && !m_controller->isPaused()) {
368 menuBar()->hide();
369 }
370#endif
371 }
372}
373
374void Window::gameStarted(GBAThread* context) {
375 char title[13] = { '\0' };
376 MutexLock(&context->stateMutex);
377 if (context->state < THREAD_EXITING) {
378 emit startDrawing(m_controller->drawContext(), context);
379 GBAGetGameTitle(context->gba, title);
380 } else {
381 MutexUnlock(&context->stateMutex);
382 return;
383 }
384 MutexUnlock(&context->stateMutex);
385 foreach (QAction* action, m_gameActions) {
386 action->setDisabled(false);
387 }
388 appendMRU(context->fname);
389 setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
390 attachWidget(m_display);
391 m_screenWidget->setScaledContents(true);
392
393#ifndef Q_OS_MAC
394 if(isFullScreen()) {
395 menuBar()->hide();
396 }
397#endif
398
399 m_fpsTimer.start();
400}
401
402void Window::gameStopped() {
403 foreach (QAction* action, m_gameActions) {
404 action->setDisabled(true);
405 }
406 setWindowTitle(tr(PROJECT_NAME));
407 detachWidget(m_display);
408 m_screenWidget->setScaledContents(false);
409 redoLogo();
410
411 m_fpsTimer.stop();
412}
413
414void Window::gameCrashed(const QString& errorMessage) {
415 QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
416 tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
417 QMessageBox::Ok, this, Qt::Sheet);
418 crash->setAttribute(Qt::WA_DeleteOnClose);
419 crash->show();
420}
421
422void Window::gameFailed() {
423 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
424 tr("Could not load game. Are you sure it's in the correct format?"),
425 QMessageBox::Ok, this, Qt::Sheet);
426 fail->setAttribute(Qt::WA_DeleteOnClose);
427 fail->show();
428}
429
430void Window::redoLogo() {
431 if (m_controller->isLoaded()) {
432 return;
433 }
434 QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
435 logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
436 m_screenWidget->setPixmap(logo);
437}
438
439void Window::recordFrame() {
440 m_frameList.append(QDateTime::currentDateTime());
441 while (m_frameList.count() > FRAME_LIST_SIZE) {
442 m_frameList.removeFirst();
443 }
444}
445
446void Window::showFPS() {
447 char title[13] = { '\0' };
448 GBAGetGameTitle(m_controller->thread()->gba, title);
449 if (m_frameList.isEmpty()) {
450 setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
451 return;
452 }
453 qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
454 float fps = (m_frameList.count() - 1) * 10000.f / interval;
455 fps = round(fps) / 10.f;
456 setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
457}
458
459void Window::openStateWindow(LoadSave ls) {
460 if (m_stateWindow) {
461 return;
462 }
463 bool wasPaused = m_controller->isPaused();
464 m_stateWindow = new LoadSaveState(m_controller);
465 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
466 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
467 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
468 m_screenWidget->layout()->removeWidget(m_stateWindow);
469 m_stateWindow = nullptr;
470 setFocus();
471 });
472 if (!wasPaused) {
473 m_controller->setPaused(true);
474 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
475 }
476 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
477 m_stateWindow->setMode(ls);
478 attachWidget(m_stateWindow);
479}
480
481void Window::setupMenu(QMenuBar* menubar) {
482 menubar->clear();
483 QMenu* fileMenu = menubar->addMenu(tr("&File"));
484 m_shortcutController->addMenu(fileMenu);
485 installEventFilter(m_shortcutController);
486 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
487 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
488 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
489
490 m_mruMenu = fileMenu->addMenu(tr("Recent"));
491
492 fileMenu->addSeparator();
493
494 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
495 loadState->setShortcut(tr("F10"));
496 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
497 m_gameActions.append(loadState);
498 addControlledAction(fileMenu, loadState, "loadState");
499
500 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
501 saveState->setShortcut(tr("Shift+F10"));
502 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
503 m_gameActions.append(saveState);
504 addControlledAction(fileMenu, saveState, "saveState");
505
506 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
507 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
508 int i;
509 for (i = 1; i < 10; ++i) {
510 QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
511 quickLoad->setShortcut(tr("F%1").arg(i));
512 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
513 m_gameActions.append(quickLoad);
514 addAction(quickLoad);
515 quickLoadMenu->addAction(quickLoad);
516
517 QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
518 quickSave->setShortcut(tr("Shift+F%1").arg(i));
519 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
520 m_gameActions.append(quickSave);
521 addAction(quickSave);
522 quickSaveMenu->addAction(quickSave);
523 }
524
525#ifndef Q_OS_MAC
526 fileMenu->addSeparator();
527 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
528#endif
529
530 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
531 m_shortcutController->addMenu(emulationMenu);
532 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
533 reset->setShortcut(tr("Ctrl+R"));
534 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
535 m_gameActions.append(reset);
536 addControlledAction(emulationMenu, reset, "reset");
537
538 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
539 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
540 m_gameActions.append(shutdown);
541 addControlledAction(emulationMenu, shutdown, "shutdown");
542 emulationMenu->addSeparator();
543
544 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
545 pause->setChecked(false);
546 pause->setCheckable(true);
547 pause->setShortcut(tr("Ctrl+P"));
548 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
549 connect(m_controller, &GameController::gamePaused, [this, pause]() {
550 pause->setChecked(true);
551
552 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
553 QPixmap pixmap;
554 pixmap.convertFromImage(currentImage.rgbSwapped());
555 m_screenWidget->setPixmap(pixmap);
556 });
557 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
558 m_gameActions.append(pause);
559 addControlledAction(emulationMenu, pause, "pause");
560
561 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
562 frameAdvance->setShortcut(tr("Ctrl+N"));
563 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
564 m_gameActions.append(frameAdvance);
565 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
566
567 emulationMenu->addSeparator();
568
569 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
570 turbo->setCheckable(true);
571 turbo->setChecked(false);
572 turbo->setShortcut(tr("Shift+Tab"));
573 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
574 addControlledAction(emulationMenu, turbo, "fastForward");
575
576 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
577 rewind->setShortcut(tr("`"));
578 connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
579 m_gameActions.append(rewind);
580 addControlledAction(emulationMenu, rewind, "rewind");
581
582 ConfigOption* videoSync = m_config->addOption("videoSync");
583 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
584 videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
585 m_config->updateOption("videoSync");
586
587 ConfigOption* audioSync = m_config->addOption("audioSync");
588 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
589 audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
590 m_config->updateOption("audioSync");
591
592 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
593 m_shortcutController->addMenu(avMenu);
594 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
595 m_shortcutController->addMenu(frameMenu, avMenu);
596 for (int i = 1; i <= 6; ++i) {
597 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
598 connect(setSize, &QAction::triggered, [this, i]() {
599 showNormal();
600 resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
601 });
602 addControlledAction(frameMenu, setSize, tr("frame%1x").arg(QString::number(i)));
603 }
604 addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
605
606 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
607 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
608 lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
609 m_config->updateOption("lockAspectRatio");
610
611 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
612 resampleVideo->addBoolean(tr("Resample video"), avMenu);
613 resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
614 m_config->updateOption("resampleVideo");
615
616 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
617 ConfigOption* skip = m_config->addOption("frameskip");
618 skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
619 for (int i = 0; i <= 10; ++i) {
620 skip->addValue(QString::number(i), i, skipMenu);
621 }
622 m_config->updateOption("frameskip");
623
624 avMenu->addSeparator();
625
626 QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
627 ConfigOption* buffers = m_config->addOption("audioBuffers");
628 buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
629 buffers->addValue(tr("512"), 512, buffersMenu);
630 buffers->addValue(tr("768"), 768, buffersMenu);
631 buffers->addValue(tr("1024"), 1024, buffersMenu);
632 buffers->addValue(tr("2048"), 2048, buffersMenu);
633 buffers->addValue(tr("4096"), 4096, buffersMenu);
634 m_config->updateOption("audioBuffers");
635
636 avMenu->addSeparator();
637
638 QMenu* target = avMenu->addMenu("FPS target");
639 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
640 fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
641 fpsTargetOption->addValue(tr("15"), 15, target);
642 fpsTargetOption->addValue(tr("30"), 30, target);
643 fpsTargetOption->addValue(tr("45"), 45, target);
644 fpsTargetOption->addValue(tr("60"), 60, target);
645 fpsTargetOption->addValue(tr("90"), 90, target);
646 fpsTargetOption->addValue(tr("120"), 120, target);
647 fpsTargetOption->addValue(tr("240"), 240, target);
648 m_config->updateOption("fpsTarget");
649
650#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
651 avMenu->addSeparator();
652#endif
653
654#ifdef USE_PNG
655 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
656 screenshot->setShortcut(tr("F12"));
657 connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
658 m_gameActions.append(screenshot);
659 addControlledAction(avMenu, screenshot, "screenshot");
660#endif
661
662#ifdef USE_FFMPEG
663 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
664 recordOutput->setShortcut(tr("F11"));
665 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
666 addControlledAction(avMenu, recordOutput, "recordOutput");
667#endif
668
669#ifdef USE_MAGICK
670 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
671 recordGIF->setShortcut(tr("Shift+F11"));
672 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
673 addControlledAction(avMenu, recordGIF, "recordGIF");
674#endif
675
676 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
677 m_shortcutController->addMenu(toolsMenu);
678 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
679 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
680 addControlledAction(toolsMenu, viewLogs, "viewLogs");
681
682 QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
683 connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
684 addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
685
686 QAction* gamePakSensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
687 connect(gamePakSensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
688 addControlledAction(toolsMenu, gamePakSensors, "gamePakSensors");
689
690#ifdef USE_GDB_STUB
691 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
692 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
693 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
694#endif
695
696 toolsMenu->addSeparator();
697 QAction* solarIncrease = new QAction(tr("Increase solar level"), toolsMenu);
698 connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
699 addControlledAction(toolsMenu, solarIncrease, "increaseLuminanceLevel");
700
701 QAction* solarDecrease = new QAction(tr("Decrease solar level"), toolsMenu);
702 connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
703 addControlledAction(toolsMenu, solarDecrease, "decreaseLuminanceLevel");
704
705 QAction* maxSolar = new QAction(tr("Brightest solar level"), toolsMenu);
706 connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
707 addControlledAction(toolsMenu, maxSolar, "maxLuminanceLevel");
708
709 QAction* minSolar = new QAction(tr("Darkest solar level"), toolsMenu);
710 connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
711 addControlledAction(toolsMenu, minSolar, "minLuminanceLevel");
712
713 toolsMenu->addSeparator();
714 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
715 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
716
717 QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
718 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
719 addControlledAction(toolsMenu, keymap, "remapKeyboard");
720
721#ifdef BUILD_SDL
722 QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
723 connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
724 addControlledAction(toolsMenu, gamepad, "remapGamepad");
725#endif
726
727 ConfigOption* skipBios = m_config->addOption("skipBios");
728 skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
729
730 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
731 rewindEnable->connect([this](const QVariant& value) { m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
732
733 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
734 rewindBufferCapacity->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
735
736 ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
737 rewindBufferInterval->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); });
738
739 QMenu* other = new QMenu(tr("Other"), this);
740 m_shortcutController->addMenu(other);
741 m_shortcutController->addFunctions(other, [this]() {
742 m_controller->setTurbo(true, false);
743 }, [this]() {
744 m_controller->setTurbo(false, false);
745 }, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
746
747 foreach (QAction* action, m_gameActions) {
748 action->setDisabled(true);
749 }
750}
751
752void Window::attachWidget(QWidget* widget) {
753 m_screenWidget->layout()->addWidget(widget);
754 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
755}
756
757void Window::detachWidget(QWidget* widget) {
758 m_screenWidget->layout()->removeWidget(widget);
759}
760
761void Window::appendMRU(const QString& fname) {
762 int index = m_mruFiles.indexOf(fname);
763 if (index >= 0) {
764 m_mruFiles.removeAt(index);
765 }
766 m_mruFiles.prepend(fname);
767 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
768 m_mruFiles.removeLast();
769 }
770 updateMRU();
771}
772
773void Window::updateMRU() {
774 if (!m_mruMenu) {
775 return;
776 }
777 m_mruMenu->clear();
778 int i = 0;
779 for (const QString& file : m_mruFiles) {
780 QAction* item = new QAction(file, m_mruMenu);
781 item->setShortcut(QString("Ctrl+%1").arg(i));
782 connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
783 m_mruMenu->addAction(item);
784 ++i;
785 }
786 m_config->setMRU(m_mruFiles);
787 m_config->write();
788 m_mruMenu->setEnabled(i > 0);
789}
790
791QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
792 m_shortcutController->addAction(menu, action, name);
793 menu->addAction(action);
794 addAction(action);
795 return action;
796}
797
798WindowBackground::WindowBackground(QWidget* parent)
799 : QLabel(parent)
800{
801 setLayout(new QStackedLayout());
802 layout()->setContentsMargins(0, 0, 0, 0);
803 setAlignment(Qt::AlignCenter);
804 QPalette p = palette();
805 p.setColor(backgroundRole(), Qt::black);
806 setPalette(p);
807 setAutoFillBackground(true);
808}
809
810void WindowBackground::setSizeHint(const QSize& hint) {
811 m_sizeHint = hint;
812}
813
814QSize WindowBackground::sizeHint() const {
815 return m_sizeHint;
816}