diff --git a/daemon/actions/bundled/brightnesscontrol.cpp b/daemon/actions/bundled/brightnesscontrol.cpp --- a/daemon/actions/bundled/brightnesscontrol.cpp +++ b/daemon/actions/bundled/brightnesscontrol.cpp @@ -198,12 +198,7 @@ int BrightnessControl::brightnessPercent(float value) const { - const float maxBrightness = brightnessMax(); - if (maxBrightness <= 0) { - return 0; - } - - return qRound(value / maxBrightness * 100); + return backend()->brightnessPercent(value); } } diff --git a/daemon/powerdevilbackendinterface.h b/daemon/powerdevilbackendinterface.h --- a/daemon/powerdevilbackendinterface.h +++ b/daemon/powerdevilbackendinterface.h @@ -175,6 +175,15 @@ */ virtual int brightness(BrightnessControlType type = Screen) const; + /** + * Gets the device brightness value in percent. + * + * @param value the brightness value to convert to percent, as an integer from 0 to brightnessValueMax + * @param type the device type + * @return the brightness of the device, as an integer from 0 to 100 + */ + virtual int brightnessPercent(int value, BrightnessControlType type = Screen) const; + /** * Gets the maximum device brightness value. * diff --git a/daemon/powerdevilbackendinterface.cpp b/daemon/powerdevilbackendinterface.cpp --- a/daemon/powerdevilbackendinterface.cpp +++ b/daemon/powerdevilbackendinterface.cpp @@ -94,6 +94,12 @@ return d->brightnessLogic.value(type)->value(); } +int BackendInterface::brightnessPercent(int value, BackendInterface::BrightnessControlType type) const +{ + BrightnessLogic *logic = d->brightnessLogic.value(type); + return logic->percentage(value); +} + int BackendInterface::brightnessMax(BackendInterface::BrightnessControlType type) const { return d->brightnessLogic.value(type)->valueMax(); diff --git a/daemon/powerdevilbrightnesslogic.cpp b/daemon/powerdevilbrightnesslogic.cpp --- a/daemon/powerdevilbrightnesslogic.cpp +++ b/daemon/powerdevilbrightnesslogic.cpp @@ -16,6 +16,7 @@ * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ +#include #include "powerdevilbrightnesslogic.h" #include "powerdevilbackendinterface.h" @@ -55,35 +56,52 @@ int BrightnessLogic::increased() const { + int step; + if (m_value == m_valueMax) { return m_valueMax; // we are at the maximum already } - // Add 1 and round upwards to the nearest step - int step = m_steps - (m_valueMax - m_value - 1) * m_steps / m_valueMax; + if (m_valueMax < 100) { + // Add 1 and round upwards to the nearest step + step = m_steps - (m_valueMax - m_value - 1) * m_steps / m_valueMax; + + } else { + step = valueToStep(m_value) + 1; - if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) <= qRound(percentage(m_value))) { - // When no visible change was made, add 1 step. - // This can happen only if valueMax > 100, else 1 >= 1%. - step++; + int percent = qRound(percentage(stepToValue(step))); + if (percent > 1 && percent <= qRound(percentage(m_value))) { + // When no visible change was made, add 1 step. + // This can happen only if valueMax > 100, else 1 >= 1%. + step++; + } } return stepToValue(step); } int BrightnessLogic::decreased() const { + int step; + if (m_value == 0) { - return 0; // we are at the minimum already + return m_value; // we are at the minimum already } - // Subtract 1 and round downwards to the nearest Step - int step = (m_value - 1) * m_steps / m_valueMax; - - if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) >= qRound(percentage(m_value))) { - // When no visible change was made, subtract 1 step. - // This can happen only if valueMax > 100, else 1 >= 1%. - step--; + if (m_valueMax < 100) { + // Subtract 1 and round downwards to the nearest Step + step = (m_value - 1) * m_steps / m_valueMax; + } else { + step = valueToStep(m_value); + + if (step > 0) { + step--; + if (qRound(percentage(stepToValue(step))) >= qRound(percentage(m_value))) { + // When no visible change was made, subtract 1 step. + // This can happen only if valueMax > 100, else 1 >= 1%. + step--; + } + } } return stepToValue(step); @@ -112,7 +130,11 @@ float BrightnessLogic::percentage(int value) const { - return value * 100.0 / m_valueMax; + if (m_valueMax < 100) { + return value * 100.0 / m_valueMax; + } else { + return valueToStep(value) * 100.0 / m_steps; + } } const BrightnessLogic::BrightnessInfo BrightnessLogic::info() const @@ -124,12 +146,21 @@ int BrightnessLogic::stepToValue(int step) const { - return qBound(0, qRound(step * 1.0 * m_valueMax / m_steps), m_valueMax); + if (m_valueMax < 100) { + return qBound(0, qRound(step * 1.0 * m_valueMax / m_steps), m_valueMax); + } else { + // Quadratic progression to make better use of small values in a dark environment + return qBound(0, qRound(step * 1.0 * step * m_valueMax / (m_steps * m_steps)), m_valueMax); + } } int BrightnessLogic::valueToStep(int value) const { - return qBound(0, qRound(value * 1.0 * m_steps / m_valueMax), m_steps); + if (m_valueMax < 100) { + return qBound(0, qRound(value * 1.0 * m_steps / m_valueMax), m_steps); + } else { + return qBound(0, qRound(qSqrt(value * 1.0 * m_steps * m_steps / m_valueMax)), m_steps); + } } }