From 96562b80ba7f1b28e55a7ea7ae3a8e0e7bc80f00 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 (cherry picked from commit c3b2fad5e680e600fdee13981c43d342483d5635) --- src/configuredialog.cpp | 4 ++-- src/hardware.cpp | 37 +++++++++++++++++++------------------ src/tdepowersave.cpp | 18 ++++++++---------- src/tdepowersave.h | 4 ++-- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/configuredialog.cpp b/src/configuredialog.cpp index 1669d24..d453ce6 100644 --- a/src/configuredialog.cpp +++ b/src/configuredialog.cpp @@ -78,9 +78,9 @@ ConfigureDialog::ConfigureDialog( TDEConfig *_config, HardwareInfo *_hwinfo, Set scheme_changed = false; displayed_WARN_autosuspend = false; - // check if brightness is supporte + // check if brightness is supported if(hwinfo->supportBrightness()) { - brightnessLevels = hwinfo->getMaxBrightnessLevel() -1; + brightnessLevels = hwinfo->getMaxBrightnessLevel(); brightness_last = hwinfo->getCurrentBrightnessLevel(); } else { brightnessLevels = -1; 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; } diff --git a/src/tdepowersave.cpp b/src/tdepowersave.cpp index b57bfef..41ade35 100644 --- a/src/tdepowersave.cpp +++ b/src/tdepowersave.cpp @@ -23,6 +23,8 @@ #include #endif +#include + // KDE headers: #include #include @@ -1073,7 +1075,7 @@ void tdepowersave::do_downDimm() { if (hwinfo->supportBrightness()) { if (!AUTODIMM_Timer->isActive()) { - int dimmToLevel = (int)((float)hwinfo->getMaxBrightnessLevel()*((float)settings->autoDimmTo/100.0)); + int dimmToLevel = (int)round(hwinfo->getMaxBrightnessLevel() * settings->autoDimmTo / 100.0); // check if we really need to dimm down if (dimmToLevel < hwinfo->getCurrentBrightnessLevel()) { @@ -1112,7 +1114,7 @@ void tdepowersave::do_upDimm() { if (hwinfo->supportBrightness()) { if (!AUTODIMM_Timer->isActive()) { - int dimmToLevel = (int)((float)hwinfo->getMaxBrightnessLevel()*((float)settings->brightnessValue/100.0)); + int dimmToLevel = (int)round(hwinfo->getMaxBrightnessLevel() * settings->autoDimmTo / 100.0); // check if we really need to dimm up if (dimmToLevel > hwinfo->getCurrentBrightnessLevel()) { @@ -1153,8 +1155,7 @@ void tdepowersave::do_dimm() { if (autoDimmDown) { // dimm the display down - if (current > 0 && - current > ((int)((float)hwinfo->getMaxBrightnessLevel()*((float)settings->autoDimmTo/100.0))-1)) { + if (current > (int)round(hwinfo->getMaxBrightnessLevel() * settings->autoDimmTo / 100.0)) { hwinfo->setBrightnessLevel(current -1); } else { AUTODIMM_Timer->stop(); @@ -1166,7 +1167,7 @@ void tdepowersave::do_dimm() { } } else { // dimm the display up - if (current < ((int)((float)hwinfo->getMaxBrightnessLevel()*((float)settings->brightnessValue/100.0))-1)) { + if (current < (int)round(hwinfo->getMaxBrightnessLevel() * settings->autoDimmTo / 100.0)) { hwinfo->setBrightnessLevel(current +1); } else { AUTODIMM_Timer->stop(); @@ -2938,7 +2939,7 @@ int tdepowersave::brightnessGet() { int retval = -1; if (hwinfo->supportBrightness()) { - retval = (int)(100.0 * hwinfo->getCurrentBrightnessLevel() / hwinfo->getMaxBrightnessLevel()); + retval = (int)round(100.0 * hwinfo->getCurrentBrightnessLevel() / hwinfo->getMaxBrightnessLevel()); } kdDebugFuncOut(trace); @@ -2951,10 +2952,7 @@ void tdepowersave::brightnessSet(int percentage) { kdDebugFuncIn(trace); - if (hwinfo->supportBrightness() && percentage >= 0 && percentage <= 100) - { - hwinfo->setBrightnessPercentage(percentage); - } + hwinfo->setBrightnessPercentage(percentage); kdDebugFuncOut(trace); } diff --git a/src/tdepowersave.h b/src/tdepowersave.h index 521184a..326d8be 100644 --- a/src/tdepowersave.h +++ b/src/tdepowersave.h @@ -440,8 +440,8 @@ k_dcop: //! dcop function to open the configure dialog bool openConfigureDialog(); - //! dcop function to find out if tdepowersave manages DPMS - bool currentSchemeManagesDPMS(); + //! dcop function to find out if tdepowersave manages DPMS + bool currentSchemeManagesDPMS(); //! dcop funtion to get the current brightness level int brightnessGet(); //! dcop funtion to set the brightness level -- cgit v1.2.1