Skip to content
Snippets Groups Projects
Commit 9c3b785e authored by Jonas Höppner's avatar Jonas Höppner
Browse files

Config: Move the read rotation function inside the config class

parent 3bc95fd9
No related branches found
No related tags found
1 merge request!18Add server and client to compositor allowing window control
#include "config.h" #include "config.h"
#include <QStringList> #include <QStringList>
#include <QDebug> #include <QDebug>
#include <QRegularExpression>
#include <QFile>
void Config::setRotation(const QString &str) void Config::setRotation(const QString &str)
{ {
QStringList validRotations = {"0", "90", "180", "270"}; if(!rotationIsValid(&str)){
if (!validRotations.contains(str))
{
qWarning() << "Can not set invalid rotation: " << str; qWarning() << "Can not set invalid rotation: " << str;
return; return;
} }
...@@ -20,3 +20,59 @@ QString Config::rotation() const ...@@ -20,3 +20,59 @@ QString Config::rotation() const
{ {
return m_rotation; 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);
}
...@@ -8,10 +8,16 @@ class Config : public QObject ...@@ -8,10 +8,16 @@ class Config : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString rotation READ rotation CONSTANT) Q_PROPERTY(QString rotation READ rotation CONSTANT)
public: public:
Config(const QString &rotation, QObject *parent = nullptr);
void setRotation(const QString &str); void setRotation(const QString &str);
QString rotation() const; QString rotation() const;
private: private:
QString m_rotation; QString m_rotation;
static const QRegularExpression findRotation;
void readRotation(QString path);
bool rotationIsValid(const QString *rotation);
}; };
#endif #endif
...@@ -55,58 +55,12 @@ ...@@ -55,58 +55,12 @@
#include <QDebug> #include <QDebug>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QCommandLineOption> #include <QCommandLineOption>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QFile>
#include <QQmlContext> #include <QQmlContext>
#include "config.h" #include "config.h"
#include <message-server.h> #include <message-server.h>
#include "compositor-messages.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[]) int main(int argc, char *argv[])
{ {
...@@ -119,11 +73,11 @@ int main(int argc, char *argv[]) ...@@ -119,11 +73,11 @@ int main(int argc, char *argv[])
parser.addOption(screenRotation); parser.addOption(screenRotation);
parser.process(app); parser.process(app);
Config config; QString rotationArg = "unknown";
if (parser.isSet(screenRotation)) if (parser.isSet(screenRotation))
config.setRotation(parser.value(screenRotation)); rotationArg = parser.value(screenRotation);
else Config * config = new Config(rotationArg);
config.setRotation(readRotation("/sys/class/graphics/fbcon/rotate"));
MessageServer server; MessageServer server;
if ( ! server.listen()) if ( ! server.listen())
...@@ -137,7 +91,7 @@ int main(int argc, char *argv[]) ...@@ -137,7 +91,7 @@ int main(int argc, char *argv[])
&commands, SLOT(messageReceived(QString,quint32,quint32))); &commands, SLOT(messageReceived(QString,quint32,quint32)));
QQmlApplicationEngine appEngine; QQmlApplicationEngine appEngine;
appEngine.rootContext()->setContextProperty("config", &config); appEngine.rootContext()->setContextProperty("config", config);
appEngine.rootContext()->setContextProperty("commands", &commands); appEngine.rootContext()->setContextProperty("commands", &commands);
appEngine.load(QUrl("qrc:///qml/main.qml")); appEngine.load(QUrl("qrc:///qml/main.qml"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment