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 "platform/commandline.h"
19#include "util/socket.h"
20}
21
22using namespace QGBA;
23
24static GBAApp* g_app = nullptr;
25
26GBAApp::GBAApp(int& argc, char* argv[])
27 : QApplication(argc, argv)
28 , m_windows{}
29{
30 g_app = this;
31
32#ifdef BUILD_SDL
33 SDL_Init(SDL_INIT_NOPARACHUTE);
34#endif
35
36 setWindowIcon(QIcon(":/res/mgba-1024.png"));
37
38 SocketSubsystemInit();
39 qRegisterMetaType<const uint32_t*>("const uint32_t*");
40
41 QApplication::setApplicationName(projectName);
42 QApplication::setApplicationVersion(projectVersion);
43
44 if (!m_configController.getQtOption("displayDriver").isNull()) {
45 Display::setDriver(static_cast<Display::Driver>(m_configController.getQtOption("displayDriver").toInt()));
46 }
47
48 Window* w = new Window(&m_configController);
49 connect(w, &Window::destroyed, [this]() {
50 m_windows[0] = nullptr;
51 });
52 m_windows[0] = w;
53
54#ifndef Q_OS_MAC
55 w->show();
56#endif
57
58 GBAArguments args;
59 if (m_configController.parseArguments(&args, argc, argv)) {
60 w->argumentsPassed(&args);
61 } else {
62 w->loadConfig();
63 }
64 freeArguments(&args);
65
66 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
67 w->controller()->reloadAudioDriver();
68
69 w->controller()->setMultiplayerController(&m_multiplayer);
70#ifdef Q_OS_MAC
71 w->show();
72#endif
73}
74
75bool GBAApp::event(QEvent* event) {
76 if (event->type() == QEvent::FileOpen) {
77 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
78 return true;
79 }
80 return QApplication::event(event);
81}
82
83Window* GBAApp::newWindow() {
84 if (m_multiplayer.attached() >= MAX_GBAS) {
85 return nullptr;
86 }
87 Window* w = new Window(&m_configController, m_multiplayer.attached());
88 int windowId = m_multiplayer.attached();
89 connect(w, &Window::destroyed, [this, windowId]() {
90 m_windows[windowId] = nullptr;
91 });
92 m_windows[windowId] = w;
93 w->setAttribute(Qt::WA_DeleteOnClose);
94#ifndef Q_OS_MAC
95 w->show();
96#endif
97 w->loadConfig();
98 w->controller()->setMultiplayerController(&m_multiplayer);
99#ifdef Q_OS_MAC
100 w->show();
101#endif
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}