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