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 GBAArguments args;
49 bool loaded = m_configController.parseArguments(&args, argc, argv);
50 if (loaded && args.showHelp) {
51 usage(argv[0], 0);
52 ::exit(0);
53 return;
54 }
55
56 AudioProcessor::setDriver(static_cast<AudioProcessor::Driver>(m_configController.getQtOption("audioDriver").toInt()));
57 Window* w = new Window(&m_configController);
58 connect(w, &Window::destroyed, [this]() {
59 m_windows[0] = nullptr;
60 });
61 m_windows[0] = w;
62
63 if (loaded) {
64 w->argumentsPassed(&args);
65 } else {
66 w->loadConfig();
67 }
68 freeArguments(&args);
69 w->show();
70
71 w->controller()->setMultiplayerController(&m_multiplayer);
72}
73
74bool GBAApp::event(QEvent* event) {
75 if (event->type() == QEvent::FileOpen) {
76 m_windows[0]->controller()->loadGame(static_cast<QFileOpenEvent*>(event)->file());
77 return true;
78 }
79 return QApplication::event(event);
80}
81
82Window* GBAApp::newWindow() {
83 if (m_multiplayer.attached() >= MAX_GBAS) {
84 return nullptr;
85 }
86 Window* w = new Window(&m_configController, m_multiplayer.attached());
87 int windowId = m_multiplayer.attached();
88 connect(w, &Window::destroyed, [this, windowId]() {
89 m_windows[windowId] = nullptr;
90 });
91 m_windows[windowId] = w;
92 w->setAttribute(Qt::WA_DeleteOnClose);
93 w->loadConfig();
94 w->show();
95 w->controller()->setMultiplayerController(&m_multiplayer);
96 return w;
97}
98
99GBAApp* GBAApp::app() {
100 return g_app;
101}
102
103void GBAApp::interruptAll() {
104 for (int i = 0; i < MAX_GBAS; ++i) {
105 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
106 continue;
107 }
108 m_windows[i]->controller()->threadInterrupt();
109 }
110}
111
112void GBAApp::continueAll() {
113 for (int i = 0; i < MAX_GBAS; ++i) {
114 if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
115 continue;
116 }
117 m_windows[i]->controller()->threadContinue();
118 }
119}
120
121QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
122 interruptAll();
123 QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
124 continueAll();
125 if (!filename.isEmpty()) {
126 m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
127 }
128 return filename;
129}
130
131QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
132 interruptAll();
133 QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
134 continueAll();
135 if (!filename.isEmpty()) {
136 m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
137 }
138 return filename;
139}
140
141QFileDialog* GBAApp::getOpenFileDialog(QWidget* owner, const QString& title, const QString& filter) {
142 FileDialog* dialog = new FileDialog(this, owner, title, filter);
143 dialog->setAcceptMode(QFileDialog::AcceptOpen);
144 return dialog;
145}
146
147QFileDialog* GBAApp::getSaveFileDialog(QWidget* owner, const QString& title, const QString& filter) {
148 FileDialog* dialog = new FileDialog(this, owner, title, filter);
149 dialog->setAcceptMode(QFileDialog::AcceptSave);
150 return dialog;
151}
152
153GBAApp::FileDialog::FileDialog(GBAApp* app, QWidget* parent, const QString& caption, const QString& filter)
154 : QFileDialog(parent, caption, app->m_configController.getQtOption("lastDirectory").toString(), filter)
155 , m_app(app)
156{
157}
158
159int GBAApp::FileDialog::exec() {
160 m_app->interruptAll();
161 bool didAccept = QFileDialog::exec() == QDialog::Accepted;
162 QStringList filenames = selectedFiles();
163 if (!filenames.isEmpty()) {
164 m_app->m_configController.setQtOption("lastDirectory", QFileInfo(filenames[0]).dir().path());
165 }
166 m_app->continueAll();
167 return didAccept;
168}