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