From d3f30f8debce28f81593f27bf861f346b2dd176c Mon Sep 17 00:00:00 2001 From: pjx206 Date: Wed, 2 Jun 2021 00:20:52 +0800 Subject: [PATCH] Make brightness changes smoother --- screen-brightness/CMakeLists.txt | 2 + screen-brightness/brightnesshelper.cpp | 118 ++++++++++++++++++++++ screen-brightness/brightnesshelper.h | 81 +++++++++++++++ screen-brightness/main.cpp | 132 +++---------------------- 4 files changed, 213 insertions(+), 120 deletions(-) create mode 100644 screen-brightness/brightnesshelper.cpp create mode 100644 screen-brightness/brightnesshelper.h diff --git a/screen-brightness/CMakeLists.txt b/screen-brightness/CMakeLists.txt index c46bb3f..d62b2ce 100644 --- a/screen-brightness/CMakeLists.txt +++ b/screen-brightness/CMakeLists.txt @@ -3,6 +3,8 @@ set(PROJECT_NAME cutefish-screen-brightness) set(SRCS main.cpp + brightnesshelper.h + brightnesshelper.cpp ) add_executable(${PROJECT_NAME} diff --git a/screen-brightness/brightnesshelper.cpp b/screen-brightness/brightnesshelper.cpp new file mode 100644 index 0000000..cc1f9a2 --- /dev/null +++ b/screen-brightness/brightnesshelper.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: pjx + * rekols + * + * 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 + * 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 . + */ + +#include "brightnesshelper.h" + +BrightnessHelper::BrightnessHelper(QObject *parent) : QObject(parent) +{ + init(); + anime.setEasingCurve(QEasingCurve::Linear); + connect(&anime, &QVariantAnimation::valueChanged, this, + [this](const QVariant &value){ + if (QAbstractAnimation::Running == anime.state()) { + writeBrightness(value.toInt()); + } + }); + connect(&anime, &QVariantAnimation::finished, this, [this](){ + qApp->quit(); + }); +} + +BrightnessHelper::~BrightnessHelper(){ +} + +void BrightnessHelper::init() +{ + const char kSysBrightnessDir[] = "/sys/class/backlight"; + const char kMaxFile[] = "max_brightness"; + const char kActualFile[] = "actual_brightness"; + QDir sys(kSysBrightnessDir); + + QStringList firmware, platform, raw; + + QFile file; + QByteArray typeBuffer; + for (const QFileInfo &device: sys.entryInfoList( + QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable, + QDir::Name | QDir::Reversed)) { + const QString dev_path = device.filePath(); + file.setFileName(dev_path + "/type"); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + continue; + } + + typeBuffer = file.readLine().trimmed(); + + if (typeBuffer == "firmware") { + firmware.append(dev_path); + } else if(typeBuffer == "platform") { + platform.append(dev_path); + } else if (typeBuffer == "raw") { + raw.append(dev_path); + } else { + qWarning() << "unsupported type in '" + << file.fileName() << "' : " << QString(typeBuffer); + } + + file.close(); + } + + if (!firmware.isEmpty()) + name = firmware.constFirst(); + else if (!platform.isEmpty()) + name = platform.constFirst(); + else if (!raw.isEmpty()) + name = raw.constFirst(); + + actual = readFile(QDir(name).filePath(kActualFile)).toInt(); + maxValue = readFile(QDir(name).filePath(kMaxFile)).toInt(); +} + +void BrightnessHelper::setBrightness(int value){ + if (value <= 0) + value = 1; + if (value > 100) + value = 100; + anime.setDuration(200); + anime.setStartValue(actual); + const int end = static_cast(value /100.0 * maxValue); + anime.setEndValue(end); + anime.start(); +} + +void BrightnessHelper::increaseBrightness() { + int value = static_cast(actual / (double)maxValue * 100); + setBrightness(value + 20); +} + +void BrightnessHelper::decreaseBrightness() { + int value = static_cast(actual / (double)maxValue * 100); + setBrightness(value - 20); +} + +void BrightnessHelper::writeBrightness(int brightness) { + if (brightness < 0) + brightness = 0; + if (brightness > maxValue) + brightness = maxValue; + QString dbg_s = QDir(name).filePath("brightness"); + writeFile(QDir(name).filePath("brightness"), QString::number(brightness)); +} \ No newline at end of file diff --git a/screen-brightness/brightnesshelper.h b/screen-brightness/brightnesshelper.h new file mode 100644 index 0000000..25b119c --- /dev/null +++ b/screen-brightness/brightnesshelper.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: pjx + * rekols + * + * 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 + * 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 . + */ + +#ifndef BRIGHTNESSHELPER_H +#define BRIGHTNESSHELPER_H + +#include +#include +#include +#include +#include +#include +#include + +inline QString readFile(const QString &fileName) +{ + QFile file(fileName); + if (file.open(QIODevice::ReadOnly)) { + QString data = QString::fromUtf8(file.readAll()); + file.close(); + return data; + } + + return QString(); +} + +inline bool writeFile(const QString &fileName, const QString &data) +{ + QFile file(fileName); + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { + QTextStream textStream(&file); + textStream << data; + textStream.flush(); + file.close(); + return true; + } + + return false; +} + + +class BrightnessHelper : public QObject +{ + Q_OBJECT +public: + explicit BrightnessHelper(QObject *parent = nullptr); + ~BrightnessHelper(); + + void init(); + + void setBrightness(int value); + void increaseBrightness(); + void decreaseBrightness(); + + int actual; + int maxValue; + QString name; + QVariantAnimation anime; + +private: + void writeBrightness(int brightness); +}; + +#endif // BRIGHTNESSHELPER_H diff --git a/screen-brightness/main.cpp b/screen-brightness/main.cpp index 2ec9ebb..bdc264c 100644 --- a/screen-brightness/main.cpp +++ b/screen-brightness/main.cpp @@ -2,6 +2,7 @@ * Copyright (C) 2021 CutefishOS Team. * * Author: rekols + * pjx * * 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 @@ -17,120 +18,8 @@ * along with this program. If not, see . */ -#include #include -#include -#include -#include -#include - -inline QString readFile(const QString &fileName) -{ - QFile file(fileName); - if (file.open(QIODevice::ReadOnly)) { - QString data = QString::fromUtf8(file.readAll()); - file.close(); - return data; - } - - return QString(); -} - -inline bool writeFile(const QString &fileName, const QString &data) -{ - QFile file(fileName); - - if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { - QTextStream textStream(&file); - textStream << data; - textStream.flush(); - file.close(); - return true; - } - - return false; -} - -class ScreenDevice -{ -public: - void set(int value) { - if (value <= 0) - value = 1; - else if (value > 100) - value = 100; - - const int brightness = static_cast(value * maxValue * 0.01); - QString brightnessData = QString::number(brightness, 10); - const QString brightnessFile = QDir(name).filePath("brightness"); - writeFile(brightnessFile, brightnessData); - } - - int actual; - int maxValue; - QString name; -}; - -inline QList getDeviceList() -{ - const char kSysBrightnessDir[] = "/sys/class/backlight"; - const char kMaxFile[] = "max_brightness"; - const char kActualFile[] = "actual_brightness"; - QList list; - QDir sys(kSysBrightnessDir); - - for (const QFileInfo &device : sys.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) { - const QString dev_path = device.filePath(); - QDir dev(dev_path); - const QString max_brightness = readFile(dev.filePath(kMaxFile)); - bool max_ok; - const int max = max_brightness.toInt(&max_ok, 10); - - if (!max_ok) { - qWarning() << "Failed to read max brightness:" << dev_path; - continue; - } - - const QString actual_brightness = readFile(dev.filePath(kActualFile)); - bool actual_ok; - const int actual = actual_brightness.toInt(&actual_ok, 10); - if (!actual_ok) { - qWarning() << "Failed to read actual brightness:" << dev_path; - continue; - } - - ScreenDevice screen; - screen.actual = actual; - screen.maxValue = max; - screen.name = dev_path; - list << screen; - } - - return list; -} - -inline void setBrightness(int value) -{ - for (ScreenDevice &device : getDeviceList()) { - device.set(value); - } -} - -inline void increaseBrightness() -{ - for (ScreenDevice &device : getDeviceList()) { - int value = static_cast(device.actual * 100.0 / device.maxValue); - device.set(value + 20); - } -} - -inline void decreaseBrightness() -{ - for (ScreenDevice &device : getDeviceList()) { - int value = static_cast(device.actual * 100.0 / device.maxValue); - device.set(value - 20); - } -} +#include "brightnesshelper.h" int main(int argc, char *argv[]) { @@ -151,14 +40,17 @@ int main(int argc, char *argv[]) parser.process(app); - if (parser.isSet(setOption)) { + BrightnessHelper *brightnessHelper = new BrightnessHelper(); + + if (parser.isSet(setOption)) + { int value = parser.positionalArguments().value(0).toInt(); - setBrightness(value); - } else if (parser.isSet(increaseOption)){ - increaseBrightness(); - } else if (parser.isSet(decreaseOption)) { - decreaseBrightness(); + brightnessHelper->setBrightness(value); } + else if (parser.isSet(increaseOption)) + brightnessHelper->increaseBrightness(); + else if (parser.isSet(decreaseOption)) + brightnessHelper->decreaseBrightness(); - return 0; + app.exec(); }