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