all repos — mgba @ 87caf0a6acc558dcfbf90b7981e38d0a6a5128da

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/nointro.h>
 23#include <mgba-util/vfs.h>
 24/*
 25#include "feature/commandline.h"
 26*/
 27using namespace QGBA;
 28
 29static GBAApp* g_app = nullptr;
 30
 31mLOG_DEFINE_CATEGORY(QT, "Qt");
 32
 33GBAApp::GBAApp(int& argc, char* argv[])
 34	: QApplication(argc, argv)
 35	, m_windows{}
 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]() {
 83		m_windows[0] = nullptr;
 84	});
 85	m_windows[0] = 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
107bool GBAApp::event(QEvent* event) {
108	if (event->type() == QEvent::FileOpen) {
109		m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
110		return true;
111	}
112	return QApplication::event(event);
113}
114
115Window* GBAApp::newWindow() {
116	if (m_multiplayer.attached() >= MAX_GBAS) {
117		return nullptr;
118	}
119	Window* w = new Window(&m_configController, m_multiplayer.attached());
120	int windowId = m_multiplayer.attached();
121	connect(w, &Window::destroyed, [this, windowId]() {
122		m_windows[windowId] = nullptr;
123	});
124	m_windows[windowId] = w;
125	w->setAttribute(Qt::WA_DeleteOnClose);
126	w->loadConfig();
127	w->show();
128	w->controller()->setMultiplayerController(&m_multiplayer);
129	w->multiplayerChanged();
130	return w;
131}
132
133GBAApp* GBAApp::app() {
134	return g_app;
135}
136
137void GBAApp::pauseAll(QList<int>* paused) {
138	for (int i = 0; i < MAX_GBAS; ++i) {
139		if (!m_windows[i] || !m_windows[i]->controller()->isLoaded() || m_windows[i]->controller()->isPaused()) {
140			continue;
141		}
142		m_windows[i]->controller()->setPaused(true);
143		paused->append(i);
144	}
145}
146
147void GBAApp::continueAll(const QList<int>* paused) {
148	for (int i : *paused) {
149		m_windows[i]->controller()->setPaused(false);
150	}
151}
152
153QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
154	QList<int> paused;
155	pauseAll(&paused);
156	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
157	continueAll(&paused);
158	if (!filename.isEmpty()) {
159		m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
160	}
161	return filename;
162}
163
164QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
165	QList<int> paused;
166	pauseAll(&paused);
167	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
168	continueAll(&paused);
169	if (!filename.isEmpty()) {
170		m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
171	}
172	return filename;
173}
174
175QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
176	QList<int> paused;
177	pauseAll(&paused);
178	QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController.getOption("lastDirectory"));
179	continueAll(&paused);
180	if (!filename.isEmpty()) {
181		m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
182	}
183	return filename;
184}
185
186QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
187	FileDialog* dialog = new FileDialog(this, owner, title, filter);
188	dialog->setAcceptMode(QFileDialog::AcceptOpen);
189	return dialog;
190}
191
192QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
193	FileDialog* dialog = new FileDialog(this, owner, title, filter);
194	dialog->setAcceptMode(QFileDialog::AcceptSave);
195	return dialog;
196}
197
198QString GBAApp::dataDir() {
199#ifdef DATADIR
200	QString path = QString::fromUtf8(DATADIR);
201#else
202	QString path = QCoreApplication::applicationDirPath();
203#ifdef Q_OS_MAC
204	path += QLatin1String("/../Resources");
205#endif
206#endif
207	return path;
208}
209
210bool GBAApp::reloadGameDB() {
211	NoIntroDB* db = nullptr;
212	VFile* vf = VFileDevice::open(dataDir() + "/nointro.dat", O_RDONLY);
213	if (vf) {
214		db = NoIntroDBLoad(vf);
215		vf->close(vf);
216	}
217	if (db && m_db) {
218		NoIntroDBDestroy(m_db);
219	}
220	if (db) {
221		m_db = db;
222		return true;
223	}
224	return false;
225}
226
227GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
228	: QFileDialog(parent, caption, app->m_configController.getOption("lastDirectory"), filter)
229	, m_app(app)
230{
231}
232
233int GBAApp::FileDialog::exec() {
234	QList<int> paused;
235	m_app->pauseAll(&paused);
236	bool didAccept = QFileDialog::exec() == QDialog::Accepted;
237	QStringList filenames = selectedFiles();
238	if (!filenames.isEmpty()) {
239		m_app->m_configController.setOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
240	}
241	m_app->continueAll(&paused);
242	return didAccept;
243}