all repos — mgba @ 40ae214a7dfb422f30ac4a138fdaedd45fc66065

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