all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

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", "platform.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		for (Window* w : m_windows) {
131			w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
132		}
133	});
134	m_windows.append(w);
135	w->setAttribute(Qt::WA_DeleteOnClose);
136	w->loadConfig();
137	w->show();
138	w->controller()->setMultiplayerController(&m_multiplayer);
139	w->multiplayerChanged();
140	for (Window* w : m_windows) {
141		w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
142	}
143	return w;
144}
145
146GBAApp* GBAApp::app() {
147	return g_app;
148}
149
150void GBAApp::pauseAll(QList<Window*>* paused) {
151	for (auto& window : m_windows) {
152		if (!window->controller()->isLoaded() || window->controller()->isPaused()) {
153			continue;
154		}
155		window->controller()->setPaused(true);
156		paused->append(window);
157	}
158}
159
160void GBAApp::continueAll(const QList<Window*>& paused) {
161	for (auto& window : paused) {
162		window->controller()->setPaused(false);
163	}
164}
165
166QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
167	QList<Window*> paused;
168	pauseAll(&paused);
169	QString filename = QFileDialog::getOpenFileName(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::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		if (m_parseThread.isRunning()) {
220			m_parseThread.quit();
221			m_parseThread.wait();
222		}
223		GameDBParser* parser = new GameDBParser(db);
224		m_parseThread.start();
225		parser->moveToThread(&m_parseThread);
226		QMetaObject::invokeMethod(parser, "parseNoIntroDB");
227		m_db = db;
228		return true;
229	}
230	return false;
231}
232#else
233bool GBAApp::reloadGameDB() {
234	return false;
235}
236#endif
237
238#ifdef USE_SQLITE3
239GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
240	: QObject(parent)
241	, m_db(db)
242{
243	// Nothing to do
244}
245
246void GameDBParser::parseNoIntroDB() {
247	VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
248	if (vf) {
249		NoIntroDBLoadClrMamePro(m_db, vf);
250		vf->close(vf);
251	}
252}
253
254#endif