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