Skip to content

Commit

Permalink
Wait 5s before enforcing an inhibition
Browse files Browse the repository at this point in the history
Whenever Chrome plays a sound it posts an inhibition. This is great, except that it does that
also when playing a short sound, eg. when receiving a message.
This patch makes PowerDevil wait 5 seconds before actually enforcing the inhibition. In
any case I don't want to have the system wake up for any such short inhibitions.

Also cleanup; the inhibition and inhibition with explicit dbus service methods were
virtually identical.

REVIEW: 126145
BUG: 352235
FIXED-IN: 5.6.0
  • Loading branch information
kbroulik committed Dec 14, 2015
1 parent 13788d4 commit eca7913
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
77 changes: 42 additions & 35 deletions daemon/powerdevilpolicyagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QDBusPendingReply>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
#include <QTimer>

#include <KIdleTime>

Expand Down Expand Up @@ -429,56 +430,55 @@ void PolicyAgent::finishSessionInterruption()
m_sessionIsBeingInterrupted = false;
}

uint PolicyAgent::addInhibitionWithExplicitDBusService(uint types, const QString& appName,
const QString& reason, const QString& service)
uint PolicyAgent::addInhibitionWithExplicitDBusService(uint types, const QString &appName,
const QString &reason, const QString &service)
{
++m_lastCookie;

m_cookieToAppName.insert(m_lastCookie, qMakePair<QString, QString>(appName, reason));
const int cookie = m_lastCookie; // when the Timer below fires, m_lastCookie might be different already

if (!m_busWatcher.isNull() && !service.isEmpty()) {
m_cookieToBusService.insert(m_lastCookie, service);
m_busWatcher.data()->addWatchedService(service);
}

qCDebug(POWERDEVIL) << "Added inhibition from an explicit DBus service, " << service << ", with cookie " <<
m_lastCookie << " from " << appName << " with " << reason;
m_pendingInhibitions.append(cookie);

addInhibitionTypeHelper(m_lastCookie, static_cast< PolicyAgent::RequiredPolicies >(types));
qCDebug(POWERDEVIL) << "Scheduling inhibition from" << service << appName << "with cookie"
<< cookie << "and reason" << reason;

emit InhibitionsChanged({ {qMakePair(appName, reason)} }, QStringList());
// wait 5s before actually enforcing the inhibition
// there might be short interruptions (such as receiving a message) where an app might automatically
// post an inhibition but we don't want the system to constantly wakeup because of this
QTimer::singleShot(5000, this, [=] {
qCDebug(POWERDEVIL) << "Enforcing inhibition from" << service << appName << "with cookie"
<< cookie << "and reason" << reason;

return m_lastCookie;
}

uint PolicyAgent::AddInhibition(uint types,
const QString& appName,
const QString& reason)
{
++m_lastCookie;
if (!m_pendingInhibitions.contains(cookie)) {
qCDebug(POWERDEVIL) << "By the time we wanted to enforce the inhibition it was already gone; discarding it";
return;
}

m_cookieToAppName.insert(m_lastCookie, qMakePair<QString, QString>(appName, reason));
m_cookieToAppName.insert(cookie, qMakePair<QString, QString>(appName, reason));

// Retrieve the service, if we've been called from DBus
if (calledFromDBus() && !m_busWatcher.isNull()) {
if (!message().service().isEmpty()) {
qCDebug(POWERDEVIL) << "DBus service " << message().service() << " is requesting inhibition";
m_cookieToBusService.insert(m_lastCookie, message().service());
m_busWatcher.data()->addWatchedService(message().service());
if (!m_busWatcher.isNull() && !service.isEmpty()) {
m_cookieToBusService.insert(cookie, service);
m_busWatcher.data()->addWatchedService(service);
}
}

qCDebug(POWERDEVIL) << "Added inhibition with cookie " << m_lastCookie << " from " <<
appName << " with " << reason;
addInhibitionTypeHelper(cookie, static_cast< PolicyAgent::RequiredPolicies >(types));

addInhibitionTypeHelper(m_lastCookie, static_cast< PolicyAgent::RequiredPolicies >(types));
emit InhibitionsChanged({ {qMakePair(appName, reason)} }, {});

emit InhibitionsChanged({ {qMakePair(appName, reason)} }, QStringList());
m_pendingInhibitions.removeOne(cookie);
});

return m_lastCookie;
return cookie;
}


uint PolicyAgent::AddInhibition(uint types, const QString &appName, const QString &reason)
{
if (calledFromDBus()) {
return addInhibitionWithExplicitDBusService(types, appName, reason, message().service());
} else {
return addInhibitionWithExplicitDBusService(types, appName, reason, QString());
}
}

void PolicyAgent::addInhibitionTypeHelper(uint cookie, PolicyAgent::RequiredPolicies types)
{
Expand Down Expand Up @@ -518,7 +518,14 @@ void PolicyAgent::addInhibitionTypeHelper(uint cookie, PolicyAgent::RequiredPoli

void PolicyAgent::ReleaseInhibition(uint cookie)
{
qCDebug(POWERDEVIL) << "Released inhibition with cookie " << cookie;
qCDebug(POWERDEVIL) << "Releasing inhibition with cookie " << cookie;

if (m_pendingInhibitions.contains(cookie)) {
qCDebug(POWERDEVIL) << "It was only scheduled for inhibition but not enforced yet, just discarding it";
m_pendingInhibitions.removeOne(cookie);
return;
}

emit InhibitionsChanged(QList<InhibitionInfo>(), { {m_cookieToAppName.value(cookie).first} });
m_cookieToAppName.remove(cookie);

Expand Down
3 changes: 3 additions & 0 deletions daemon/powerdevilpolicyagent.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QHash>
#include <QStringList>
#include <QWeakPointer>
#include <QVector>

#include <QDBusContext>
#include <QDBusUnixFileDescriptor>
Expand Down Expand Up @@ -134,6 +135,8 @@ private Q_SLOTS:
QHash< uint, QString > m_cookieToBusService;
QHash< RequiredPolicy, QList< uint > > m_typesToCookie;

QVector<int> m_pendingInhibitions;

uint m_lastCookie;

QWeakPointer< QDBusServiceWatcher > m_busWatcher;
Expand Down

0 comments on commit eca7913

Please sign in to comment.