Add PowerMonitor for sleep and wake events
This commit is contained in:
parent
8d550c4d7b
commit
2ec185654f
|
|
@ -130,6 +130,7 @@ set(SOURCES
|
|||
src/customwidget.cpp
|
||||
src/screenlockdetector.cpp
|
||||
src/screenlockdetector_base.cpp
|
||||
src/powermonitor.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
|
|
@ -138,6 +139,7 @@ set(HEADERS
|
|||
src/customwidget.h
|
||||
src/screenlockdetector.h
|
||||
src/screenlockdetector_base.h
|
||||
src/powermonitor.h
|
||||
)
|
||||
|
||||
# Add Vulkan widget if Vulkan is available
|
||||
|
|
|
|||
|
|
@ -31,6 +31,9 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
// 初始化锁屏检测器
|
||||
m_lockDetector = new ScreenLockDetector(this);
|
||||
|
||||
// 初始化电源监视器
|
||||
m_powerMonitor = new PowerMonitor(this);
|
||||
|
||||
// 初始化UI
|
||||
setupUI();
|
||||
|
||||
|
|
@ -163,6 +166,12 @@ void MainWindow::setupConnections()
|
|||
connect(m_lockDetector, &ScreenLockDetector::lockStateChanged,
|
||||
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,
|
||||
this, &MainWindow::onEnableRenderingClicked);
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@
|
|||
#include <QGroupBox>
|
||||
#include <QTimer>
|
||||
#include "screenlockdetector.h"
|
||||
#include "powermonitor.h"
|
||||
#include "renderwidgetbase.h"
|
||||
|
||||
/**
|
||||
* @brief 主窗口类
|
||||
*
|
||||
*
|
||||
* 整合锁屏检测器和渲染组件,提供用户界面
|
||||
* 使用统一的RenderWidgetBase基类指针,根据编译选项选择Vulkan或QPainter实现
|
||||
*/
|
||||
|
|
@ -81,18 +82,19 @@ private:
|
|||
private:
|
||||
// 核心组件
|
||||
ScreenLockDetector *m_lockDetector;
|
||||
PowerMonitor *m_powerMonitor;
|
||||
RenderWidgetBase *m_renderWidget; // 统一的渲染组件指针
|
||||
|
||||
|
||||
// UI组件
|
||||
QWidget *m_centralWidget;
|
||||
QVBoxLayout *m_mainLayout;
|
||||
|
||||
|
||||
// 控制面板
|
||||
QGroupBox *m_controlGroup;
|
||||
QPushButton *m_enableRenderBtn;
|
||||
QPushButton *m_disableRenderBtn;
|
||||
QPushButton *m_resetFrameBtn;
|
||||
|
||||
|
||||
// 状态显示
|
||||
QGroupBox *m_statusGroup;
|
||||
QLabel *m_rendererTypeLabel;
|
||||
|
|
@ -101,9 +103,9 @@ private:
|
|||
QLabel *m_frameCountLabel;
|
||||
QLabel *m_detectorStatusLabel;
|
||||
QLabel *m_initStatusLabel;
|
||||
|
||||
|
||||
// 更新定时器
|
||||
QTimer *m_updateTimer;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
#endif // MAINWINDOW_H
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
};
|
||||
Loading…
Reference in New Issue