Fix rounding error when setting brightness by percentage

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
pull/17/head
Michele Calgaro 1 year ago
parent aee72d11d4
commit c3b2fad5e6
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -78,9 +78,9 @@ ConfigureDialog::ConfigureDialog( TDEConfig *_config, HardwareInfo *_hwinfo, Set
scheme_changed = false; scheme_changed = false;
displayed_WARN_autosuspend = false; displayed_WARN_autosuspend = false;
// check if brightness is supporte // check if brightness is supported
if(hwinfo->supportBrightness()) { if(hwinfo->supportBrightness()) {
brightnessLevels = hwinfo->getMaxBrightnessLevel() -1; brightnessLevels = hwinfo->getMaxBrightnessLevel();
brightness_last = hwinfo->getCurrentBrightnessLevel(); brightness_last = hwinfo->getCurrentBrightnessLevel();
} else { } else {
brightnessLevels = -1; brightnessLevels = -1;

@ -30,6 +30,7 @@
// include global header // include global header
#include <fcntl.h> #include <fcntl.h>
#include <math.h>
// include QT header // include QT header
#include <tqtimer.h> #include <tqtimer.h>
@ -956,7 +957,6 @@ bool HardwareInfo::setBrightnessPercentage(int percent)
if (trace) kdDebug() << funcinfo << "IN: " << " percent: " << percent << endl; if (trace) kdDebug() << funcinfo << "IN: " << " percent: " << percent << endl;
bool retval = false; bool retval = false;
int maxBrightness = getMaxBrightnessLevel();
// Use the first backlight in the list // Use the first backlight in the list
TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::Backlight); TDEGenericHardwareList hwlist = m_hwdevices->listByDeviceClass(TDEGenericDeviceType::Backlight);
@ -969,6 +969,10 @@ bool HardwareInfo::setBrightnessPercentage(int percent)
checkBrightness(); checkBrightness();
} }
if (!brightness) {
kdError() << "Change brightness not supported " << endl;
}
else {
// Make sure percentage is within valid range // Make sure percentage is within valid range
if (percent < 0) { if (percent < 0) {
percent = 0; percent = 0;
@ -976,15 +980,13 @@ bool HardwareInfo::setBrightnessPercentage(int percent)
else if (percent > 100) { else if (percent > 100) {
percent = 100; percent = 100;
} }
int level = (int)(maxBrightness * percent / 100.0);
if (!brightness) { int level = (int)round(getMaxBrightnessLevel() * percent / 100.0);
kdError() << "Change brightness not supported " << endl;
} else {
if (currentBrightnessLevel == level) { if (currentBrightnessLevel == level) {
kdDebug() << "Brightness level not changed, requested level == current level" << endl; kdDebug() << "Brightness level not changed, requested level == current level" << endl;
retval = true; retval = true;
} else { }
else {
backlightdevice->setRawBrightness(level); backlightdevice->setRawBrightness(level);
retval = true; retval = true;
} }
@ -1181,10 +1183,9 @@ bool HardwareInfo::setBrightnessUp(int percentageStep) {
checkCurrentBrightness(); checkCurrentBrightness();
if (supportBrightness() && getCurrentBrightnessLevel() < getMaxBrightnessLevel() && if (supportBrightness() && percentageStep > 0)
percentageStep > 0)
{ {
int currentPerc = (int)(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel()); int currentPerc = (int)round(100.0 * getCurrentBrightnessLevel() / getMaxBrightnessLevel());
retval = setBrightnessPercentage(currentPerc + percentageStep); retval = setBrightnessPercentage(currentPerc + percentageStep);
} }
@ -1206,8 +1207,8 @@ bool HardwareInfo::setBrightnessDown(int percentageStep) {
checkCurrentBrightness(); checkCurrentBrightness();
if (supportBrightness() && getCurrentBrightnessLevel() > 0 && 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); retval = setBrightnessPercentage(currentPerc - percentageStep);
} }
@ -1245,7 +1246,7 @@ bool HardwareInfo::getLidclose() const {
*/ */
int HardwareInfo::getMaxBrightnessLevel() const { int HardwareInfo::getMaxBrightnessLevel() const {
if (brightness) if (brightness)
return availableBrightnessLevels; return availableBrightnessLevels - 1;
else else
return -1; return -1;
} }

@ -23,6 +23,8 @@
#include <config.h> #include <config.h>
#endif #endif
#include <math.h>
// KDE headers: // KDE headers:
#include <tdeaboutapplication.h> #include <tdeaboutapplication.h>
#include <tdeapplication.h> #include <tdeapplication.h>
@ -1073,7 +1075,7 @@ void tdepowersave::do_downDimm() {
if (hwinfo->supportBrightness()) { if (hwinfo->supportBrightness()) {
if (!AUTODIMM_Timer->isActive()) { 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 // check if we really need to dimm down
if (dimmToLevel < hwinfo->getCurrentBrightnessLevel()) { if (dimmToLevel < hwinfo->getCurrentBrightnessLevel()) {
@ -1112,7 +1114,7 @@ void tdepowersave::do_upDimm() {
if (hwinfo->supportBrightness()) { if (hwinfo->supportBrightness()) {
if (!AUTODIMM_Timer->isActive()) { 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 // check if we really need to dimm up
if (dimmToLevel > hwinfo->getCurrentBrightnessLevel()) { if (dimmToLevel > hwinfo->getCurrentBrightnessLevel()) {
@ -1153,8 +1155,7 @@ void tdepowersave::do_dimm() {
if (autoDimmDown) { if (autoDimmDown) {
// dimm the display down // dimm the display down
if (current > 0 && if (current > (int)round(hwinfo->getMaxBrightnessLevel() * settings->autoDimmTo / 100.0)) {
current > ((int)((float)hwinfo->getMaxBrightnessLevel()*((float)settings->autoDimmTo/100.0))-1)) {
hwinfo->setBrightnessLevel(current -1); hwinfo->setBrightnessLevel(current -1);
} else { } else {
AUTODIMM_Timer->stop(); AUTODIMM_Timer->stop();
@ -1166,7 +1167,7 @@ void tdepowersave::do_dimm() {
} }
} else { } else {
// dimm the display up // 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); hwinfo->setBrightnessLevel(current +1);
} else { } else {
AUTODIMM_Timer->stop(); AUTODIMM_Timer->stop();
@ -2938,7 +2939,7 @@ int tdepowersave::brightnessGet() {
int retval = -1; int retval = -1;
if (hwinfo->supportBrightness()) { if (hwinfo->supportBrightness()) {
retval = (int)(100.0 * hwinfo->getCurrentBrightnessLevel() / hwinfo->getMaxBrightnessLevel()); retval = (int)round(100.0 * hwinfo->getCurrentBrightnessLevel() / hwinfo->getMaxBrightnessLevel());
} }
kdDebugFuncOut(trace); kdDebugFuncOut(trace);
@ -2951,10 +2952,7 @@ void tdepowersave::brightnessSet(int percentage)
{ {
kdDebugFuncIn(trace); kdDebugFuncIn(trace);
if (hwinfo->supportBrightness() && percentage >= 0 && percentage <= 100)
{
hwinfo->setBrightnessPercentage(percentage); hwinfo->setBrightnessPercentage(percentage);
}
kdDebugFuncOut(trace); kdDebugFuncOut(trace);
} }

Loading…
Cancel
Save