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#include "VFileDevice.h"
13
14#include <QFileInfo>
15#include <QFileOpenEvent>
16#include <QIcon>
17#include <QTranslator>
18
19#include <mgba/core/version.h>
20#include <mgba/internal/gba/video.h>
21#include <mgba-util/socket.h>
22#include <mgba-util/vfs.h>
23
24#ifdef USE_SQLITE3
25#include "feature/sqlite3/no-intro.h"
26#endif
27
28using namespace QGBA;
29
30static GBAApp* g_app = nullptr;
31
32mLOG_DEFINE_CATEGORY(QT, "Qt");
33
34GBAApp::GBAApp(int& argc, char* argv[])
35 : QApplication(argc, argv)
36 , m_windows{}
37 , m_db(nullptr)
38{
39 g_app = this;
40
41#ifdef BUILD_SDL
42 SDL_Init(SDL_INIT_NOPARACHUTE);
43#endif
44
45#ifndef Q_OS_MAC
46 setWindowIcon(QIcon(":/res/mgba-1024.png"));
47#endif
48
49 QTranslator* translator = new QTranslator(this);
50 if (translator->load(QLocale(), QLatin1String(binaryName), QLatin1String("-"), QLatin1String(":/translations"))) {
51 installTranslator(translator);
52 }
53
54
55 SocketSubsystemInit();
56 qRegisterMetaType<const uint32_t*>("const uint32_t*");
57 qRegisterMetaType<mCoreThread*>("mCoreThread*");
58
59 QApplication::setApplicationName(projectName);
60 QApplication::setApplicationVersion(projectVersion);
61
62 if (!m_configController.getQtOption("displayDriver").isNull()) {
63 Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
64 }
65
66 mArguments args;
67 mGraphicsOpts graphicsOpts;
68 mSubParser subparser;
69 initParserForGraphics(&subparser, &graphicsOpts);
70 bool loaded = m_configController.parseArguments(&args, argc, argv, &subparser);
71 if (loaded && args.showHelp) {
72 usage(argv[0], subparser.usage);
73 ::exit(0);
74 return;
75 }
76
77 reloadGameDB();
78
79 if (!m_configController.getQtOption("audioDriver").isNull()) {
80 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
81 }
82 Window* w = new Window(&m_configController);
83 connect(w, &Window::destroyed, [this]() {
84 m_windows[0] = nullptr;
85 });
86 m_windows[0] = w;
87
88 if (loaded) {
89 w->argumentsPassed(&args);
90 } else {
91 w->loadConfig();
92 }
93 freeArguments(&args);
94
95 if (graphicsOpts.multiplier) {
96 w->resizeFrame(QSize(VIDEO_HORIZONTAL_PIXELS * graphicsOpts.multiplier, VIDEO_VERTICAL_PIXELS * graphicsOpts.multiplier));
97 }
98 if (graphicsOpts.fullscreen) {
99 w->enterFullScreen();
100 }
101
102 w->show();
103
104 w->controller()->setMultiplayerController(&m_multiplayer);
105 w->multiplayerChanged();
106}
107
108bool GBAApp::event(QEvent* event) {
109 if (event->type() == QEvent::FileOpen) {
110 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
111 return true;
112 }
113 return QApplication::event(event);
114}
115
116Window* GBAApp::newWindow() {
117 if (m_multiplayer.attached() >= MAX_GBAS) {
118 return nullptr;
119 }
120 Window* w = new Window(&m_configController, m_multiplayer.attached());
121 int windowId = m_multiplayer.attached();
122 connect(w, &Window::destroyed, [this, windowId]() {
123 m_windows[windowId] = nullptr;
124 });
125 m_windows[windowId] = w;
126 w->setAttribute(Qt::WA_DeleteOnClose);
127 w->loadConfig();
128 w->show();
129 w->controller()->setMultiplayerController(&m_multiplayer);
130 w->multiplayerChanged();
131 return w;
132}
133
134GBAApp* GBAApp::app() {
135 return g_app;
136}
137
138void GBAApp::pauseAll(QList<int>* paused) {
139 for (int i = 0; i < MAX_GBAS; ++i) {
140 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded() || m_windows[i]->controller()->isPaused()) {
141 continue;
142 }
143 m_windows[i]->controller()->setPaused(true);
144 paused->append(i);
145 }
146}
147
148void GBAApp::continueAll(const QList<int>* paused) {
149 for (int i : *paused) {
150 m_windows[i]->controller()->setPaused(false);
151 }
152}
153
154QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
155 QList<int> paused;
156 pauseAll(&paused);
157 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
158 continueAll(&paused);
159 if (!filename.isEmpty()) {
160 m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
161 }
162 return filename;
163}
164
165QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
166 QList<int> paused;
167 pauseAll(&paused);
168 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getOption("lastDirectory"), filter);
169 continueAll(&paused);
170 if (!filename.isEmpty()) {
171 m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
172 }
173 return filename;
174}
175
176QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
177 QList<int> paused;
178 pauseAll(&paused);
179 QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController.getOption("lastDirectory"));
180 continueAll(&paused);
181 if (!filename.isEmpty()) {
182 m_configController.setOption("lastDirectory", QFileInfo(filename).dir().path());
183 }
184 return filename;
185}
186
187QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
188 FileDialog* dialog = new FileDialog(this, owner, title, filter);
189 dialog->setAcceptMode(QFileDialog::AcceptOpen);
190 return dialog;
191}
192
193QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
194 FileDialog* dialog = new FileDialog(this, owner, title, filter);
195 dialog->setAcceptMode(QFileDialog::AcceptSave);
196 return dialog;
197}
198
199QString GBAApp::dataDir() {
200#ifdef DATADIR
201 QString path = QString::fromUtf8(DATADIR);
202#else
203 QString path = QCoreApplication::applicationDirPath();
204#ifdef Q_OS_MAC
205 path += QLatin1String("/../Resources");
206#endif
207#endif
208 return path;
209}
210
211bool GBAApp::reloadGameDB() {
212#ifdef USE_SQLITE3
213 NoIntroDB* db = nullptr;
214 db = NoIntroDBLoad((m_configController.configDir() + "/nointro.sqlite3").toLocal8Bit().constData());
215 if (db && m_db) {
216 NoIntroDBDestroy(m_db);
217 }
218 if (db) {
219 VFile* vf = VFileDevice::open(dataDir() + "/nointro.dat", O_RDONLY);
220 if (vf) {
221 NoIntroDBLoadClrMamePro(db, vf);
222 vf->close(vf);
223 }
224 m_db = db;
225 return true;
226 }
227#endif
228 return false;
229}
230
231GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
232 : QFileDialog(parent, caption, app->m_configController.getOption("lastDirectory"), filter)
233 , m_app(app)
234{
235}
236
237int GBAApp::FileDialog::exec() {
238 QList<int> paused;
239 m_app->pauseAll(&paused);
240 bool didAccept = QFileDialog::exec() == QDialog::Accepted;
241 QStringList filenames = selectedFiles();
242 if (!filenames.isEmpty()) {
243 m_app->m_configController.setOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
244 }
245 m_app->continueAll(&paused);
246 return didAccept;
247}