2025-11-07 10:56:45 +08:00
|
|
|
|
#ifndef SCREENLOCKDETECTOR_H
|
|
|
|
|
|
#define SCREENLOCKDETECTOR_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
|
|
#include <QDBusInterface>
|
|
|
|
|
|
#include <QDBusReply>
|
2025-11-07 15:47:48 +08:00
|
|
|
|
#include <QDBusContext>
|
|
|
|
|
|
#include <QCoreApplication>
|
2025-11-07 10:56:45 +08:00
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 屏幕锁定检测器类
|
|
|
|
|
|
*
|
|
|
|
|
|
* 通过监听Linux系统的DBus信号来检测屏幕锁定/解锁状态
|
2025-11-07 14:57:13 +08:00
|
|
|
|
* 支持多种桌面环境:Deepin DDE, GNOME, KDE, XFCE等
|
2025-11-07 10:56:45 +08:00
|
|
|
|
*/
|
2025-11-07 15:47:48 +08:00
|
|
|
|
class ScreenLockDetector : public QObject, protected QDBusContext
|
2025-11-07 10:56:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
explicit ScreenLockDetector(QObject *parent = nullptr);
|
|
|
|
|
|
~ScreenLockDetector();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取当前锁屏状态
|
|
|
|
|
|
* @return true 如果屏幕已锁定,否则返回 false
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool isScreenLocked() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 初始化DBus连接
|
|
|
|
|
|
* @return true 如果初始化成功,否则返回 false
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool initialize();
|
|
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 屏幕锁定信号
|
|
|
|
|
|
* 当检测到屏幕被锁定时发出
|
|
|
|
|
|
*/
|
|
|
|
|
|
void screenLocked();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 屏幕解锁信号
|
|
|
|
|
|
* 当检测到屏幕被解锁时发出
|
|
|
|
|
|
*/
|
|
|
|
|
|
void screenUnlocked();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 锁屏状态改变信号
|
|
|
|
|
|
* @param locked true表示已锁定,false表示已解锁
|
|
|
|
|
|
*/
|
|
|
|
|
|
void lockStateChanged(bool locked);
|
|
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理GNOME屏幕保护程序的DBus信号
|
|
|
|
|
|
* @param active 屏幕保护程序是否激活
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onScreenSaverActiveChanged(bool active);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理登录管理器的会话锁定信号
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onSessionLocked();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理登录管理器的会话解锁信号
|
|
|
|
|
|
*/
|
|
|
|
|
|
void onSessionUnlocked();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 设置锁屏状态
|
|
|
|
|
|
* @param locked 新的锁屏状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
void setLockState(bool locked);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 连接到GNOME屏幕保护程序的DBus接口
|
|
|
|
|
|
* @return true 如果连接成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool connectToGnomeScreenSaver();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 连接到登录管理器的DBus接口
|
|
|
|
|
|
* @return true 如果连接成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool connectToLoginManager();
|
|
|
|
|
|
|
2025-11-07 14:57:13 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 连接到Deepin DDE的DBus接口
|
|
|
|
|
|
* @return true 如果连接成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool connectToDeepinDDE();
|
|
|
|
|
|
|
2025-11-07 10:56:45 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 查询当前的锁屏状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
void queryCurrentLockState();
|
|
|
|
|
|
|
2025-11-07 15:47:48 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取当前会话的 DBus 路径
|
|
|
|
|
|
* @return 当前会话的路径,如果无法确定则返回空字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
QString getCurrentSessionPath();
|
|
|
|
|
|
|
2025-11-07 10:56:45 +08:00
|
|
|
|
private:
|
|
|
|
|
|
bool m_isLocked; // 当前锁屏状态
|
|
|
|
|
|
QDBusInterface *m_gnomeInterface; // GNOME屏幕保护程序接口
|
|
|
|
|
|
QDBusInterface *m_loginInterface; // 登录管理器接口
|
2025-11-07 14:57:13 +08:00
|
|
|
|
QDBusInterface *m_deepinInterface; // Deepin DDE接口
|
2025-11-07 10:56:45 +08:00
|
|
|
|
bool m_gnomeConnected; // GNOME接口是否连接成功
|
|
|
|
|
|
bool m_loginConnected; // 登录管理器接口是否连接成功
|
2025-11-07 14:57:13 +08:00
|
|
|
|
bool m_deepinConnected; // Deepin DDE接口是否连接成功
|
2025-11-07 10:56:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // SCREENLOCKDETECTOR_H
|