all repos — mgba @ 688fcbb89cd491cc07b4902b6f7912c7587243b8

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