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

0001 /***************************************************************************
0002  *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
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_BACKENDINTERFACE_H
0022 #define POWERDEVIL_BACKENDINTERFACE_H
0023 
0024 #include <QObject>
0025 #include <QHash>
0026 
0027 #include "powerdevilbrightnesslogic.h"
0028 
0029 class KJob;
0030 
0031 namespace PowerDevil {
0032 
0033 class Q_DECL_EXPORT BackendInterface : public QObject
0034 {
0035     Q_OBJECT
0036     Q_DISABLE_COPY(BackendInterface)
0037 
0038 public:
0039     explicit BackendInterface(QObject* parent = nullptr);
0040     ~BackendInterface() override;
0041 
0042     /**
0043      * This enum type defines the different states of the AC adapter.
0044      *
0045      * - UnknownAcAdapterState: The AC adapter has an unknown state
0046      * - Plugged: The AC adapter is plugged
0047      * - Unplugged: The AC adapter is unplugged
0048      */
0049     enum AcAdapterState{ UnknownAcAdapterState, Plugged, Unplugged };
0050     Q_ENUM(AcAdapterState)
0051 
0052     /**
0053      * This enum type defines the types of system button events.
0054      *
0055      * - UnknownButtonType: An unknown button
0056      * - PowerDown: A power down pressed event, generally used to turn on or off the system. KWin emits on long power button presses.
0057      * - PowerButton: A power button pressed event, generally used to turn on or off the system
0058      * - SleepButton: A sleep button pressed event, generally used to make the system asleep
0059      * - LidOpen: A laptop lid open event
0060      * - LidClose: A laptop lid close event
0061      */
0062     enum ButtonType{ UnknownButtonType, PowerButton, PowerDownButton, SleepButton, LidOpen, LidClose, HibernateButton };
0063     Q_ENUM(ButtonType)
0064 
0065     /**
0066      * This enum type defines the different suspend methods.
0067      *
0068      * - UnknownSuspendMethod: The name says it all
0069      * - Standby: Processes are stopped, some hardware is deactivated (ACPI S1)
0070      * - ToRam: Most devices are deactivated, only RAM is powered (ACPI S3)
0071      * - ToDisk: State of the machine is saved to disk, and it's powered down (ACPI S4)
0072      * - SuspendThenHibernate: Same as ToRam, but after a delay it switches to ToDisk
0073      */
0074     enum SuspendMethod{ UnknownSuspendMethod = 0, Standby = 1, ToRam = 2, ToDisk = 4, HybridSuspend = 8, SuspendThenHibernate = 16 };
0075     Q_ENUM(SuspendMethod)
0076 
0077     /**
0078      * This type stores an OR combination of SuspendMethod values.
0079      */
0080     Q_DECLARE_FLAGS(SuspendMethods, SuspendMethod)
0081 
0082     /**
0083      * This enum defines the different types of brightness controls.
0084      *
0085      * - UnknownBrightnessControl: Unknown
0086      * - Screen: Brightness control for a monitor or laptop panel
0087      * - Keyboard: Brightness control for a keyboard backlight
0088      */
0089     enum BrightnessControlType{ UnknownBrightnessControl = 0, Screen = 1, Keyboard = 2 };
0090     Q_ENUM(BrightnessControlType)
0091 
0092     typedef QHash<QString, BrightnessControlType> BrightnessControlsList;
0093 
0094     /**
0095      * This enum defines capabilities of the backend
0096      *
0097      * - SignalResumeFromSuspend: The backend is able to stream the @c resumeFromSuspend signal accurately
0098      */
0099     enum Capability { NoCapabilities = 0, SignalResumeFromSuspend = 1 };
0100     Q_ENUM(Capability)
0101 
0102     Q_DECLARE_FLAGS(Capabilities, Capability)
0103 
0104     /**
0105      * Initializes the backend. This function @b MUST be called before the backend is usable. Using
0106      * any method in BackendInterface without initializing it might lead to undefined behavior. The signal
0107      * @c backendReady or @c backendError will be streamed upon completion.
0108      *
0109      * @note Backend implementations @b MUST reimplement this function
0110      */
0111     virtual void init() = 0;
0112 
0113     /**
0114      * @returns the capabilities of the backend
0115      * @see PowerDevil::BackendInterface::Capability
0116      */
0117     Capabilities capabilities() const;
0118 
0119     /**
0120      * Retrieves the current estimated remaining time of the system batteries
0121      *
0122      * @return the current global estimated remaining time in milliseconds
0123      */
0124     qulonglong batteryRemainingTime() const;
0125 
0126     /**
0127      * Retrieves the current estimated remaining time of the system batteries,
0128      * with exponential moving average filter applied to the history records.
0129      *
0130      * @return the current global estimated remaining time in milliseconds
0131      */
0132     qulonglong smoothedBatteryRemainingTime() const;
0133 
0134     /**
0135      * Retrieves the current state of the system AC adapter.
0136      *
0137      * @return the current AC adapter state
0138      * @see PowerDevil::BackendInterface::AcAdapterState
0139      */
0140     AcAdapterState acAdapterState() const;
0141 
0142     /**
0143      * Retrieves the set of suspend methods supported by the system.
0144      *
0145      * @return the suspend methods supported by this system
0146      * @see PowerDevil::BackendInterface::SuspendMethod
0147      * @see PowerDevil::BackendInterface::SuspendMethods
0148      */
0149     SuspendMethods supportedSuspendMethods() const;
0150 
0151     /**
0152      * Requests a suspend of the system.
0153      *
0154      * @param method the suspend method to use
0155      * @return the job handling the operation
0156      */
0157     virtual KJob *suspend(SuspendMethod method) = 0;
0158 
0159     /**
0160      * Checks if brightness controls are enabled on this system.
0161      *
0162      * @return a list of the devices available to control
0163      */
0164     BrightnessControlsList brightnessControlsAvailable() const;
0165 
0166     /**
0167      * Gets the device brightness value.
0168      *
0169      * @param device the name of the device that you would like to control
0170      * @return the brightness of the device, as an integer from 0 to brightnessValueMax
0171      */
0172     virtual int brightness(BrightnessControlType type = Screen) const;
0173 
0174     /**
0175      * Gets the maximum device brightness value.
0176      *
0177      * @param device the name of the device that you would like to control
0178      * @return the maximum brightness of the device
0179      */
0180     virtual int brightnessMax(BrightnessControlType type = Screen) const;
0181 
0182     /**
0183      * Gets the maximum device brightness step.
0184      *
0185      * @param device the name of the device that you would like to control
0186      * @return the maximum brightness of the device
0187      */
0188     virtual int brightnessSteps(BrightnessControlType type = Screen) const;
0189 
0190     /**
0191      * @returns whether the lid is closed or not.
0192      */
0193     bool isLidClosed() const;
0194     /**
0195      * @returns whether the a lid is present
0196      */
0197     bool isLidPresent() const;
0198 
0199     void setLidPresent(bool present);
0200 
0201     /**
0202      * Sets the device brightness value.
0203      *
0204      * @param brightnessValue the desired device brightness, as an integer from 0 to brightnessValueMax
0205      * @param device the name of the device that you would like to control
0206      * @return true if the brightness change succeeded, false otherwise
0207      */
0208     virtual void setBrightness(int value, BrightnessControlType type = Screen) = 0;
0209 
0210     /**
0211      * Should be called when the user presses a brightness key.
0212      *
0213      * @param type the type of the brightness key press
0214      * @return the new brightness value, or -1 if it could not be changed or determined
0215      * @see PowerDevil::BrightnessLogic::BrightnessKeyType
0216      */
0217     virtual int brightnessKeyPressed(BrightnessLogic::BrightnessKeyType type, BrightnessControlType controlType = Screen) = 0;
0218 
0219     /**
0220      * Retrieves the capacities of the installed batteries in percentage.
0221      *
0222      * @returns A dictionary with the battery's capacity percentage mapped to the battery uuid.
0223      */
0224     QHash<QString, uint> capacities() const;
0225 
0226 Q_SIGNALS:
0227     /**
0228      * This signal is emitted when the AC adapter is plugged or unplugged.
0229      *
0230      * @param newState the new state of the AC adapter, it's one of the
0231      * type @see PowerDevil::BackendInterface::AcAdapterState
0232      */
0233     void acAdapterStateChanged(PowerDevil::BackendInterface::AcAdapterState newState);
0234 
0235     /**
0236      * This signal is emitted when a button has been pressed.
0237      *
0238      * @param buttonType the pressed button type, it's one of the
0239      * type @see PowerDevil::BackendInterface::ButtonType
0240      */
0241     void buttonPressed(PowerDevil::BackendInterface::ButtonType buttonType);
0242 
0243     /**
0244      * This signal is emitted when the brightness changes.
0245      *
0246      * @param brightnessInfo a copy of the current brightness information
0247      * @param type the device type
0248      */
0249     void brightnessChanged(const BrightnessLogic::BrightnessInfo &brightnessInfo, BrightnessControlType type);
0250 
0251     /**
0252      * This signal is emitted when the estimated battery remaining time changes.
0253      *
0254      * @param time the new remaining time
0255      */
0256     void batteryRemainingTimeChanged(qulonglong time);
0257 
0258     /**
0259      * This signal is emitted when the estimated battery remaining time changes.
0260      *
0261      * @param time the new remaining time
0262      */
0263     void smoothedBatteryRemainingTimeChanged(qulonglong time);
0264 
0265     /**
0266      * This signal is emitted when the backend is ready to be used
0267      *
0268      * @see init
0269      */
0270     void backendReady();
0271 
0272     /**
0273      * This signal is emitted if the backend could not be initialized
0274      *
0275      * @param error Details about the error occurred
0276      * @see init
0277      */
0278     void backendError(const QString &error);
0279 
0280     /**
0281      * This signal is emitted when the PC is resuming from suspension
0282      */
0283     void resumeFromSuspend();
0284 
0285     /**
0286      * This signal is emitted when the PC is about to suspend
0287      */
0288     void aboutToSuspend();
0289 
0290     /**
0291      * This signal is emitted when the laptop lid is closed or opened
0292      *
0293      * @param closed Whether the lid is now closed or not
0294      */
0295     void lidClosedChanged(bool closed);
0296 
0297 protected:
0298     void setCapabilities(Capabilities capabilities);
0299 
0300     void onBrightnessChanged(BrightnessControlType device, int value, int valueMax);
0301     void setBatteryEnergy(double energy);
0302     void setBatteryEnergyFull(double energy);
0303     void setBatteryRate(double rate, qulonglong timestamp);
0304     void setButtonPressed(PowerDevil::BackendInterface::ButtonType type);
0305     void setAcAdapterState(PowerDevil::BackendInterface::AcAdapterState state);
0306 
0307     void setCapacityForBattery(const QString &batteryId, uint percent);
0308 
0309     void setBackendIsReady(const BrightnessControlsList &availableBrightnessControls, SuspendMethods supportedSuspendMethods);
0310     void setBackendHasError(const QString &errorDetails);
0311 
0312     // Steps logic
0313     int calculateNextStep(int value, int valueMax, BrightnessControlType controlType, BrightnessLogic::BrightnessKeyType keyType);
0314 
0315 private:
0316     class Private;
0317     Private * const d;
0318 
0319     friend class Core;
0320 };
0321 
0322 }
0323 
0324 Q_DECLARE_OPERATORS_FOR_FLAGS(PowerDevil::BackendInterface::Capabilities)
0325 Q_DECLARE_OPERATORS_FOR_FLAGS(PowerDevil::BackendInterface::SuspendMethods)
0326 
0327 #endif // POWERDEVIL_BACKENDINTERFACE_H