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