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