Skip to content

Commit 95b5de7

Browse files
committed
refactor: remove PowerManager interface dependency
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. 验证锁屏电源管理功能没有回归问题
1 parent e88849d commit 95b5de7

File tree

7 files changed

+53
-37
lines changed

7 files changed

+53
-37
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ if (NOT DISABLE_DSS_SNIPE)
184184
"${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.Accounts1.User.xml userinterface"
185185
"${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.ImageEffect1.xml imageeffect1interface"
186186
"${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.Logined.xml loginedinterface"
187-
"${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.PowerManager1.xml powermanager1interface"
188187
"${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.SystemPower1.xml systempower1interface"
189188
"${CMAKE_SOURCE_DIR}/xml/snipe/com.deepin.wm.xml wminterface"
190189
"${CMAKE_SOURCE_DIR}/xml/snipe/org.deepin.dde.Authenticate1.Session2.xml session2interface"

src/dde-lock/lockworker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ void LockWorker::doPowerAction(const SessionBaseModel::PowerAction action)
559559
if (delayTime < 0) {
560560
delayTime = 500;
561561
}
562-
if (m_powerManagerInter->CanHibernate()){
562+
if (canHibernate()){
563563
WarningContent::instance()->tryGrabKeyboard();
564564
QTimer::singleShot(delayTime, this, [=] {
565565
// 待机休眠前设置Locked为true,避免刚唤醒时locked状态不对

src/global_util/dbusconstant.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ namespace DSS_DBUS {
1515
const QString accountsUserPath = "/com/deepin/daemon/Accounts/User%1";
1616
const QString accountsUserInterface = "com.deepin.daemon.Accounts.User";
1717
const QString loginedPath = "/com/deepin/daemon/Logined";
18-
const QString powerManagerService = "com.deepin.daemon.PowerManager";
19-
const QString powerManagerPath = "/com/deepin/daemon/PowerManager";
2018
const QString powerService = "com.deepin.system.Power";
2119
const QString powerPath = "/com/deepin/system/Power";
2220
const QString sessionPowerService = "com.deepin.daemon.Power";
@@ -62,8 +60,6 @@ namespace DSS_DBUS {
6260
const QString accountsUserPath = "/org/deepin/dde/Accounts1/User%1";
6361
const QString accountsUserInterface = "org.deepin.dde.Accounts1.User";
6462
const QString loginedPath = "/org/deepin/dde/Logined";
65-
const QString powerManagerService = "org.deepin.dde.PowerManager1";
66-
const QString powerManagerPath = "/org/deepin/dde/PowerManager1";
6763
const QString powerService = "org.deepin.dde.Power1";
6864
const QString powerPath = "/org/deepin/dde/Power1";
6965
const QString sessionPowerService = "org.deepin.dde.Power1";

src/session-widgets/authinterface.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ AuthInterface::AuthInterface(SessionBaseModel *const model, QObject *parent)
2121
, m_accountsInter(new AccountsInter(DSS_DBUS::accountsService, DSS_DBUS::accountsPath, QDBusConnection::systemBus(), this))
2222
, m_loginedInter(new LoginedInter(DSS_DBUS::accountsService, DSS_DBUS::loginedPath, QDBusConnection::systemBus(), this))
2323
, m_login1Inter(new DBusLogin1Manager("org.freedesktop.login1", "/org/freedesktop/login1", QDBusConnection::systemBus(), this))
24-
, m_powerManagerInter(new PowerManagerInter(DSS_DBUS::powerManagerService, DSS_DBUS::powerManagerPath, QDBusConnection::systemBus(), this))
2524
, m_dbusInter(new DBusObjectInter("org.freedesktop.DBus", "/org/freedesktop/DBus", QDBusConnection::systemBus(), this))
2625
, m_lastLogoutUid(0)
2726
, m_currentUserUid(0)
2827
, m_loginUserList(0)
28+
, m_isVM(detectVirtualMachine())
2929
{
3030
#ifndef ENABLE_DSS_SNIPE
3131
// 需要先初始化m_gsettings
@@ -241,15 +241,11 @@ void AuthInterface::checkPowerInfo()
241241
// 替换接口org.freedesktop.login1 为com.deepin.sessionManager,原接口的是否支持待机和休眠的信息不准确
242242
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
243243
#ifndef ENABLE_DSS_SNIPE
244-
bool can_sleep = env.contains(POWER_CAN_SLEEP) ? QVariant(env.value(POWER_CAN_SLEEP)).toBool()
245-
: getGSettings("Power","sleep").toBool() && m_powerManagerInter->CanSuspend();
246-
bool can_hibernate = env.contains(POWER_CAN_HIBERNATE) ? QVariant(env.value(POWER_CAN_HIBERNATE)).toBool()
247-
: getGSettings("Power","hibernate").toBool() && m_powerManagerInter->CanHibernate();
244+
bool can_sleep = getGSettings("Power","sleep").toBool() && canSuspend();
245+
bool can_hibernate = getGSettings("Power","hibernate").toBool() && canHibernate();
248246
#else
249-
bool can_sleep = env.contains(POWER_CAN_SLEEP) ? QVariant(env.value(POWER_CAN_SLEEP)).toBool()
250-
: getDconfigValue("sleep", true).toBool() && m_powerManagerInter->CanSuspend();
251-
bool can_hibernate = env.contains(POWER_CAN_HIBERNATE) ? QVariant(env.value(POWER_CAN_HIBERNATE)).toBool()
252-
: getDconfigValue("hibernate", true).toBool() && m_powerManagerInter->CanHibernate();
247+
bool can_sleep = getDconfigValue("sleep", true).toBool() && canSuspend();
248+
bool can_hibernate = getDconfigValue("hibernate", true).toBool() && canHibernate();
253249
#endif
254250

255251
m_model->setCanSleep(can_sleep);
@@ -261,3 +257,44 @@ bool AuthInterface::checkIsADDomain()
261257
//只有加入AD域后,才会生成此文件
262258
return QFile::exists("/etc/krb5.keytab");
263259
}
260+
261+
bool AuthInterface::canSuspend()
262+
{
263+
if (QString(getenv("POWER_CAN_SLEEP")) == "0" || m_isVM)
264+
return false;
265+
266+
// 检查内存休眠支持文件是否存在
267+
if (!QFile::exists("/sys/power/mem_sleep"))
268+
return false;
269+
270+
QString canSuspend = m_login1Inter->CanSuspend();
271+
return canSuspend == "yes";
272+
}
273+
274+
bool AuthInterface::canHibernate()
275+
{
276+
if (QString(getenv("POWER_CAN_HIBERNATE")) == "0" || m_isVM)
277+
return false;
278+
279+
QString canHibernate = m_login1Inter->CanHibernate();
280+
return canHibernate == "yes";
281+
}
282+
283+
bool AuthInterface::detectVirtualMachine()
284+
{
285+
QProcess process;
286+
process.start("/usr/bin/systemd-detect-virt", QStringList());
287+
// 添加超时限制,例如 3000ms
288+
if (!process.waitForFinished(3000)) {
289+
qWarning() << "Timeout detecting virtual machine";
290+
return false;
291+
}
292+
293+
if (process.exitCode() != 0) {
294+
qWarning() << "Failed to detect virtual machine, error:" << process.errorString();
295+
return false;
296+
}
297+
298+
QString name = QString::fromUtf8(process.readAllStandardOutput()).trimmed();
299+
return name != "none" && !name.isEmpty();
300+
}

src/session-widgets/authinterface.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
#include <com_deepin_daemon_logined.h>
1515
#include <com_deepin_daemon_authenticate.h>
1616
#include <org_freedesktop_login1_session_self.h>
17-
#include <com_deepin_daemon_powermanager.h>
1817
#include <org_freedesktop_dbus.h>
1918
#include <QGSettings>
2019
#else
2120
#include "authenticate1interface.h"
2221
#include "accounts1interface.h"
2322
#include "loginedinterface.h"
24-
#include "powermanager1interface.h"
2523
#include "dbusinterface.h"
2624
#include "selfinterface.h"
2725
#include <DConfig>
@@ -34,15 +32,13 @@
3432
using AccountsInter = com::deepin::daemon::Accounts;
3533
using LoginedInter = com::deepin::daemon::Logined;
3634
using Login1SessionSelf = org::freedesktop::login1::Session;
37-
using PowerManagerInter = com::deepin::daemon::PowerManager;
3835
using DBusObjectInter = org::freedesktop::DBus;
3936

4037
using com::deepin::daemon::Authenticate;
4138
#else
4239
using AccountsInter = org::deepin::dde::Accounts1;
4340
using LoginedInter = org::deepin::dde::Logined;
4441
using Login1SessionSelf = org::freedesktop::login1::Session;
45-
using PowerManagerInter = org::deepin::dde::PowerManager1;
4642
using DBusObjectInter = org::freedesktop::DBus;
4743

4844
using Authenticate = org::deepin::dde::Authenticate1;
@@ -96,13 +92,16 @@ class AuthInterface : public QObject {
9692
failback);
9793
}
9894

95+
bool canSuspend();
96+
bool canHibernate();
97+
bool detectVirtualMachine();
98+
9999
protected:
100100
SessionBaseModel* m_model;
101101
AccountsInter * m_accountsInter;
102102
LoginedInter* m_loginedInter;
103103
DBusLogin1Manager* m_login1Inter;
104104
Login1SessionSelf* m_login1SessionSelf = nullptr;
105-
PowerManagerInter* m_powerManagerInter;
106105
DBusObjectInter* m_dbusInter;
107106
#ifndef ENABLE_DSS_SNIPE
108107
QGSettings* m_gsettings = nullptr;
@@ -112,6 +111,7 @@ class AuthInterface : public QObject {
112111
uint m_lastLogoutUid;
113112
uint m_currentUserUid;
114113
std::list<uint> m_loginUserList;
114+
bool m_isVM;
115115
};
116116
} // namespace Auth
117117

toolGenerate/qdbusxml2cpp/org.deepin.dde.PowerManager1Adaptor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,4 @@ bool PowerManager1Adaptor::CanSuspend()
6363
bool out0;
6464
QMetaObject::invokeMethod(parent(), "CanSuspend", Q_RETURN_ARG(bool, out0));
6565
return out0;
66-
}
67-
66+
}

xml/snipe/org.deepin.dde.PowerManager1.xml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)