From 9c52ab2daa4075e9b16819b8eb7f6dafad0d2c2b Mon Sep 17 00:00:00 2001
From: meizhigang <meizhigang@kylinsec.com.cn>
Date: Wed, 31 Jan 2024 11:38:24 +0800
Subject: [PATCH] fix(power):Fix dpms force level effective while state changed
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 -修复电源模式切换时屏幕状态显示

 Related #24589
---
 plugins/power/save/power-save-dpms.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/plugins/power/save/power-save-dpms.cpp b/plugins/power/save/power-save-dpms.cpp
index 94d7e7b..ab6b31a 100644
--- a/plugins/power/save/power-save-dpms.cpp
+++ b/plugins/power/save/power-save-dpms.cpp
@@ -61,11 +61,12 @@ bool PowerSaveDpms::set_level(PowerDpmsLevel level)
         return false;
     }
 
-    // dpms功能未开启
+    // dpms功能未开启，执行开启操作
     if (!current_enabled)
     {
-        KLOG_WARNING_POWER("DPMS not enabled");
-        return false;
+        KLOG_WARNING_POWER("DPMS default is not enabled then enable it");
+        // DPMSEnable返回值只有成功，这里不需要处理失败操作
+        DPMSEnable(this->xdisplay_);
     }
 
     auto current_level = this->get_level();
@@ -74,7 +75,11 @@ bool PowerSaveDpms::set_level(PowerDpmsLevel level)
     {
         state = this->level_enum2card(level);
 
-        if (!DPMSForceLevel(this->xdisplay_, state))
+        // 这里添加延迟，为了避免息屏后按键触发其它事件导致的亮屏
+        g_usleep(1000 * 100);
+
+        // DPMSForceLevel返回值只有为1表示成功
+        if (1 != DPMSForceLevel(this->xdisplay_, state))
         {
             KLOG_WARNING_POWER("Couldn't change DPMS mode");
             return false;
-- 
2.27.0

