diff options
author | Michele Calgaro <[email protected]> | 2023-08-30 14:27:52 +0900 |
---|---|---|
committer | Michele Calgaro <[email protected]> | 2023-09-04 09:54:27 +0900 |
commit | c3b2fad5e680e600fdee13981c43d342483d5635 (patch) | |
tree | 027de3e0c7ebd643db327f35321fb89d7eeb70fa /src/hardware.cpp | |
parent | aee72d11d40febd7cf65634f7569e091edbc26f7 (diff) | |
download | tdepowersave-c3b2fad5e680e600fdee13981c43d342483d5635.tar.gz tdepowersave-c3b2fad5e680e600fdee13981c43d342483d5635.zip |
Fix rounding error when setting brightness by percentage
Signed-off-by: Michele Calgaro <[email protected]>
Diffstat (limited to 'src/hardware.cpp')
-rw-r--r-- | src/hardware.cpp | 37 |
1 files changed, 19 insertions, 18 deletions
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 <fcntl.h> +#include <math.h> // include QT header #include <tqtimer.h> @@ -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; } |