all repos — mgba @ 28c53fb3f2e47d4ed5b3ec3e0ae84bcc3dd0acda

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
 13#include <QFileInfo>
 14#include <QFileOpenEvent>
 15#include <QIcon>
 16
 17extern "C" {
 18#include "gba/supervisor/thread.h"
 19#include "platform/commandline.h"
 20#include "util/socket.h"
 21}
 22
 23using namespace QGBA;
 24
 25static GBAApp* g_app = nullptr;
 26
 27GBAApp::GBAApp(int& argc, char* argv[])
 28	: QApplication(argc, argv)
 29	, m_windows{}
 30{
 31	g_app = this;
 32
 33#ifdef BUILD_SDL
 34	SDL_Init(SDL_INIT_NOPARACHUTE);
 35#endif
 36
 37#ifndef Q_OS_MAC
 38	setWindowIcon(QIcon(":/res/mgba-1024.png"));
 39#endif
 40
 41	SocketSubsystemInit();
 42	qRegisterMetaType<const uint32_t*>("const uint32_t*");
 43	qRegisterMetaType<GBAThread*>("GBAThread*");
 44
 45	QApplication::setApplicationName(projectName);
 46	QApplication::setApplicationVersion(projectVersion);
 47
 48	if (!m_configController.getQtOption("displayDriver").isNull()) {
 49		Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
 50	}
 51
 52	GBAArguments args;
 53	bool loaded = m_configController.parseArguments(&args, argc, argv);
 54	if (loaded && args.showHelp) {
 55		usage(argv[0], 0);
 56		::exit(0);
 57		return;
 58	}
 59
 60	if (!m_configController.getQtOption("audioDriver").isNull()) {
 61		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
 62	}
 63	Window* w = new Window(&m_configController);
 64	connect(w, &Window::destroyed, [this]() {
 65		m_windows[0] = nullptr;
 66	});
 67	m_windows[0] = w;
 68
 69	if (loaded) {
 70		w->argumentsPassed(&args);
 71	} else {
 72		w->loadConfig();
 73	}
 74	freeArguments(&args);
 75	w->show();
 76
 77	w->controller()->setMultiplayerController(&m_multiplayer);
 78	w->multiplayerChanged();
 79}
 80
 81bool GBAApp::event(QEvent* event) {
 82	if (event->type() == QEvent::FileOpen) {
 83		m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
 84		return true;
 85	}
 86	return QApplication::event(event);
 87}
 88
 89Window* GBAApp::newWindow() {
 90	if (m_multiplayer.attached() >= MAX_GBAS) {
 91		return nullptr;
 92	}
 93	Window* w = new Window(&m_configController, m_multiplayer.attached());
 94	int windowId = m_multiplayer.attached();
 95	connect(w, &Window::destroyed, [this, windowId]() {
 96		m_windows[windowId] = nullptr;
 97	});
 98	m_windows[windowId] = w;
 99	w->setAttribute(Qt::WA_DeleteOnClose);
100	w->loadConfig();
101	w->show();
102	w->controller()->setMultiplayerController(&m_multiplayer);
103	w->multiplayerChanged();
104	return w;
105}
106
107GBAApp* GBAApp::app() {
108	return g_app;
109}
110
111void GBAApp::interruptAll() {
112	for (int i = 0; i < MAX_GBAS; ++i) {
113		if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
114			continue;
115		}
116		m_windows[i]->controller()->threadInterrupt();
117	}
118}
119
120void GBAApp::continueAll() {
121	for (int i = 0; i < MAX_GBAS; ++i) {
122		if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
123			continue;
124		}
125		m_windows[i]->controller()->threadContinue();
126	}
127}
128
129QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
130	interruptAll();
131	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
132	continueAll();
133	if (!filename.isEmpty()) {
134		m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
135	}
136	return filename;
137}
138
139QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
140	interruptAll();
141	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
142	continueAll();
143	if (!filename.isEmpty()) {
144		m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
145	}
146	return filename;
147}
148
149QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
150	FileDialog* dialog = new FileDialog(this, owner, title, filter);
151	dialog->setAcceptMode(QFileDialog::AcceptOpen);
152	return dialog;
153}
154
155QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
156	FileDialog* dialog = new FileDialog(this, owner, title, filter);
157	dialog->setAcceptMode(QFileDialog::AcceptSave);
158	return dialog;
159}
160
161GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
162	: QFileDialog(parent, caption, app->m_configController.getQtOption("lastDirectory").toString(), filter)
163	, m_app(app)
164{
165}
166
167int GBAApp::FileDialog::exec() {
168	m_app->interruptAll();
169	bool didAccept = QFileDialog::exec() == QDialog::Accepted;
170	QStringList filenames = selectedFiles();
171	if (!filenames.isEmpty()) {
172		m_app->m_configController.setQtOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
173	}
174	m_app->continueAll();
175	return didAccept;
176}