File indexing completed on 2024-04-28 16:55:13

0001 /***************************************************************************
0002  *   Copyright (C) 2014 by Nikita Skovoroda <chalkerx@gmail.com>            *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 
0021 #ifndef POWERDEVIL_BRIGHTNESSLOGIC_H
0022 #define POWERDEVIL_BRIGHTNESSLOGIC_H
0023 
0024 namespace PowerDevil {
0025 
0026 class BrightnessLogic
0027 {
0028 
0029 public:
0030     BrightnessLogic();
0031     virtual ~BrightnessLogic() = default;
0032 
0033     /**
0034      * This enum defines the different types brightness keys.
0035      *
0036      * - Increase: Key to increase brightness (Qt::Key_MonBrightnessUp or Qt::Key_KeyboardBrightnessUp)
0037      * - Decrease: Key to decrease brightness (Qt::Key_MonBrightnessDown or Qt::Key_KeyboardBrightnessDown)
0038      * - Toggle: Key to toggle backlight (Qt::Key_KeyboardBacklightOnOff)
0039      */
0040     enum BrightnessKeyType { Increase, Decrease, Toggle };
0041 
0042     /**
0043      * This struct contains information about current brightness state for a single device
0044      */
0045     struct BrightnessInfo {
0046         /** The raw brightness value, from 0 to valueMax */
0047         int value;
0048         /** The maximum possible brightness value for this device */
0049         int valueMax;
0050         /** The maximum possible brightness step for this device */
0051         int steps;
0052     };
0053 
0054     /**
0055      * Sets the current brightness value.
0056      *
0057      * @param value Raw brightness value
0058      */
0059     void setValue(int value);
0060 
0061     /**
0062      * Sets the maximum brightness value.
0063      *
0064      * @param value Maximum brightness value
0065      */
0066     void setValueMax(int valueMax);
0067 
0068     /**
0069      * Calculate new brightness value that should be set by an action.
0070      *
0071      * @param type The action type of the key that was pressed.
0072      * @return The brightness value that the action should set, or -1 if nothing should be done
0073      */
0074     int action(BrightnessKeyType type) const;
0075 
0076     /**
0077      * Calculates the brightness value of the closest step upwards.
0078      * (Closest step that is higher than current brightness value).
0079      *
0080      * @return The brightness value of the closest step upwards
0081      */
0082     virtual int increased() const;
0083 
0084     /**
0085      * Calculates the brightness value of the closest step downwards.
0086      * (Closest step that is lower than current brightness value).
0087      *
0088      * @return The brightness value of the closest step downwards
0089      */
0090     virtual int decreased() const;
0091 
0092     /**
0093      * Calculates the brightness value of the toggled state.
0094      * (Sets the brightness value to either 0 or valueMax).
0095      *
0096      * @return The brightness value that should be set, or -1 if nothing should be done
0097      */
0098     virtual int toggled() const;
0099 
0100     /**
0101      * Retrieve the current brightness value.
0102      *
0103      * @return Raw brightness value, from 0 to valueMax
0104      */
0105     int value() const;
0106 
0107     /**
0108      * Retrieve the maximum possible brightness value for this device.
0109      *
0110      * @return Maximum possible brightness value
0111      */
0112     int valueMax() const;
0113 
0114     /**
0115      * Retrieve the brightness step that is closest to the current brightness value.
0116      *
0117      * @return Nearest brightness step
0118      */
0119     int step() const;
0120 
0121     /**
0122      * Retrieve the maximum possible brightness step for this instance
0123      *
0124      * @return Maximum possible brightness step
0125      */
0126     int steps() const;
0127 
0128     /**
0129      * Retrieve the supplied brightness value expressed as a percentage from 0 to 100
0130      *
0131      * @param value Brightness value, from 0 to valueMax
0132      * @return The brightness percentage for the supplied value
0133      */
0134     float percentage(int value) const;
0135 
0136     /**
0137      * Retrieve a copy of the current brightness state
0138      *
0139      * @return A struct that contains the current brightness state.
0140      */
0141     const BrightnessInfo info() const;
0142 
0143     /**
0144      * Convert brightness step to raw brightness value
0145      *
0146      * @param step Brightness step, from 0 to steps
0147      * @return Brightness value that corresponds to the given step
0148      */
0149     int stepToValue(int step) const;
0150 
0151     /**
0152      * Convert raw brightness value to brightness step
0153      *
0154      * @param value Brightness value, from 0 to valueMax
0155      * @return Brightness step that is nearest to the given brightness value
0156      */
0157     int valueToStep(int value) const;
0158 
0159 protected:
0160 
0161     /**
0162      * Calculate the optimal number of brightness steps.
0163      *
0164      * It should be based on three assumptions:
0165      * 1) The user generally expects to see equal brightness steps, and likes round percentage numbers.
0166      * 2) Actual brightness is rounded to whole percents before being displayed to the user.
0167      * 3) An assumption on a generally good number of brightness steps, which varies with implementations.
0168      *
0169      * This function does not depend on anything except the argument.
0170      *
0171      * @param valueMax the maximum brightness value for which we want to calculate the number of steps
0172      * @return the optimal maximum step number
0173      */
0174     virtual int calculateSteps(int valueMax) const = 0;
0175 
0176 private:
0177     int m_value = -1;
0178     int m_valueMax = -1;
0179     int m_steps = -1;
0180 };
0181 
0182 }
0183 
0184 #endif // POWERDEVIL_BRIGHTNESSLOGIC_H