From 7e919ddef9b2401859436b25ceb011ac9be66cd6 Mon Sep 17 00:00:00 2001 From: ubuntu1804 Date: Sat, 8 Nov 2025 10:51:45 +0800 Subject: [PATCH] Enable Login Manager and stop emitting unlock Call connectToLoginManager() in initialize(). Comment out lockStateChanged and screenUnlocked emissions to silence them. --- src/screenlockdetector.cpp | 125 ++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/src/screenlockdetector.cpp b/src/screenlockdetector.cpp index fd7e402..7611d34 100644 --- a/src/screenlockdetector.cpp +++ b/src/screenlockdetector.cpp @@ -22,12 +22,12 @@ ScreenLockDetector::~ScreenLockDetector() delete m_gnomeInterface; m_gnomeInterface = nullptr; } - + if (m_loginInterface) { delete m_loginInterface; m_loginInterface = nullptr; } - + if (m_deepinInterface) { delete m_deepinInterface; m_deepinInterface = nullptr; @@ -44,29 +44,28 @@ bool ScreenLockDetector::initialize() qDebug() << "================================================="; qDebug() << "Initializing ScreenLockDetector..."; qDebug() << "================================================="; - + // 尝试连接到不同的DBus接口 bool deepinOk = connectToDeepinDDE(); bool gnomeOk = connectToGnomeScreenSaver(); - //bool loginOk = connectToLoginManager(); - bool loginOk = false; - + bool loginOk = connectToLoginManager(); + if (!deepinOk && !gnomeOk && !loginOk) { qWarning() << "Failed to connect to any screen lock detection service"; qWarning() << "Make sure you are running on a supported Linux desktop environment"; return false; } - + // 查询当前锁屏状态 queryCurrentLockState(); - + qDebug() << "================================================="; qDebug() << "ScreenLockDetector initialized successfully"; qDebug() << "Deepin DDE connected:" << m_deepinConnected; qDebug() << "GNOME ScreenSaver connected:" << m_gnomeConnected; qDebug() << "Login Manager connected:" << m_loginConnected; qDebug() << "================================================="; - + return true; } @@ -74,17 +73,17 @@ void ScreenLockDetector::setLockState(bool locked) { if (m_isLocked != locked) { m_isLocked = locked; - + qDebug() << "##################################################"; qDebug() << "## Screen lock state changed:" << (locked ? "LOCKED" : "UNLOCKED"); qDebug() << "##################################################"; - - emit lockStateChanged(locked); - + + // emit lockStateChanged(locked); + if (locked) { emit screenLocked(); } else { - emit screenUnlocked(); + // emit screenUnlocked(); } } } @@ -92,7 +91,7 @@ void ScreenLockDetector::setLockState(bool locked) bool ScreenLockDetector::connectToGnomeScreenSaver() { qDebug() << "\n--- Connecting to GNOME ScreenSaver ---"; - + // 连接到GNOME屏幕保护程序的DBus接口 m_gnomeInterface = new QDBusInterface( "org.gnome.ScreenSaver", @@ -101,17 +100,17 @@ bool ScreenLockDetector::connectToGnomeScreenSaver() QDBusConnection::sessionBus(), this ); - + if (!m_gnomeInterface->isValid()) { - qDebug() << "GNOME ScreenSaver interface not available:" + qDebug() << "GNOME ScreenSaver interface not available:" << m_gnomeInterface->lastError().message(); delete m_gnomeInterface; m_gnomeInterface = nullptr; return false; } - + qDebug() << "GNOME ScreenSaver interface is valid"; - + // 连接ActiveChanged信号 bool connected = QDBusConnection::sessionBus().connect( "org.gnome.ScreenSaver", @@ -121,16 +120,16 @@ bool ScreenLockDetector::connectToGnomeScreenSaver() this, SLOT(onScreenSaverActiveChanged(bool)) ); - + qDebug() << "GNOME ActiveChanged signal connected:" << connected; - + if (!connected) { qWarning() << "Failed to connect to GNOME ScreenSaver ActiveChanged signal"; delete m_gnomeInterface; m_gnomeInterface = nullptr; return false; } - + m_gnomeConnected = true; qDebug() << "Successfully connected to GNOME ScreenSaver"; return true; @@ -139,7 +138,7 @@ bool ScreenLockDetector::connectToGnomeScreenSaver() bool ScreenLockDetector::connectToLoginManager() { qDebug() << "\n--- Connecting to Login Manager (systemd-logind) ---"; - + // 首先获取当前会话的路径 QString sessionPath = getCurrentSessionPath(); if (sessionPath.isEmpty()) { @@ -148,7 +147,7 @@ bool ScreenLockDetector::connectToLoginManager() } else { qDebug() << "Current session path:" << sessionPath; } - + // 方法1: 连接到特定会话路径(如果获取到了) if (!sessionPath.isEmpty()) { m_loginInterface = new QDBusInterface( @@ -158,10 +157,10 @@ bool ScreenLockDetector::connectToLoginManager() QDBusConnection::systemBus(), this ); - + if (m_loginInterface->isValid()) { qDebug() << "Login Manager interface is valid for session:" << sessionPath; - + // 连接Lock和Unlock信号到特定会话 bool lockConnected = QDBusConnection::systemBus().connect( "org.freedesktop.login1", @@ -171,7 +170,7 @@ bool ScreenLockDetector::connectToLoginManager() this, SLOT(onSessionLocked()) ); - + bool unlockConnected = QDBusConnection::systemBus().connect( "org.freedesktop.login1", sessionPath, @@ -180,10 +179,10 @@ bool ScreenLockDetector::connectToLoginManager() this, SLOT(onSessionUnlocked()) ); - + qDebug() << "Session Lock signal connected:" << lockConnected; qDebug() << "Session Unlock signal connected:" << unlockConnected; - + if (lockConnected || unlockConnected) { m_loginConnected = true; qDebug() << "Successfully connected to Login Manager via session path"; @@ -195,10 +194,10 @@ bool ScreenLockDetector::connectToLoginManager() m_loginInterface = nullptr; } } - + // 方法2: 监听所有会话的Lock/Unlock信号(不指定具体路径) qDebug() << "Attempting to connect to all login1 Session signals..."; - + bool lockConnected = QDBusConnection::systemBus().connect( "org.freedesktop.login1", "", // 空路径表示监听所有对象 @@ -207,7 +206,7 @@ bool ScreenLockDetector::connectToLoginManager() this, SLOT(onSessionLocked()) ); - + bool unlockConnected = QDBusConnection::systemBus().connect( "org.freedesktop.login1", "", // 空路径表示监听所有对象 @@ -216,16 +215,16 @@ bool ScreenLockDetector::connectToLoginManager() this, SLOT(onSessionUnlocked()) ); - + qDebug() << "Generic Lock signal connected:" << lockConnected; qDebug() << "Generic Unlock signal connected:" << unlockConnected; - + if (lockConnected || unlockConnected) { m_loginConnected = true; qDebug() << "Successfully connected to Login Manager via generic signals"; return true; } - + qWarning() << "Failed to connect to Login Manager signals"; return false; } @@ -239,7 +238,7 @@ QString ScreenLockDetector::getCurrentSessionPath() qDebug() << "Session path from XDG_SESSION_ID:" << path; return path; } - + // 尝试通过DBus调用获取当前会话 QDBusInterface managerIface( "org.freedesktop.login1", @@ -247,7 +246,7 @@ QString ScreenLockDetector::getCurrentSessionPath() "org.freedesktop.login1.Manager", QDBusConnection::systemBus() ); - + if (managerIface.isValid()) { // 获取当前用户的会话列表 QDBusReply reply = managerIface.call("GetSessionByPID", (quint32)QCoreApplication::applicationPid()); @@ -259,14 +258,14 @@ QString ScreenLockDetector::getCurrentSessionPath() qDebug() << "GetSessionByPID failed:" << reply.error().message(); } } - + return QString(); } bool ScreenLockDetector::connectToDeepinDDE() { qDebug() << "\n--- Connecting to Deepin DDE ---"; - + // 可能的服务配置列表 struct DeepinService { QString service; @@ -275,18 +274,18 @@ bool ScreenLockDetector::connectToDeepinDDE() QString lockSignal; QString unlockSignal; }; - + QList services = { // Deepin 20/23 主要接口 {"com.deepin.dde.lockFront", "/com/deepin/dde/lockFront", "com.deepin.dde.lockFront", "Visible", "Visible"}, - + // 备用接口 {"com.deepin.daemon.ScreenSaver", "/com/deepin/daemon/ScreenSaver", "com.deepin.daemon.ScreenSaver", "ActiveChanged", "ActiveChanged"}, }; - + for (const auto& svc : services) { qDebug() << "Trying Deepin service:" << svc.service; - + m_deepinInterface = new QDBusInterface( svc.service, svc.path, @@ -294,16 +293,16 @@ bool ScreenLockDetector::connectToDeepinDDE() QDBusConnection::sessionBus(), this ); - + if (!m_deepinInterface->isValid()) { qDebug() << " Interface not available:" << m_deepinInterface->lastError().message(); delete m_deepinInterface; m_deepinInterface = nullptr; continue; } - + qDebug() << " Interface is valid, connecting signals..."; - + // 尝试连接锁屏信号 bool visibleConnected = QDBusConnection::sessionBus().connect( svc.service, @@ -313,21 +312,21 @@ bool ScreenLockDetector::connectToDeepinDDE() this, SLOT(onLockFrontVisible(bool)) ); - + qDebug() << " Visible signal (" << svc.lockSignal << ") connected:" << visibleConnected; - + if (visibleConnected) { m_deepinConnected = true; qDebug() << "Successfully connected to Deepin DDE via" << svc.service; qDebug() << "Listening for signals:" << svc.lockSignal; return true; } - + qWarning() << " Failed to connect signals for" << svc.service; delete m_deepinInterface; m_deepinInterface = nullptr; } - + qWarning() << "Failed to connect to any Deepin DDE lock service"; return false; } @@ -335,11 +334,11 @@ bool ScreenLockDetector::connectToDeepinDDE() void ScreenLockDetector::queryCurrentLockState() { qDebug() << "\n--- Querying current lock state ---"; - + // 尝试从Deepin DDE查询当前状态 if (m_deepinInterface && m_deepinInterface->isValid()) { qDebug() << "Querying Deepin DDE lock state..."; - + QStringList possibleMethods = {"GetLocked", "IsLocked", "GetActive", "GetSessionLocked"}; for (const QString& method : possibleMethods) { QDBusReply reply = m_deepinInterface->call(method); @@ -350,10 +349,10 @@ void ScreenLockDetector::queryCurrentLockState() return; } } - + qDebug() << " No query method worked, waiting for signals"; } - + // 尝试从GNOME屏幕保护程序查询当前状态 if (m_gnomeInterface && m_gnomeInterface->isValid()) { qDebug() << "Querying GNOME ScreenSaver state..."; @@ -365,11 +364,11 @@ void ScreenLockDetector::queryCurrentLockState() return; } } - + // 尝试从登录管理器查询锁定状态 if (m_loginInterface && m_loginInterface->isValid()) { qDebug() << "Querying Login Manager lock state..."; - + // 尝试读取 LockedHint 属性 QDBusMessage msg = QDBusMessage::createMethodCall( "org.freedesktop.login1", @@ -378,7 +377,7 @@ void ScreenLockDetector::queryCurrentLockState() "Get" ); msg << "org.freedesktop.login1.Session" << "LockedHint"; - + QDBusReply reply = QDBusConnection::systemBus().call(msg); if (reply.isValid()) { bool locked = reply.value().toBool(); @@ -389,7 +388,7 @@ void ScreenLockDetector::queryCurrentLockState() qDebug() << " Could not read LockedHint:" << reply.error().message(); } } - + qDebug() << "Could not query initial lock state, will detect on next lock/unlock event"; } @@ -417,7 +416,7 @@ void ScreenLockDetector::onSessionLocked() qDebug() << "## LOCK SIGNAL RECEIVED"; qDebug() << "## Screen is now LOCKED"; qDebug() << "## Sender:" << sender(); - + // 获取信号发送者的详细信息 QDBusMessage msg = QDBusContext::message(); if (msg.type() != QDBusMessage::InvalidMessage) { @@ -427,7 +426,7 @@ void ScreenLockDetector::onSessionLocked() qDebug() << "## Interface:" << msg.interface(); qDebug() << "## Member:" << msg.member(); } - + qDebug() << "##################################################"; setLockState(true); } @@ -438,7 +437,7 @@ void ScreenLockDetector::onSessionUnlocked() qDebug() << "## UNLOCK SIGNAL RECEIVED"; qDebug() << "## Screen is now UNLOCKED"; qDebug() << "## Sender:" << sender(); - + // 获取信号发送者的详细信息 QDBusMessage msg = QDBusContext::message(); if (msg.type() != QDBusMessage::InvalidMessage) { @@ -448,7 +447,7 @@ void ScreenLockDetector::onSessionUnlocked() qDebug() << "## Interface:" << msg.interface(); qDebug() << "## Member:" << msg.member(); } - + qDebug() << "##################################################"; setLockState(false); -} \ No newline at end of file +}