Add PowerMonitor for sleep and wake events

This commit is contained in:
ubuntu1804 2025-11-11 15:21:39 +08:00
parent 8d550c4d7b
commit 2ec185654f
5 changed files with 106 additions and 6 deletions

View File

@ -130,6 +130,7 @@ set(SOURCES
src/customwidget.cpp src/customwidget.cpp
src/screenlockdetector.cpp src/screenlockdetector.cpp
src/screenlockdetector_base.cpp src/screenlockdetector_base.cpp
src/powermonitor.cpp
) )
set(HEADERS set(HEADERS
@ -138,6 +139,7 @@ set(HEADERS
src/customwidget.h src/customwidget.h
src/screenlockdetector.h src/screenlockdetector.h
src/screenlockdetector_base.h src/screenlockdetector_base.h
src/powermonitor.h
) )
# Add Vulkan widget if Vulkan is available # Add Vulkan widget if Vulkan is available

View File

@ -31,6 +31,9 @@ MainWindow::MainWindow(QWidget *parent)
// 初始化锁屏检测器 // 初始化锁屏检测器
m_lockDetector = new ScreenLockDetector(this); m_lockDetector = new ScreenLockDetector(this);
// 初始化电源监视器
m_powerMonitor = new PowerMonitor(this);
// 初始化UI // 初始化UI
setupUI(); setupUI();
@ -163,6 +166,12 @@ void MainWindow::setupConnections()
connect(m_lockDetector, &ScreenLockDetector::lockStateChanged, connect(m_lockDetector, &ScreenLockDetector::lockStateChanged,
this, &MainWindow::onLockStateChanged); this, &MainWindow::onLockStateChanged);
// 连接PowerMonitor信号
connect(m_powerMonitor, &PowerMonitor::aboutToSleep,
this, &MainWindow::onScreenLocked);
connect(m_powerMonitor, &PowerMonitor::aboutToWakeUp,
this, &MainWindow::onScreenUnlocked);
// 连接按钮信号 // 连接按钮信号
connect(m_enableRenderBtn, &QPushButton::clicked, connect(m_enableRenderBtn, &QPushButton::clicked,
this, &MainWindow::onEnableRenderingClicked); this, &MainWindow::onEnableRenderingClicked);

View File

@ -9,11 +9,12 @@
#include <QGroupBox> #include <QGroupBox>
#include <QTimer> #include <QTimer>
#include "screenlockdetector.h" #include "screenlockdetector.h"
#include "powermonitor.h"
#include "renderwidgetbase.h" #include "renderwidgetbase.h"
/** /**
* @brief * @brief
* *
* *
* 使RenderWidgetBase基类指针Vulkan或QPainter实现 * 使RenderWidgetBase基类指针Vulkan或QPainter实现
*/ */
@ -81,18 +82,19 @@ private:
private: private:
// 核心组件 // 核心组件
ScreenLockDetector *m_lockDetector; ScreenLockDetector *m_lockDetector;
PowerMonitor *m_powerMonitor;
RenderWidgetBase *m_renderWidget; // 统一的渲染组件指针 RenderWidgetBase *m_renderWidget; // 统一的渲染组件指针
// UI组件 // UI组件
QWidget *m_centralWidget; QWidget *m_centralWidget;
QVBoxLayout *m_mainLayout; QVBoxLayout *m_mainLayout;
// 控制面板 // 控制面板
QGroupBox *m_controlGroup; QGroupBox *m_controlGroup;
QPushButton *m_enableRenderBtn; QPushButton *m_enableRenderBtn;
QPushButton *m_disableRenderBtn; QPushButton *m_disableRenderBtn;
QPushButton *m_resetFrameBtn; QPushButton *m_resetFrameBtn;
// 状态显示 // 状态显示
QGroupBox *m_statusGroup; QGroupBox *m_statusGroup;
QLabel *m_rendererTypeLabel; QLabel *m_rendererTypeLabel;
@ -101,9 +103,9 @@ private:
QLabel *m_frameCountLabel; QLabel *m_frameCountLabel;
QLabel *m_detectorStatusLabel; QLabel *m_detectorStatusLabel;
QLabel *m_initStatusLabel; QLabel *m_initStatusLabel;
// 更新定时器 // 更新定时器
QTimer *m_updateTimer; QTimer *m_updateTimer;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

48
src/powermonitor.cpp Normal file
View File

@ -0,0 +1,48 @@
#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) {
qDebug() << "✅ 成功连接 login1 电源管理接口";
} else {
qDebug() << "❌ 连接 login1 信号失败:" << QDBusConnection::systemBus().lastError().message();
}
} else {
qDebug() << "❌ 无法创建 login1 接口:" << m_login1Interface->lastError().message();
// 可选fallback 到 GNOME 专用会话管理接口(见下文)
// initGnomeSessionManager();
}
}
// 槽函数:处理睡眠/唤醒事件
void PowerMonitor::onPrepareForSleep(bool enteringSleep) {
if (enteringSleep) {
// 系统即将进入 休眠/待机 状态
qDebug() << "\n📥 系统即将进入睡眠(休眠/待机)";
emit aboutToSleep();
} else {
// 系统从睡眠中唤醒
qDebug() << "📤 系统已从睡眠中唤醒";
emit aboutToWakeUp();
}
}

39
src/powermonitor.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <QObject>
#include <QDBusInterface>
#include <QDBusConnection>
#include <QDebug>
class PowerMonitor : public QObject
{
Q_OBJECT
public:
explicit PowerMonitor(QObject *parent = nullptr) : QObject(parent) { initLogin1Dbus(); }
signals:
/*
* @brief
*/
void aboutToWakeUp();
/*
* @brief
*/
void aboutToSleep();
private slots:
/*
* @brief /
* @param enteringSleep
*/
void onPrepareForSleep(bool enteringSleep);
private:
/*
* @brief Login1 DBus接口
*/
void initLogin1Dbus();
QDBusInterface *m_login1Interface = nullptr;
};