all repos — mgba @ dbe55c2fa80c91adce7f1f3dcdeb581e0ee738d5

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