src/platform/qt/GBAApp.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "GBAApp.h"
7
8#include "AudioProcessor.h"
9#include "Display.h"
10#include "GameController.h"
11#include "Window.h"
12
13#include <QFileInfo>
14#include <QFileOpenEvent>
15#include <QIcon>
16
17extern "C" {
18#include "gba/supervisor/thread.h"
19#include "platform/commandline.h"
20#include "util/socket.h"
21}
22
23using namespace QGBA;
24
25static GBAApp* g_app = nullptr;
26
27GBAApp::GBAApp(int& argc, char* argv[])
28 : QApplication(argc, argv)
29 , m_windows{}
30{
31 g_app = this;
32
33#ifdef BUILD_SDL
34 SDL_Init(SDL_INIT_NOPARACHUTE);
35#endif
36
37#ifndef Q_OS_MAC
38 setWindowIcon(QIcon(":/res/mgba-1024.png"));
39#endif
40
41 SocketSubsystemInit();
42 qRegisterMetaType<const uint32_t*>("const uint32_t*");
43 qRegisterMetaType<GBAThread*>("GBAThread*");
44
45 QApplication::setApplicationName(projectName);
46 QApplication::setApplicationVersion(projectVersion);
47
48 if (!m_configController.getQtOption("displayDriver").isNull()) {
49 Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
50 }
51
52 GBAArguments args;
53 GraphicsOpts graphicsOpts;
54 SubParser subparser;
55 initParserForGraphics(&subparser, &graphicsOpts);
56 bool loaded = m_configController.parseArguments(&args, argc, argv, &subparser);
57 if (loaded && args.showHelp) {
58 usage(argv[0], subparser.usage);
59 ::exit(0);
60 return;
61 }
62
63 if (!m_configController.getQtOption("audioDriver").isNull()) {
64 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
65 }
66 Window* w = new Window(&m_configController);
67 connect(w, &Window::destroyed, [this]() {
68 m_windows[0] = nullptr;
69 });
70 m_windows[0] = w;
71
72 if (loaded) {
73 w->argumentsPassed(&args);
74 } else {
75 w->loadConfig();
76 }
77 freeArguments(&args);
78
79 if (graphicsOpts.multiplier) {
80 w->resizeFrame(VIDEO_HORIZONTAL_PIXELS * graphicsOpts.multiplier, VIDEO_VERTICAL_PIXELS * graphicsOpts.multiplier);
81 }
82 if (graphicsOpts.fullscreen) {
83 w->enterFullScreen();
84 }
85
86 w->show();
87
88 w->controller()->setMultiplayerController(&m_multiplayer);
89 w->multiplayerChanged();
90}
91
92bool GBAApp::event(QEvent* event) {
93 if (event->type() == QEvent::FileOpen) {
94 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
95 return true;
96 }
97 return QApplication::event(event);
98}
99
100Window* GBAApp::newWindow() {
101 if (m_multiplayer.attached() >= MAX_GBAS) {
102 return nullptr;
103 }
104 Window* w = new Window(&m_configController, m_multiplayer.attached());
105 int windowId = m_multiplayer.attached();
106 connect(w, &Window::destroyed, [this, windowId]() {
107 m_windows[windowId] = nullptr;
108 });
109 m_windows[windowId] = w;
110 w->setAttribute(Qt::WA_DeleteOnClose);
111 w->loadConfig();
112 w->show();
113 w->controller()->setMultiplayerController(&m_multiplayer);
114 w->multiplayerChanged();
115 return w;
116}
117
118GBAApp* GBAApp::app() {
119 return g_app;
120}
121
122void GBAApp::interruptAll() {
123 for (int i = 0; i < MAX_GBAS; ++i) {
124 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
125 continue;
126 }
127 m_windows[i]->controller()->threadInterrupt();
128 }
129}
130
131void GBAApp::continueAll() {
132 for (int i = 0; i < MAX_GBAS; ++i) {
133 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
134 continue;
135 }
136 m_windows[i]->controller()->threadContinue();
137 }
138}
139
140QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
141 interruptAll();
142 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
143 continueAll();
144 if (!filename.isEmpty()) {
145 m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
146 }
147 return filename;
148}
149
150QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
151 interruptAll();
152 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
153 continueAll();
154 if (!filename.isEmpty()) {
155 m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
156 }
157 return filename;
158}
159
160QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
161 FileDialog* dialog = new FileDialog(this, owner, title, filter);
162 dialog->setAcceptMode(QFileDialog::AcceptOpen);
163 return dialog;
164}
165
166QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
167 FileDialog* dialog = new FileDialog(this, owner, title, filter);
168 dialog->setAcceptMode(QFileDialog::AcceptSave);
169 return dialog;
170}
171
172GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
173 : QFileDialog(parent, caption, app->m_configController.getQtOption("lastDirectory").toString(), filter)
174 , m_app(app)
175{
176}
177
178int GBAApp::FileDialog::exec() {
179 m_app->interruptAll();
180 bool didAccept = QFileDialog::exec() == QDialog::Accepted;
181 QStringList filenames = selectedFiles();
182 if (!filenames.isEmpty()) {
183 m_app->m_configController.setQtOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
184 }
185 m_app->continueAll();
186 return didAccept;
187}