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