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