diff --git a/CMakeLists.txt b/CMakeLists.txt index 421de28..63f7814 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 26cc1e6..56702dd 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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); diff --git a/src/mainwindow.h b/src/mainwindow.h index 3170e51..0a1ea1a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -9,11 +9,12 @@ #include #include #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 \ No newline at end of file +#endif // MAINWINDOW_H diff --git a/src/powermonitor.cpp b/src/powermonitor.cpp new file mode 100644 index 0000000..20c6faf --- /dev/null +++ b/src/powermonitor.cpp @@ -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(); + } +} diff --git a/src/powermonitor.h b/src/powermonitor.h new file mode 100644 index 0000000..8770a62 --- /dev/null +++ b/src/powermonitor.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include + +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; +};