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