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 "CoreController.h"
10#include "CoreManager.h"
11#include "ConfigController.h"
12#include "Display.h"
13#include "LogController.h"
14#include "VFileDevice.h"
15#include "Window.h"
16
17#include <QFileInfo>
18#include <QFileOpenEvent>
19#include <QIcon>
20
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
28#ifdef USE_DISCORD_RPC
29#include "DiscordCoordinator.h"
30#endif
31
32using namespace QGBA;
33
34static GBAApp* g_app = nullptr;
35
36mLOG_DEFINE_CATEGORY(QT, "Qt", "platform.qt");
37
38GBAApp::GBAApp(int& argc, char* argv[], ConfigController* config)
39 : QApplication(argc, argv)
40 , m_configController(config)
41{
42 g_app = this;
43
44#ifdef BUILD_SDL
45 SDL_Init(SDL_INIT_NOPARACHUTE);
46#endif
47
48 SocketSubsystemInit();
49 qRegisterMetaType<const uint32_t*>("const uint32_t*");
50 qRegisterMetaType<mCoreThread*>("mCoreThread*");
51
52 if (!m_configController->getQtOption("displayDriver").isNull()) {
53 Display::setDriver(static_cast<Display::Driver>(m_configController->getQtOption("displayDriver").toInt()));
54 }
55
56 reloadGameDB();
57
58 m_manager.setConfig(m_configController->config());
59 m_manager.setMultiplayerController(&m_multiplayer);
60
61 if (!m_configController->getQtOption("audioDriver").isNull()) {
62 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController->getQtOption("audioDriver").toInt()));
63 }
64
65 LogController::global()->load(m_configController);
66
67#ifdef USE_DISCORD_RPC
68 ConfigOption* useDiscordPresence = m_configController->addOption("useDiscordPresence");
69 useDiscordPresence->addBoolean(tr("Enable Discord Rich Presence"));
70 useDiscordPresence->connect([](const QVariant& value) {
71 if (value.toBool()) {
72 DiscordCoordinator::init();
73 } else {
74 DiscordCoordinator::deinit();
75 }
76 }, this);
77 m_configController->updateOption("useDiscordPresence");
78#endif
79
80 connect(this, &GBAApp::aboutToQuit, this, &GBAApp::cleanup);
81}
82
83void GBAApp::cleanup() {
84 m_workerThreads.waitForDone();
85
86 while (!m_workerJobs.isEmpty()) {
87 finishJob(m_workerJobs.firstKey());
88 }
89
90#ifdef USE_SQLITE3
91 if (m_db) {
92 NoIntroDBDestroy(m_db);
93 }
94#endif
95
96#ifdef USE_DISCORD_RPC
97 DiscordCoordinator::deinit();
98#endif
99}
100
101bool GBAApp::event(QEvent* event) {
102 if (event->type() == QEvent::FileOpen) {
103 CoreController* core = m_manager.loadGame(static_cast<QFileOpenEvent*>(event)->file());
104 m_windows[0]->setController(core, static_cast<QFileOpenEvent*>(event)->file());
105 return true;
106 }
107 return QApplication::event(event);
108}
109
110Window* GBAApp::newWindow() {
111 if (m_windows.count() >= MAX_GBAS) {
112 return nullptr;
113 }
114 Window* w = new Window(&m_manager, m_configController, m_multiplayer.attached());
115 connect(w, &Window::destroyed, [this, w]() {
116 m_windows.removeAll(w);
117 for (Window* w : m_windows) {
118 w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
119 }
120 });
121 m_windows.append(w);
122 w->setAttribute(Qt::WA_DeleteOnClose);
123 w->loadConfig();
124 w->show();
125 w->multiplayerChanged();
126 for (Window* w : m_windows) {
127 w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
128 }
129 return w;
130}
131
132GBAApp* GBAApp::app() {
133 return g_app;
134}
135
136void GBAApp::pauseAll(QList<Window*>* paused) {
137 for (auto& window : m_windows) {
138 if (!window->controller() || window->controller()->isPaused()) {
139 continue;
140 }
141 window->controller()->setPaused(true);
142 paused->append(window);
143 }
144}
145
146void GBAApp::continueAll(const QList<Window*>& paused) {
147 for (auto& window : paused) {
148 if (window->controller()) {
149 window->controller()->setPaused(false);
150 }
151 }
152}
153
154QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
155 QList<Window*> paused;
156 pauseAll(&paused);
157 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
158 continueAll(paused);
159 if (!filename.isEmpty()) {
160 m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
161 }
162 return filename;
163}
164
165QStringList GBAApp::getOpenFileNames(QWidget* owner, const QString& title, const QString& filter) {
166 QList<Window*> paused;
167 pauseAll(&paused);
168 QStringList filenames = QFileDialog::getOpenFileNames(owner, title, m_configController->getOption("lastDirectory"), filter);
169 continueAll(paused);
170 if (!filenames.isEmpty()) {
171 m_configController->setOption("lastDirectory", QFileInfo(filenames.at(0)).dir().canonicalPath());
172 }
173 return filenames;
174}
175
176QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
177 QList<Window*> paused;
178 pauseAll(&paused);
179 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
180 continueAll(paused);
181 if (!filename.isEmpty()) {
182 m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
183 }
184 return filename;
185}
186
187QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title, const QString& path) {
188 QList<Window*> paused;
189 pauseAll(&paused);
190 QString filename = QFileDialog::getExistingDirectory(owner, title, !path.isNull() ? path : m_configController->getOption("lastDirectory"));
191 continueAll(paused);
192 if (path.isNull() && !filename.isEmpty()) {
193 m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
194 }
195 return filename;
196}
197
198QString GBAApp::dataDir() {
199#ifdef DATADIR
200 QString path = QString::fromUtf8(DATADIR);
201#else
202 QString path = QCoreApplication::applicationDirPath();
203#ifdef Q_OS_MAC
204 path += QLatin1String("/../Resources");
205#endif
206#endif
207 return path;
208}
209
210#ifdef USE_SQLITE3
211bool GBAApp::reloadGameDB() {
212 NoIntroDB* db = nullptr;
213 db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
214 if (db && m_db) {
215 NoIntroDBDestroy(m_db);
216 }
217 if (db) {
218 std::shared_ptr<GameDBParser> parser = std::make_shared<GameDBParser>(db);
219 submitWorkerJob(std::bind(&GameDBParser::parseNoIntroDB, parser));
220 m_db = db;
221 return true;
222 }
223 return false;
224}
225#else
226bool GBAApp::reloadGameDB() {
227 return false;
228}
229#endif
230
231qint64 GBAApp::submitWorkerJob(std::function<void ()> job, std::function<void ()> callback) {
232 return submitWorkerJob(job, nullptr, callback);
233}
234
235qint64 GBAApp::submitWorkerJob(std::function<void ()> job, QObject* context, std::function<void ()> callback) {
236 qint64 jobId = m_nextJob;
237 ++m_nextJob;
238 WorkerJob* jobRunnable = new WorkerJob(jobId, job, this);
239 m_workerJobs.insert(jobId, jobRunnable);
240 if (callback) {
241 waitOnJob(jobId, context, callback);
242 }
243 m_workerThreads.start(jobRunnable);
244 return jobId;
245}
246
247bool GBAApp::removeWorkerJob(qint64 jobId) {
248 for (auto& job : m_workerJobCallbacks.values(jobId)) {
249 disconnect(job);
250 }
251 m_workerJobCallbacks.remove(jobId);
252 if (!m_workerJobs.contains(jobId)) {
253 return true;
254 }
255 bool success = false;
256#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
257 success = m_workerThreads.tryTake(m_workerJobs[jobId]);
258#endif
259 if (success) {
260 m_workerJobs.remove(jobId);
261 }
262 return success;
263}
264
265
266bool GBAApp::waitOnJob(qint64 jobId, QObject* context, std::function<void ()> callback) {
267 if (!m_workerJobs.contains(jobId)) {
268 return false;
269 }
270 if (!context) {
271 context = this;
272 }
273 QMetaObject::Connection connection = connect(this, &GBAApp::jobFinished, context, [jobId, callback](qint64 testedJobId) {
274 if (jobId != testedJobId) {
275 return;
276 }
277 callback();
278 });
279 m_workerJobCallbacks.insert(m_nextJob, connection);
280 return true;
281}
282
283void GBAApp::finishJob(qint64 jobId) {
284 m_workerJobs.remove(jobId);
285 emit jobFinished(jobId);
286 m_workerJobCallbacks.remove(jobId);
287}
288
289GBAApp::WorkerJob::WorkerJob(qint64 id, std::function<void ()> job, GBAApp* owner)
290 : m_id(id)
291 , m_job(job)
292 , m_owner(owner)
293{
294 setAutoDelete(true);
295}
296
297void GBAApp::WorkerJob::run() {
298 m_job();
299 QMetaObject::invokeMethod(m_owner, "finishJob", Q_ARG(qint64, m_id));
300}
301
302#ifdef USE_SQLITE3
303GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
304 : QObject(parent)
305 , m_db(db)
306{
307 // Nothing to do
308}
309
310void GameDBParser::parseNoIntroDB() {
311 VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
312 if (vf) {
313 NoIntroDBLoadClrMamePro(m_db, vf);
314 vf->close(vf);
315 }
316}
317
318#endif