core: add pragmas

UseQApplication: use QApplication over QGuiApplication (for qqc2-desktop-style)
NativeTextRendering: use NativeTextRendering over QtRendering for text
Env VAR = VAL: define environment variables (usually qt ones)
This commit is contained in:
outfoxxed 2024-04-07 22:30:37 -07:00
parent c0847366dd
commit 082c3c480f
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
2 changed files with 245 additions and 175 deletions

View file

@ -57,8 +57,8 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
set(QT_DEPS Qt6::Gui Qt6::Qml Qt6::Quick Qt6::QuickControls2)
set(QT_FPDEPS Gui Qml Quick QuickControls2)
set(QT_DEPS Qt6::Gui Qt6::Qml Qt6::Quick Qt6::QuickControls2 Qt6::Widgets)
set(QT_FPDEPS Gui Qml Quick QuickControls2 Widgets)
if (BUILD_TESTING)
enable_testing()

View file

@ -1,11 +1,14 @@
#include "main.hpp"
#include <iostream>
#include <qapplication.h>
#include <qcommandlineoption.h>
#include <qcommandlineparser.h>
#include <qcoreapplication.h>
#include <qdir.h>
#include <qfileinfo.h>
#include <qguiapplication.h>
#include <qhash.h>
#include <qlogging.h>
#include <qobject.h>
#include <qquickwindow.h>
@ -19,9 +22,17 @@
#include "rootwrapper.hpp"
int qs_main(int argc, char** argv) {
const auto app = QGuiApplication(argc, argv);
QGuiApplication::setApplicationName("quickshell");
QGuiApplication::setApplicationVersion("0.1.0 (" GIT_REVISION ")");
QString configFilePath;
QString workingDirectory;
auto useQApplication = false;
auto nativeTextRendering = false;
QHash<QString, QString> envOverrides;
{
const auto app = QCoreApplication(argc, argv);
QCoreApplication::setApplicationName("quickshell");
QCoreApplication::setApplicationVersion("0.1.0 (" GIT_REVISION ")");
QCommandLineParser parser;
parser.addHelpOption();
@ -42,7 +53,6 @@ int qs_main(int argc, char** argv) {
parser.addOption(workdirOption);
parser.process(app);
QString configFilePath;
{
auto printCurrent = parser.isSet(currentOption);
@ -138,7 +148,7 @@ int qs_main(int argc, char** argv) {
foundname:;
if (configPathLevel == 0 && configNameLevel == 0) {
qFatal() << "Pass only one of --path or --config";
qCritical() << "Pass only one of --path or --config";
return -1;
}
@ -156,7 +166,8 @@ int qs_main(int argc, char** argv) {
auto split = line.split('=');
if (split.length() != 2) {
qFatal() << "manifest line not in expected format 'name = relativepath':" << line;
qCritical() << "manifest line not in expected format 'name = relativepath':"
<< line;
return -1;
}
@ -166,10 +177,10 @@ int qs_main(int argc, char** argv) {
}
}
qFatal() << "configuration" << configName << "not found in manifest" << manifestPath;
qCritical() << "configuration" << configName << "not found in manifest" << manifestPath;
return -1;
} else if (manifestPathLevel < 2) {
qFatal() << "cannot open config manifest at" << manifestPath;
qCritical() << "cannot open config manifest at" << manifestPath;
return -1;
}
}
@ -177,10 +188,10 @@ int qs_main(int argc, char** argv) {
{
auto basePathInfo = QFileInfo(basePath);
if (!basePathInfo.exists()) {
qFatal() << "base path does not exist:" << basePath;
qCritical() << "base path does not exist:" << basePath;
return -1;
} else if (!QFileInfo(basePathInfo.canonicalFilePath()).isDir()) {
qFatal() << "base path is not a directory" << basePath;
qCritical() << "base path is not a directory" << basePath;
return -1;
}
@ -192,7 +203,7 @@ int qs_main(int argc, char** argv) {
}
}
qFatal() << "no directory named " << configName << "found in base path" << basePath;
qCritical() << "no directory named " << configName << "found in base path" << basePath;
return -1;
}
haspath:;
@ -202,7 +213,7 @@ int qs_main(int argc, char** argv) {
auto configFile = QFileInfo(configFilePath);
if (!configFile.exists()) {
qFatal() << "config path does not exist:" << configFilePath;
qCritical() << "config path does not exist:" << configFilePath;
return -1;
}
@ -212,20 +223,20 @@ int qs_main(int argc, char** argv) {
configFile = QFileInfo(configFilePath);
if (!configFile.exists()) {
qFatal() << "no shell.qml found in config path:" << configFilePath;
qCritical() << "no shell.qml found in config path:" << configFilePath;
return -1;
} else if (configFile.isDir()) {
qFatal() << "shell.qml is a directory:" << configFilePath;
qCritical() << "shell.qml is a directory:" << configFilePath;
return -1;
}
configFilePath = QFileInfo(configFilePath).canonicalFilePath();
configFile = QFileInfo(configFilePath);
if (!configFile.exists()) {
qFatal() << "config file does not exist:" << configFilePath;
qCritical() << "config file does not exist:" << configFilePath;
return -1;
} else if (configFile.isDir()) {
qFatal() << "config file is a directory:" << configFilePath;
qCritical() << "config file is a directory:" << configFilePath;
return -1;
}
@ -243,7 +254,62 @@ int qs_main(int argc, char** argv) {
}
if (parser.isSet(workdirOption)) {
QDir::setCurrent(parser.value(workdirOption));
workingDirectory = parser.value(workdirOption);
}
auto file = QFile(configFilePath);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qCritical() << "could not open config file";
return -1;
}
auto stream = QTextStream(&file);
while (!stream.atEnd()) {
auto line = stream.readLine().trimmed();
if (line.startsWith("//@ pragma ")) {
auto pragma = line.sliced(11).trimmed();
if (pragma == "UseQApplication") useQApplication = true;
else if (pragma == "NativeTextRendering") nativeTextRendering = true;
else if (pragma.startsWith("Env ")) {
auto envPragma = pragma.sliced(4);
auto splitIdx = envPragma.indexOf('=');
if (splitIdx == -1) {
qCritical() << "Env pragma" << pragma << "not in the form 'VAR = VALUE'";
return -1;
}
auto var = envPragma.sliced(0, splitIdx).trimmed();
auto val = envPragma.sliced(splitIdx + 1).trimmed();
envOverrides.insert(var, val);
} else {
qCritical() << "Unrecognized pragma" << pragma;
return -1;
}
} else if (line.startsWith("import")) break;
}
file.close();
}
for (auto [var, val]: envOverrides.asKeyValueRange()) {
qputenv(var.toUtf8(), val.toUtf8());
}
QCoreApplication* app = nullptr;
if (useQApplication) {
app = new QApplication(argc, argv);
} else {
app = new QGuiApplication(argc, argv);
}
QCoreApplication::setApplicationName("quickshell");
QCoreApplication::setApplicationVersion("0.1.0 (" GIT_REVISION ")");
if (!workingDirectory.isEmpty()) {
QDir::setCurrent(workingDirectory);
}
QuickshellPlugin::initPlugins();
@ -252,8 +318,12 @@ int qs_main(int argc, char** argv) {
// Use a fully transparent window with a colored rect.
QQuickWindow::setDefaultAlphaBuffer(true);
if (nativeTextRendering) {
QQuickWindow::setTextRenderType(QQuickWindow::NativeTextRendering);
}
auto root = RootWrapper(configFilePath);
QGuiApplication::setQuitOnLastWindowClosed(false);
return QGuiApplication::exec();
return QCoreApplication::exec();
}