1 /*  This file is part of the KDE project
2     Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
3     Copyright (C) 2008-2010 Dario Freddi <drf@kde.org>
4     Copyright (C) 2010 Alejandro Fiestas <alex@eyeos.org>
5     Copyright (C) 2015 Kai Uwe Broulik <kde@privat.broulik.de>
6 
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License version 2 as published by the Free Software Foundation.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Library General Public License for more details.
15 
16     You should have received a copy of the GNU Library General Public License
17     along with this library; see the file COPYING.LIB.  If not, write to
18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19     Boston, MA 02110-1301, USA.
20 
21 */
22 
23 #ifndef POWERDEVILUPOWERBACKEND_H
24 #define POWERDEVILUPOWERBACKEND_H
25 
26 #include <powerdevilbackendinterface.h>
27 
28 #include <QDBusConnection>
29 #include <QDBusInterface>
30 
31 #include "upower_device_interface.h"
32 #include "upower_interface.h"
33 #include "upower_kbdbacklight_interface.h"
34 #include "udevqt.h"
35 
36 #define UPOWER_SERVICE "org.freedesktop.UPower"
37 #define UPOWER_PATH "/org/freedesktop/UPower"
38 #define UPOWER_IFACE "org.freedesktop.UPower"
39 #define UPOWER_IFACE_DEVICE "org.freedesktop.UPower.Device"
40 
41 #define LOGIN1_SERVICE "org.freedesktop.login1"
42 #define CONSOLEKIT2_SERVICE "org.freedesktop.ConsoleKit"
43 
44 class QPropertyAnimation;
45 class QTimer;
46 class DDCutilBrightness;
47 class Q_DECL_EXPORT PowerDevilUPowerBackend : public PowerDevil::BackendInterface
48 {
49     Q_OBJECT
50     Q_DISABLE_COPY(PowerDevilUPowerBackend)
51     Q_PLUGIN_METADATA(IID "org.kde.powerdevil.upowerbackend");
52 
53 public:
54     explicit PowerDevilUPowerBackend(QObject* parent = nullptr);
55     ~PowerDevilUPowerBackend() override;
56 
57     void init() override;
58     static bool isAvailable();
59 
60     int brightness(BrightnessControlType type = Screen) const override;
61     int brightnessMax(BrightnessControlType type = Screen) const override;
62 
63     int brightnessKeyPressed(PowerDevil::BrightnessLogic::BrightnessKeyType type, BrightnessControlType controlType) override;
64     void setBrightness(int value, PowerDevil::BackendInterface::BrightnessControlType type = Screen) override;
65     KJob* suspend(PowerDevil::BackendInterface::SuspendMethod method) override;
66 
67 Q_SIGNALS:
68     void brightnessSupportQueried(bool available);
69 
70 private:
71     void enumerateDevices();
72     void addDevice(const QString &);
73 
74 private Q_SLOTS:
75     void updateDeviceProps();
76     void slotDeviceAdded(const QString &);
77     void slotDeviceRemoved(const QString &);
78     void slotDeviceAdded(const QDBusObjectPath & path);
79     void slotDeviceRemoved(const QDBusObjectPath & path);
80     void slotDeviceChanged(const QString &);
81     void slotPropertyChanged();
82     void slotLogin1PrepareForSleep(bool active);
83     void slotScreenBrightnessChanged();
84     void onDeviceChanged(const UdevQt::Device &device);
85     void onKeyboardBrightnessChanged(int);
86 
87     void onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps);
88     void onDevicePropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps);
89 
90 private:
91     void animationValueChanged(const QVariant &value);
92     void initWithBrightness(bool brightnessSupport);
93 
94     // upower devices
95     QMap<QString, OrgFreedesktopUPowerDeviceInterface *> m_devices;
96     OrgFreedesktopUPowerDeviceInterface *m_displayDevice;
97 
98     // brightness
99     QMap<BrightnessControlType, int> m_cachedBrightnessMap;
100     DDCutilBrightness *m_ddcBrightnessControl;
101 
102     OrgFreedesktopUPowerInterface *m_upowerInterface;
103     OrgFreedesktopUPowerKbdBacklightInterface *m_kbdBacklight;
104     int m_kbdMaxBrightness;
105     int m_brightnessMax = 0;
106 
107     QPropertyAnimation *m_brightnessAnimation = nullptr;
108 
109     QTimer *m_brightnessAnimationTimer = nullptr;
110 
111     // login1 interface
112     QPointer<QDBusInterface> m_login1Interface;
113     bool m_useUPowerSuspend = false;
114 
115     // buttons
116     bool m_lidIsPresent;
117     bool m_lidIsClosed;
118     bool m_onBattery;
119 
120     // property if brightness control is leds subsystem
121     bool m_isLedBrightnessControl;
122 
123     //helper path
124     QString m_syspath;
125 };
126 
127 #endif // POWERDEVILUPOWERBACKEND_H
128