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