all repos — mgba @ 709149458319ec6bdf1a95c615a3ce5a6bbe1cb0

mGBA Game Boy Advance Emulator

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	int windowId = m_multiplayer.attached();
116	connect(w, &Window::destroyed, [this, w]() {
117		m_windows.removeAll(w);
118		for (Window* w : m_windows) {
119			w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
120		}
121	});
122	m_windows.append(w);
123	w->setAttribute(Qt::WA_DeleteOnClose);
124	w->loadConfig();
125	w->show();
126	w->multiplayerChanged();
127	for (Window* w : m_windows) {
128		w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
129	}
130	return w;
131}
132
133GBAApp* GBAApp::app() {
134	return g_app;
135}
136
137void GBAApp::pauseAll(QList<Window*>* paused) {
138	for (auto& window : m_windows) {
139		if (!window->controller() || window->controller()->isPaused()) {
140			continue;
141		}
142		window->controller()->setPaused(true);
143		paused->append(window);
144	}
145}
146
147void GBAApp::continueAll(const QList<Window*>& paused) {
148	for (auto& window : paused) {
149		if (window->controller()) {
150			window->controller()->setPaused(false);
151		}
152	}
153}
154
155QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
156	QList<Window*> paused;
157	pauseAll(&paused);
158	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
159	continueAll(paused);
160	if (!filename.isEmpty()) {
161		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
162	}
163	return filename;
164}
165
166QStringList GBAApp::getOpenFileNames(QWidget* owner, const QString& title, const QString& filter) {
167	QList<Window*> paused;
168	pauseAll(&paused);
169	QStringList filenames = QFileDialog::getOpenFileNames(owner, title, m_configController->getOption("lastDirectory"), filter);
170	continueAll(paused);
171	if (!filenames.isEmpty()) {
172		m_configController->setOption("lastDirectory", QFileInfo(filenames.at(0)).dir().canonicalPath());
173	}
174	return filenames;
175}
176
177QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
178	QList<Window*> paused;
179	pauseAll(&paused);
180	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
181	continueAll(paused);
182	if (!filename.isEmpty()) {
183		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
184	}
185	return filename;
186}
187
188QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
189	QList<Window*> paused;
190	pauseAll(&paused);
191	QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController->getOption("lastDirectory"));
192	continueAll(paused);
193	if (!filename.isEmpty()) {
194		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
195	}
196	return filename;
197}
198
199QString GBAApp::dataDir() {
200#ifdef DATADIR
201	QString path = QString::fromUtf8(DATADIR);
202#else
203	QString path = QCoreApplication::applicationDirPath();
204#ifdef Q_OS_MAC
205	path += QLatin1String("/../Resources");
206#endif
207#endif
208	return path;
209}
210
211#ifdef USE_SQLITE3
212bool GBAApp::reloadGameDB() {
213	NoIntroDB* db = nullptr;
214	db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
215	if (db && m_db) {
216		NoIntroDBDestroy(m_db);
217	}
218	if (db) {
219		std::shared_ptr<GameDBParser> parser = std::make_shared<GameDBParser>(db);
220		submitWorkerJob(std::bind(&GameDBParser::parseNoIntroDB, parser));
221		m_db = db;
222		return true;
223	}
224	return false;
225}
226#else
227bool GBAApp::reloadGameDB() {
228	return false;
229}
230#endif
231
232qint64 GBAApp::submitWorkerJob(std::function<void ()> job, std::function<void ()> callback) {
233	return submitWorkerJob(job, nullptr, callback);
234}
235
236qint64 GBAApp::submitWorkerJob(std::function<void ()> job, QObject* context, std::function<void ()> callback) {
237	qint64 jobId = m_nextJob;
238	++m_nextJob;
239	WorkerJob* jobRunnable = new WorkerJob(jobId, job, this);
240	m_workerJobs.insert(jobId, jobRunnable);
241	if (callback) {
242		waitOnJob(jobId, context, callback);
243	}
244	m_workerThreads.start(jobRunnable);
245	return jobId;
246}
247
248bool GBAApp::removeWorkerJob(qint64 jobId) {
249	for (auto& job : m_workerJobCallbacks.values(jobId)) {
250		disconnect(job);
251	}
252	m_workerJobCallbacks.remove(jobId);
253	if (!m_workerJobs.contains(jobId)) {
254		return true;
255	}
256	bool success = false;
257#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
258	success = m_workerThreads.tryTake(m_workerJobs[jobId]);
259#endif
260	if (success) {
261		m_workerJobs.remove(jobId);
262	}
263	return success;
264}
265
266
267bool GBAApp::waitOnJob(qint64 jobId, QObject* context, std::function<void ()> callback) {
268	if (!m_workerJobs.contains(jobId)) {
269		return false;
270	}
271	if (!context) {
272		context = this;
273	}
274	QMetaObject::Connection connection = connect(this, &GBAApp::jobFinished, context, [jobId, callback](qint64 testedJobId) {
275		if (jobId != testedJobId) {
276			return;
277		}
278		callback();
279	});
280	m_workerJobCallbacks.insert(m_nextJob, connection);
281	return true;
282}
283
284void GBAApp::finishJob(qint64 jobId) {
285	m_workerJobs.remove(jobId);
286	emit jobFinished(jobId);
287	m_workerJobCallbacks.remove(jobId);
288}
289
290GBAApp::WorkerJob::WorkerJob(qint64 id, std::function<void ()> job, GBAApp* owner)
291	: m_id(id)
292	, m_job(job)
293	, m_owner(owner)
294{
295	setAutoDelete(true);
296}
297
298void GBAApp::WorkerJob::run() {
299	m_job();
300	QMetaObject::invokeMethod(m_owner, "finishJob", Q_ARG(qint64, m_id));
301}
302
303#ifdef USE_SQLITE3
304GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
305	: QObject(parent)
306	, m_db(db)
307{
308	// Nothing to do
309}
310
311void GameDBParser::parseNoIntroDB() {
312	VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
313	if (vf) {
314		NoIntroDBLoadClrMamePro(m_db, vf);
315		vf->close(vf);
316	}
317}
318
319#endif