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