all repos — mgba @ f2d5c3ff4dd58d2a66f599aab5a42079a66e7fdb

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