File indexing completed on 2024-04-28 05:36:16

0001 /*
0002  *   SPDX-FileCopyrightText: 2014 Nikita Skovoroda <chalkerx@gmail.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "powerdevilscreenbrightnesslogic.h"
0008 #include "powerdevil_debug.h"
0009 #include <QDebug>
0010 
0011 namespace PowerDevil
0012 {
0013 int ScreenBrightnessLogic::toggled() const
0014 {
0015     // ignore, we won't toggle the display off
0016     return -1;
0017 }
0018 
0019 int ScreenBrightnessLogic::valueMin() const
0020 {
0021     // 0 can turn off the backlight with some drivers
0022     return 1;
0023 }
0024 
0025 int ScreenBrightnessLogic::calculateSteps(int maxValue) const
0026 {
0027     // We assume that the preferred number of steps for screen brightness is 20, but we don't want more.
0028 
0029     if (maxValue <= 20) {
0030         // Too few steps, return the number of actual steps.
0031         // Those would be uniform, but not round in some cases.
0032         return maxValue;
0033     }
0034 
0035     if (maxValue >= 100 || maxValue % 20 == 0 || (maxValue >= 80 && maxValue % 4 == 0)) {
0036         // In this case all 20 steps are perfect.
0037         return 20;
0038     }
0039 
0040     // At this point we have maxValue in the range 21-79 which probably is a rare case.
0041 
0042     if (maxValue >= 34 || maxValue == 32 || maxValue == 28) {
0043         // In this case all 20 steps are matched +-1%, which is fine.
0044         return 20;
0045     }
0046 
0047     // At this point we have maxValue in the range 21-33.
0048     // Trying to make 20 steps from here will make them not uniform.
0049 
0050     if (maxValue % 5 == 0) {
0051         // For maxValue == 30 there are 10 even and round steps.
0052         // For maxValue == 25 steps are shown as
0053         //  0% 12% 20% 32% 40% 52% 60% 72% 80% 92% 100%, which is also fine.
0054         return 10;
0055     }
0056 
0057     // Trying hard to find an uniform steps set
0058     for (int steps = 9; steps <= 14; steps++) {
0059         if (maxValue % steps == 0) {
0060             return steps;
0061         }
0062     }
0063 
0064     // 4 different maxValue values left: 21, 23, 29, 31.
0065     // Those produce +-2% on 10 steps, there is nothing better we can do here.
0066     return 10;
0067 }
0068 
0069 };