all repos — mgba @ 44175d9381936c0445f30a611e034a9c23103649

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
166QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
167	QList<Window*> paused;
168	pauseAll(&paused);
169	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
170	continueAll(paused);
171	if (!filename.isEmpty()) {
172		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
173	}
174	return filename;
175}
176
177QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
178	QList<Window*> paused;
179	pauseAll(&paused);
180	QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController->getOption("lastDirectory"));
181	continueAll(paused);
182	if (!filename.isEmpty()) {
183		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
184	}
185	return filename;
186}
187
188QString GBAApp::dataDir() {
189#ifdef DATADIR
190	QString path = QString::fromUtf8(DATADIR);
191#else
192	QString path = QCoreApplication::applicationDirPath();
193#ifdef Q_OS_MAC
194	path += QLatin1String("/../Resources");
195#endif
196#endif
197	return path;
198}
199
200#ifdef USE_SQLITE3
201bool GBAApp::reloadGameDB() {
202	NoIntroDB* db = nullptr;
203	db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
204	if (db && m_db) {
205		NoIntroDBDestroy(m_db);
206	}
207	if (db) {
208		std::shared_ptr<GameDBParser> parser = std::make_shared<GameDBParser>(db);
209		submitWorkerJob(std::bind(&GameDBParser::parseNoIntroDB, parser));
210		m_db = db;
211		return true;
212	}
213	return false;
214}
215#else
216bool GBAApp::reloadGameDB() {
217	return false;
218}
219#endif
220
221qint64 GBAApp::submitWorkerJob(std::function<void ()> job, std::function<void ()> callback) {
222	return submitWorkerJob(job, nullptr, callback);
223}
224
225qint64 GBAApp::submitWorkerJob(std::function<void ()> job, QObject* context, std::function<void ()> callback) {
226	qint64 jobId = m_nextJob;
227	++m_nextJob;
228	WorkerJob* jobRunnable = new WorkerJob(jobId, job, this);
229	m_workerJobs.insert(jobId, jobRunnable);
230	if (callback) {
231		waitOnJob(jobId, context, callback);
232	}
233	m_workerThreads.start(jobRunnable);
234	return jobId;
235}
236
237bool GBAApp::removeWorkerJob(qint64 jobId) {
238	for (auto& job : m_workerJobCallbacks.values(jobId)) {
239		disconnect(job);
240	}
241	m_workerJobCallbacks.remove(jobId);
242	if (!m_workerJobs.contains(jobId)) {
243		return true;
244	}
245	bool success = false;
246#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
247	success = m_workerThreads.tryTake(m_workerJobs[jobId]);
248#endif
249	if (success) {
250		m_workerJobs.remove(jobId);
251	}
252	return success;
253}
254
255
256bool GBAApp::waitOnJob(qint64 jobId, QObject* context, std::function<void ()> callback) {
257	if (!m_workerJobs.contains(jobId)) {
258		return false;
259	}
260	if (!context) {
261		context = this;
262	}
263	QMetaObject::Connection connection = connect(this, &GBAApp::jobFinished, context, [jobId, callback](qint64 testedJobId) {
264		if (jobId != testedJobId) {
265			return;
266		}
267		callback();
268	});
269	m_workerJobCallbacks.insert(m_nextJob, connection);
270	return true;
271}
272
273void GBAApp::finishJob(qint64 jobId) {
274	m_workerJobs.remove(jobId);
275	emit jobFinished(jobId);
276	m_workerJobCallbacks.remove(jobId);
277}
278
279GBAApp::WorkerJob::WorkerJob(qint64 id, std::function<void ()> job, GBAApp* owner)
280	: m_id(id)
281	, m_job(job)
282	, m_owner(owner)
283{
284	setAutoDelete(true);
285}
286
287void GBAApp::WorkerJob::run() {
288	m_job();
289	QMetaObject::invokeMethod(m_owner, "finishJob", Q_ARG(qint64, m_id));
290}
291
292#ifdef USE_SQLITE3
293GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
294	: QObject(parent)
295	, m_db(db)
296{
297	// Nothing to do
298}
299
300void GameDBParser::parseNoIntroDB() {
301	VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
302	if (vf) {
303		NoIntroDBLoadClrMamePro(m_db, vf);
304		vf->close(vf);
305	}
306}
307
308#endif