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 "ConfigController.h"
10#include "Display.h"
11#include "GameController.h"
12#include "Window.h"
13#include "VFileDevice.h"
14
15#include <QFileInfo>
16#include <QFileOpenEvent>
17#include <QIcon>
18
19#include <mgba/core/version.h>
20#include <mgba-util/socket.h>
21#include <mgba-util/vfs.h>
22
23#ifdef USE_SQLITE3
24#include "feature/sqlite3/no-intro.h"
25#endif
26
27using namespace QGBA;
28
29static GBAApp* g_app = nullptr;
30
31mLOG_DEFINE_CATEGORY(QT, "Qt", "platform.qt");
32
33GBAApp::GBAApp(int& argc, char* argv[], ConfigController* config)
34 : QApplication(argc, argv)
35 , m_configController(config)
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-512.png"));
45#endif
46
47 SocketSubsystemInit();
48 qRegisterMetaType<const uint32_t*>("const uint32_t*");
49 qRegisterMetaType<mCoreThread*>("mCoreThread*");
50
51 QApplication::setApplicationName(projectName);
52 QApplication::setApplicationVersion(projectVersion);
53
54 if (!m_configController->getQtOption("displayDriver").isNull()) {
55 Display::setDriver(static_cast<Display::Driver>(m_configController->getQtOption("displayDriver").toInt()));
56 }
57
58 reloadGameDB();
59
60 if (!m_configController->getQtOption("audioDriver").isNull()) {
61 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController->getQtOption("audioDriver").toInt()));
62 }
63}
64
65GBAApp::~GBAApp() {
66#ifdef USE_SQLITE3
67 m_parseThread.quit();
68 m_parseThread.wait();
69#endif
70}
71
72bool GBAApp::event(QEvent* event) {
73 if (event->type() == QEvent::FileOpen) {
74 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
75 return true;
76 }
77 return QApplication::event(event);
78}
79
80Window* GBAApp::newWindow() {
81 if (m_windows.count() >= MAX_GBAS) {
82 return nullptr;
83 }
84 Window* w = new Window(m_configController, m_multiplayer.attached());
85 int windowId = m_multiplayer.attached();
86 connect(w, &Window::destroyed, [this, w]() {
87 m_windows.removeAll(w);
88 for (Window* w : m_windows) {
89 w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
90 }
91 });
92 m_windows.append(w);
93 w->setAttribute(Qt::WA_DeleteOnClose);
94 w->loadConfig();
95 w->show();
96 w->controller()->setMultiplayerController(&m_multiplayer);
97 w->multiplayerChanged();
98 for (Window* w : m_windows) {
99 w->updateMultiplayerStatus(m_windows.count() < MAX_GBAS);
100 }
101 return w;
102}
103
104GBAApp* GBAApp::app() {
105 return g_app;
106}
107
108void GBAApp::pauseAll(QList<Window*>* paused) {
109 for (auto& window : m_windows) {
110 if (!window->controller()->isLoaded() || window->controller()->isPaused()) {
111 continue;
112 }
113 window->controller()->setPaused(true);
114 paused->append(window);
115 }
116}
117
118void GBAApp::continueAll(const QList<Window*>& paused) {
119 for (auto& window : paused) {
120 window->controller()->setPaused(false);
121 }
122}
123
124QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
125 QList<Window*> paused;
126 pauseAll(&paused);
127 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
128 continueAll(paused);
129 if (!filename.isEmpty()) {
130 m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
131 }
132 return filename;
133}
134
135QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
136 QList<Window*> paused;
137 pauseAll(&paused);
138 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController->getOption("lastDirectory"), filter);
139 continueAll(paused);
140 if (!filename.isEmpty()) {
141 m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
142 }
143 return filename;
144}
145
146QString GBAApp::getOpenDirectoryName(QWidget* owner, const QString& title) {
147 QList<Window*> paused;
148 pauseAll(&paused);
149 QString filename = QFileDialog::getExistingDirectory(owner, title, m_configController->getOption("lastDirectory"));
150 continueAll(paused);
151 if (!filename.isEmpty()) {
152 m_configController->setOption("lastDirectory", QFileInfo(filename).dir().canonicalPath());
153 }
154 return filename;
155}
156
157QString GBAApp::dataDir() {
158#ifdef DATADIR
159 QString path = QString::fromUtf8(DATADIR);
160#else
161 QString path = QCoreApplication::applicationDirPath();
162#ifdef Q_OS_MAC
163 path += QLatin1String("/../Resources");
164#endif
165#endif
166 return path;
167}
168
169#ifdef USE_SQLITE3
170bool GBAApp::reloadGameDB() {
171 NoIntroDB* db = nullptr;
172 db = NoIntroDBLoad((ConfigController::configDir() + "/nointro.sqlite3").toUtf8().constData());
173 if (db && m_db) {
174 NoIntroDBDestroy(m_db);
175 }
176 if (db) {
177 if (m_parseThread.isRunning()) {
178 m_parseThread.quit();
179 m_parseThread.wait();
180 }
181 GameDBParser* parser = new GameDBParser(db);
182 m_parseThread.start();
183 parser->moveToThread(&m_parseThread);
184 QMetaObject::invokeMethod(parser, "parseNoIntroDB");
185 m_db = db;
186 return true;
187 }
188 return false;
189}
190#else
191bool GBAApp::reloadGameDB() {
192 return false;
193}
194#endif
195
196#ifdef USE_SQLITE3
197GameDBParser::GameDBParser(NoIntroDB* db, QObject* parent)
198 : QObject(parent)
199 , m_db(db)
200{
201 // Nothing to do
202}
203
204void GameDBParser::parseNoIntroDB() {
205 VFile* vf = VFileDevice::open(GBAApp::dataDir() + "/nointro.dat", O_RDONLY);
206 if (vf) {
207 NoIntroDBLoadClrMamePro(m_db, vf);
208 vf->close(vf);
209 }
210}
211
212#endif