Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/dde-session/impl/sessionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QSocketNotifier>
#include <QTimer>

#include <unistd.h>
#include <signal.h>
#include <xcb/xcb.h>

static const QString Performance = QStringLiteral("performance");
static const QString PowerSave = QStringLiteral("powersave");
static const QString LowBattery = QStringLiteral("lowBattery");

#define MASK_SERVICE(service) \
{\
auto reply = m_systemd1ManagerInter->MaskUnitFiles(QStringList() << service, true, true);\
Expand Down Expand Up @@ -124,6 +129,14 @@ void SessionManager::initConnections()
const bool active = m_login1SessionInter->active();
Q_UNUSED(active)

connect(m_login1ManagerInter, &org::freedesktop::login1::Manager::PrepareForSleep, [=](bool sleep) {
qDebug() << "system is preparing for sleep: " << sleep;
if (!sleep) {
// 唤醒后恢复之前的电源模式
recoverySystemPowerMode();
}
});

connect(m_login1SessionInter, &org::freedesktop::login1::Session::ActiveChanged, [=](bool active) {
qDebug() << "session active status changed to:" << active;
if (active) {
Expand Down Expand Up @@ -404,6 +417,7 @@ bool SessionManager::Register(const QString &id)

void SessionManager::RequestHibernate()
{
setTlpMode(Performance);
QDBusPendingReply<> reply = m_login1ManagerInter->Hibernate(false);
if (reply.isError()) {
qWarning() << "failed to hibernate, error: " << reply.error().name();
Expand Down Expand Up @@ -447,11 +461,13 @@ void SessionManager::RequestLogout()

void SessionManager::RequestReboot()
{
setTlpMode(Performance);
reboot(true);
}

void SessionManager::RequestShutdown()
{
setTlpMode(Performance);
shutdown(true);
}

Expand All @@ -463,6 +479,8 @@ void SessionManager::RequestSuspend()
return;
}

setTlpMode(Performance);

// 使用窗管接口进行黑屏处理
if (Dconf::SetValue("com.deepin.dde.startdde", "", "quick-black-screen", QVariant(false))) {
QDBusInterface inter("org.kde.KWin", "/BlackScreen", "org.kde.kwin.BlackScreen", QDBusConnection::sessionBus(), this);
Expand Down Expand Up @@ -546,6 +564,10 @@ void SessionManager::init()
if (!Utils::IS_WAYLAND_DISPLAY) {
watchXConnection();
}

QTimer::singleShot(0, this, [this] {
recoverySystemPowerMode();
});

qInfo() << "session manager init finished";
}
Expand Down Expand Up @@ -860,6 +882,68 @@ void SessionManager::handleOSSignal()
signal(SIGSEGV, sig_crash);
}

void SessionManager::setTlpMode(const QString &mode)
{
qInfo() << "setTlpMode mode:" << mode;
QDBusInterface inter("org.deepin.dde.Power1", "/org/deepin/dde/Power1", "org.deepin.dde.Power1", QDBusConnection::systemBus());
QDBusPendingReply<> reply = inter.asyncCall("SetTlpMode", mode);
Q_UNUSED(reply)
}

void SessionManager::recoverySystemPowerMode()
{
qInfo() << "recoverySystemPowerMode";
QDBusInterface inter("org.deepin.dde.Power1", "/org/deepin/dde/Power1",
"org.freedesktop.DBus.Properties", QDBusConnection::systemBus());

// 获取 TlpMode
QDBusMessage tlpMsg = inter.call("Get", "org.deepin.dde.Power1", "TlpMode");
QString tlpMode;
if (tlpMsg.type() == QDBusMessage::ReplyMessage) {
tlpMode = tlpMsg.arguments().value(0).value<QDBusVariant>().variant().toString();
} else {
qWarning() << "Get DBus property TlpMode failed:" << tlpMsg.errorMessage();
return;
}

// 获取 Mode
QDBusMessage modeMsg = inter.call("Get", "org.deepin.dde.Power1", "Mode");
QString mode;
if (modeMsg.type() == QDBusMessage::ReplyMessage) {
mode = modeMsg.arguments().value(0).value<QDBusVariant>().variant().toString();
} else {
qWarning() << "Get DBus property Mode failed:" << modeMsg.errorMessage();
return;
}

qInfo() << "DBus property of TlpMode:" << tlpMode << ", Mode:" << mode;

if (mode == tlpMode)
return;

// 如果目标是节能模式,检查电池状态决定是否需要切换到低电量模式
if (mode == PowerSave) {
QDBusMessage batteryMsg = inter.call("Get", "org.deepin.dde.Power1", "HasBattery");
if (batteryMsg.type() == QDBusMessage::ReplyMessage) {
bool hasBattery = batteryMsg.arguments().value(0).value<QDBusVariant>().variant().toBool();
if (!hasBattery) {
setTlpMode(mode);
return;
}

QDBusMessage capacityMsg = inter.call("Get", "org.deepin.dde.Power1", "BatteryCapacity");
if (capacityMsg.type() == QDBusMessage::ReplyMessage) {
double batteryCapacity = capacityMsg.arguments().value(0).value<QDBusVariant>().variant().toDouble();
if (batteryCapacity <= 20.0) {
mode = LowBattery;
}
}
}
}

setTlpMode(mode);
}

void SessionManager::shutdown(bool force)
{
prepareShutdown(force);
Expand Down
2 changes: 2 additions & 0 deletions src/dde-session/impl/sessionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public Q_SLOTS:
void watchXConnection();

void shutdown(bool force);
void setTlpMode(const QString &mode);
void recoverySystemPowerMode();
void reboot(bool force);

// 主动触发DBus的PropertiesChanged信息,否则调用方无法监听属性变化
Expand Down
Loading