all repos — mgba @ 5966f46355d18ba472404a7f7ed3cbeebf846635

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	GraphicsOpts graphicsOpts;
 54	SubParser subparser;
 55	initParserForGraphics(&subparser, &graphicsOpts);
 56	bool loaded = m_configController.parseArguments(&args, argc, argv, &subparser);
 57	if (loaded && args.showHelp) {
 58		usage(argv[0], subparser.usage);
 59		::exit(0);
 60		return;
 61	}
 62
 63	if (!m_configController.getQtOption("audioDriver").isNull()) {
 64		AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
 65	}
 66	Window* w = new Window(&m_configController);
 67	connect(w, &Window::destroyed, [this]() {
 68		m_windows[0] = nullptr;
 69	});
 70	m_windows[0] = w;
 71
 72	if (loaded) {
 73		w->argumentsPassed(&args);
 74	} else {
 75		w->loadConfig();
 76	}
 77	freeArguments(&args);
 78
 79	if (graphicsOpts.multiplier) {
 80		w->resizeFrame(VIDEO_HORIZONTAL_PIXELS * graphicsOpts.multiplier, VIDEO_VERTICAL_PIXELS * graphicsOpts.multiplier);
 81	}
 82	if (graphicsOpts.fullscreen) {
 83		w->enterFullScreen();
 84	}
 85
 86	w->show();
 87
 88	w->controller()->setMultiplayerController(&m_multiplayer);
 89}
 90
 91bool GBAApp::event(QEvent* event) {
 92	if (event->type() == QEvent::FileOpen) {
 93		m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
 94		return true;
 95	}
 96	return QApplication::event(event);
 97}
 98
 99Window* GBAApp::newWindow() {
100	if (m_multiplayer.attached() >= MAX_GBAS) {
101		return nullptr;
102	}
103	Window* w = new Window(&m_configController, m_multiplayer.attached());
104	int windowId = m_multiplayer.attached();
105	connect(w, &Window::destroyed, [this, windowId]() {
106		m_windows[windowId] = nullptr;
107	});
108	m_windows[windowId] = w;
109	w->setAttribute(Qt::WA_DeleteOnClose);
110	w->loadConfig();
111	w->show();
112	w->controller()->setMultiplayerController(&m_multiplayer);
113	return w;
114}
115
116GBAApp* GBAApp::app() {
117	return g_app;
118}
119
120void GBAApp::interruptAll() {
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()->threadInterrupt();
126	}
127}
128
129void GBAApp::continueAll() {
130	for (int i = 0; i < MAX_GBAS; ++i) {
131		if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
132			continue;
133		}
134		m_windows[i]->controller()->threadContinue();
135	}
136}
137
138QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
139	interruptAll();
140	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
141	continueAll();
142	if (!filename.isEmpty()) {
143		m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
144	}
145	return filename;
146}
147
148QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
149	interruptAll();
150	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
151	continueAll();
152	if (!filename.isEmpty()) {
153		m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
154	}
155	return filename;
156}
157
158QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
159	FileDialog* dialog = new FileDialog(this, owner, title, filter);
160	dialog->setAcceptMode(QFileDialog::AcceptOpen);
161	return dialog;
162}
163
164QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
165	FileDialog* dialog = new FileDialog(this, owner, title, filter);
166	dialog->setAcceptMode(QFileDialog::AcceptSave);
167	return dialog;
168}
169
170GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
171	: QFileDialog(parent, caption, app->m_configController.getQtOption("lastDirectory").toString(), filter)
172	, m_app(app)
173{
174}
175
176int GBAApp::FileDialog::exec() {
177	m_app->interruptAll();
178	bool didAccept = QFileDialog::exec() == QDialog::Accepted;
179	QStringList filenames = selectedFiles();
180	if (!filenames.isEmpty()) {
181		m_app->m_configController.setQtOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
182	}
183	m_app->continueAll();
184	return didAccept;
185}