src/platform/qt/Window.cpp (view raw)
1#include "Window.h"
2
3#include <QFileDialog>
4#include <QKeyEvent>
5#include <QKeySequence>
6#include <QMenuBar>
7#include <QStackedLayout>
8
9#include "ConfigController.h"
10#include "GameController.h"
11#include "GBAKeyEditor.h"
12#include "GDBController.h"
13#include "GDBWindow.h"
14#include "GIFView.h"
15#include "LoadSaveState.h"
16#include "LogView.h"
17#include "VideoView.h"
18
19extern "C" {
20#include "platform/commandline.h"
21}
22
23using namespace QGBA;
24
25Window::Window(ConfigController* config, QWidget* parent)
26 : QMainWindow(parent)
27 , m_logView(new LogView())
28 , m_stateWindow(nullptr)
29 , m_screenWidget(new WindowBackground())
30 , m_logo(":/res/mgba-1024.png")
31 , m_config(config)
32#ifdef USE_FFMPEG
33 , m_videoView(nullptr)
34#endif
35#ifdef USE_MAGICK
36 , m_gifView(nullptr)
37#endif
38#ifdef USE_GDB_STUB
39 , m_gdbController(nullptr)
40#endif
41{
42 setWindowTitle(PROJECT_NAME);
43 m_controller = new GameController(this);
44 m_controller->setInputController(&m_inputController);
45
46 QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
47 format.setSwapInterval(1);
48 m_display = new Display(format);
49
50 m_screenWidget->setMinimumSize(m_display->minimumSize());
51 m_screenWidget->setSizePolicy(m_display->sizePolicy());
52 m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
53 setCentralWidget(m_screenWidget);
54
55 connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
56 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
57 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
58 connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
59 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
60#ifndef Q_OS_MAC
61 connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
62 connect(m_controller, &GameController::gameUnpaused, [this]() {
63 if(isFullScreen()) {
64 menuBar()->hide();
65 }
66 });
67#endif
68 connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
69 connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
70 connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
71 connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
72 connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
73 connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
74 connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
75 connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
76
77 setupMenu(menuBar());
78}
79
80Window::~Window() {
81 delete m_logView;
82
83#ifdef USE_FFMPEG
84 delete m_videoView;
85#endif
86
87#ifdef USE_MAGICK
88 delete m_gifView;
89#endif
90}
91
92void Window::argumentsPassed(GBAArguments* args) {
93 loadConfig();
94
95 if (args->patch) {
96 m_controller->loadPatch(args->patch);
97 }
98
99 if (args->fname) {
100 m_controller->loadGame(args->fname, args->dirmode);
101 }
102}
103
104void Window::setConfig(ConfigController* config) {
105 m_config = config;
106}
107
108void Window::loadConfig() {
109 const GBAOptions* opts = m_config->options();
110
111 m_logView->setLevels(opts->logLevel);
112
113 m_controller->setFrameskip(opts->frameskip);
114 m_controller->setAudioSync(opts->audioSync);
115 m_controller->setVideoSync(opts->videoSync);
116
117 if (opts->bios) {
118 m_controller->loadBIOS(opts->bios);
119 }
120
121 if (opts->fpsTarget) {
122 emit fpsTargetChanged(opts->fpsTarget);
123 }
124
125 if (opts->audioBuffers) {
126 emit audioBufferSamplesChanged(opts->audioBuffers);
127 }
128
129 if (opts->width && opts->height) {
130 m_screenWidget->setSizeHint(QSize(opts->width, opts->height));
131 }
132
133 m_inputController.setConfiguration(m_config);
134}
135
136void Window::saveConfig() {
137 m_config->write();
138}
139
140void Window::selectROM() {
141 QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
142 if (!filename.isEmpty()) {
143 m_controller->loadGame(filename);
144 }
145}
146
147void Window::selectBIOS() {
148 QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
149 if (!filename.isEmpty()) {
150 m_controller->loadBIOS(filename);
151 }
152}
153
154void Window::selectPatch() {
155 QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), QString(), tr("Patches (*.ips *.ups)"));
156 if (!filename.isEmpty()) {
157 m_controller->loadPatch(filename);
158 }
159}
160
161void Window::openKeymapWindow() {
162 GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
163 connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
164 keyEditor->setAttribute(Qt::WA_DeleteOnClose);
165 keyEditor->show();
166}
167
168#ifdef USE_FFMPEG
169void Window::openVideoWindow() {
170 if (!m_videoView) {
171 m_videoView = new VideoView();
172 connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
173 connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
174 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
175 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
176 connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
177 }
178 m_videoView->show();
179}
180#endif
181
182#ifdef USE_MAGICK
183void Window::openGIFWindow() {
184 if (!m_gifView) {
185 m_gifView = new GIFView();
186 connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
187 connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
188 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
189 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
190 connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
191 }
192 m_gifView->show();
193}
194#endif
195
196#ifdef USE_GDB_STUB
197void Window::gdbOpen() {
198 if (!m_gdbController) {
199 m_gdbController = new GDBController(m_controller, this);
200 }
201 GDBWindow* window = new GDBWindow(m_gdbController);
202 window->show();
203}
204#endif
205
206void Window::keyPressEvent(QKeyEvent* event) {
207 if (event->isAutoRepeat()) {
208 QWidget::keyPressEvent(event);
209 return;
210 }
211 if (event->key() == Qt::Key_Tab) {
212 m_controller->setTurbo(true, false);
213 }
214 GBAKey key = m_inputController.mapKeyboard(event->key());
215 if (key == GBA_KEY_NONE) {
216 QWidget::keyPressEvent(event);
217 return;
218 }
219 m_controller->keyPressed(key);
220 event->accept();
221}
222
223void Window::keyReleaseEvent(QKeyEvent* event) {
224 if (event->isAutoRepeat()) {
225 QWidget::keyReleaseEvent(event);
226 return;
227 }
228 if (event->key() == Qt::Key_Tab) {
229 m_controller->setTurbo(false, false);
230 }
231 GBAKey key = m_inputController.mapKeyboard(event->key());
232 if (key == GBA_KEY_NONE) {
233 QWidget::keyPressEvent(event);
234 return;
235 }
236 m_controller->keyReleased(key);
237 event->accept();
238}
239
240void Window::resizeEvent(QResizeEvent*) {
241 redoLogo();
242 m_config->setOption("height", m_screenWidget->height());
243 m_config->setOption("width", m_screenWidget->width());
244}
245
246void Window::closeEvent(QCloseEvent* event) {
247 emit shutdown();
248 QMainWindow::closeEvent(event);
249}
250
251void Window::toggleFullScreen() {
252 if (isFullScreen()) {
253 showNormal();
254 menuBar()->show();
255 } else {
256 showFullScreen();
257#ifndef Q_OS_MAC
258 if (!m_controller->isPaused()) {
259 menuBar()->hide();
260 }
261#endif
262 }
263}
264
265void Window::gameStarted(GBAThread* context) {
266 emit startDrawing(m_controller->drawContext(), context);
267 foreach (QAction* action, m_gameActions) {
268 action->setDisabled(false);
269 }
270 char title[13] = { '\0' };
271 GBAGetGameTitle(context->gba, title);
272 setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
273 attachWidget(m_display);
274 m_screenWidget->setScaledContents(true);
275}
276
277void Window::gameStopped() {
278 foreach (QAction* action, m_gameActions) {
279 action->setDisabled(true);
280 }
281 setWindowTitle(tr(PROJECT_NAME));
282 detachWidget(m_display);
283 m_screenWidget->setScaledContents(false);
284 redoLogo();
285}
286
287void Window::redoLogo() {
288 if (m_controller->isLoaded()) {
289 return;
290 }
291 QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
292 logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
293 m_screenWidget->setPixmap(logo);
294}
295
296void Window::openStateWindow(LoadSave ls) {
297 if (m_stateWindow) {
298 return;
299 }
300 bool wasPaused = m_controller->isPaused();
301 m_stateWindow = new LoadSaveState(m_controller);
302 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
303 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
304 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
305 m_screenWidget->layout()->removeWidget(m_stateWindow);
306 m_stateWindow = nullptr;
307 setFocus();
308 });
309 if (!wasPaused) {
310 m_controller->setPaused(true);
311 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
312 }
313 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
314 m_stateWindow->setMode(ls);
315 attachWidget(m_stateWindow);
316}
317
318void Window::setupMenu(QMenuBar* menubar) {
319 menubar->clear();
320 QMenu* fileMenu = menubar->addMenu(tr("&File"));
321 addAction(fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open));
322 fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS()));
323 fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch()));
324
325 fileMenu->addSeparator();
326
327 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
328 loadState->setShortcut(tr("F10"));
329 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
330 m_gameActions.append(loadState);
331 addAction(loadState);
332 fileMenu->addAction(loadState);
333
334 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
335 saveState->setShortcut(tr("Shift+F10"));
336 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
337 m_gameActions.append(saveState);
338 addAction(saveState);
339 fileMenu->addAction(saveState);
340
341 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
342 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
343 int i;
344 for (i = 1; i < 10; ++i) {
345 QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
346 quickLoad->setShortcut(tr("F%1").arg(i));
347 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
348 m_gameActions.append(quickLoad);
349 addAction(quickLoad);
350 quickLoadMenu->addAction(quickLoad);
351
352 QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
353 quickSave->setShortcut(tr("Shift+F%1").arg(i));
354 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
355 m_gameActions.append(quickSave);
356 addAction(quickSave);
357 quickSaveMenu->addAction(quickSave);
358 }
359
360#ifndef Q_OS_MAC
361 fileMenu->addSeparator();
362 fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
363#endif
364
365 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
366 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
367 reset->setShortcut(tr("Ctrl+R"));
368 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
369 m_gameActions.append(reset);
370 addAction(reset);
371 emulationMenu->addAction(reset);
372
373 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
374 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
375 m_gameActions.append(shutdown);
376 emulationMenu->addAction(shutdown);
377 emulationMenu->addSeparator();
378
379 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
380 pause->setChecked(false);
381 pause->setCheckable(true);
382 pause->setShortcut(tr("Ctrl+P"));
383 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
384 connect(m_controller, &GameController::gamePaused, [this, pause]() {
385 pause->setChecked(true);
386
387 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
388 QPixmap pixmap;
389 pixmap.convertFromImage(currentImage.rgbSwapped());
390 m_screenWidget->setPixmap(pixmap);
391 });
392 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
393 m_gameActions.append(pause);
394 addAction(pause);
395 emulationMenu->addAction(pause);
396
397 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
398 frameAdvance->setShortcut(tr("Ctrl+N"));
399 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
400 m_gameActions.append(frameAdvance);
401 addAction(frameAdvance);
402 emulationMenu->addAction(frameAdvance);
403
404 emulationMenu->addSeparator();
405
406 QAction* turbo = new QAction(tr("T&urbo"), emulationMenu);
407 turbo->setCheckable(true);
408 turbo->setChecked(false);
409 turbo->setShortcut(tr("Shift+Tab"));
410 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
411 addAction(turbo);
412 emulationMenu->addAction(turbo);
413
414 ConfigOption* videoSync = m_config->addOption("videoSync");
415 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
416 videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
417 m_config->updateOption("videoSync");
418
419 ConfigOption* audioSync = m_config->addOption("audioSync");
420 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
421 audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
422 m_config->updateOption("audioSync");
423
424 emulationMenu->addSeparator();
425 QAction* keymap = new QAction(tr("Remap keyboard..."), emulationMenu);
426 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
427 emulationMenu->addAction(keymap);
428
429 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
430 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
431 QAction* setSize = new QAction(tr("1x"), avMenu);
432 connect(setSize, &QAction::triggered, [this]() {
433 showNormal();
434 resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
435 });
436 frameMenu->addAction(setSize);
437 setSize = new QAction(tr("2x"), avMenu);
438 connect(setSize, &QAction::triggered, [this]() {
439 showNormal();
440 resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
441 });
442 frameMenu->addAction(setSize);
443 setSize = new QAction(tr("3x"), avMenu);
444 connect(setSize, &QAction::triggered, [this]() {
445 showNormal();
446 resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
447 });
448 frameMenu->addAction(setSize);
449 setSize = new QAction(tr("4x"), avMenu);
450 connect(setSize, &QAction::triggered, [this]() {
451 showNormal();
452 resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
453 });
454 frameMenu->addAction(setSize);
455 addAction(frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")));
456
457 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
458 ConfigOption* skip = m_config->addOption("frameskip");
459 skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
460 for (int i = 0; i <= 10; ++i) {
461 skip->addValue(QString::number(i), i, skipMenu);
462 }
463 m_config->updateOption("frameskip");
464
465 avMenu->addSeparator();
466
467 QMenu* buffersMenu = avMenu->addMenu(tr("Buffer &size"));
468 ConfigOption* buffers = m_config->addOption("audioBuffers");
469 buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
470 buffers->addValue(tr("512"), 512, buffersMenu);
471 buffers->addValue(tr("768"), 768, buffersMenu);
472 buffers->addValue(tr("1024"), 1024, buffersMenu);
473 buffers->addValue(tr("2048"), 2048, buffersMenu);
474 buffers->addValue(tr("4096"), 4096, buffersMenu);
475 m_config->updateOption("audioBuffers");
476
477 avMenu->addSeparator();
478
479 QMenu* target = avMenu->addMenu("FPS target");
480 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
481 fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
482 fpsTargetOption->addValue(tr("15"), 15, target);
483 fpsTargetOption->addValue(tr("30"), 30, target);
484 fpsTargetOption->addValue(tr("45"), 45, target);
485 fpsTargetOption->addValue(tr("60"), 60, target);
486 fpsTargetOption->addValue(tr("90"), 90, target);
487 fpsTargetOption->addValue(tr("120"), 120, target);
488 fpsTargetOption->addValue(tr("240"), 240, target);
489 m_config->updateOption("fpsTarget");
490
491#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
492 avMenu->addSeparator();
493#endif
494
495#ifdef USE_PNG
496 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
497 screenshot->setShortcut(tr("F12"));
498 connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
499 m_gameActions.append(screenshot);
500 addAction(screenshot);
501 avMenu->addAction(screenshot);
502#endif
503
504#ifdef USE_FFMPEG
505 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
506 recordOutput->setShortcut(tr("F11"));
507 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
508 addAction(recordOutput);
509 avMenu->addAction(recordOutput);
510#endif
511
512#ifdef USE_MAGICK
513 QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
514 recordGIF->setShortcut(tr("Shift+F11"));
515 connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
516 addAction(recordGIF);
517 avMenu->addAction(recordGIF);
518#endif
519
520 QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
521 QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
522 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
523 debuggingMenu->addAction(viewLogs);
524#ifdef USE_GDB_STUB
525 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
526 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
527 debuggingMenu->addAction(gdbWindow);
528#endif
529
530 foreach (QAction* action, m_gameActions) {
531 action->setDisabled(true);
532 }
533}
534
535void Window::attachWidget(QWidget* widget) {
536 m_screenWidget->layout()->addWidget(widget);
537 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
538}
539
540void Window::detachWidget(QWidget* widget) {
541 m_screenWidget->layout()->removeWidget(widget);
542}
543
544WindowBackground::WindowBackground(QWidget* parent)
545 : QLabel(parent)
546{
547 setLayout(new QStackedLayout());
548 layout()->setContentsMargins(0, 0, 0, 0);
549 setAlignment(Qt::AlignCenter);
550 QPalette p = palette();
551 p.setColor(backgroundRole(), Qt::black);
552 setPalette(p);
553 setAutoFillBackground(true);
554}
555
556void WindowBackground::setSizeHint(const QSize& hint) {
557 m_sizeHint = hint;
558}
559
560QSize WindowBackground::sizeHint() const {
561 return m_sizeHint;
562}