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