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