9 #define LOC QString("PowerDBus: ")
11 #define FREE_SERVICE (QString("org.freedesktop."))
12 #define FREE_PATH (QString("/org/freedesktop/"))
13 #define UPOWER (QString("UPower"))
14 #define LOGIN1 (QString("login1"))
15 #define UPOWER_SERVICE (FREE_SERVICE + UPOWER)
16 #define UPOWER_PATH (FREE_PATH + UPOWER)
17 #define UPOWER_INTERFACE (UPOWER_SERVICE)
18 #define LOGIN1_SERVICE (FREE_SERVICE + LOGIN1)
19 #define LOGIN1_PATH (FREE_PATH + LOGIN1)
20 #define LOGIN1_INTERFACE (LOGIN1_SERVICE + QString(".Manager"))
44 QMutexLocker locker(&
s_lock);
45 static bool s_available =
false;
46 static bool s_checked =
false;
50 auto* upower =
new QDBusInterface(
52 auto* login1 =
new QDBusInterface(
54 s_available = upower->isValid() || login1->isValid();
73 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Closing interfaces");
103 LOG(VB_GENERAL, LOG_ERR,
LOC +
"No UPower interface. Unable to monitor battery state");
105 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"No login1 interface. Cannot change system power state");
116 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Failed to listen for sleep events");
121 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Failed to listen for shutdown events");
128 QDBusReply<QList<QDBusObjectPath> > response =
130 if (response.isValid())
132 QList devices = response.value();
133 for (
const auto& device : qAsConst(devices))
139 LOG(VB_GENERAL, LOG_ERR,
"Failed to register for Changed");
145 LOG(VB_GENERAL, LOG_ERR,
"Failed to register for DeviceChanged");
151 LOG(VB_GENERAL, LOG_ERR,
"Failed to register for DeviceAdded");
157 LOG(VB_GENERAL, LOG_ERR,
"Failed to register for DeviceRemoved");
225 if (property.isValid() && property.toBool())
231 if (property.isValid() && property.toBool())
251 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Trying to delay system %1 for %2 seconds")
267 QMutexLocker locker(&
s_lock);
299 auto time = nowAsDuration<std::chrono::milliseconds>();
302 std::chrono::milliseconds millisecs = time + Delay;
310 QDBusReply<void> reply =
312 static_cast<qint64
>(millisecs.count()));
314 if (reply.isValid() && !reply.error().isValid())
318 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"%1 scheduled via logind")
324 LOG(VB_GENERAL, LOG_DEBUG,
LOC +
325 QString(
"Failed to schedule %1 - falling back to default behaviour")
327 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"Error %1 Message %2")
328 .arg(reply.error().name(), reply.error().message()));
346 QMutexLocker locker(&
s_lock);
355 QMutexLocker locker(&
s_lock);
363 QMutexLocker locker(&
s_lock);
368 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Added UPower.Device '%1'").arg(
Device.path()));
375 QMutexLocker locker(&
s_lock);
380 LOG(VB_GENERAL, LOG_INFO, QString(
"Removed UPower.Device '%1'").arg(
Device.path()));
392 QMutexLocker locker(&
s_lock);
407 QDBusReply<QString> cansuspend =
m_logindInterface->call(QLatin1String(
"CanSuspend"));
408 if (cansuspend.isValid() && cansuspend.value() ==
"yes")
410 QDBusReply<QString> canshutdown =
m_logindInterface->call(QLatin1String(
"CanPowerOff"));
411 if (canshutdown.isValid() && canshutdown.value() ==
"yes")
413 QDBusReply<QString> canrestart =
m_logindInterface->call(QLatin1String(
"CanReboot"));
414 if (canrestart.isValid() && canrestart.value() ==
"yes")
416 QDBusReply<QString> canhibernate =
m_logindInterface->call(QLatin1String(
"CanHibernate"));
417 if (canhibernate.isValid() && canhibernate.value() ==
"yes")
419 QDBusReply<QString> canhybrid =
m_logindInterface->call(QLatin1String(
"CanHybridSleep"));
420 if (canhybrid.isValid() && canhybrid.value() ==
"yes")
426 auto value = std::chrono::microseconds(delay.toUInt());
428 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"Max inhibit delay: %1seconds")
440 QMutexLocker locker(&
s_lock);
448 if (level >= 0 && level <= 100)
451 total +=
static_cast<qreal
>(level);
456 newlevel = lround(total / count);
462 if (acpower.isValid() && acpower.toBool())
473 if (interface.isValid())
475 QVariant battery = interface.property(
"IsRechargeable");
476 if (battery.isValid() && battery.toBool())
478 QVariant percent = interface.property(
"Percentage");
479 if (percent.isValid())
481 int result =
static_cast<int>(lroundf(percent.toFloat() * 100.0F));
482 if (result >= 0 && result <= 100)
491 QVariant
type = interface.property(
"Type");
494 QString typestr =
type.toString();
495 if (typestr ==
"Line Power")
497 if (typestr ==
"Ups")
518 QMutexLocker locker(&
s_lock);
522 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Already hold delay lock");
531 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Unknown delay requests");
535 QDBusReply<QDBusUnixFileDescriptor> reply =
537 QLatin1String(
"MythTV"), QLatin1String(
""), QLatin1String(
"delay"));
538 if (!reply.isValid())
540 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Failed to delay %1: %2")
541 .arg(
types.join(
","), reply.error().message()));
557 QMutexLocker locker(&
s_lock);
564 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"Releasing delay FD: %1").arg(
m_lockHandle));