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