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