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#include "VFileDevice.h"
13
14#include <QFileInfo>
15#include <QFileOpenEvent>
16#include <QIcon>
17#include <QTranslator>
18
19#include <mgba/core/version.h>
20#include <mgba/internal/gba/video.h>
21#include <mgba-util/socket.h>
22#include <mgba-util/vfs.h>
23
24#ifdef USE_SQLITE3
25#include "feature/sqlite3/no-intro.h"
26#endif
27
28using namespace QGBA;
29
30static GBAApp* g_app = nullptr;
31
32mLOG_DEFINE_CATEGORY(QT, "Qt");
33
34GBAApp::GBAApp(int& argc, char* argv[])
35 : QApplication(argc, argv)
36 , m_windows{}
37 , m_db(nullptr)
38{
39 g_app = this;
40
41#ifdef BUILD_SDL
42 SDL_Init(SDL_INIT_NOPARACHUTE);
43#endif
44
45#ifndef Q_OS_MAC
46 setWindowIcon(QIcon(":/res/mgba-1024.png"));
47#endif
48
49 QTranslator* translator = new QTranslator(this);
50 if (translator->load(QLocale(), QLatin1String(binaryName), QLatin1String("-"), QLatin1String(":/translations"))) {
51 installTranslator(translator);
52 }
53
54
55 SocketSubsystemInit();
56 qRegisterMetaType<const uint32_t*>("const uint32_t*");
57 qRegisterMetaType<mCoreThread*>("mCoreThread*");
58
59 QApplication::setApplicationName(projectName);
60 QApplication::setApplicationVersion(projectVersion);
61
62 if (!m_configController.getQtOption("displayDriver").isNull()) {
63 Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
64 }
65
66 mArguments args;
67 mGraphicsOpts graphicsOpts;
68 mSubParser subparser;
69 initParserForGraphics(&subparser, &graphicsOpts);
70 bool loaded = m_configController.parseArguments(&args, argc, argv, &subparser);
71 if (loaded && args.showHelp) {
72 usage(argv[0], subparser.usage);
73 ::exit(0);
74 return;
75 }
76
77 reloadGameDB();
78
79 if (!m_configController.getQtOption("audioDriver").isNull()) {
80 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
81 }
82 Window* w = new Window(&m_configController);
83 connect(w, &Window::destroyed, [this]() {
84 m_windows[0] = nullptr;
85 });
86 m_windows[0] = w;
87
88 if (loaded) {
89 w->argumentsPassed(&args);
90 } else {
91 w->loadConfig();
92 }
93 freeArguments(&args);
94
95 if (graphicsOpts.multiplier) {
96 w->resizeFrame(QSize(VIDEO_HORIZONTAL_PIXELS * graphicsOpts.multiplier, VIDEO_VERTICAL_PIXELS * graphicsOpts.multiplier));
97 }
98 if (graphicsOpts.fullscreen) {
99 w->enterFullScreen();
100 }
101
102 w->show();
103
104 w->controller()->setMultiplayerController(&m_multiplayer);
105 w->multiplayerChanged();
106}
107
108GBAApp::~GBAApp() {
109#ifdef USE_SQLITE3
110 m_parseThread.quit();
111 m_parseThread.wait();
112#endif
113}
114
115bool GBAApp::event(QEvent* event) {
116 if (event->type() == QEvent::FileOpen) {
117 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
118 return true;
119 }
120 return QApplication::event(event);
121}
122
123Window* GBAApp::newWindow() {
124 if (m_multiplayer.attached() >= MAX_GBAS) {
125 return nullptr;
126 }
127 Window* w = new Window(&m_configController, m_multiplayer.attached());
128 int windowId = m_multiplayer.attached();
129 connect(w, &Window::destroyed, [this, windowId]() {
130 m_windows[windowId] = nullptr;
131 });
132 m_windows[windowId] = w;
133 w->setAttribute(Qt::WA_DeleteOnClose);
134 w->loadConfig();
135 w->show();
136 w->controller()->setMultiplayerController(&m_multiplayer);
137 w->multiplayerChanged();
138 return w;
139}
140
141GBAApp* GBAApp::app() {
142 return g_app;
143}
144
145void GBAApp::pauseAll(QList<int>* paused) {
146 for (int i = 0; i < MAX_GBAS; ++i) {
147 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded() || m_windows[i]->controller()->isPaused()) {
148 continue;
149 }
150 m_windows[i]->controller()->setPaused(true);
151 paused->append(i);
152 }
153}
154
155void GBAApp::continueAll(const QList<int>* paused) {
156 for (int i : *paused) {
157 m_windows[i]->controller()->setPaused(false);
158 }
159}
160
161QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
162 QList<int> paused;
163 pauseAll(&paused);
164 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
165 continueAll(&paused);
166 if (!filename.isEmpty()) {
167 m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
168 }
169 return filename;
170}
171
172QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
173 QList<int> paused;
174 pauseAll(&paused);
175 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
176 continueAll(&paused);
177 if (!filename.isEmpty()) {
178 m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
179 }
180 return filename;
181}
182
183QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
184 QList<int> paused;
185 pauseAll(&paused);
186 QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController.getOption("lastDirectory"));
187 continueAll(&paused);
188 if (!filename.isEmpty()) {
189 m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
190 }
191 return filename;
192}
193
194QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
195 FileDialog* dialog = new FileDialog(this, owner, title, filter);
196 dialog->setAcceptMode(QFileDialog::AcceptOpen);
197 return dialog;
198}
199
200QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
201 FileDialog* dialog = new FileDialog(this, owner, title, filter);
202 dialog->setAcceptMode(QFileDialog::AcceptSave);
203 return dialog;
204}
205
206QString GBAApp::dataDir() {
207#ifdef DATADIR
208 QString path = QString::fromUtf8(DATADIR);
209#else
210 QString path = QCoreApplication::applicationDirPath();
211#ifdef Q_OS_MAC
212 path += QLatin1String("/../Resources");
213#endif
214#endif
215 return path;
216}
217
218#ifdef USE_SQLITE3
219bool GBAApp::reloadGameDB() {
220 NoIntroDB* db = nullptr;
221 db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
222 if (db && m_db) {
223 NoIntroDBDestroy(m_db);
224 }
225 if (db) {
226 if (m_parseThread.isRunning()) {
227 m_parseThread.quit();
228 m_parseThread.wait();
229 }
230 GameDBParser* parser = new GameDBParser(db);
231 m_parseThread.start();
232 parser->moveToThread(&m_parseThread);
233 QMetaObject::invokeMethod(parser, "parseNoIntroDB");
234 m_db = db;
235 return true;
236 }
237 return false;
238}
239#else
240bool GBAApp::reloadGameDB() {
241 return false;
242}
243#endif
244
245GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
246 : QFileDialog(parent, caption, app->m_configController.getOption("lastDirectory"), filter)
247 , m_app(app)
248{
249}
250
251int GBAApp::FileDialog::exec() {
252 QList<int> paused;
253 m_app->pauseAll(&paused);
254 bool didAccept = QFileDialog::exec() == QDialog::Accepted;
255 QStringList filenames = selectedFiles();
256 if (!filenames.isEmpty()) {
257 m_app->m_configController.setOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
258 }
259 m_app->continueAll(&paused);
260 return didAccept;
261}
262
263#ifdef USE_SQLITE3
264GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
265 : QObject(parent)
266 , m_db(db)
267{
268 // Nothing to do
269}
270
271void GameDBParser::parseNoIntroDB() {
272 VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
273 if (vf) {
274 NoIntroDBLoadClrMamePro(m_db, vf);
275 vf->close(vf);
276 }
277}
278
279#endif