1 /***************************************************************************
2  *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
18  ***************************************************************************/
19 
20 
21 #ifndef POWERDEVILCORE_H
22 #define POWERDEVILCORE_H
23 
24 #include "powerdevilbackendinterface.h"
25 
26 #include <QPointer>
27 #include <QSet>
28 #include <QStringList>
29 
30 #include <QDBusMessage>
31 #include <QDBusError>
32 #include <QDBusContext>
33 #include <QDBusObjectPath>
34 
35 #include <QSocketNotifier>
36 
37 #include <KSharedConfig>
38 
39 namespace KActivities
40 {
41     class Consumer;
42 } // namespace KActivities
43 
44 class QDBusServiceWatcher;
45 class QTimer;
46 class KNotification;
47 
48 namespace Solid {
49 class Battery;
50 }
51 
52 namespace PowerDevil
53 {
54 
55 class BackendInterface;
56 class Action;
57 
58 struct WakeupInfo {
59     QString service;
60     QDBusObjectPath path;
61     int cookie;
62     qint64 timeout;
63 };
64 
65 class Q_DECL_EXPORT Core : public QObject, protected QDBusContext
66 {
67     Q_OBJECT
68     Q_DISABLE_COPY(Core)
69     Q_CLASSINFO("D-Bus Interface", "org.kde.Solid.PowerManagement")
70 
71 public:
72     explicit Core(QObject* parent);
73     ~Core() override;
74 
75     void reloadProfile(int state);
76 
77     void emitRichNotification(const QString &evid, const QString &title, const QString &message = QString());
78 
79     void emitNotification(const QString &eventId, const QString &title, const QString &message, const QString &iconName);
80 
81     bool emitBatteryChargePercentNotification(int currentPercent, int previousPercent, const QString &udi = QString());
82 
83     BackendInterface *backend();
84 
85     // More...
86 
87 public Q_SLOTS:
88     void loadCore(PowerDevil::BackendInterface *backend);
89     // Set of common action - useful for the DBus interface
90     uint backendCapabilities();
91     void refreshStatus();
92     void reparseConfiguration();
93 
94     QString currentProfile() const;
95     void loadProfile(bool force = false);
96 
97     qulonglong batteryRemainingTime() const;
98 
99     bool isLidClosed() const;
100     bool isLidPresent() const;
101     bool isActionSupported(const QString &actionName);
102     bool hasDualGpu() const;
103     int chargeStartThreshold() const;
104     int chargeStopThreshold() const;
105 
106     // service - dbus interface to ping when wakeup is done
107     // path - dbus path on service
108     // cookie - data to pass back
109     // silent - true if silent wakeup is needed
110     uint scheduleWakeup(const QString &service, const QDBusObjectPath &path, qint64 timeout);
111     void wakeup();
112     void clearWakeup(int cookie);
113 
114 Q_SIGNALS:
115     void coreReady();
116     void profileChanged(const QString &newProfile);
117     void configurationReloaded();
118     void batteryRemainingTimeChanged(qulonglong time);
119     void lidClosedChanged(bool closed);
120     void chargeStartThresholdChanged(int threshold);
121     void chargeStopThresholdChanged(int threshold);
122 
123 private:
124     void registerActionTimeout(Action *action, int timeout);
125     void unregisterActionTimeouts(Action *action);
126     void handleLowBattery(int percent);
127     void handleCriticalBattery(int percent);
128 
129     void readChargeThreshold();
130 
131     /**
132      * Computes the current global charge percentage.
133      * Sum of all battery charges.
134      */
135     int currentChargePercent() const;
136 
137     friend class Action;
138 
139     bool m_hasDualGpu;
140     int m_chargeStartThreshold = 0;
141     int m_chargeStopThreshold = 100;
142 
143     BackendInterface *m_backend;
144 
145     QDBusServiceWatcher *m_notificationsWatcher;
146     bool m_notificationsReady = false;
147 
148     KSharedConfigPtr m_profilesConfig;
149 
150     QString m_currentProfile;
151 
152     QHash<QString, int> m_batteriesPercent;
153     QHash<QString, int> m_peripheralBatteriesPercent;
154     QHash<QString, bool> m_batteriesCharged;
155 
156     QPointer<KNotification> m_lowBatteryNotification;
157     QTimer *m_criticalBatteryTimer;
158     QPointer<KNotification> m_criticalBatteryNotification;
159 
160     KActivities::Consumer *m_activityConsumer;
161 
162     // Idle time management
163     QHash< Action*, QList< int > > m_registeredActionTimeouts;
164     QSet<Action *> m_pendingResumeFromIdleActions;
165     bool m_pendingWakeupEvent;
166 
167     // Scheduled wakeups and alarms
168     QList<WakeupInfo> m_scheduledWakeups;
169     int m_lastWakeupCookie = 0;
170     int m_timerFd = -1;
171     QSocketNotifier *m_timerFdSocketNotifier = nullptr;
172 
173     // Activity inhibition management
174     QHash< QString, int > m_sessionActivityInhibit;
175     QHash< QString, int > m_screenActivityInhibit;
176 
177 private Q_SLOTS:
178     void onBackendReady();
179     void onAcAdapterStateChanged(PowerDevil::BackendInterface::AcAdapterState);
180     void onBatteryChargePercentChanged(int,const QString&);
181     void onBatteryChargeStateChanged(int,const QString&);
182     void onBatteryRemainingTimeChanged(qulonglong);
183     void onKIdleTimeoutReached(int,int);
184     void onResumingFromIdle();
185     void onDeviceAdded(const QString &udi);
186     void onDeviceRemoved(const QString &udi);
187     void onCriticalBatteryTimerExpired();
188     void onNotificationTimeout();
189     void onServiceRegistered(const QString &service);
190     void onLidClosedChanged(bool closed);
191     void onAboutToSuspend();
192     // handlers for handling wakeup dbus call
193     void resetAndScheduleNextWakeup();
194     void timerfdEventHandler();
195 };
196 
197 }
198 
199 #endif // POWERDEVILCORE_H
200