Make the screen helper more generic

This commit is contained in:
Michael Zanetti 2020-12-18 22:14:39 +01:00
parent eea91a2d7f
commit 4b62ead366
5 changed files with 55 additions and 29 deletions

View File

@ -16,7 +16,7 @@ HEADERS += \
mainmenumodel.h \
nfchelper.h \
nfcthingactionwriter.h \
platformintegration/generic/raspberrypihelper.h \
platformintegration/generic/screenhelper.h \
stylecontroller.h \
pushnotifications.h \
platformhelper.h \
@ -28,7 +28,7 @@ SOURCES += main.cpp \
mainmenumodel.cpp \
nfchelper.cpp \
nfcthingactionwriter.cpp \
platformintegration/generic/raspberrypihelper.cpp \
platformintegration/generic/screenhelper.cpp \
stylecontroller.cpp \
pushnotifications.cpp \
platformhelper.cpp \

View File

@ -32,7 +32,7 @@
PlatformHelperGeneric::PlatformHelperGeneric(QObject *parent) : PlatformHelper(parent)
{
m_piHelper = new RaspberryPiHelper(this);
m_piHelper = new ScreenHelper(this);
}
void PlatformHelperGeneric::requestPermissions()

View File

@ -33,7 +33,7 @@
#include <QObject>
#include "platformhelper.h"
#include "raspberrypihelper.h"
#include "screenhelper.h"
class PlatformHelperGeneric : public PlatformHelper
{
@ -61,7 +61,7 @@ public:
Q_INVOKABLE virtual void vibrate(HapticsFeedback feedbyckType) override;
private:
RaspberryPiHelper *m_piHelper = nullptr;
ScreenHelper *m_piHelper = nullptr;
};
#endif // PLATFORMHELPERGENERIC_H

View File

@ -28,31 +28,56 @@
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "raspberrypihelper.h"
#include "screenhelper.h"
#include <QDebug>
#include <QApplication>
#include <QWindow>
#include <QSettings>
#include <QFileInfo>
#include <QDir>
RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
ScreenHelper::ScreenHelper(QObject *parent) : QObject(parent)
{
m_powerFile.setFileName("/sys/class/backlight/rpi_backlight/bl_power");
bool available = m_powerFile.open(QFile::ReadWrite | QFile::Text);
// Try generic backlight
QDir backlightDir("/sys/class/backlight");
foreach (const QFileInfo &fi, backlightDir.entryInfoList({"*_backlight"}, QDir::Dirs)) {
qDebug() << "Checking backlight directory:" << fi.absoluteFilePath();
m_powerFile.setFileName(fi.absoluteFilePath() + "/bl_power");
m_brightnessFile.setFileName(fi.absoluteFilePath() + "/brightness");
if (!m_powerFile.open(QFile::ReadWrite | QFile::Text)) {
qWarning() << "Cannot open" << m_powerFile.fileName() << "for writing";
continue;
}
if (!m_brightnessFile.open(QFile::ReadWrite | QFile::Text)) {
qWarning() << "Cannot open" << m_brightnessFile.fileName() << "for writing";
continue;
}
QFile maxBrightnessFile(fi.absoluteFilePath() + "/max_brightness");
if (!maxBrightnessFile.open(QFile::ReadOnly)) {
qWarning() << "Cannot open" << m_brightnessFile.fileName() << "for reading";
continue;
}
bool ok;
m_maxBrightness = maxBrightnessFile.readAll().toInt(&ok);
if (!ok) {
qWarning() << "Error reading max brightness value from" << maxBrightnessFile.fileName();
m_maxBrightness = -1;
continue;
}
// All good. Let's use this and not check more files
break;
}
if (!available) {
if (!m_powerFile.isOpen() || !m_brightnessFile.isOpen()) {
qWarning() << "No backlight support on this platform";
return;
}
qDebug() << "Backlight control enabled on" << m_powerFile.fileName();
qDebug() << "Raspberry Pi detected. Enabling backlight control";
m_brightnessFile.setFileName("/sys/class/backlight/rpi_backlight/brightness");
if (!m_brightnessFile.open(QFile::ReadWrite | QFile::Text)) {
qWarning() << "Failed to open brightness file";
}
QByteArray currentBrightness = m_brightnessFile.readLine();
m_currentBrightness = currentBrightness.trimmed().toInt() * 100 / 255;
qDebug() << "Current brightness is:" << currentBrightness << m_currentBrightness;
m_currentBrightness = currentBrightness.trimmed().toInt() * 100 / m_maxBrightness;
qDebug() << "Current brightness is: Absolute:" << currentBrightness << "Percentage:" << m_currentBrightness;
screenOn();
@ -63,7 +88,7 @@ RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
QSettings settings;
m_screenOffTimer.setInterval(settings.value("screenOffTimeout", 15000).toInt());
m_screenOffTimer.setSingleShot(true);
connect(&m_screenOffTimer, &QTimer::timeout, this, &RaspberryPiHelper::screenOff);
connect(&m_screenOffTimer, &QTimer::timeout, this, &ScreenHelper::screenOff);
if (m_screenOffTimer.interval() > 0) {
m_screenOffTimer.start();
}
@ -73,17 +98,17 @@ RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
m_cursorHidden = true;
}
bool RaspberryPiHelper::active() const
bool ScreenHelper::active() const
{
return m_powerFile.isOpen();
}
int RaspberryPiHelper::screenTimeout() const
int ScreenHelper::screenTimeout() const
{
return m_screenOffTimer.interval();
}
void RaspberryPiHelper::setScreenTimeout(int timeout)
void ScreenHelper::setScreenTimeout(int timeout)
{
m_screenOffTimer.setInterval(timeout);
QSettings settings;
@ -95,19 +120,19 @@ void RaspberryPiHelper::setScreenTimeout(int timeout)
}
}
int RaspberryPiHelper::screenBrightness() const
int ScreenHelper::screenBrightness() const
{
return m_currentBrightness;
}
void RaspberryPiHelper::setScreenBrightness(int percent)
void ScreenHelper::setScreenBrightness(int percent)
{
m_currentBrightness = percent;
m_brightnessFile.write(QString("%1\n").arg(percent * 255 / 100).toUtf8());
m_brightnessFile.flush();
}
bool RaspberryPiHelper::eventFilter(QObject *watched, QEvent *event)
bool ScreenHelper::eventFilter(QObject *watched, QEvent *event)
{
if (m_screenOffTimer.interval() == 0) {
return QObject::eventFilter(watched, event);
@ -162,7 +187,7 @@ bool RaspberryPiHelper::eventFilter(QObject *watched, QEvent *event)
return QObject::eventFilter(watched, event);
}
void RaspberryPiHelper::screenOn()
void ScreenHelper::screenOn()
{
qDebug() << "Turning screen on";
int ret = m_powerFile.write("0\n");
@ -172,7 +197,7 @@ void RaspberryPiHelper::screenOn()
}
}
void RaspberryPiHelper::screenOff()
void ScreenHelper::screenOff()
{
qDebug() << "Turning screen off";
int ret = m_powerFile.write("1\n");

View File

@ -35,11 +35,11 @@
#include <QTimer>
#include <QFile>
class RaspberryPiHelper : public QObject
class ScreenHelper : public QObject
{
Q_OBJECT
public:
explicit RaspberryPiHelper(QObject *parent = nullptr);
explicit ScreenHelper(QObject *parent = nullptr);
bool active() const;
int screenTimeout() const;
@ -61,6 +61,7 @@ private:
bool m_cursorHidden = false;
int m_maxBrightness = -1;
int m_currentBrightness = 255;
};