Debian Codegraph @

garglk/launchqt.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 //
2 // Copyright (C) 2006-2009 by Tor Andersson.
3 // Copyright (C) 2009 by Baltasar García Perez-Schofield.
4 // Copyright (C) 2010 by Ben Cressey.
5 // Copyright (C) 2021 by Chris Spiegel.
6 //
7 // This file is part of Gargoyle.
8 //
9 // Gargoyle is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Gargoyle is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Gargoyle; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 #include <algorithm>
24 #include <cstdlib>
25 #include <iostream>
26 #include <iterator>
27 #include <string>
28
29 #include <QApplication>
30 #include <QCommandLineParser>
31 #include <QDir>
32 #include <QFile>
33 #include <QFileDialog>
34 #include <QList>
35 #include <QMessageBox>
36 #include <QProcess>
37 #include <QPushButton>
38 #include <QString>
39 #include <QStringList>
40 #include <QVector>
41
42 #include "garglk.h"
43 #include "garversion.h"
44 #include "launcher.h"
45
46 #include GARGLKINI_H
47
48 static const char *AppName = "Gargoyle " GARGOYLE_VERSION;
49
50 namespace {
51
52 class Filter {
53 public:
54 Filter(QString name, QStringList extensions) : m_name(std::move(name)), m_extensions(std::move(extensions)) {}
55
56 QString format() const {
57 return QString("%1 Games (%2)")
58 .arg(m_name, format_extensions().join(" "));
59 }
60
61 QStringList format_extensions() const {
62 QList<QString> mapped_extensions;
63 std::transform(m_extensions.begin(), m_extensions.end(),
64 std::back_inserter(mapped_extensions),
65 [](const QString &ext) { return QString("*.") + ext; });
66 return mapped_extensions;
67 }
68
69 private:
70 QString m_name;
71 QStringList m_extensions;
72 };
73
74 }
75
76 void garglk::winmsg(const std::string &msg)
77 {
78 QMessageBox::critical(nullptr, "Error", msg.c_str());
79 }
80
81 static QString winbrowsefile()
82 {
83 const QVector<Filter> filters = {
84 Filter("Adrift", {"taf"}),
85 Filter("AdvSys", {"dat"}),
86 Filter("AGT", {"agx", "d$$"}),
87 Filter("Alan", {"acd", "a3c"}),
88 Filter("Glulx", {"ulx", "blb", "blorb", "glb", "gblorb"}),
89 Filter("Hugo", {"hex"}),
90 Filter("JACL", {"jacl", "j2"}),
91 Filter("Level 9", {"l9", "sna"}),
92 Filter("Magnetic Scrolls", {"mag"}),
93 Filter("TADS", {"gam", "t3"}),
94 Filter("Z-code", {"z1", "z2", "z3", "z4", "z5", "z6", "z7", "z8", "zlb", "zblorb"}),
95 };
96 QList<QString> mapped_filters;
97 std::transform(filters.begin(), filters.end(),
98 std::back_inserter(mapped_filters),
99 [](const Filter &filter) { return filter.format(); });
100
101 QStringList all_extensions;
102 for (const auto &filter : filters) {
103 all_extensions << filter.format_extensions();
104 }
105
106 QString filter_string = QString("All Games (%1);;All Files (*);;%2")
107 .arg(all_extensions.join(" "), mapped_filters.join(";;"));
108
109 // Hide filter details because the sheer number in "All Games" makes
110 // the dialog ridiculously wide (Qt probably should cut it off, but
111 // it doesn't, so try to compensate here).
112 QFileDialog::Options options(QFileDialog::HideNameFilterDetails);
113 #ifdef GARGLK_CONFIG_NO_NATIVE_FILE_DIALOGS
114 options |= QFileDialog::DontUseNativeDialog;
115 #endif
116 return QFileDialog::getOpenFileName(nullptr, AppName, "", filter_string, nullptr, options);
117 }
118
119 bool garglk::winterp(const std::string &exe, const std::vector<std::string> &flags, const std::string &game)
120 {
121 // Find the directory that contains the interpreters. By default
122 // this is GARGLK_CONFIG_INTERPRETER_DIR but if that is not set, it
123 // is the containing directory of the gargoyle executable.
124 //
125 // For development purposes, the environment variable
126 // $GARGLK_INTERPRETER_DIR can be set to the interpreter build
127 // directory to allow the gargoyle binary to load the newly-built
128 // interpreters instead of the system-wide interpreters (or instead
129 // of failing if there are no interpreters installed). If this is
130 // set, the standard directory will *not* be used at all, even if no
131 // interpreter is found.
132 QString interpreter_dir = std::getenv("GARGLK_INTERPRETER_DIR");
133 if (interpreter_dir.isNull()) {
134 #ifdef GARGLK_CONFIG_INTERPRETER_DIR
135 interpreter_dir = GARGLK_CONFIG_INTERPRETER_DIR;
136 #else
137 interpreter_dir = QCoreApplication::applicationDirPath();
138 #endif
139 }
140
141 QString argv0 = QDir(interpreter_dir).absoluteFilePath(exe.c_str());
142
143 QStringList args;
144 for (const auto &flag : flags) {
145 args.push_back(QString::fromStdString(flag));
146 }
147 args.push_back(QString::fromStdString(game));
148
149 QProcess proc;
150 proc.setProcessChannelMode(QProcess::ForwardedChannels);
151 proc.start(argv0, args);
152
153 if (!proc.waitForStarted(5000)) {
154 garglk::winmsg("Could not start interpreter " + argv0.toStdString());
155 return false;
156 }
157
158 proc.waitForFinished(-1);
159
160 if (proc.exitStatus() != QProcess::NormalExit) {
161 return false;
162 } else {
163 return proc.exitCode() == 0;
164 }
165 }
166
167 static QString parse_args(const QApplication &app)
168 {
169 QCommandLineParser parser;
170
171 // Manually add -h to avoid --help-all: don't show Qt-specific
172 // options, as this only affects the launcher. If the user selects a
173 // style, for example, that won't carry on to the interpreter, as
174 // it's a separate program. Qt options are still _supported_ (as
175 // they're passed to QApplication's constructor), but at least don't
176 // advertise their existence.
177 //
178 // The "right" approach would be to synthesize empty arguments for
179 // Qt and then parse arguments with something like getopt_long(),
180 // but that's a GNU extension and would have to be pulled in from
181 // glibc, musl libc, or similar. This is good enough.
182 parser.addOptions({
183 {{"d", "dump-config"}, "Dump the default config file to standard out."},
184 {{"e", "edit-config"}, "Edit the configuration file."},
185 {{"h", "help"}, "Displays help on commandline options."},
186 {{"m", "migrate-config"}, "Move a legacy configuration file to the preferred location."},
187 {{"p", "paths"}, "Displays configuration file and theme paths."},
188 {{"t", "themes"}, "Displays all available color themes."},
189 });
190
191 parser.addVersionOption();
192 parser.addPositionalArgument("STORY", "The story/game file to run. If not provided, a file chooser will be displayed.", "[STORY]");
193 parser.process(app);
194
195 auto positional = parser.positionalArguments();
196
197 if (positional.size() > 1) {
198 std::cerr << "warning: extra positional arguments are ignored." << std::endl;
199 }
200
201 QString gamefile = positional.isEmpty() ?
202 "" :
203 positional.first();
204
205 if (parser.isSet("d")) {
206 std::cout << garglkini;
207 std::exit(0);
208 } else if (parser.isSet("e")) {
209 gli_edit_config();
210 std::exit(0);
211 } else if (parser.isSet("h")) {
212 std::cout << parser.helpText().toStdString() << std::endl;
213 std::exit(0);
214 } else if (parser.isSet("m")) {
215 auto configs = garglk::configs("");
216 configs.erase(std::remove_if(configs.begin(), configs.end(), [](const auto &config) {
217 return config.type != garglk::ConfigFile::Type::User;
218 }), configs.end());
219
220 if (configs.empty()) {
221 std::cerr << "Unable to determine configuration file locations.\n";
222 std::exit(1);
223 }
224
225 auto preferred = QString::fromStdString(configs.front().path);
226 if (QFile::exists(preferred)) {
227 std::cout << "Preferred configuration file " << preferred.toStdString() << " already exists.\n";
228 } else {
229 std::vector<garglk::ConfigFile> existing;
230
231 std::copy_if(configs.begin(), configs.end(), std::back_inserter(existing), [&preferred](const auto &config) {
232 auto path = QString::fromStdString(config.path);
233 return path != preferred && QFile::exists(path);
234 });
235
236 if (existing.empty()) {
237 std::cout << "No existing configuration files found.\n";
238 } else if (existing.size() != 1) {
239 std::cout << "Won't migrate, found multiple existing configuration files:\n\n";
240 for (const auto &config : existing) {
241 std::cout << config.path << std::endl;
242 }
243 } else {
244 auto old = existing.front().path;
245 std::cout << "Renaming " << old << " to " << preferred.toStdString() << std::endl;
246 QFile file(QString::fromStdString(old));
247 if (!file.rename(preferred)) {
248 std::cerr << "Unable to rename file: " << file.errorString().toStdString() << std::endl;
249 std::exit(1);
250 }
251 }
252 }
253
254 std::exit(0);
255 } else if (parser.isSet("p")) {
256 // Convert to native separators and return absolute path.
257 auto canonicalize = [](const std::string &path) {
258 auto qpath = QString::fromStdString(path);
259 qpath = QDir(qpath).absolutePath();
260 return QDir::toNativeSeparators(qpath).toStdString();
261 };
262
263 std::cout << "Configuration file paths:\n\n";
264 for (const auto &config : garglk::configs(gamefile.toStdString())) {
265 auto path = canonicalize(config.path);
266 auto type = QString::fromStdString(config.format_type());
267
268 std::cout << path << " " << type.toStdString() << std::endl;
269 }
270
271 std::cout << "\nTheme paths:\n\n";
272 auto theme_paths = garglk::theme::paths();
273 std::reverse(theme_paths.begin(), theme_paths.end());
274 for (const auto &path : theme_paths) {
275 std::cout << canonicalize(path) << std::endl;
276 }
277
278 std::exit(0);
279 } else if (parser.isSet("t")) {
280 garglk::theme::init();
281 for (const auto &theme_name : garglk::theme::names()) {
282 std::cout << theme_name << std::endl;
283 }
284
285 std::exit(0);
286 }
287
288 return gamefile;
289 }
290
291 int main(int argc, char **argv)
292 {
293 QApplication app(argc, argv);
294
295 QApplication::setApplicationName("gargoyle");
296 QApplication::setApplicationVersion(GARGOYLE_VERSION);
297
298 auto story = parse_args(app);
299
300 if (story.isEmpty()) {
301 story = winbrowsefile();
302 }
303
304 if (story.isEmpty()) {
305 return 1;
306 }
307
308 // run story file
309 return garglk::rungame(story.toStdString()) ? 0 : 1;
310 }