main/core: changed code so that headless would run truly headless with QCoreApplication instead of QApplication

QApplication is designed for GUI/widget programs and thus fails to launch
if no window system is available. For a true headless mode, we must swap
out with QCoreApplication. However, what complicates this is that
QCommandLineParser - the class we use to determine whether we should run
in GUI mode or not - requires an active application instance to work.
This creates an unfortunate catch-22 that ultimately ended in writing a
custom command line parser.

Now we can correctly swap out with QCoreApplication, however this has
exposed other issues regarding functions that unnecessarily rely on QWidget
functions. So they'll need to be sorted out eventually.
This commit is contained in:
itsmattkc
2020-08-22 00:22:19 +10:00
parent 9aabde303e
commit 48ac8ea509
6 changed files with 450 additions and 122 deletions
+2
View File
@@ -21,6 +21,8 @@ set(OLIVE_SOURCES
common/cancelableobject.h
common/channellayout.h
common/clamp.h
common/commandlineparser.h
common/commandlineparser.cpp
common/crashhandler.h
common/crashhandler.cpp
common/crashpadinterface.cpp
+149
View File
@@ -0,0 +1,149 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "commandlineparser.h"
#include <QCoreApplication>
#include <QDebug>
CommandLineParser::~CommandLineParser()
{
foreach (const KnownOption& o, options_) {
delete o.option;
}
foreach (const KnownPositionalArgument& a, positional_args_) {
delete a.option;
}
}
const CommandLineParser::Option *CommandLineParser::AddOption(const QStringList &strings, const QString &description)
{
Option* o = new Option();
options_.append({strings, description, o});
return o;
}
const CommandLineParser::PositionalArgument *CommandLineParser::AddPositionalArgument(const QString &name, const QString &description, bool required)
{
PositionalArgument* a = new PositionalArgument();
positional_args_.append({name, description, a, required});
return a;
}
void CommandLineParser::Process(int argc, char **argv)
{
int positional_index = 0;
for (int i=1; i<argc; i++) {
if (argv[i][0] == '-') {
// Must be an option
// Skip past first dashes
const char* arg_basename = &argv[i][1];
bool matched_known = false;
for (int j=0; j<options_.size(); j++) {
KnownOption& o = options_[j];
foreach (const QString& s, o.args) {
if (!s.compare(arg_basename, Qt::CaseInsensitive)) {
// Flag discovered!
o.option->Set();
matched_known = true;
goto found_flag;
}
}
}
found_flag:
if (!matched_known) {
qWarning() << "Unknown parameter:" << argv[i];
}
} else {
// Must be a positional flag
if (positional_index < positional_args_.size()) {
positional_args_[positional_index].option->SetSetting(argv[i]);
positional_index++;
} else {
qWarning() << "Unknown parameter:" << argv[i];
}
}
}
}
void CommandLineParser::PrintHelp(const char* filename)
{
printf("%s %s\n",
QCoreApplication::applicationName().toUtf8().constData(),
QCoreApplication::applicationVersion().toUtf8().constData());
printf("Copyright (C) 2018-2020 Olive Team\n");
QString positional_args;
for (int i=0; i<positional_args_.size(); i++) {
if (i > 0) {
positional_args.append(' ');
}
positional_args.append('[');
positional_args.append(positional_args_.at(i).name);
positional_args.append(']');
}
const char* basename;
#ifdef Q_OS_WINDOWS
basename = strrchr(filename, '\\');
if (!basename) {
basename = strrchr(filename, '/');
}
#else
basename = strrchr(filename, '/');
#endif
printf("Usage: %s [options] %s\n\n", basename + 1, positional_args.toUtf8().constData());
foreach (const KnownOption& o, options_) {
QString all_args;
for (int i=0; i<o.args.size(); i++) {
if (i > 0) {
all_args.append(QStringLiteral(", "));
}
const QString& this_arg = o.args.at(i);
all_args.append('-');
all_args.append(this_arg);
}
printf(" %s\n", all_args.toUtf8().constData());
printf(" %s\n\n", o.description.toUtf8().constData());
}
printf("\n");
}
+107
View File
@@ -0,0 +1,107 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef COMMANDLINEPARSER_H
#define COMMANDLINEPARSER_H
#include <QStringList>
#include "common/define.h"
class CommandLineParser
{
public:
~CommandLineParser();
DISABLE_COPY_MOVE(CommandLineParser);
class Option {
public:
Option()
{
is_set_ = false;
}
bool IsSet() const
{
return is_set_;
}
void Set()
{
is_set_ = true;
}
private:
bool is_set_;
};
class PositionalArgument
{
public:
PositionalArgument() = default;
const QString& GetSetting() const
{
return setting_;
}
void SetSetting(const QString& s)
{
setting_ = s;
}
private:
QString setting_;
};
CommandLineParser() = default;
const Option* AddOption(const QStringList& strings, const QString& description);
const PositionalArgument* AddPositionalArgument(const QString& name, const QString& description, bool required = false);
void Process(int argc, char** argv);
void PrintHelp(const char* filename);
private:
struct KnownOption {
QStringList args;
QString description;
Option* option;
};
struct KnownPositionalArgument {
QString name;
QString description;
PositionalArgument* option;
bool required;
};
QVector<KnownOption> options_;
QVector<KnownPositionalArgument> positional_args_;
};
#endif // COMMANDLINEPARSER_H