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 "OverrideView.h"
27#include "SensorView.h"
28#include "SettingsView.h"
29#include "ShortcutController.h"
30#include "ShortcutView.h"
31#include "VideoView.h"
32
33extern "C" {
34#include "platform/commandline.h"
35}
36
37using namespace QGBA;
38
39Window::Window(ConfigController* config, QWidget* parent)
40 : QMainWindow(parent)
41 , m_logView(new LogView())
42 , m_stateWindow(nullptr)
43 , m_screenWidget(new WindowBackground())
44 , m_logo(":/res/mgba-1024.png")
45 , m_config(config)
46#ifdef USE_FFMPEG
47 , m_videoView(nullptr)
48#endif
49#ifdef USE_MAGICK
50 , m_gifView(nullptr)
51#endif
52#ifdef USE_GDB_STUB
53 , m_gdbController(nullptr)
54#endif
55 , m_mruMenu(nullptr)
56 , m_shortcutController(new ShortcutController(this))
57{
58 setWindowTitle(PROJECT_NAME);
59 setFocusPolicy(Qt::StrongFocus);
60 setAcceptDrops(true);
61 m_controller = new GameController(this);
62 m_controller->setInputController(&m_inputController);
63 m_controller->setOverrides(m_config->overrides());
64
65 QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
66 format.setSwapInterval(1);
67 m_display = new Display(format);
68
69 m_logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
70 m_logo = m_logo; // Free memory left over in old pixmap
71
72 m_screenWidget->setMinimumSize(m_display->minimumSize());
73 m_screenWidget->setSizePolicy(m_display->sizePolicy());
74 m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
75 m_screenWidget->setPixmap(m_logo);
76 m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
77 setCentralWidget(m_screenWidget);
78
79 connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
80 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
81 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
82 connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
83 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
84#ifndef Q_OS_MAC
85 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
86 connect(m_controller, &GameController::gameUnpaused, [this]() {
87 if(isFullScreen()) {
88 menuBar()->hide();
89 }
90 });
91#endif
92 connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
93 connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
94 connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
95 connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
96 connect(m_controller, SIGNAL(gameFailed()), this, SLOT(gameFailed()));
97 connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
98 connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
99 connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
100 connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
101 connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
102 connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
103 connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
104 connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
105 connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
106 connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
107
108 m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL);
109 m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
110
111 m_shortcutController->setConfigController(m_config);
112 setupMenu(menuBar());
113}
114
115Window::~Window() {
116 delete m_logView;
117
118#ifdef USE_FFMPEG
119 delete m_videoView;
120#endif
121
122#ifdef USE_MAGICK
123 delete m_gifView;
124#endif
125}
126
127void Window::argumentsPassed(GBAArguments* args) {
128 loadConfig();
129
130 if (args->patch) {
131 m_controller->loadPatch(args->patch);
132 }
133
134 if (args->fname) {
135 m_controller->loadGame(args->fname, args->dirmode);
136 }
137}
138
139void Window::resizeFrame(int width, int height) {
140 QSize newSize(width, height);
141 newSize -= m_screenWidget->size();
142 newSize += size();
143 resize(newSize);
144}
145
146void Window::setConfig(ConfigController* config) {
147 m_config = config;
148}
149
150void Window::loadConfig() {
151 const GBAOptions* opts = m_config->options();
152
153 m_logView->setLevels(opts->logLevel);
154
155 m_controller->setOptions(opts);
156 m_display->lockAspectRatio(opts->lockAspectRatio);
157 m_display->filter(opts->resampleVideo);
158
159 if (opts->bios) {
160 m_controller->loadBIOS(opts->bios);
161 }
162
163 if (opts->fpsTarget) {
164 emit fpsTargetChanged(opts->fpsTarget);
165 }
166
167 if (opts->audioBuffers) {
168 emit audioBufferSamplesChanged(opts->audioBuffers);
169 }
170
171 if (opts->width && opts->height) {
172 resizeFrame(opts->width, opts->height);
173 }
174
175 m_mruFiles = m_config->getMRU();
176 updateMRU();
177
178 m_inputController.setConfiguration(m_config);
179}
180
181void Window::saveConfig() {
182 m_config->write();
183}
184
185void Window::selectROM() {
186 QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"), m_config->getQtOption("lastDirectory").toString(), tr("Game Boy Advance ROMs (*.gba *.zip *.rom *.bin)"));
187 if (!filename.isEmpty()) {
188 m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
189 m_controller->loadGame(filename);
190 }
191}
192
193void Window::selectBIOS() {
194 QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"), m_config->getQtOption("lastDirectory").toString());
195 if (!filename.isEmpty()) {
196 m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
197 m_config->setOption("bios", filename);
198 m_config->updateOption("bios");
199 m_controller->loadBIOS(filename);
200 }
201}
202
203void Window::selectPatch() {
204 QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), m_config->getQtOption("lastDirectory").toString(), tr("Patches (*.ips *.ups *.bps)"));
205 if (!filename.isEmpty()) {
206 m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
207 m_controller->loadPatch(filename);
208 }
209}
210
211void Window::openKeymapWindow() {
212 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
213 connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
214 keyEditor->setAttribute(Qt::WA_DeleteOnClose);
215 keyEditor->show();
216}
217
218void Window::openSettingsWindow() {
219 SettingsView* settingsWindow = new SettingsView(m_config);
220 connect(this, SIGNAL(shutdown()), settingsWindow, SLOT(close()));
221 connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
222 connect(settingsWindow, SIGNAL(audioDriverChanged()), m_controller, SLOT(reloadAudioDriver()));
223 settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
224 settingsWindow->show();
225}
226
227void Window::openShortcutWindow() {
228 ShortcutView* shortcutView = new ShortcutView();
229 shortcutView->setController(m_shortcutController);
230 connect(this, SIGNAL(shutdown()), shortcutView, SLOT(close()));
231 shortcutView->setAttribute(Qt::WA_DeleteOnClose);
232 shortcutView->show();
233}
234
235void Window::openOverrideWindow() {
236 OverrideView* overrideWindow = new OverrideView(m_controller, m_config);
237 connect(this, SIGNAL(shutdown()), overrideWindow, SLOT(close()));
238 overrideWindow->setAttribute(Qt::WA_DeleteOnClose);
239 overrideWindow->show();
240}
241
242void Window::openSensorWindow() {
243 SensorView* sensorWindow = new SensorView(m_controller);
244 connect(this, SIGNAL(shutdown()), sensorWindow, SLOT(close()));
245 sensorWindow->setAttribute(Qt::WA_DeleteOnClose);
246 sensorWindow->show();
247}
248
249void Window::openCheatsWindow() {
250 CheatsView* cheatsWindow = new CheatsView(m_controller);
251 connect(this, SIGNAL(shutdown()), cheatsWindow, SLOT(close()));
252 cheatsWindow->setAttribute(Qt::WA_DeleteOnClose);
253 cheatsWindow->show();
254}
255
256#ifdef BUILD_SDL
257void Window::openGamepadWindow() {
258 const char* profile = m_inputController.profileForType(SDL_BINDING_BUTTON);
259 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON, profile);
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#ifndef Q_OS_MAC
527 fileMenu->addSeparator();
528 addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
529#endif
530
531 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
532 m_shortcutController->addMenu(emulationMenu);
533 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
534 reset->setShortcut(tr("Ctrl+R"));
535 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
536 m_gameActions.append(reset);
537 addControlledAction(emulationMenu, reset, "reset");
538
539 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
540 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
541 m_gameActions.append(shutdown);
542 addControlledAction(emulationMenu, shutdown, "shutdown");
543 emulationMenu->addSeparator();
544
545 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
546 pause->setChecked(false);
547 pause->setCheckable(true);
548 pause->setShortcut(tr("Ctrl+P"));
549 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
550 connect(m_controller, &GameController::gamePaused, [this, pause]() {
551 pause->setChecked(true);
552
553 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
554 QPixmap pixmap;
555 pixmap.convertFromImage(currentImage.rgbSwapped());
556 m_screenWidget->setPixmap(pixmap);
557 m_screenWidget->setLockAspectRatio(3, 2);
558 });
559 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
560 m_gameActions.append(pause);
561 addControlledAction(emulationMenu, pause, "pause");
562
563 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
564 frameAdvance->setShortcut(tr("Ctrl+N"));
565 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
566 m_gameActions.append(frameAdvance);
567 addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
568
569 emulationMenu->addSeparator();
570
571 QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
572 turbo->setCheckable(true);
573 turbo->setChecked(false);
574 turbo->setShortcut(tr("Shift+Tab"));
575 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
576 addControlledAction(emulationMenu, turbo, "fastForward");
577
578 QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
579 rewind->setShortcut(tr("`"));
580 connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
581 m_gameActions.append(rewind);
582 addControlledAction(emulationMenu, rewind, "rewind");
583
584 ConfigOption* videoSync = m_config->addOption("videoSync");
585 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
586 videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
587 m_config->updateOption("videoSync");
588
589 ConfigOption* audioSync = m_config->addOption("audioSync");
590 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
591 audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
592 m_config->updateOption("audioSync");
593
594 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
595 m_shortcutController->addMenu(avMenu);
596 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
597 m_shortcutController->addMenu(frameMenu, avMenu);
598 for (int i = 1; i <= 6; ++i) {
599 QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
600 connect(setSize, &QAction::triggered, [this, i]() {
601 showNormal();
602 resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
603 });
604 addControlledAction(frameMenu, setSize, tr("frame%1x").arg(QString::number(i)));
605 }
606 addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
607
608 ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
609 lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
610 lockAspectRatio->connect([this](const QVariant& value) { m_display->lockAspectRatio(value.toBool()); });
611 m_config->updateOption("lockAspectRatio");
612
613 ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
614 resampleVideo->addBoolean(tr("Resample video"), avMenu);
615 resampleVideo->connect([this](const QVariant& value) { m_display->filter(value.toBool()); });
616 m_config->updateOption("resampleVideo");
617
618 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
619 ConfigOption* skip = m_config->addOption("frameskip");
620 skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
621 for (int i = 0; i <= 10; ++i) {
622 skip->addValue(QString::number(i), i, skipMenu);
623 }
624 m_config->updateOption("frameskip");
625
626 avMenu->addSeparator();
627
628 QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
629 ConfigOption* buffers = m_config->addOption("audioBuffers");
630 buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
631 buffers->addValue(tr("512"), 512, buffersMenu);
632 buffers->addValue(tr("768"), 768, buffersMenu);
633 buffers->addValue(tr("1024"), 1024, buffersMenu);
634 buffers->addValue(tr("2048"), 2048, buffersMenu);
635 buffers->addValue(tr("4096"), 4096, buffersMenu);
636 m_config->updateOption("audioBuffers");
637
638 avMenu->addSeparator();
639
640 QMenu* target = avMenu->addMenu("FPS target");
641 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
642 fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
643 fpsTargetOption->addValue(tr("15"), 15, target);
644 fpsTargetOption->addValue(tr("30"), 30, target);
645 fpsTargetOption->addValue(tr("45"), 45, target);
646 fpsTargetOption->addValue(tr("60"), 60, target);
647 fpsTargetOption->addValue(tr("90"), 90, target);
648 fpsTargetOption->addValue(tr("120"), 120, target);
649 fpsTargetOption->addValue(tr("240"), 240, target);
650 m_config->updateOption("fpsTarget");
651
652#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
653 avMenu->addSeparator();
654#endif
655
656#ifdef USE_PNG
657 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
658 screenshot->setShortcut(tr("F12"));
659 connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
660 m_gameActions.append(screenshot);
661 addControlledAction(avMenu, screenshot, "screenshot");
662#endif
663
664#ifdef USE_FFMPEG
665 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
666 recordOutput->setShortcut(tr("F11"));
667 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
668 addControlledAction(avMenu, recordOutput, "recordOutput");
669#endif
670
671#ifdef USE_MAGICK
672 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
673 recordGIF->setShortcut(tr("Shift+F11"));
674 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
675 addControlledAction(avMenu, recordGIF, "recordGIF");
676#endif
677
678 QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
679 m_shortcutController->addMenu(toolsMenu);
680 QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
681 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
682 addControlledAction(toolsMenu, viewLogs, "viewLogs");
683
684 QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
685 connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
686 addControlledAction(toolsMenu, overrides, "overrideWindow");
687
688 QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
689 connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
690 addControlledAction(toolsMenu, sensors, "sensorWindow");
691
692 QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
693 connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
694 addControlledAction(toolsMenu, cheats, "cheatsWindow");
695
696#ifdef USE_GDB_STUB
697 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
698 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
699 addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
700#endif
701
702 QMenu* solarMenu = toolsMenu->addMenu(tr("Solar sensor"));
703 QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
704 connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
705 addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
706
707 QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
708 connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
709 addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
710
711 QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
712 connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
713 addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
714
715 QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
716 connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
717 addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
718
719 toolsMenu->addSeparator();
720 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
721 addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
722
723 QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
724 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
725 addControlledAction(toolsMenu, keymap, "remapKeyboard");
726
727#ifdef BUILD_SDL
728 QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
729 connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
730 addControlledAction(toolsMenu, gamepad, "remapGamepad");
731#endif
732
733 ConfigOption* skipBios = m_config->addOption("skipBios");
734 skipBios->connect([this](const QVariant& value) { m_controller->setSkipBIOS(value.toBool()); });
735
736 ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
737 rewindEnable->connect([this](const QVariant& value) { m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
738
739 ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
740 rewindBufferCapacity->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt()); });
741
742 ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
743 rewindBufferInterval->connect([this](const QVariant& value) { m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt()); });
744
745 QMenu* other = new QMenu(tr("Other"), this);
746 m_shortcutController->addMenu(other);
747 m_shortcutController->addFunctions(other, [this]() {
748 m_controller->setTurbo(true, false);
749 }, [this]() {
750 m_controller->setTurbo(false, false);
751 }, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
752
753 foreach (QAction* action, m_gameActions) {
754 action->setDisabled(true);
755 }
756}
757
758void Window::attachWidget(QWidget* widget) {
759 m_screenWidget->layout()->addWidget(widget);
760 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
761}
762
763void Window::detachWidget(QWidget* widget) {
764 m_screenWidget->layout()->removeWidget(widget);
765}
766
767void Window::appendMRU(const QString& fname) {
768 int index = m_mruFiles.indexOf(fname);
769 if (index >= 0) {
770 m_mruFiles.removeAt(index);
771 }
772 m_mruFiles.prepend(fname);
773 while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
774 m_mruFiles.removeLast();
775 }
776 updateMRU();
777}
778
779void Window::updateMRU() {
780 if (!m_mruMenu) {
781 return;
782 }
783 m_mruMenu->clear();
784 int i = 0;
785 for (const QString& file : m_mruFiles) {
786 QAction* item = new QAction(file, m_mruMenu);
787 item->setShortcut(QString("Ctrl+%1").arg(i));
788 connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
789 m_mruMenu->addAction(item);
790 ++i;
791 }
792 m_config->setMRU(m_mruFiles);
793 m_config->write();
794 m_mruMenu->setEnabled(i > 0);
795}
796
797QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
798 m_shortcutController->addAction(menu, action, name);
799 menu->addAction(action);
800 addAction(action);
801 return action;
802}
803
804WindowBackground::WindowBackground(QWidget* parent)
805 : QLabel(parent)
806{
807 setLayout(new QStackedLayout());
808 layout()->setContentsMargins(0, 0, 0, 0);
809 setAlignment(Qt::AlignCenter);
810}
811
812void WindowBackground::setSizeHint(const QSize& hint) {
813 m_sizeHint = hint;
814}
815
816QSize WindowBackground::sizeHint() const {
817 return m_sizeHint;
818}
819
820void WindowBackground::setLockAspectRatio(int width, int height) {
821 m_aspectWidth = width;
822 m_aspectHeight = height;
823}
824
825void WindowBackground::paintEvent(QPaintEvent*) {
826 QPainter painter(this);
827 painter.setRenderHint(QPainter::SmoothPixmapTransform);
828 const QPixmap* logo = pixmap();
829 painter.fillRect(QRect(QPoint(), size()), Qt::black);
830 if (!logo) {
831 return;
832 }
833 QSize s = size();
834 QSize ds = s;
835 if (s.width() * m_aspectHeight > s.height() * m_aspectWidth) {
836 ds.setWidth(s.height() * m_aspectWidth / m_aspectHeight);
837 } else if (s.width() * m_aspectHeight < s.height() * m_aspectWidth) {
838 ds.setHeight(s.width() * m_aspectHeight / m_aspectWidth);
839 }
840 QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
841 QRect full(origin, ds);
842 painter.drawPixmap(full, *logo);
843}