Enable Login Manager and stop emitting unlock

Call connectToLoginManager() in initialize(). Comment out
lockStateChanged and screenUnlocked emissions to silence them.
This commit is contained in:
ubuntu1804 2025-11-08 10:51:45 +08:00
parent e6fec6dfea
commit 7e919ddef9
1 changed files with 62 additions and 63 deletions

View File

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