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