ScreenLockDetector/src/platform/powermonitor_linux.cpp

79 lines
2.5 KiB
C++
Raw Normal View History

#include "powermonitor_linux.h"
#ifdef Q_OS_LINUX
#include <QDebug>
PowerMonitorLinux::PowerMonitorLinux(QObject *parent)
: PowerMonitorBase(parent)
, m_login1Interface(nullptr)
{
}
PowerMonitorLinux::~PowerMonitorLinux()
{
if (m_login1Interface) {
delete m_login1Interface;
m_login1Interface = nullptr;
}
}
bool PowerMonitorLinux::initialize()
{
return initLogin1Dbus();
}
bool PowerMonitorLinux::initLogin1Dbus()
{
// 连接 org.freedesktop.login1 服务(系统总线)
m_login1Interface = new QDBusInterface(
"org.freedesktop.login1", // DBus 服务名
"/org/freedesktop/login1", // 对象路径
"org.freedesktop.login1.Manager", // 接口名
QDBusConnection::systemBus(), // 系统总线(关键!不是会话总线)
this
);
// 检查接口是否有效
if (!m_login1Interface->isValid()) {
qDebug() << "=================================================";
qDebug() << "❌ 无法创建 login1 接口:" << m_login1Interface->lastError().message();
qDebug() << "=================================================";
return false;
}
// 监听 PrepareForSleep 信号签名b = bool
bool connectOk = QDBusConnection::systemBus().connect(
"org.freedesktop.login1", // 服务名
"/org/freedesktop/login1", // 对象路径
"org.freedesktop.login1.Manager", // 接口名
"PrepareForSleep", // 信号名
this,
SLOT(onPrepareForSleep(bool)) // 接收信号的槽函数
);
if (connectOk) {
qDebug() << "=================================================";
qDebug() << "✅ 成功连接 login1 电源管理接口Linux";
qDebug() << "=================================================";
return true;
} else {
qDebug() << "=================================================";
qDebug() << "❌ 连接 login1 信号失败:" << QDBusConnection::systemBus().lastError().message();
qDebug() << "=================================================";
return false;
}
}
void PowerMonitorLinux::onPrepareForSleep(bool enteringSleep)
{
if (enteringSleep) {
// 系统即将进入 休眠/待机 状态
emitAboutToSleep();
} else {
// 系统从睡眠中唤醒
emitAboutToWakeUp();
}
}
#endif // Q_OS_LINUX