all repos — mgba @ c0c903eb3e07a19800a1ff996bb3f6311e78e0c5

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