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