all repos — mgba @ 805e0b17eb3e7cb1cb6e01370811d7c9dd7f0377

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