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