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