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