2025-11-11 15:21:39 +08:00
|
|
|
|
#include "powermonitor.h"
|
|
|
|
|
|
|
|
|
|
|
|
void PowerMonitor::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()) {
|
|
|
|
|
|
// 监听 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) {
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
qDebug() << "✅ 成功连接 login1 电源管理接口";
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
} else {
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
qDebug() << "❌ 连接 login1 信号失败:" << QDBusConnection::systemBus().lastError().message();
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
qDebug() << "❌ 无法创建 login1 接口:" << m_login1Interface->lastError().message();
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 槽函数:处理睡眠/唤醒事件
|
|
|
|
|
|
void PowerMonitor::onPrepareForSleep(bool enteringSleep) {
|
|
|
|
|
|
if (enteringSleep) {
|
|
|
|
|
|
// 系统即将进入 休眠/待机 状态
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
qDebug() << "\n📥 系统即将进入睡眠(休眠/待机)";
|
|
|
|
|
|
emit aboutToSleep();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 系统从睡眠中唤醒
|
2025-11-11 15:57:11 +08:00
|
|
|
|
qDebug() << "📤 系统已从睡眠中唤醒\n";
|
|
|
|
|
|
qDebug() << "=================================================";
|
2025-11-11 15:21:39 +08:00
|
|
|
|
emit aboutToWakeUp();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|