all repos — mgba @ 9b0393d50ffcd27c487740098c3c0bb72b16529f

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/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
 69GBAApp::~GBAApp() {
 70#ifdef USE_SQLITE3
 71	m_parseThread.quit();
 72	m_parseThread.wait();
 73#endif
 74}
 75
 76bool GBAApp::event(QEvent* event) {
 77	if (event->type() == QEvent::FileOpen) {
 78		CoreController* core = m_manager.loadGame(static_cast<QFileOpenEvent*>(event)->file());
 79		m_windows[0]->setController(core, static_cast<QFileOpenEvent*>(event)->file());
 80		return true;
 81	}
 82	return QApplication::event(event);
 83}
 84
 85Window* GBAApp::newWindow() {
 86	if (m_windows.count() >= MAX_GBAS) {
 87		return nullptr;
 88	}
 89	Window* w = new Window(&m_manager, m_configController, m_multiplayer.attached());
 90	int windowId = m_multiplayer.attached();
 91	connect(w, &Window::destroyed, [this, w]() {
 92		m_windows.removeAll(w);
 93		for (Window* w : m_windows) {
 94			w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
 95		}
 96	});
 97	m_windows.append(w);
 98	w->setAttribute(Qt::WA_DeleteOnClose);
 99	w->loadConfig();
100	w->show();
101	w->multiplayerChanged();
102	for (Window* w : m_windows) {
103		w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
104	}
105	return w;
106}
107
108GBAApp* GBAApp::app() {
109	return g_app;
110}
111
112void GBAApp::pauseAll(QList<Window*>* paused) {
113	for (auto& window : m_windows) {
114		if (!window->controller() || window->controller()->isPaused()) {
115			continue;
116		}
117		window->controller()->setPaused(true);
118		paused->append(window);
119	}
120}
121
122void GBAApp::continueAll(const QList<Window*>& paused) {
123	for (auto& window : paused) {
124		if (window->controller()) {
125			window->controller()->setPaused(false);
126		}
127	}
128}
129
130QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
131	QList<Window*> paused;
132	pauseAll(&paused);
133	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
134	continueAll(paused);
135	if (!filename.isEmpty()) {
136		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
137	}
138	return filename;
139}
140
141QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
142	QList<Window*> paused;
143	pauseAll(&paused);
144	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
145	continueAll(paused);
146	if (!filename.isEmpty()) {
147		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
148	}
149	return filename;
150}
151
152QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
153	QList<Window*> paused;
154	pauseAll(&paused);
155	QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController->getOption("lastDirectory"));
156	continueAll(paused);
157	if (!filename.isEmpty()) {
158		m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
159	}
160	return filename;
161}
162
163QString GBAApp::dataDir() {
164#ifdef DATADIR
165	QString path = QString::fromUtf8(DATADIR);
166#else
167	QString path = QCoreApplication::applicationDirPath();
168#ifdef Q_OS_MAC
169	path += QLatin1String("/../Resources");
170#endif
171#endif
172	return path;
173}
174
175#ifdef USE_SQLITE3
176bool GBAApp::reloadGameDB() {
177	NoIntroDB* db = nullptr;
178	db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
179	if (db && m_db) {
180		NoIntroDBDestroy(m_db);
181	}
182	if (db) {
183		if (m_parseThread.isRunning()) {
184			m_parseThread.quit();
185			m_parseThread.wait();
186		}
187		GameDBParser* parser = new GameDBParser(db);
188		m_parseThread.start();
189		parser->moveToThread(&m_parseThread);
190		QMetaObject::invokeMethod(parser, "parseNoIntroDB");
191		m_db = db;
192		return true;
193	}
194	return false;
195}
196#else
197bool GBAApp::reloadGameDB() {
198	return false;
199}
200#endif
201
202#ifdef USE_SQLITE3
203GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
204	: QObject(parent)
205	, m_db(db)
206{
207	// Nothing to do
208}
209
210void GameDBParser::parseNoIntroDB() {
211	VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
212	if (vf) {
213		NoIntroDBLoadClrMamePro(m_db, vf);
214		vf->close(vf);
215	}
216}
217
218#endif