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