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