all repos — mgba @ c0fcec22b71e93d46300e8540a0870e2d9af810a

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	Window* w = new Window(&m_configController);
 45	m_windows[0] = w;
 46
 47#ifndef Q_OS_MAC
 48	w->show();
 49#endif
 50
 51	GBAArguments args;
 52	if (m_configController.parseArguments(&args, argc, argv)) {
 53		w->argumentsPassed(&args);
 54	} else {
 55		w->loadConfig();
 56	}
 57	freeArguments(&args);
 58
 59	Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("videoDriver").toInt()));
 60	AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
 61	w->controller()->reloadAudioDriver();
 62
 63	w->controller()->setMultiplayerController(&m_multiplayer);
 64#ifdef Q_OS_MAC
 65	w->show();
 66#endif
 67}
 68
 69bool GBAApp::event(QEvent* event) {
 70	if (event->type() == QEvent::FileOpen) {
 71		m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
 72		return true;
 73	}
 74	return QApplication::event(event);
 75}
 76
 77Window* GBAApp::newWindow() {
 78	if (m_multiplayer.attached() >= MAX_GBAS) {
 79		return nullptr;
 80	}
 81	Window* w = new Window(&m_configController, m_multiplayer.attached());
 82	m_windows[m_multiplayer.attached()] = w;
 83	w->setAttribute(Qt::WA_DeleteOnClose);
 84#ifndef Q_OS_MAC
 85	w->show();
 86#endif
 87	w->loadConfig();
 88	w->controller()->setMultiplayerController(&m_multiplayer);
 89#ifdef Q_OS_MAC
 90	w->show();
 91#endif
 92	return w;
 93}
 94
 95GBAApp* GBAApp::app() {
 96	return g_app;
 97}
 98
 99void GBAApp::interruptAll() {
100	for (int i = 0; i < MAX_GBAS; ++i) {
101		if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
102			continue;
103		}
104		m_windows[i]->controller()->threadInterrupt();
105	}
106}
107
108void GBAApp::continueAll() {
109	for (int i = 0; i < MAX_GBAS; ++i) {
110		if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
111			continue;
112		}
113		m_windows[i]->controller()->threadContinue();
114	}
115}
116
117QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
118	interruptAll();
119	QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
120	continueAll();
121	if (!filename.isEmpty()) {
122		m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
123	}
124	return filename;
125}
126
127QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
128	interruptAll();
129	QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
130	continueAll();
131	if (!filename.isEmpty()) {
132		m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
133	}
134	return filename;
135}
136
137QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
138	FileDialog* dialog = new FileDialog(this, owner, title, filter);
139	dialog->setAcceptMode(QFileDialog::AcceptOpen);
140	return dialog;
141}
142
143QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
144	FileDialog* dialog = new FileDialog(this, owner, title, filter);
145	dialog->setAcceptMode(QFileDialog::AcceptSave);
146	return dialog;
147}
148
149GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
150	: QFileDialog(parent, caption, app->m_configController.getQtOption("lastDirectory").toString(), filter)
151	, m_app(app)
152{
153}
154
155int GBAApp::FileDialog::exec() {
156	m_app->interruptAll();
157	bool didAccept = QFileDialog::exec() == QDialog::Accepted;
158	QStringList filenames = selectedFiles();
159	if (!filenames.isEmpty()) {
160		m_app->m_configController.setQtOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
161	}
162	m_app->continueAll();
163	return didAccept;
164}