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 window->show();
280}
281#endif
282
283void Window::keyPressEvent(QKeyEvent* event) {
284 if (event->isAutoRepeat()) {
285 QWidget::keyPressEvent(event);
286 return;
287 }
288 GBAKey key = m_inputController.mapKeyboard(event->key());
289 if (key == GBA_KEY_NONE) {
290 QWidget::keyPressEvent(event);
291 return;
292 }
293 m_controller->keyPressed(key);
294 event->accept();
295}
296
297void Window::keyReleaseEvent(QKeyEvent* event) {
298 if (event->isAutoRepeat()) {
299 QWidget::keyReleaseEvent(event);
300 return;
301 }
302 GBAKey key = m_inputController.mapKeyboard(event->key());
303 if (key == GBA_KEY_NONE) {
304 QWidget::keyPressEvent(event);
305 return;
306 }
307 m_controller->keyReleased(key);
308 event->accept();
309}
310
311void Window::resizeEvent(QResizeEvent*) {
312 redoLogo();
313 m_config->setOption("height", m_screenWidget->height());
314 m_config->setOption("width", m_screenWidget->width());
315}
316
317void Window::closeEvent(QCloseEvent* event) {
318 emit shutdown();
319 QMainWindow::closeEvent(event);
320}
321
322void Window::focusOutEvent(QFocusEvent*) {
323 m_controller->setTurbo(false, false);
324 m_controller->clearKeys();
325}
326
327void Window::toggleFullScreen() {
328 if (isFullScreen()) {
329 showNormal();
330 menuBar()->show();
331 } else {
332 showFullScreen();
333#ifndef Q_OS_MAC
334 if (m_controller->isLoaded() && !m_controller->isPaused()) {
335 menuBar()->hide();
336 }
337#endif
338 }
339}
340
341void Window::gameStarted(GBAThread* context) {
342 char title[13] = { '\0' };
343 MutexLock(&context->stateMutex);
344 if (context->state < THREAD_EXITING) {
345 emit startDrawing(m_controller->drawContext(), context);
346 GBAGetGameTitle(context->gba, title);
347 } else {
348 MutexUnlock(&context->stateMutex);
349 return;
350 }
351 MutexUnlock(&context->stateMutex);
352 foreach (QAction* action, m_gameActions) {
353 action->setDisabled(false);
354 }
355 appendMRU(context->fname);
356 setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
357 attachWidget(m_display);
358 m_screenWidget->setScaledContents(true);
359
360#ifndef Q_OS_MAC
361 if(isFullScreen()) {
362 menuBar()->hide();
363 }
364#endif
365
366 m_fpsTimer.start();
367}
368
369void Window::gameStopped() {
370 foreach (QAction* action, m_gameActions) {
371 action->setDisabled(true);
372 }
373 setWindowTitle(tr(PROJECT_NAME));
374 detachWidget(m_display);
375 m_screenWidget->setScaledContents(false);
376 redoLogo();
377
378 m_fpsTimer.stop();
379}
380
381void Window::gameCrashed(const QString& errorMessage) {
382 QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
383 tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
384 QMessageBox::Ok, this, Qt::Sheet);
385 crash->setAttribute(Qt::WA_DeleteOnClose);
386 crash->show();
387}
388
389void Window::gameFailed() {
390 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
391 tr("Could not load game. Are you sure it's in the correct format?"),
392 QMessageBox::Ok, this, Qt::Sheet);
393 fail->setAttribute(Qt::WA_DeleteOnClose);
394 fail->show();
395}
396
397void Window::redoLogo() {
398 if (m_controller->isLoaded()) {
399 return;
400 }
401 QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
402 logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
403 m_screenWidget->setPixmap(logo);
404}
405
406void Window::recordFrame() {
407 m_frameList.append(QDateTime::currentDateTime());
408 while (m_frameList.count() > FRAME_LIST_SIZE) {
409 m_frameList.removeFirst();
410 }
411}
412
413void Window::showFPS() {
414 qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
415 float fps = (m_frameList.count() - 1) * 10000.f / interval;
416 fps = round(fps) / 10.f;
417 char title[13] = { '\0' };
418 GBAGetGameTitle(m_controller->thread()->gba, title);
419 setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
420}
421
422void Window::openStateWindow(LoadSave ls) {
423 if (m_stateWindow) {
424 return;
425 }
426 bool wasPaused = m_controller->isPaused();
427 m_stateWindow = new LoadSaveState(m_controller);
428 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
429 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
430 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
431 m_screenWidget->layout()->removeWidget(m_stateWindow);
432 m_stateWindow = nullptr;
433 setFocus();
434 });
435 if (!wasPaused) {
436 m_controller->setPaused(true);
437 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
438 }
439 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
440 m_stateWindow->setMode(ls);
441 attachWidget(m_stateWindow);
442}
443
444void Window::setupMenu(QMenuBar* menubar) {
445 menubar->clear();
446 QMenu* fileMenu = menubar->addMenu(tr("&File"));
447 m_shortcutController->addMenu(fileMenu);
448 installEventFilter(m_shortcutController);
449 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
450 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
451 addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
452
453 m_mruMenu = fileMenu->addMenu(tr("Recent"));
454
455 fileMenu->addSeparator();
456
457 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
458 loadState->setShortcut(tr("F10"));
459 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
460 m_gameActions.append(loadState);
461 addControlledAction(fileMenu, loadState, "loadState");
462
463 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
464 saveState->setShortcut(tr("Shift+F10"));
465 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
466 m_gameActions.append(saveState);
467 addControlledAction(fileMenu, saveState, "saveState");
468
469 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
470 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
471 int i;
472 for (i = 1; i < 10; ++i) {
473 QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
474 quickLoad->setShortcut(tr("F%1").arg(i));
475 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
476 m_gameActions.append(quickLoad);
477 addAction(quickLoad);
478 quickLoadMenu->addAction(quickLoad);
479
480 QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
481 quickSave->setShortcut(tr("Shift+F%1").arg(i));
482 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
483 m_gameActions.append(quickSave);
484 addAction(quickSave);
485 quickSaveMenu->addAction(quickSave);
486 }
487
488#ifndef Q_OS_MAC
489 fileMenu->addSeparator();
490 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
491#endif
492
493 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
494 m_shortcutController->addMenu(emulationMenu);
495 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
496 reset->setShortcut(tr("Ctrl+R"));
497 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
498 m_gameActions.append(reset);
499 addControlledAction(emulationMenu, reset, "reset");
500
501 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
502 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
503 m_gameActions.append(shutdown);
504 addControlledAction(emulationMenu, shutdown, "shutdown");
505 emulationMenu->addSeparator();
506
507 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
508 pause->setChecked(false);
509 pause->setCheckable(true);
510 pause->setShortcut(tr("Ctrl+P"));
511 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
512 connect(m_controller, &GameController::gamePaused, [this, pause]() {
513 pause->setChecked(true);
514
515 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
516 QPixmap pixmap;
517 pixmap.convertFromImage(currentImage.rgbSwapped());
518 m_screenWidget->setPixmap(pixmap);
519 });
520 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
521 m_gameActions.append(pause);
522 addControlledAction(emulationMenu, pause, "pause");
523
524 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
525 frameAdvance->setShortcut(tr("Ctrl+N"));
526 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
527 m_gameActions.append(frameAdvance);
528 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
529
530 emulationMenu->addSeparator();
531
532 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
533 turbo->setCheckable(true);
534 turbo->setChecked(false);
535 turbo->setShortcut(tr("Shift+Tab"));
536 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
537 addControlledAction(emulationMenu, turbo, "fastForward");
538
539 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
540 rewind->setShortcut(tr("`"));
541 connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
542 m_gameActions.append(rewind);
543 addControlledAction(emulationMenu, rewind, "rewind");
544
545 ConfigOption* videoSync = m_config->addOption("videoSync");
546 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
547 videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
548 m_config->updateOption("videoSync");
549
550 ConfigOption* audioSync = m_config->addOption("audioSync");
551 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
552 audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
553 m_config->updateOption("audioSync");
554
555 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
556 m_shortcutController->addMenu(avMenu);
557 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
558 m_shortcutController->addMenu(frameMenu, avMenu);
559 for (int i = 1; i <= 6; ++i) {
560 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
561 connect(setSize, &QAction::triggered, [this, i]() {
562 showNormal();
563 resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
564 });
565 addControlledAction(frameMenu, setSize, tr("frame%1x").arg(QString::number(i)));
566 }
567 addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
568
569 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
570 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
571 lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
572 m_config->updateOption("lockAspectRatio");
573
574 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
575 resampleVideo->addBoolean(tr("Resample video"), avMenu);
576 resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
577 m_config->updateOption("resampleVideo");
578
579 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
580 ConfigOption* skip = m_config->addOption("frameskip");
581 skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
582 for (int i = 0; i <= 10; ++i) {
583 skip->addValue(QString::number(i), i, skipMenu);
584 }
585 m_config->updateOption("frameskip");
586
587 avMenu->addSeparator();
588
589 QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
590 ConfigOption* buffers = m_config->addOption("audioBuffers");
591 buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
592 buffers->addValue(tr("512"), 512, buffersMenu);
593 buffers->addValue(tr("768"), 768, buffersMenu);
594 buffers->addValue(tr("1024"), 1024, buffersMenu);
595 buffers->addValue(tr("2048"), 2048, buffersMenu);
596 buffers->addValue(tr("4096"), 4096, buffersMenu);
597 m_config->updateOption("audioBuffers");
598
599 avMenu->addSeparator();
600
601 QMenu* target = avMenu->addMenu("FPS target");
602 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
603 fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
604 fpsTargetOption->addValue(tr("15"), 15, target);
605 fpsTargetOption->addValue(tr("30"), 30, target);
606 fpsTargetOption->addValue(tr("45"), 45, target);
607 fpsTargetOption->addValue(tr("60"), 60, target);
608 fpsTargetOption->addValue(tr("90"), 90, target);
609 fpsTargetOption->addValue(tr("120"), 120, target);
610 fpsTargetOption->addValue(tr("240"), 240, target);
611 m_config->updateOption("fpsTarget");
612
613#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
614 avMenu->addSeparator();
615#endif
616
617#ifdef USE_PNG
618 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
619 screenshot->setShortcut(tr("F12"));
620 connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
621 m_gameActions.append(screenshot);
622 addControlledAction(avMenu, screenshot, "screenshot");
623#endif
624
625#ifdef USE_FFMPEG
626 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
627 recordOutput->setShortcut(tr("F11"));
628 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
629 addControlledAction(avMenu, recordOutput, "recordOutput");
630#endif
631
632#ifdef USE_MAGICK
633 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
634 recordGIF->setShortcut(tr("Shift+F11"));
635 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
636 addControlledAction(avMenu, recordGIF, "recordGIF");
637#endif
638
639 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
640 m_shortcutController->addMenu(toolsMenu);
641 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
642 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
643 addControlledAction(toolsMenu, viewLogs, "viewLogs");
644
645 QAction* gamePak = new QAction(tr("Game &Pak overrides..."), toolsMenu);
646 connect(gamePak, SIGNAL(triggered()), this, SLOT(openGamePakWindow()));
647 addControlledAction(toolsMenu, gamePak, "gamePakOverrides");
648
649#ifdef USE_GDB_STUB
650 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
651 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
652 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
653#endif
654
655 toolsMenu->addSeparator();
656 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
657 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
658
659 QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
660 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
661 addControlledAction(toolsMenu, keymap, "remapKeyboard");
662
663#ifdef BUILD_SDL
664 QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
665 connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
666 addControlledAction(toolsMenu, gamepad, "remapGamepad");
667#endif
668
669 ConfigOption* skipBios = m_config->addOption("skipBios");
670 skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
671
672 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
673 rewindEnable->connect([this](const QVariant& value) { m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
674
675 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
676 rewindBufferCapacity->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
677
678 ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
679 rewindBufferInterval->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); });
680
681 QMenu* other = new QMenu(tr("Other"), this);
682 m_shortcutController->addMenu(other);
683 m_shortcutController->addFunctions(other, [this]() {
684 m_controller->setTurbo(true, false);
685 }, [this]() {
686 m_controller->setTurbo(false, false);
687 }, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
688
689 foreach (QAction* action, m_gameActions) {
690 action->setDisabled(true);
691 }
692}
693
694void Window::attachWidget(QWidget* widget) {
695 m_screenWidget->layout()->addWidget(widget);
696 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
697}
698
699void Window::detachWidget(QWidget* widget) {
700 m_screenWidget->layout()->removeWidget(widget);
701}
702
703void Window::appendMRU(const QString& fname) {
704 int index = m_mruFiles.indexOf(fname);
705 if (index >= 0) {
706 m_mruFiles.removeAt(index);
707 }
708 m_mruFiles.prepend(fname);
709 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
710 m_mruFiles.removeLast();
711 }
712 updateMRU();
713}
714
715void Window::updateMRU() {
716 if (!m_mruMenu) {
717 return;
718 }
719 m_mruMenu->clear();
720 int i = 0;
721 for (const QString& file : m_mruFiles) {
722 QAction* item = new QAction(file, m_mruMenu);
723 item->setShortcut(QString("Ctrl+%1").arg(i));
724 connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
725 m_mruMenu->addAction(item);
726 ++i;
727 }
728 m_config->setMRU(m_mruFiles);
729 m_config->write();
730 m_mruMenu->setEnabled(i > 0);
731}
732
733QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
734 m_shortcutController->addAction(menu, action, name);
735 menu->addAction(action);
736 addAction(action);
737 return action;
738}
739
740WindowBackground::WindowBackground(QWidget* parent)
741 : QLabel(parent)
742{
743 setLayout(new QStackedLayout());
744 layout()->setContentsMargins(0, 0, 0, 0);
745 setAlignment(Qt::AlignCenter);
746 QPalette p = palette();
747 p.setColor(backgroundRole(), Qt::black);
748 setPalette(p);
749 setAutoFillBackground(true);
750}
751
752void WindowBackground::setSizeHint(const QSize& hint) {
753 m_sizeHint = hint;
754}
755
756QSize WindowBackground::sizeHint() const {
757 return m_sizeHint;
758}