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
13#include <QFileInfo>
14#include <QFileOpenEvent>
15#include <QIcon>
16
17extern "C" {
18#include "gba/supervisor/thread.h"
19#include "platform/commandline.h"
20#include "util/socket.h"
21}
22
23using namespace QGBA;
24
25static GBAApp* g_app = nullptr;
26
27GBAApp::GBAApp(int& argc, char* argv[])
28 : QApplication(argc, argv)
29 , m_windows{}
30{
31 g_app = this;
32
33#ifdef BUILD_SDL
34 SDL_Init(SDL_INIT_NOPARACHUTE);
35#endif
36
37#ifndef Q_OS_MAC
38 setWindowIcon(QIcon(":/res/mgba-1024.png"));
39#endif
40
41 SocketSubsystemInit();
42 qRegisterMetaType<const uint32_t*>("const uint32_t*");
43 qRegisterMetaType<GBAThread*>("GBAThread*");
44
45 QApplication::setApplicationName(projectName);
46 QApplication::setApplicationVersion(projectVersion);
47
48 if (!m_configController.getQtOption("displayDriver").isNull()) {
49 Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
50 }
51
52 GBAArguments args;
53 bool loaded = m_configController.parseArguments(&args, argc, argv);
54 if (loaded && args.showHelp) {
55 usage(argv[0], 0);
56 ::exit(0);
57 return;
58 }
59
60 if (!m_configController.getQtOption("audioDriver").isNull()) {
61 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
62 }
63 Window* w = new Window(&m_configController);
64 connect(w, &Window::destroyed, [this]() {
65 m_windows[0] = nullptr;
66 });
67 m_windows[0] = w;
68
69 if (loaded) {
70 w->argumentsPassed(&args);
71 } else {
72 w->loadConfig();
73 }
74 freeArguments(&args);
75 w->show();
76
77 w->controller()->setMultiplayerController(&m_multiplayer);
78}
79
80bool GBAApp::event(QEvent* event) {
81 if (event->type() == QEvent::FileOpen) {
82 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
83 return true;
84 }
85 return QApplication::event(event);
86}
87
88Window* GBAApp::newWindow() {
89 if (m_multiplayer.attached() >= MAX_GBAS) {
90 return nullptr;
91 }
92 Window* w = new Window(&m_configController, m_multiplayer.attached());
93 int windowId = m_multiplayer.attached();
94 connect(w, &Window::destroyed, [this, windowId]() {
95 m_windows[windowId] = nullptr;
96 });
97 m_windows[windowId] = w;
98 w->setAttribute(Qt::WA_DeleteOnClose);
99 w->loadConfig();
100 w->show();
101 w->controller()->setMultiplayerController(&m_multiplayer);
102 return w;
103}
104
105GBAApp* GBAApp::app() {
106 return g_app;
107}
108
109void GBAApp::interruptAll() {
110 for (int i = 0; i < MAX_GBAS; ++i) {
111 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
112 continue;
113 }
114 m_windows[i]->controller()->threadInterrupt();
115 }
116}
117
118void GBAApp::continueAll() {
119 for (int i = 0; i < MAX_GBAS; ++i) {
120 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
121 continue;
122 }
123 m_windows[i]->controller()->threadContinue();
124 }
125}
126
127QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
128 interruptAll();
129 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
130 continueAll();
131 if (!filename.isEmpty()) {
132 m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
133 }
134 return filename;
135}
136
137QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
138 interruptAll();
139 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
140 continueAll();
141 if (!filename.isEmpty()) {
142 m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
143 }
144 return filename;
145}
146
147QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
148 FileDialog* dialog = new FileDialog(this, owner, title, filter);
149 dialog->setAcceptMode(QFileDialog::AcceptOpen);
150 return dialog;
151}
152
153QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
154 FileDialog* dialog = new FileDialog(this, owner, title, filter);
155 dialog->setAcceptMode(QFileDialog::AcceptSave);
156 return dialog;
157}
158
159GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
160 : QFileDialog(parent, caption, app->m_configController.getQtOption("lastDirectory").toString(), filter)
161 , m_app(app)
162{
163}
164
165int GBAApp::FileDialog::exec() {
166 m_app->interruptAll();
167 bool didAccept = QFileDialog::exec() == QDialog::Accepted;
168 QStringList filenames = selectedFiles();
169 if (!filenames.isEmpty()) {
170 m_app->m_configController.setQtOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
171 }
172 m_app->continueAll();
173 return didAccept;
174}