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