diff --git a/multiscreen-compositor/config.cpp b/multiscreen-compositor/config.cpp index 0e70ff387b1bf14d3cb5e8bb977e900c241ea71a..5ddc26cdb4509d2f3f80729968bd1b8442ecf2f7 100644 --- a/multiscreen-compositor/config.cpp +++ b/multiscreen-compositor/config.cpp @@ -1,12 +1,12 @@ #include "config.h" #include <QStringList> #include <QDebug> +#include <QRegularExpression> +#include <QFile> void Config::setRotation(const QString &str) { - QStringList validRotations = {"0", "90", "180", "270"}; - if (!validRotations.contains(str)) - { + if(!rotationIsValid(&str)){ qWarning() << "Can not set invalid rotation: " << str; return; } @@ -20,3 +20,59 @@ QString Config::rotation() const { return m_rotation; } + +const QRegularExpression Config::findRotation("[0-3]"); + +void Config::readRotation (QString path) +{ + QFile file(path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qWarning() << "Could not read file: " << path; + qWarning() << "Use fallback rotation:0"; + setRotation( "0"); + } + + // We send the file content to an QByteArray to use TextStream for ro files + QByteArray bytes = file.readAll(); + file.close(); + QTextStream in(&bytes); + QString line; + QString match="0"; + bool patternFound = false; + while (!in.atEnd()) + { + line = in.readLine(); + QRegularExpressionMatch matched = findRotation.match(line); + if (matched.hasMatch()) + { + match = matched.captured(0); + patternFound = true; + } + } + if (!patternFound) + qWarning() << "No valid rotation is specified in: " << path; + + // Map to degree + switch (match.toInt()) + { + case 0: match = "0"; break; + case 1: match = "90"; break; + case 2: match = "180"; break; + case 3: match = "270"; break; + } + setRotation( match); +} + +Config::Config(const QString &rotation, QObject *parent) : QObject(parent), + m_rotation(rotation) +{ + if(! rotationIsValid(&m_rotation)) + readRotation("/sys/class/graphics/fbcon/rotate"); +} + +inline bool Config::rotationIsValid(const QString * rotation ) +{ + QStringList validRotations = {"0", "90", "180", "270"}; + return validRotations.contains(*rotation); +} diff --git a/multiscreen-compositor/config.h b/multiscreen-compositor/config.h index 216497647499fb73f9e7ed45360dc944fb27452f..610c8c62b017dc5a5f5ff70f9996f97a34a88fdc 100644 --- a/multiscreen-compositor/config.h +++ b/multiscreen-compositor/config.h @@ -8,10 +8,16 @@ class Config : public QObject Q_OBJECT Q_PROPERTY(QString rotation READ rotation CONSTANT) public: + Config(const QString &rotation, QObject *parent = nullptr); + void setRotation(const QString &str); QString rotation() const; private: QString m_rotation; + static const QRegularExpression findRotation; + void readRotation(QString path); + bool rotationIsValid(const QString *rotation); }; + #endif diff --git a/multiscreen-compositor/main.cpp b/multiscreen-compositor/main.cpp index 72c5d221f4698221613c7bfce97f56d580d767cf..17738a2eeb61a9bcb19634c50ed24619829e6ebc 100644 --- a/multiscreen-compositor/main.cpp +++ b/multiscreen-compositor/main.cpp @@ -55,58 +55,12 @@ #include <QDebug> #include <QCommandLineParser> #include <QCommandLineOption> -#include <QRegularExpression> -#include <QRegularExpressionMatch> -#include <QFile> #include <QQmlContext> #include "config.h" #include <message-server.h> #include "compositor-messages.h" -static QRegularExpression findRotation("[0-3]"); - -QString readRotation (QString path) -{ - QFile file(path); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) - { - qWarning() << "Could not read file: " << path; - qWarning() << "Use fallback rotation:0"; - return "0"; - } - - // We send the file content to an QByteArray to use TextStream for ro files - QByteArray bytes = file.readAll(); - file.close(); - QTextStream in(&bytes); - QString line; - QString match="0"; - bool patternFound = false; - while (!in.atEnd()) - { - line = in.readLine(); - QRegularExpressionMatch matched = findRotation.match(line); - if (matched.hasMatch()) - { - match = matched.captured(0); - patternFound = true; - } - } - if (!patternFound) - qWarning() << "No valid rotation is specified in: " << path; - - // Map to degree - switch (match.toInt()) - { - case 0: match = "0"; break; - case 1: match = "90"; break; - case 2: match = "180"; break; - case 3: match = "270"; break; - } - return match; -} - int main(int argc, char *argv[]) { @@ -119,11 +73,11 @@ int main(int argc, char *argv[]) parser.addOption(screenRotation); parser.process(app); - Config config; + QString rotationArg = "unknown"; + if (parser.isSet(screenRotation)) - config.setRotation(parser.value(screenRotation)); - else - config.setRotation(readRotation("/sys/class/graphics/fbcon/rotate")); + rotationArg = parser.value(screenRotation); + Config * config = new Config(rotationArg); MessageServer server; if ( ! server.listen()) @@ -137,7 +91,7 @@ int main(int argc, char *argv[]) &commands, SLOT(messageReceived(QString,quint32,quint32))); QQmlApplicationEngine appEngine; - appEngine.rootContext()->setContextProperty("config", &config); + appEngine.rootContext()->setContextProperty("config", config); appEngine.rootContext()->setContextProperty("commands", &commands); appEngine.load(QUrl("qrc:///qml/main.qml"));