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