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

0001 /*
0002  *   SPDX-FileCopyrightText: 2014 Nikita Skovoroda <chalkerx@gmail.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 namespace PowerDevil
0010 {
0011 class BrightnessLogic
0012 {
0013 public:
0014     BrightnessLogic();
0015     virtual ~BrightnessLogic() = default;
0016 
0017     /**
0018      * This enum defines the different types brightness keys.
0019      *
0020      * - Increase: Key to increase brightness (Qt::Key_MonBrightnessUp or Qt::Key_KeyboardBrightnessUp)
0021      * - Decrease: Key to decrease brightness (Qt::Key_MonBrightnessDown or Qt::Key_KeyboardBrightnessDown)
0022      * - Toggle: Key to toggle backlight (Qt::Key_KeyboardBacklightOnOff)
0023      * - IncreaseSmall: Key to increase brightness by 1% (Qt::Key_MonBrightnessUp or Qt::Key_KeyboardBrightnessUp + Qt::ShiftModifier)
0024      * - DecreaseSmall: Key to decrease brightness by 1% (Qt::Key_MonBrightnessDown or Qt::Key_KeyboardBrightnessDown + Qt::ShiftModifier)
0025      */
0026     enum BrightnessKeyType {
0027         Increase,
0028         Decrease,
0029         Toggle,
0030         IncreaseSmall,
0031         DecreaseSmall,
0032     };
0033 
0034     /**
0035      * This struct contains information about current brightness state for a single device
0036      */
0037     struct BrightnessInfo {
0038         /** The raw brightness value, from 0 to valueMax */
0039         int value;
0040         /** The maximum possible brightness value for this device */
0041         int valueMax;
0042         /** The most recent brightness before toggling off */
0043         int valueBeforeTogglingOff;
0044         /** The maximum possible brightness step for this device */
0045         int steps;
0046     };
0047 
0048     /**
0049      * Sets the current brightness value.
0050      *
0051      * @param value Raw brightness value
0052      */
0053     void setValue(int value);
0054 
0055     /**
0056      * Sets the maximum brightness value.
0057      *
0058      * @param value Maximum brightness value
0059      */
0060     void setValueMax(int valueMax);
0061 
0062     /**
0063      * Sets the last active brightness value.
0064      *
0065      * @param value Last active brightness value
0066      */
0067     void setValueBeforeTogglingOff(int valueBeforeTogglingOff);
0068 
0069     /**
0070      * Calculate new brightness value that should be set by an action.
0071      *
0072      * @param type The action type of the key that was pressed.
0073      * @return The brightness value that the action should set, or -1 if nothing should be done
0074      */
0075     int action(BrightnessKeyType type) const;
0076 
0077     /**
0078      * Calculates the brightness value of the closest step upwards.
0079      * (Closest step that is higher than current brightness value).
0080      *
0081      * @return The brightness value of the closest step upwards
0082      */
0083     int increased() const;
0084 
0085     /**
0086      * Calculates the brightness value of the closest step downwards.
0087      * (Closest step that is lower than current brightness value).
0088      *
0089      * @return The brightness value of the closest step downwards
0090      */
0091     int decreased() const;
0092 
0093     /**
0094      * Calculates the brightness value of the toggled state.
0095      * (Sets the brightness value to either minimum, last active brightness or maximum).
0096      *
0097      * @return The brightness value that should be set, or -1 if nothing should be done
0098      */
0099     virtual int toggled() const;
0100 
0101     /**
0102      * Calculates the brightness value of the closest step upwards.
0103      * (Closest step that is higher than current brightness value).
0104      *
0105      * @return The brightness value of the closest step upwards
0106      */
0107     int increasedSmall() const;
0108 
0109     /**
0110      * Calculates the brightness value of the closest step downwards.
0111      * (Closest step that is lower than current brightness value).
0112      *
0113      * @return The brightness value of the closest step downwards
0114      */
0115     int decreasedSmall() const;
0116 
0117     /**
0118      * Retrieve the maximum possible brightness step for this instance
0119      *
0120      * @return Maximum possible brightness step
0121      */
0122     int steps() const;
0123 
0124     /**
0125      * Retrieve the supplied brightness value expressed as a percentage from 0 to 100
0126      *
0127      * @param value Brightness value, from 0 to valueMax
0128      * @return The brightness percentage for the supplied value
0129      */
0130     float percentage(int value) const;
0131 
0132     /**
0133      * Retrieve a copy of the current brightness state
0134      *
0135      * @return A struct that contains the current brightness state.
0136      */
0137     const BrightnessInfo info() const;
0138 
0139     /**
0140      * Convert brightness step to raw brightness value
0141      *
0142      * @param step Brightness step, from 0 to steps
0143      * @return Brightness value that corresponds to the given step
0144      */
0145     int stepToValue(int step) const;
0146 
0147     /**
0148      * Retrieve the minimum practical brightness value for this device. Defaults
0149      * to 0, but should be overridden in subclasses to be a higher value for device
0150      * types where a higher minimum value brightness makes sense. For example,
0151      * for screens where a value of 0 sometimes turns off the backlight completely,
0152      * which is not wanted there.
0153      *
0154      * @return Minimum practical brightness value
0155      */
0156     virtual int valueMin() const
0157     {
0158         return 0;
0159     }
0160 
0161 protected:
0162     /**
0163      * Calculate the optimal number of brightness steps.
0164      *
0165      * It should be based on three assumptions:
0166      * 1) The user generally expects to see equal brightness steps, and likes round percentage numbers.
0167      * 2) Actual brightness is rounded to whole percents before being displayed to the user.
0168      * 3) An assumption on a generally good number of brightness steps, which varies with implementations.
0169      *
0170      * This function does not depend on anything except the argument.
0171      *
0172      * @param valueMax the maximum brightness value for which we want to calculate the number of steps
0173      * @return the optimal maximum step number
0174      */
0175     virtual int calculateSteps(int valueMax) const = 0;
0176 
0177 private:
0178     int m_value = -1;
0179     int m_valueMax = -1;
0180     int m_valueBeforeTogglingOff = -1;
0181     int m_steps = -1;
0182 };
0183 
0184 }