• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Solid

  • solid
  • solid
powermanagement.cpp
Go to the documentation of this file.
1/*
2 Copyright 2006-2007 Kevin Ottens <ervin@kde.org>
3 Copyright 2013 Lukas Tinkl <ltinkl@redhat.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) version 3, or any
9 later version accepted by the membership of KDE e.V. (or its
10 successor approved by the membership of KDE e.V.), which shall
11 act as a proxy defined in Section 6 of version 3 of the license.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "powermanagement.h"
23#include "powermanagement_p.h"
24
25#include "soliddefs_p.h"
26
27#include <QtCore/QCoreApplication>
28
29SOLID_GLOBAL_STATIC(Solid::PowerManagementPrivate, globalPowerManager)
30
31Solid::PowerManagementPrivate::PowerManagementPrivate()
32 : managerIface(QLatin1String("org.freedesktop.PowerManagement"),
33 QLatin1String("/org/freedesktop/PowerManagement"),
34 QDBusConnection::sessionBus()),
35 policyAgentIface(QLatin1String("org.kde.Solid.PowerManagement.PolicyAgent"),
36 QLatin1String("/org/kde/Solid/PowerManagement/PolicyAgent"),
37 QDBusConnection::sessionBus()),
38 inhibitIface(QLatin1String("org.freedesktop.PowerManagement.Inhibit"),
39 QLatin1String("/org/freedesktop/PowerManagement/Inhibit"),
40 QDBusConnection::sessionBus()),
41 serviceWatcher(QLatin1String("org.kde.Solid.PowerManagement"),
42 QDBusConnection::sessionBus(),
43 QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration),
44 powerSaveStatus(false)
45{
46 serviceWatcher.addWatchedService(QLatin1String("org.freedesktop.PowerManagement"));
47
48 connect(&managerIface, SIGNAL(CanSuspendChanged(bool)),
49 this, SLOT(slotCanSuspendChanged(bool)));
50 connect(&managerIface, SIGNAL(CanHibernateChanged(bool)),
51 this, SLOT(slotCanHibernateChanged(bool)));
52 connect(&managerIface, SIGNAL(CanHybridSuspendChanged(bool)),
53 this, SLOT(slotCanHybridSuspendChanged(bool)));
54 connect(&managerIface, SIGNAL(PowerSaveStatusChanged(bool)),
55 this, SLOT(slotPowerSaveStatusChanged(bool)));
56 connect(&serviceWatcher, SIGNAL(serviceRegistered(QString)),
57 this, SLOT(slotServiceRegistered(QString)));
58 connect(&serviceWatcher, SIGNAL(serviceUnregistered(QString)),
59 this, SLOT(slotServiceUnregistered(QString)));
60
61 // If the service is registered, trigger the connection immediately
62 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.Solid.PowerManagement"))) {
63 slotServiceRegistered(QLatin1String("org.kde.Solid.PowerManagement"));
64 }
65 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"))) {
66 slotServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"));
67 }
68}
69
70Solid::PowerManagementPrivate::~PowerManagementPrivate()
71{
72}
73
74Solid::PowerManagement::Notifier::Notifier()
75{
76}
77
78bool Solid::PowerManagement::appShouldConserveResources()
79{
80 return globalPowerManager->powerSaveStatus;
81}
82
83QSet<Solid::PowerManagement::SleepState> Solid::PowerManagement::supportedSleepStates()
84{
85 return globalPowerManager->supportedSleepStates;
86}
87
88void Solid::PowerManagement::requestSleep(SleepState state, QObject *receiver, const char *member)
89{
90 Q_UNUSED(receiver)
91 Q_UNUSED(member)
92
93 if (!globalPowerManager->supportedSleepStates.contains(state)) {
94 return;
95 }
96
97 switch (state)
98 {
99 case StandbyState:
100 case SuspendState:
101 globalPowerManager->managerIface.Suspend();
102 break;
103 case HibernateState:
104 globalPowerManager->managerIface.Hibernate();
105 break;
106 }
107}
108
109int Solid::PowerManagement::beginSuppressingSleep(const QString &reason)
110{
111 QDBusReply<uint> reply;
112 if (globalPowerManager->policyAgentIface.isValid()) {
113 reply = globalPowerManager->policyAgentIface.AddInhibition(
114 (uint)PowerManagementPrivate::InterruptSession,
115 QCoreApplication::applicationName(), reason);
116 } else {
117 // Fallback to the fd.o Inhibit interface
118 reply = globalPowerManager->inhibitIface.Inhibit(QCoreApplication::applicationName(), reason);
119 }
120
121 if (reply.isValid())
122 return reply;
123 else
124 return -1;
125}
126
127bool Solid::PowerManagement::stopSuppressingSleep(int cookie)
128{
129 if (globalPowerManager->policyAgentIface.isValid()) {
130 return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
131 } else {
132 // Fallback to the fd.o Inhibit interface
133 return globalPowerManager->inhibitIface.UnInhibit(cookie).isValid();
134 }
135}
136
137int Solid::PowerManagement::beginSuppressingScreenPowerManagement(const QString& reason)
138{
139 if (globalPowerManager->policyAgentIface.isValid()) {
140 QDBusReply<uint> reply = globalPowerManager->policyAgentIface.AddInhibition(
141 (uint)PowerManagementPrivate::ChangeScreenSettings,
142 QCoreApplication::applicationName(), reason);
143
144 if (reply.isValid()) {
145 QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.ScreenSaver"),
146 QLatin1String("/ScreenSaver"),
147 QLatin1String("org.freedesktop.ScreenSaver"),
148 QLatin1String("Inhibit"));
149 message << QCoreApplication::applicationName();
150 message << reason;
151
152 QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
153 ssReply.waitForFinished();
154 if (ssReply.isValid()) {
155 globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
156 }
157
158 return reply;
159 } else {
160 return -1;
161 }
162 } else {
163 // No way to fallback on something, hence return failure
164 return -1;
165 }
166}
167
168bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie)
169{
170 if (globalPowerManager->policyAgentIface.isValid()) {
171 bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
172
173 if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
174 QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.ScreenSaver"),
175 QLatin1String("/ScreenSaver"),
176 QLatin1String("org.freedesktop.ScreenSaver"),
177 QLatin1String("UnInhibit"));
178 message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
179 QDBusConnection::sessionBus().asyncCall(message);
180 }
181
182 return result;
183 } else {
184 // No way to fallback on something, hence return failure
185 return false;
186 }
187}
188
189Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
190{
191 return globalPowerManager;
192}
193
194void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
195{
196 if (supportedSleepStates.contains(Solid::PowerManagement::SuspendState) == newState) {
197 return;
198 }
199
200 if (newState) {
201 supportedSleepStates+= Solid::PowerManagement::SuspendState;
202 } else {
203 supportedSleepStates-= Solid::PowerManagement::SuspendState;
204 }
205}
206
207void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
208{
209 if (supportedSleepStates.contains(Solid::PowerManagement::HibernateState) == newState) {
210 return;
211 }
212
213 if (newState) {
214 supportedSleepStates+= Solid::PowerManagement::HibernateState;
215 } else {
216 supportedSleepStates-= Solid::PowerManagement::HibernateState;
217 }
218}
219
220void Solid::PowerManagementPrivate::slotCanHybridSuspendChanged(bool newState)
221{
222 if (supportedSleepStates.contains(Solid::PowerManagement::HybridSuspendState) == newState) {
223 return;
224 }
225
226 if (newState) {
227 supportedSleepStates+= Solid::PowerManagement::HybridSuspendState;
228 } else {
229 supportedSleepStates-= Solid::PowerManagement::HybridSuspendState;
230 }
231}
232
233void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
234{
235 if (powerSaveStatus == newState) {
236 return;
237 }
238
239 powerSaveStatus = newState;
240 emit appShouldConserveResourcesChanged(powerSaveStatus);
241}
242
243void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
244{
245 if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
246 // Load all the properties
247 QDBusPendingReply<bool> suspendReply = managerIface.CanSuspend();
248 suspendReply.waitForFinished();
249 slotCanSuspendChanged(suspendReply.isValid() ? suspendReply.value() : false);
250
251 QDBusPendingReply<bool> hibernateReply = managerIface.CanHibernate();
252 hibernateReply.waitForFinished();
253 slotCanHibernateChanged(hibernateReply.isValid() ? hibernateReply.value() : false);
254
255 QDBusPendingReply<bool> hybridSuspendReply = managerIface.CanHybridSuspend();
256 hybridSuspendReply.waitForFinished();
257 slotCanHybridSuspendChanged(hybridSuspendReply.isValid() ? hybridSuspendReply.value() : false);
258
259 QDBusPendingReply<bool> saveStatusReply = managerIface.GetPowerSaveStatus();
260 saveStatusReply.waitForFinished();
261 slotPowerSaveStatusChanged(saveStatusReply.isValid() ? saveStatusReply.value() : false);
262 } else {
263 // Is the resume signal available?
264 QDBusMessage call = QDBusMessage::createMethodCall(QLatin1String("org.kde.Solid.PowerManagement"),
265 QLatin1String("/org/kde/Solid/PowerManagement"),
266 QLatin1String("org.kde.Solid.PowerManagement"),
267 QLatin1String("backendCapabilities"));
268 QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
269 reply.waitForFinished();
270
271 if (reply.isValid() && reply.value() > 0) {
272 // Connect the signal
273 QDBusConnection::sessionBus().connect(QLatin1String("org.kde.Solid.PowerManagement"),
274 QLatin1String("/org/kde/Solid/PowerManagement/Actions/SuspendSession"),
275 QLatin1String("org.kde.Solid.PowerManagement.Actions.SuspendSession"),
276 QLatin1String("resumingFromSuspend"),
277 this,
278 SIGNAL(resumingFromSuspend()));
279 }
280 }
281}
282
283void Solid::PowerManagementPrivate::slotServiceUnregistered(const QString &serviceName)
284{
285 if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
286 // Reset the values
287 slotCanSuspendChanged(false);
288 slotCanHibernateChanged(false);
289 slotCanHybridSuspendChanged(false);
290 slotPowerSaveStatusChanged(false);
291 } else {
292 // Disconnect the signal
293 QDBusConnection::sessionBus().disconnect(QLatin1String("org.kde.Solid.PowerManagement"),
294 QLatin1String("/org/kde/Solid/PowerManagement"),
295 QLatin1String("org.kde.Solid.PowerManagement"),
296 QLatin1String("resumingFromSuspend"),
297 this,
298 SIGNAL(resumingFromSuspend()));
299 }
300}
301
302#include "powermanagement_p.moc"
303#include "powermanagement.moc"
304
QObject
Solid::PowerManagementPrivate
Definition: powermanagement_p.h:35
Solid::PowerManagementPrivate::slotCanHibernateChanged
void slotCanHibernateChanged(bool newState)
Definition: powermanagement.cpp:207
Solid::PowerManagementPrivate::slotServiceRegistered
void slotServiceRegistered(const QString &serviceName)
Definition: powermanagement.cpp:243
Solid::PowerManagementPrivate::ChangeScreenSettings
@ ChangeScreenSettings
Definition: powermanagement_p.h:42
Solid::PowerManagementPrivate::InterruptSession
@ InterruptSession
Definition: powermanagement_p.h:40
Solid::PowerManagementPrivate::slotServiceUnregistered
void slotServiceUnregistered(const QString &serviceName)
Definition: powermanagement.cpp:283
Solid::PowerManagementPrivate::slotCanHybridSuspendChanged
void slotCanHybridSuspendChanged(bool newState)
Definition: powermanagement.cpp:220
Solid::PowerManagementPrivate::slotCanSuspendChanged
void slotCanSuspendChanged(bool newState)
Definition: powermanagement.cpp:194
Solid::PowerManagementPrivate::slotPowerSaveStatusChanged
void slotPowerSaveStatusChanged(bool newState)
Definition: powermanagement.cpp:233
Solid::PowerManagementPrivate::~PowerManagementPrivate
~PowerManagementPrivate()
Definition: powermanagement.cpp:70
Solid::PowerManagement::Notifier
Definition: powermanagement.h:132
Solid::PowerManagement::Notifier::Notifier
Notifier()
Definition: powermanagement.cpp:74
Solid::PowerManagement::appShouldConserveResources
SOLID_EXPORT bool appShouldConserveResources()
Retrieves a high level indication of how applications should behave according to the power management...
Definition: powermanagement.cpp:78
Solid::PowerManagement::stopSuppressingScreenPowerManagement
SOLID_EXPORT bool stopSuppressingScreenPowerManagement(int cookie)
Tell the power management that a particular screen power management suppression is no longer needed.
Definition: powermanagement.cpp:168
Solid::PowerManagement::beginSuppressingSleep
SOLID_EXPORT int beginSuppressingSleep(const QString &reason=QString())
Tell the power management subsystem to suppress automatic system sleep until further notice.
Definition: powermanagement.cpp:109
Solid::PowerManagement::SleepState
SleepState
This enum type defines the different suspend methods.
Definition: powermanagement.h:54
Solid::PowerManagement::HibernateState
@ HibernateState
Definition: powermanagement.h:54
Solid::PowerManagement::HybridSuspendState
@ HybridSuspendState
Definition: powermanagement.h:56
Solid::PowerManagement::SuspendState
@ SuspendState
Definition: powermanagement.h:54
Solid::PowerManagement::StandbyState
@ StandbyState
Definition: powermanagement.h:54
Solid::PowerManagement::requestSleep
SOLID_EXPORT void requestSleep(SleepState state, QObject *receiver, const char *member)
Requests that the system go to sleep.
Definition: powermanagement.cpp:88
Solid::PowerManagement::beginSuppressingScreenPowerManagement
SOLID_EXPORT int beginSuppressingScreenPowerManagement(const QString &reason=QString())
Tell the power management subsystem to suppress automatic screen power management until further notic...
Definition: powermanagement.cpp:137
Solid::PowerManagement::stopSuppressingSleep
SOLID_EXPORT bool stopSuppressingSleep(int cookie)
Tell the power management that a particular sleep suppression is no longer needed.
Definition: powermanagement.cpp:127
Solid::PowerManagement::notifier
SOLID_EXPORT Notifier * notifier()
Definition: powermanagement.cpp:189
Solid::PowerManagement::supportedSleepStates
SOLID_EXPORT QSet< SleepState > supportedSleepStates()
Retrieves the set of suspend methods supported by the system.
Definition: powermanagement.cpp:83
Solid
Definition: acadapter.h:29
powermanagement.h
powermanagement_p.h
soliddefs_p.h
SOLID_GLOBAL_STATIC
#define SOLID_GLOBAL_STATIC(TYPE, NAME)
Definition: soliddefs_p.h:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Solid

Skip menu "Solid"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal