From c3b2fad5e680e600fdee13981c43d342483d5635 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Wed, 30 Aug 2023 14:27:52 +0900 Subject: Fix rounding error when setting brightness by percentage Signed-off-by: Michele Calgaro --- src/hardware.cpp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/hardware.cpp') diff --git a/src/hardware.cpp b/src/hardware.cpp index 25d96dd..ef1ba23 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -30,6 +30,7 @@ // include global header #include +#include // include QT header #include @@ -956,7 +957,6 @@ bool HardwareInfo::setBrightnessPercentage(int percent) if (trace) kdDebug() << funcinfo << "IN: " << " percent: " << percent << endl; bool retval = false; - int maxBrightness = getMaxBrightnessLevel(); // Use the first backlight in the list TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::Backlight); @@ -969,22 +969,24 @@ bool HardwareInfo::setBrightnessPercentage(int percent) checkBrightness(); } - // Make sure percentage is within valid range - if (percent < 0) { - percent = 0; - } - else if (percent > 100) { - percent = 100; - } - int level = (int)(maxBrightness * percent / 100.0); - if (!brightness) { kdError() << "Change brightness not supported " << endl; - } else { + } + else { + // Make sure percentage is within valid range + if (percent < 0) { + percent = 0; + } + else if (percent > 100) { + percent = 100; + } + + int level = (int)round(getMaxBrightnessLevel() * percent / 100.0); if (currentBrightnessLevel == level) { kdDebug() << "Brightness level not changed, requested level == current level" << endl; retval = true; - } else { + } + else { backlightdevice->setRawBrightness(level); retval = true; } @@ -1181,10 +1183,9 @@ bool HardwareInfo::setBrightnessUp(int percentageStep) { checkCurrentBrightness(); - if (supportBrightness() && getCurrentBrightnessLevel() < getMaxBrightnessLevel() && - percentageStep > 0) + if (supportBrightness() && percentageStep > 0) { - int currentPerc = (int)(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); + int currentPerc = (int)round(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); retval = setBrightnessPercentage(currentPerc + percentageStep); } @@ -1206,8 +1207,8 @@ bool HardwareInfo::setBrightnessDown(int percentageStep) { checkCurrentBrightness(); - if (supportBrightness() && getCurrentBrightnessLevel() > 0 && percentageStep > 0) { - int currentPerc = (int)(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); + if (supportBrightness() && percentageStep > 0) { + int currentPerc = (int)round(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); retval = setBrightnessPercentage(currentPerc - percentageStep); } @@ -1245,7 +1246,7 @@ bool HardwareInfo::getLidclose() const { */ int HardwareInfo::getMaxBrightnessLevel() const { if (brightness) - return availableBrightnessLevels; + return availableBrightnessLevels - 1; else return -1; } -- cgit v1.2.1