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