all repos — mgba @ e0c2b3d68272e6c059f0f62f66f15756a27ce590

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 "Display.h"
 10#include "GameController.h"
 11#include "Window.h"
 12#include "VFileDevice.h"
 13
 14#include <QFileInfo>
 15#include <QFileOpenEvent>
 16#include <QIcon>
 17#include <QTranslator>
 18
 19#include <mgba/core/version.h>
 20#include <mgba/internal/gba/video.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");
 33
 34GBAApp::GBAApp(int& argc, char* argv[])
 35	: QApplication(argc, argv)
 36	, m_db(nullptr)
 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-1024.png"));
 46#endif
 47
 48	QTranslator* translator = new QTranslator(this);
 49	if (translator->load(QLocale(), QLatin1String(binaryName), QLatin1String("-"), QLatin1String(":/translations"))) {
 50		installTranslator(translator);
 51	}
 52
 53
 54	SocketSubsystemInit();
 55	qRegisterMetaType<const uint32_t*>("const uint32_t*");
 56	qRegisterMetaType<mCoreThread*>("mCoreThread*");
 57
 58	QApplication::setApplicationName(projectName);
 59	QApplication::setApplicationVersion(projectVersion);
 60
 61	if (!m_configController.getQtOption("displayDriver").isNull()) {
 62		Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
 63	}
 64
 65	mArguments args;
 66	mGraphicsOpts graphicsOpts;
 67	mSubParser subparser;
 68	initParserForGraphics(&subparser, &graphicsOpts);
 69	bool loaded = m_configController.parseArguments(&args, argc, argv, &subparser);
 70	if (loaded && args.showHelp) {
 71		usage(argv[0], subparser.usage);
 72		::exit(0);
 73		return;
 74	}
 75
 76	reloadGameDB();
 77
 78	if (!m_configController.getQtOption("audioDriver").isNull()) {
 79		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
 80	}
 81	Window* w = new Window(&m_configController);
 82	connect(w, &Window::destroyed, [this, w]() {
 83		m_windows.removeAll(w);
 84	});
 85	m_windows.append(w);
 86
 87	if (loaded) {
 88		w->argumentsPassed(&args);
 89	} else {
 90		w->loadConfig();
 91	}
 92	freeArguments(&args);
 93
 94	if (graphicsOpts.multiplier) {
 95		w->resizeFrame(QSize(VIDEO_HORIZONTAL_PIXELS * graphicsOpts.multiplier, VIDEO_VERTICAL_PIXELS * graphicsOpts.multiplier));
 96	}
 97	if (graphicsOpts.fullscreen) {
 98		w->enterFullScreen();
 99	}
100
101	w->show();
102
103	w->controller()->setMultiplayerController(&m_multiplayer);
104	w->multiplayerChanged();
105}
106
107GBAApp::~GBAApp() {
108#ifdef USE_SQLITE3
109	m_parseThread.quit();
110	m_parseThread.wait();
111#endif
112}
113
114bool GBAApp::event(QEvent* event) {
115	if (event->type() == QEvent::FileOpen) {
116		m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
117		return true;
118	}
119	return QApplication::event(event);
120}
121
122Window* GBAApp::newWindow() {
123	if (m_windows.count() >= MAX_GBAS) {
124		return nullptr;
125	}
126	Window* w = new Window(&m_configController, m_multiplayer.attached());
127	int windowId = m_multiplayer.attached();
128	connect(w, &Window::destroyed, [this, w]() {
129		m_windows.removeAll(w);
130	});
131	m_windows.append(w);
132	w->setAttribute(Qt::WA_DeleteOnClose);
133	w->loadConfig();
134	w->show();
135	w->controller()->setMultiplayerController(&m_multiplayer);
136	w->multiplayerChanged();
137	return w;
138}
139
140GBAApp* GBAApp::app() {
141	return g_app;
142}
143
144void GBAApp::pauseAll(QList<Window*>* paused) {
145	for (auto& window : m_windows) {
146		if (!window->controller()->isLoaded() || window->controller()->isPaused()) {
147			continue;
148		}
149		window->controller()->setPaused(true);
150		paused->append(window);
151	}
152}
153
154void GBAApp::continueAll(const QList<Window*>& paused) {
155	for (auto& window : paused) {
156		window->controller()->setPaused(false);
157	}
158}
159
160QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
161	QList<Window*> paused;
162	pauseAll(&paused);
163	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
164	continueAll(paused);
165	if (!filename.isEmpty()) {
166		m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
167	}
168	return filename;
169}
170
171QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
172	QList<Window*> paused;
173	pauseAll(&paused);
174	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
175	continueAll(paused);
176	if (!filename.isEmpty()) {
177		m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
178	}
179	return filename;
180}
181
182QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
183	QList<Window*> paused;
184	pauseAll(&paused);
185	QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController.getOption("lastDirectory"));
186	continueAll(paused);
187	if (!filename.isEmpty()) {
188		m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
189	}
190	return filename;
191}
192
193QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
194	FileDialog* dialog = new FileDialog(this, owner, title, filter);
195	dialog->setAcceptMode(QFileDialog::AcceptOpen);
196	return dialog;
197}
198
199QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
200	FileDialog* dialog = new FileDialog(this, owner, title, filter);
201	dialog->setAcceptMode(QFileDialog::AcceptSave);
202	return dialog;
203}
204
205QString GBAApp::dataDir() {
206#ifdef DATADIR
207	QString path = QString::fromUtf8(DATADIR);
208#else
209	QString path = QCoreApplication::applicationDirPath();
210#ifdef Q_OS_MAC
211	path += QLatin1String("/../Resources");
212#endif
213#endif
214	return path;
215}
216
217#ifdef USE_SQLITE3
218bool GBAApp::reloadGameDB() {
219	NoIntroDB* db = nullptr;
220	db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
221	if (db && m_db) {
222		NoIntroDBDestroy(m_db);
223	}
224	if (db) {
225		if (m_parseThread.isRunning()) {
226			m_parseThread.quit();
227			m_parseThread.wait();
228		}
229		GameDBParser* parser = new GameDBParser(db);
230		m_parseThread.start();
231		parser->moveToThread(&m_parseThread);
232		QMetaObject::invokeMethod(parser, "parseNoIntroDB");
233		m_db = db;
234		return true;
235	}
236	return false;
237}
238#else
239bool GBAApp::reloadGameDB() {
240	return false;
241}
242#endif
243
244GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
245	: QFileDialog(parent, caption, app->m_configController.getOption("lastDirectory"), filter)
246	, m_app(app)
247{
248}
249
250int GBAApp::FileDialog::exec() {
251	QList<Window*> paused;
252	m_app->pauseAll(&paused);
253	bool didAccept = QFileDialog::exec() == QDialog::Accepted;
254	QStringList filenames = selectedFiles();
255	if (!filenames.isEmpty()) {
256		m_app->m_configController.setOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
257	}
258	m_app->continueAll(paused);
259	return didAccept;
260}
261
262#ifdef USE_SQLITE3
263GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
264	: QObject(parent)
265	, m_db(db)
266{
267	// Nothing to do
268}
269
270void GameDBParser::parseNoIntroDB() {
271	VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
272	if (vf) {
273		NoIntroDBLoadClrMamePro(m_db, vf);
274		vf->close(vf);
275	}
276}
277
278#endif