From 32e90aebdc9aab694adfeac20679ae65e2792371 Mon Sep 17 00:00:00 2001 From: fuleyi Date: Tue, 4 Nov 2025 13:59:19 +0800 Subject: [PATCH] refactor: remove PowerManager interface dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Removed PowerManager D-Bus interface dependency from the authentication system 2. Replaced PowerManager calls with direct login1 D-Bus interface calls for power management 3. Added local power capability detection methods (canSuspend/ canHibernate) that check system files and login1 interface 4. Implemented virtual machine detection to disable power features in VM environments 5. Removed all PowerManager related XML definitions, adaptors, and constants 6. Simplified power info checking logic by removing environment variable fallbacks This change eliminates the dependency on the deprecated PowerManager service and uses the standard login1 interface directly, making the code more maintainable and reducing external dependencies. Influence: 1. Test suspend/hibernate functionality on physical machines 2. Verify power options are disabled in virtual machine environments 3. Test power actions with different system configurations 4. Ensure power state detection works correctly with login1 interface 5. Verify no regression in lock screen power management features 重构:移除 PowerManager 接口依赖 1. 从认证系统中移除了 PowerManager D-Bus 接口依赖 2. 将 PowerManager 调用替换为直接的 login1 D-Bus 接口调用进行电源管理 3. 添加了本地电源能力检测方法(canSuspend/canHibernate),检查系统文件和 login1 接口 4. 实现了虚拟机检测功能,在虚拟环境中禁用电源功能 5. 移除了所有与 PowerManager 相关的 XML 定义、适配器和常量 6. 通过移除环境变量回退简化了电源信息检查逻辑 此更改消除了对已弃用的 PowerManager 服务的依赖,直接使用标准的 login1 接 口,使代码更易于维护并减少外部依赖。 影响: 1. 在物理机上测试挂起/休眠功能 2. 验证虚拟环境中电源选项被禁用 3. 使用不同系统配置测试电源操作 4. 确保通过 login1 接口的电源状态检测正常工作 5. 验证锁屏电源管理功能没有回归问题 --- CMakeLists.txt | 1 - src/dde-lock/lockworker.cpp | 2 +- src/global_util/dbusconstant.h | 4 -- src/session-widgets/authinterface.cpp | 55 ++++++++++++++++++---- src/session-widgets/authinterface.h | 10 ++-- xml/snipe/org.deepin.dde.PowerManager1.xml | 15 ------ 6 files changed, 52 insertions(+), 35 deletions(-) delete mode 100644 xml/snipe/org.deepin.dde.PowerManager1.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 903028dc..8832348e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,7 +184,6 @@ if (NOT DISABLE_DSS_SNIPE) "${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.Accounts1.User.xml userinterface" "${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.ImageEffect1.xml imageeffect1interface" "${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.Logined.xml loginedinterface" - "${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.PowerManager1.xml powermanager1interface" "${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.SystemPower1.xml systempower1interface" "${CMAKE_SOURCE_DIR}/xml/snipe/com.deepin.wm.xml wminterface" "${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.Authenticate1.Session2.xml session2interface" diff --git a/src/dde-lock/lockworker.cpp b/src/dde-lock/lockworker.cpp index 7ce5e463..b6925a91 100644 --- a/src/dde-lock/lockworker.cpp +++ b/src/dde-lock/lockworker.cpp @@ -559,7 +559,7 @@ void LockWorker::doPowerAction(const SessionBaseModel::PowerAction action) if (delayTime < 0) { delayTime = 500; } - if (m_powerManagerInter->CanHibernate()){ + if (canHibernate()){ WarningContent::instance()->tryGrabKeyboard(); QTimer::singleShot(delayTime, this, [=] { // 待机休眠前设置Locked为true,避免刚唤醒时locked状态不对 diff --git a/src/global_util/dbusconstant.h b/src/global_util/dbusconstant.h index 2a23588c..6dc1912e 100644 --- a/src/global_util/dbusconstant.h +++ b/src/global_util/dbusconstant.h @@ -15,8 +15,6 @@ namespace DSS_DBUS { const QString accountsUserPath = "/com/deepin/daemon/Accounts/User%1"; const QString accountsUserInterface = "com.deepin.daemon.Accounts.User"; const QString loginedPath = "/com/deepin/daemon/Logined"; - const QString powerManagerService = "com.deepin.daemon.PowerManager"; - const QString powerManagerPath = "/com/deepin/daemon/PowerManager"; const QString powerService = "com.deepin.system.Power"; const QString powerPath = "/com/deepin/system/Power"; const QString sessionPowerService = "com.deepin.daemon.Power"; @@ -62,8 +60,6 @@ namespace DSS_DBUS { const QString accountsUserPath = "/org/deepin/dde/Accounts1/User%1"; const QString accountsUserInterface = "org.deepin.dde.Accounts1.User"; const QString loginedPath = "/org/deepin/dde/Logined"; - const QString powerManagerService = "org.deepin.dde.PowerManager1"; - const QString powerManagerPath = "/org/deepin/dde/PowerManager1"; const QString powerService = "org.deepin.dde.Power1"; const QString powerPath = "/org/deepin/dde/Power1"; const QString sessionPowerService = "org.deepin.dde.Power1"; diff --git a/src/session-widgets/authinterface.cpp b/src/session-widgets/authinterface.cpp index 5d9deeee..72a530be 100644 --- a/src/session-widgets/authinterface.cpp +++ b/src/session-widgets/authinterface.cpp @@ -21,11 +21,11 @@ AuthInterface::AuthInterface(SessionBaseModel *const model, QObject *parent) , m_accountsInter(new AccountsInter(DSS_DBUS::accountsService, DSS_DBUS::accountsPath, QDBusConnection::systemBus(), this)) , m_loginedInter(new LoginedInter(DSS_DBUS::accountsService, DSS_DBUS::loginedPath, QDBusConnection::systemBus(), this)) , m_login1Inter(new DBusLogin1Manager("org.freedesktop.login1", "/org/freedesktop/login1", QDBusConnection::systemBus(), this)) - , m_powerManagerInter(new PowerManagerInter(DSS_DBUS::powerManagerService, DSS_DBUS::powerManagerPath, QDBusConnection::systemBus(), this)) , m_dbusInter(new DBusObjectInter("org.freedesktop.DBus", "/org/freedesktop/DBus", QDBusConnection::systemBus(), this)) , m_lastLogoutUid(0) , m_currentUserUid(0) , m_loginUserList(0) + , m_isVM(detectVirtualMachine()) { #ifndef ENABLE_DSS_SNIPE // 需要先初始化m_gsettings @@ -241,15 +241,11 @@ void AuthInterface::checkPowerInfo() // 替换接口org.freedesktop.login1 为com.deepin.sessionManager,原接口的是否支持待机和休眠的信息不准确 QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); #ifndef ENABLE_DSS_SNIPE - bool can_sleep = env.contains(POWER_CAN_SLEEP) ? QVariant(env.value(POWER_CAN_SLEEP)).toBool() - : getGSettings("Power","sleep").toBool() && m_powerManagerInter->CanSuspend(); - bool can_hibernate = env.contains(POWER_CAN_HIBERNATE) ? QVariant(env.value(POWER_CAN_HIBERNATE)).toBool() - : getGSettings("Power","hibernate").toBool() && m_powerManagerInter->CanHibernate(); + bool can_sleep = getGSettings("Power","sleep").toBool() && canSuspend(); + bool can_hibernate = getGSettings("Power","hibernate").toBool() && canHibernate(); #else - bool can_sleep = env.contains(POWER_CAN_SLEEP) ? QVariant(env.value(POWER_CAN_SLEEP)).toBool() - : getDconfigValue("sleep", true).toBool() && m_powerManagerInter->CanSuspend(); - bool can_hibernate = env.contains(POWER_CAN_HIBERNATE) ? QVariant(env.value(POWER_CAN_HIBERNATE)).toBool() - : getDconfigValue("hibernate", true).toBool() && m_powerManagerInter->CanHibernate(); + bool can_sleep = getDconfigValue("sleep", true).toBool() && canSuspend(); + bool can_hibernate = getDconfigValue("hibernate", true).toBool() && canHibernate(); #endif m_model->setCanSleep(can_sleep); @@ -261,3 +257,44 @@ bool AuthInterface::checkIsADDomain() //只有加入AD域后,才会生成此文件 return QFile::exists("/etc/krb5.keytab"); } + +bool AuthInterface::canSuspend() +{ + if (QString(getenv("POWER_CAN_SLEEP")) == "0" || m_isVM) + return false; + + // 检查内存休眠支持文件是否存在 + if (!QFile::exists("/sys/power/mem_sleep")) + return false; + + QString canSuspend = m_login1Inter->CanSuspend(); + return canSuspend == "yes"; +} + +bool AuthInterface::canHibernate() +{ + if (QString(getenv("POWER_CAN_HIBERNATE")) == "0" || m_isVM) + return false; + + QString canHibernate = m_login1Inter->CanHibernate(); + return canHibernate == "yes"; +} + +bool AuthInterface::detectVirtualMachine() +{ + QProcess process; + process.start("/usr/bin/systemd-detect-virt", QStringList()); + // 添加超时限制,例如 3000ms + if (!process.waitForFinished(3000)) { + qWarning() << "Timeout detecting virtual machine"; + return false; + } + + if (process.exitCode() != 0) { + qWarning() << "Failed to detect virtual machine, error:" << process.errorString(); + return false; + } + + QString name = QString::fromUtf8(process.readAllStandardOutput()).trimmed(); + return name != "none" && !name.isEmpty(); +} \ No newline at end of file diff --git a/src/session-widgets/authinterface.h b/src/session-widgets/authinterface.h index 53e68a88..f9472bf6 100644 --- a/src/session-widgets/authinterface.h +++ b/src/session-widgets/authinterface.h @@ -14,14 +14,12 @@ #include #include #include -#include #include #include #else #include "authenticate1interface.h" #include "accounts1interface.h" #include "loginedinterface.h" -#include "powermanager1interface.h" #include "dbusinterface.h" #include "selfinterface.h" #include @@ -34,7 +32,6 @@ using AccountsInter = com::deepin::daemon::Accounts; using LoginedInter = com::deepin::daemon::Logined; using Login1SessionSelf = org::freedesktop::login1::Session; -using PowerManagerInter = com::deepin::daemon::PowerManager; using DBusObjectInter = org::freedesktop::DBus; using com::deepin::daemon::Authenticate; @@ -42,7 +39,6 @@ using com::deepin::daemon::Authenticate; using AccountsInter = org::deepin::dde::Accounts1; using LoginedInter = org::deepin::dde::Logined; using Login1SessionSelf = org::freedesktop::login1::Session; -using PowerManagerInter = org::deepin::dde::PowerManager1; using DBusObjectInter = org::freedesktop::DBus; using Authenticate = org::deepin::dde::Authenticate1; @@ -96,13 +92,16 @@ class AuthInterface : public QObject { failback); } + bool canSuspend(); + bool canHibernate(); + bool detectVirtualMachine(); + protected: SessionBaseModel* m_model; AccountsInter * m_accountsInter; LoginedInter* m_loginedInter; DBusLogin1Manager* m_login1Inter; Login1SessionSelf* m_login1SessionSelf = nullptr; - PowerManagerInter* m_powerManagerInter; DBusObjectInter* m_dbusInter; #ifndef ENABLE_DSS_SNIPE QGSettings* m_gsettings = nullptr; @@ -112,6 +111,7 @@ class AuthInterface : public QObject { uint m_lastLogoutUid; uint m_currentUserUid; std::list m_loginUserList; + bool m_isVM; }; } // namespace Auth diff --git a/xml/snipe/org.deepin.dde.PowerManager1.xml b/xml/snipe/org.deepin.dde.PowerManager1.xml deleted file mode 100644 index 3a7632e0..00000000 --- a/xml/snipe/org.deepin.dde.PowerManager1.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - -