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 } else {
225 showFullScreen();
226 }
227}
228
229void Window::gameStarted(GBAThread* context) {
230 emit startDrawing(m_controller->drawContext(), context);
231 foreach (QAction* action, m_gameActions) {
232 action->setDisabled(false);
233 }
234 char title[13] = { '\0' };
235 GBAGetGameTitle(context->gba, title);
236 setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
237 attachWidget(m_display);
238 m_screenWidget->setScaledContents(true);
239}
240
241void Window::gameStopped() {
242 foreach (QAction* action, m_gameActions) {
243 action->setDisabled(true);
244 }
245 setWindowTitle(tr(PROJECT_NAME));
246 detachWidget(m_display);
247 m_screenWidget->setScaledContents(false);
248 redoLogo();
249}
250
251void Window::redoLogo() {
252 if (m_controller->isLoaded()) {
253 return;
254 }
255 QPixmap logo(m_logo.scaled(m_screenWidget->size() * m_screenWidget->devicePixelRatio(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
256 logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
257 m_screenWidget->setPixmap(logo);
258}
259
260void Window::openStateWindow(LoadSave ls) {
261 if (m_stateWindow) {
262 return;
263 }
264 bool wasPaused = m_controller->isPaused();
265 m_stateWindow = new LoadSaveState(m_controller);
266 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
267 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
268 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
269 m_screenWidget->layout()->removeWidget(m_stateWindow);
270 m_stateWindow = nullptr;
271 setFocus();
272 });
273 if (!wasPaused) {
274 m_controller->setPaused(true);
275 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
276 }
277 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
278 m_stateWindow->setMode(ls);
279 attachWidget(m_stateWindow);
280}
281
282void Window::setupMenu(QMenuBar* menubar) {
283 menubar->clear();
284 QMenu* fileMenu = menubar->addMenu(tr("&File"));
285 fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
286 fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS()));
287 fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch()));
288
289 fileMenu->addSeparator();
290
291 QAction* loadState = new QAction(tr("&Load state"), fileMenu);
292 loadState->setShortcut(tr("F10"));
293 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
294 m_gameActions.append(loadState);
295 fileMenu->addAction(loadState);
296
297 QAction* saveState = new QAction(tr("&Save state"), fileMenu);
298 saveState->setShortcut(tr("Shift+F10"));
299 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
300 m_gameActions.append(saveState);
301 fileMenu->addAction(saveState);
302
303 QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
304 QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
305 int i;
306 for (i = 1; i < 10; ++i) {
307 QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
308 quickLoad->setShortcut(tr("F%1").arg(i));
309 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
310 m_gameActions.append(quickLoad);
311 quickLoadMenu->addAction(quickLoad);
312
313 QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
314 quickSave->setShortcut(tr("Shift+F%1").arg(i));
315 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
316 m_gameActions.append(quickSave);
317 quickSaveMenu->addAction(quickSave);
318 }
319
320#ifndef Q_OS_MAC
321 fileMenu->addSeparator();
322 fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
323#endif
324
325 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
326 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
327 reset->setShortcut(tr("Ctrl+R"));
328 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
329 m_gameActions.append(reset);
330 emulationMenu->addAction(reset);
331
332 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
333 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
334 m_gameActions.append(shutdown);
335 emulationMenu->addAction(shutdown);
336 emulationMenu->addSeparator();
337
338 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
339 pause->setChecked(false);
340 pause->setCheckable(true);
341 pause->setShortcut(tr("Ctrl+P"));
342 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
343 connect(m_controller, &GameController::gamePaused, [this, pause]() {
344 pause->setChecked(true);
345
346 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
347 QPixmap pixmap;
348 pixmap.convertFromImage(currentImage.rgbSwapped());
349 m_screenWidget->setPixmap(pixmap);
350 });
351 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
352 m_gameActions.append(pause);
353 emulationMenu->addAction(pause);
354
355 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
356 frameAdvance->setShortcut(tr("Ctrl+N"));
357 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
358 m_gameActions.append(frameAdvance);
359 emulationMenu->addAction(frameAdvance);
360
361 emulationMenu->addSeparator();
362
363 QAction* turbo = new QAction(tr("T&urbo"), emulationMenu);
364 turbo->setCheckable(true);
365 turbo->setChecked(false);
366 turbo->setShortcut(tr("Shift+Tab"));
367 connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
368 emulationMenu->addAction(turbo);
369
370 ConfigOption* videoSync = m_config->addOption("videoSync");
371 videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
372 videoSync->connect([this](const QVariant& value) { m_controller->setVideoSync(value.toBool()); });
373 m_config->updateOption("videoSync");
374
375 ConfigOption* audioSync = m_config->addOption("audioSync");
376 audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
377 audioSync->connect([this](const QVariant& value) { m_controller->setAudioSync(value.toBool()); });
378 m_config->updateOption("audioSync");
379
380 emulationMenu->addSeparator();
381 QAction* keymap = new QAction(tr("Remap keyboard..."), emulationMenu);
382 connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
383 emulationMenu->addAction(keymap);
384
385 QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
386 QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
387 QAction* setSize = new QAction(tr("1x"), avMenu);
388 connect(setSize, &QAction::triggered, [this]() {
389 showNormal();
390 resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
391 });
392 frameMenu->addAction(setSize);
393 setSize = new QAction(tr("2x"), avMenu);
394 connect(setSize, &QAction::triggered, [this]() {
395 showNormal();
396 resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
397 });
398 frameMenu->addAction(setSize);
399 setSize = new QAction(tr("3x"), avMenu);
400 connect(setSize, &QAction::triggered, [this]() {
401 showNormal();
402 resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
403 });
404 frameMenu->addAction(setSize);
405 setSize = new QAction(tr("4x"), avMenu);
406 connect(setSize, &QAction::triggered, [this]() {
407 showNormal();
408 resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
409 });
410 frameMenu->addAction(setSize);
411 frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F"));
412
413 QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
414 ConfigOption* skip = m_config->addOption("frameskip");
415 skip->connect([this](const QVariant& value) { m_controller->setFrameskip(value.toInt()); });
416 for (int i = 0; i <= 10; ++i) {
417 skip->addValue(QString::number(i), i, skipMenu);
418 }
419 m_config->updateOption("frameskip");
420
421 avMenu->addSeparator();
422
423 QMenu* buffersMenu = avMenu->addMenu(tr("Buffer &size"));
424 ConfigOption* buffers = m_config->addOption("audioBuffers");
425 buffers->connect([this](const QVariant& value) { emit audioBufferSamplesChanged(value.toInt()); });
426 buffers->addValue(tr("512"), 512, buffersMenu);
427 buffers->addValue(tr("768"), 768, buffersMenu);
428 buffers->addValue(tr("1024"), 1024, buffersMenu);
429 buffers->addValue(tr("2048"), 2048, buffersMenu);
430 buffers->addValue(tr("4096"), 4096, buffersMenu);
431 m_config->updateOption("audioBuffers");
432
433 avMenu->addSeparator();
434
435 QMenu* target = avMenu->addMenu("FPS target");
436 ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
437 fpsTargetOption->connect([this](const QVariant& value) { emit fpsTargetChanged(value.toInt()); });
438 fpsTargetOption->addValue(tr("15"), 15, target);
439 fpsTargetOption->addValue(tr("30"), 30, target);
440 fpsTargetOption->addValue(tr("45"), 45, target);
441 fpsTargetOption->addValue(tr("60"), 60, target);
442 fpsTargetOption->addValue(tr("90"), 90, target);
443 fpsTargetOption->addValue(tr("120"), 120, target);
444 fpsTargetOption->addValue(tr("240"), 240, target);
445 m_config->updateOption("fpsTarget");
446
447#if defined(USE_PNG) || defined(USE_FFMPEG)
448 avMenu->addSeparator();
449#endif
450
451#ifdef USE_PNG
452 QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
453 screenshot->setShortcut(tr("F12"));
454 connect(screenshot, SIGNAL(triggered()), m_display, SLOT(screenshot()));
455 m_gameActions.append(screenshot);
456 avMenu->addAction(screenshot);
457#endif
458
459#ifdef USE_FFMPEG
460 QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
461 recordOutput->setShortcut(tr("F11"));
462 connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
463 avMenu->addAction(recordOutput);
464#endif
465
466 QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
467 QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
468 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
469 debuggingMenu->addAction(viewLogs);
470#ifdef USE_GDB_STUB
471 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
472 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
473 debuggingMenu->addAction(gdbWindow);
474#endif
475
476 foreach (QAction* action, m_gameActions) {
477 action->setDisabled(true);
478 }
479}
480
481void Window::attachWidget(QWidget* widget) {
482 m_screenWidget->layout()->addWidget(widget);
483 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
484}
485
486void Window::detachWidget(QWidget* widget) {
487 m_screenWidget->layout()->removeWidget(widget);
488}
489
490WindowBackground::WindowBackground(QWidget* parent)
491 : QLabel(parent)
492{
493 setLayout(new QStackedLayout());
494 layout()->setContentsMargins(0, 0, 0, 0);
495 setAlignment(Qt::AlignCenter);
496 QPalette p = palette();
497 p.setColor(backgroundRole(), Qt::black);
498 setPalette(p);
499 setAutoFillBackground(true);
500}
501
502void WindowBackground::setSizeHint(const QSize& hint) {
503 m_sizeHint = hint;
504}
505
506QSize WindowBackground::sizeHint() const {
507 return m_sizeHint;
508}