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->fname) {
108 m_controller->loadGame(opts->fname, opts->dirmode);
109 }
110
111 if (opts->logLevel) {
112 m_logView->setLevels(opts->logLevel);
113 }
114
115 // TODO:
116 // - bios
117 // - patch
118 // - frameskip;
119 // - rewindBufferCapacity
120 // - rewindBufferInterval
121 // - DebuggerType debuggerType
122 // - debugAtStart
123}
124
125void Window::selectROM() {
126 QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
127 if (!filename.isEmpty()) {
128 m_controller->loadGame(filename);
129 }
130}
131
132#ifdef USE_GDB_STUB
133void Window::gdbOpen() {
134 if (!m_gdbController) {
135 m_gdbController = new GDBController(m_controller, this);
136 }
137 GDBWindow* window = new GDBWindow(m_gdbController);
138 window->show();
139}
140#endif
141
142void Window::keyPressEvent(QKeyEvent* event) {
143 if (event->isAutoRepeat()) {
144 QWidget::keyPressEvent(event);
145 return;
146 }
147 GBAKey key = mapKey(event->key());
148 if (key == GBA_KEY_NONE) {
149 QWidget::keyPressEvent(event);
150 return;
151 }
152 m_controller->keyPressed(key);
153 event->accept();
154}
155
156void Window::keyReleaseEvent(QKeyEvent* event) {
157 if (event->isAutoRepeat()) {
158 QWidget::keyReleaseEvent(event);
159 return;
160 }
161 GBAKey key = mapKey(event->key());
162 if (key == GBA_KEY_NONE) {
163 QWidget::keyPressEvent(event);
164 return;
165 }
166 m_controller->keyReleased(key);
167 event->accept();
168}
169
170void Window::resizeEvent(QResizeEvent*) {
171 redoLogo();
172}
173
174void Window::closeEvent(QCloseEvent* event) {
175 emit shutdown();
176 QMainWindow::closeEvent(event);
177}
178
179void Window::toggleFullScreen() {
180 if (isFullScreen()) {
181 showNormal();
182 } else {
183 showFullScreen();
184 }
185}
186
187void Window::gameStarted(GBAThread* context) {
188 emit startDrawing(m_controller->drawContext(), context);
189 foreach (QAction* action, m_gameActions) {
190 action->setDisabled(false);
191 }
192 char title[13] = { '\0' };
193 GBAGetGameTitle(context->gba, title);
194 setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
195 attachWidget(m_display);
196 m_screenWidget->setScaledContents(true);
197}
198
199void Window::gameStopped() {
200 foreach (QAction* action, m_gameActions) {
201 action->setDisabled(true);
202 }
203 setWindowTitle(tr(PROJECT_NAME));
204 detachWidget(m_display);
205 m_screenWidget->setScaledContents(false);
206 redoLogo();
207}
208
209void Window::redoLogo() {
210 if (m_controller->isLoaded()) {
211 return;
212 }
213 m_screenWidget->setPixmap(m_logo.scaled(m_screenWidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
214}
215
216void Window::openStateWindow(LoadSave ls) {
217 if (m_stateWindow) {
218 return;
219 }
220 bool wasPaused = m_controller->isPaused();
221 m_stateWindow = new LoadSaveState(m_controller);
222 connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(hide()));
223 connect(m_stateWindow, &LoadSaveState::closed, [this]() {
224 m_screenWidget->layout()->removeWidget(m_stateWindow);
225 m_stateWindow = nullptr;
226 setFocus();
227 });
228 if (!wasPaused) {
229 m_controller->setPaused(true);
230 connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
231 }
232 m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
233 m_stateWindow->setMode(ls);
234 attachWidget(m_stateWindow);
235}
236
237void Window::setupMenu(QMenuBar* menubar) {
238 menubar->clear();
239 QMenu* fileMenu = menubar->addMenu(tr("&File"));
240 fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
241#ifndef Q_OS_MAC
242 fileMenu->addSeparator();
243 fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
244#endif
245
246 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
247 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
248 reset->setShortcut(tr("Ctrl+R"));
249 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
250 m_gameActions.append(reset);
251 emulationMenu->addAction(reset);
252
253 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
254 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
255 m_gameActions.append(shutdown);
256 emulationMenu->addAction(shutdown);
257 emulationMenu->addSeparator();
258
259 QAction* loadState = new QAction(tr("&Load state"), emulationMenu);
260 loadState->setShortcut(tr("Ctrl+L"));
261 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
262 m_gameActions.append(loadState);
263 emulationMenu->addAction(loadState);
264
265 QAction* saveState = new QAction(tr("&Save state"), emulationMenu);
266 saveState->setShortcut(tr("Ctrl+S"));
267 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
268 m_gameActions.append(saveState);
269 emulationMenu->addAction(saveState);
270
271 QMenu* quickLoadMenu = emulationMenu->addMenu(tr("Quick load"));
272 QMenu* quickSaveMenu = emulationMenu->addMenu(tr("Quick save"));
273 int i;
274 for (i = 1; i < 10; ++i) {
275 QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
276 quickLoad->setShortcut(tr("F%1").arg(i));
277 connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
278 m_gameActions.append(quickLoad);
279 quickLoadMenu->addAction(quickLoad);
280
281 QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
282 quickSave->setShortcut(tr("Shift+F%1").arg(i));
283 connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
284 m_gameActions.append(quickSave);
285 quickSaveMenu->addAction(quickSave);
286 }
287
288 emulationMenu->addSeparator();
289
290 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
291 pause->setChecked(false);
292 pause->setCheckable(true);
293 pause->setShortcut(tr("Ctrl+P"));
294 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
295 connect(m_controller, &GameController::gamePaused, [this, pause]() {
296 pause->setChecked(true);
297
298 QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
299 QPixmap pixmap;
300 pixmap.convertFromImage(currentImage.rgbSwapped());
301 m_screenWidget->setPixmap(pixmap);
302 });
303 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
304 m_gameActions.append(pause);
305 emulationMenu->addAction(pause);
306
307 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
308 frameAdvance->setShortcut(tr("Ctrl+N"));
309 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
310 m_gameActions.append(frameAdvance);
311 emulationMenu->addAction(frameAdvance);
312
313 QMenu* target = emulationMenu->addMenu("FPS target");
314 QAction* setTarget = new QAction(tr("15"), emulationMenu);
315 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(15); });
316 target->addAction(setTarget);
317 setTarget = new QAction(tr("30"), emulationMenu);
318 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(30); });
319 target->addAction(setTarget);
320 setTarget = new QAction(tr("45"), emulationMenu);
321 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(45); });
322 target->addAction(setTarget);
323 setTarget = new QAction(tr("60"), emulationMenu);
324 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(60); });
325 target->addAction(setTarget);
326 setTarget = new QAction(tr("90"), emulationMenu);
327 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(90); });
328 target->addAction(setTarget);
329 setTarget = new QAction(tr("120"), emulationMenu);
330 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(120); });
331 target->addAction(setTarget);
332 setTarget = new QAction(tr("240"), emulationMenu);
333 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(240); });
334 target->addAction(setTarget);
335
336 QMenu* videoMenu = menubar->addMenu(tr("&Video"));
337 QMenu* frameMenu = videoMenu->addMenu(tr("Frame &size"));
338 QAction* setSize = new QAction(tr("1x"), videoMenu);
339 connect(setSize, &QAction::triggered, [this]() {
340 showNormal();
341 resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
342 });
343 frameMenu->addAction(setSize);
344 setSize = new QAction(tr("2x"), videoMenu);
345 connect(setSize, &QAction::triggered, [this]() {
346 showNormal();
347 resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
348 });
349 frameMenu->addAction(setSize);
350 setSize = new QAction(tr("3x"), videoMenu);
351 connect(setSize, &QAction::triggered, [this]() {
352 showNormal();
353 resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
354 });
355 frameMenu->addAction(setSize);
356 setSize = new QAction(tr("4x"), videoMenu);
357 connect(setSize, &QAction::triggered, [this]() {
358 showNormal();
359 resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
360 });
361 frameMenu->addAction(setSize);
362 frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F"));
363
364 QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
365 QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
366 QAction* setBuffer = new QAction(tr("512"), buffersMenu);
367 connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(512); });
368 buffersMenu->addAction(setBuffer);
369 setBuffer = new QAction(tr("1024"), buffersMenu);
370 connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(1024); });
371 buffersMenu->addAction(setBuffer);
372 setBuffer = new QAction(tr("2048"), buffersMenu);
373 connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(2048); });
374 buffersMenu->addAction(setBuffer);
375
376 QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
377 QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
378 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
379 debuggingMenu->addAction(viewLogs);
380#ifdef USE_GDB_STUB
381 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
382 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
383 debuggingMenu->addAction(gdbWindow);
384#endif
385
386 foreach (QAction* action, m_gameActions) {
387 action->setDisabled(true);
388 }
389}
390
391void Window::attachWidget(QWidget* widget) {
392 m_screenWidget->layout()->addWidget(widget);
393 static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
394}
395
396void Window::detachWidget(QWidget* widget) {
397 m_screenWidget->layout()->removeWidget(widget);
398}