From ca64260a4385ec5dd31e9481bb6f3f9b567a9e02 Mon Sep 17 00:00:00 2001
From: meizhigang <meizhigang@kylinsec.com.cn>
Date: Mon, 21 Aug 2023 10:53:20 +0800
Subject: [PATCH] fix(power):Change poweroff action from key press to release
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 - 修改关机动作从按键按下时变为释放时触发

 Related #11422
---
 include/power-i.h                           |  4 ++--
 plugins/power/event/power-event-button.cpp  | 13 ++++++++++---
 plugins/power/event/power-event-control.cpp |  2 +-
 plugins/power/power-manager.cpp             |  6 +++---
 plugins/power/power-utils.cpp               |  4 ++--
 5 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/include/power-i.h b/include/power-i.h
index 895c93e..ace2ec6 100644
--- a/include/power-i.h
+++ b/include/power-i.h
@@ -67,8 +67,8 @@ extern "C"
 
     enum PowerEvent
     {
-        // 按下关机键
-        POWER_EVENT_PRESSED_POWEROFF = 0,
+        // 释放关机键
+        POWER_EVENT_RELEASE_POWEROFF = 0,
         // 按下睡眠键
         POWER_EVENT_PRESSED_SLEEP,
         // 按下挂起键
diff --git a/plugins/power/event/power-event-button.cpp b/plugins/power/event/power-event-button.cpp
index 082397b..4fe7747 100644
--- a/plugins/power/event/power-event-button.cpp
+++ b/plugins/power/event/power-event-button.cpp
@@ -48,7 +48,7 @@ void PowerEventButton::init()
     auto login1 = PowerWrapperManager::get_instance()->get_default_login1();
     this->login1_inhibit_fd_ = login1->inhibit("handle-power-key:handle-suspend-key:handle-lid-switch");
 
-    this->register_button(XF86XK_PowerOff, PowerEvent::POWER_EVENT_PRESSED_POWEROFF);
+    this->register_button(XF86XK_PowerOff, PowerEvent::POWER_EVENT_RELEASE_POWEROFF);
     this->register_button(XF86XK_Suspend, PowerEvent::POWER_EVENT_PRESSED_SUSPEND);
     this->register_button(XF86XK_Sleep, PowerEvent::POWER_EVENT_PRESSED_SLEEP);
     this->register_button(XF86XK_Hibernate, PowerEvent::POWER_EVENT_PRESSED_HIBERNATE);
@@ -136,7 +136,14 @@ GdkFilterReturn PowerEventButton::window_event(GdkXEvent *gdk_event, GdkEvent *e
     auto button = (PowerEventButton *)data;
     XEvent *xevent = (XEvent *)gdk_event;
 
-    RETURN_VAL_IF_TRUE(xevent->type != KeyPress, GDK_FILTER_CONTINUE);
+    if (xevent->xkey.keycode == XKeysymToKeycode(button->xdisplay_, XF86XK_PowerOff))
+    {
+        RETURN_VAL_IF_TRUE(xevent->type != KeyRelease, GDK_FILTER_CONTINUE);
+    }
+    else
+    {
+        RETURN_VAL_IF_TRUE(xevent->type != KeyPress, GDK_FILTER_CONTINUE);
+    }
 
     auto keycode = xevent->xkey.keycode;
     auto keycode_str = fmt::format("0x{:x}", keycode);
@@ -148,7 +155,7 @@ GdkFilterReturn PowerEventButton::window_event(GdkXEvent *gdk_event, GdkEvent *e
         return GDK_FILTER_CONTINUE;
     }
 
-    KLOG_DEBUG("Receipt keycode signal: %s.", keycode_str.c_str());
+    KLOG_DEBUG("Receipt keycode signal: %s, event type: %d.", keycode_str.c_str(), xevent->type);
     button->emit_button_signal(iter->second);
 
     return GDK_FILTER_REMOVE;
diff --git a/plugins/power/event/power-event-control.cpp b/plugins/power/event/power-event-control.cpp
index d852c80..c3c0345 100644
--- a/plugins/power/event/power-event-control.cpp
+++ b/plugins/power/event/power-event-control.cpp
@@ -157,7 +157,7 @@ void PowerEventControl::on_button_changed(PowerEvent evnet)
 
     switch (evnet)
     {
-    case POWER_EVENT_PRESSED_POWEROFF:
+    case POWER_EVENT_RELEASE_POWEROFF:
     {
         action = PowerAction(this->power_settings_->get_enum(POWER_SCHEMA_BUTTON_POWER_ACTION));
         PowerSave::get_instance()->do_save(action, error);
diff --git a/plugins/power/power-manager.cpp b/plugins/power/power-manager.cpp
index cf5d8d5..e1f90d2 100644
--- a/plugins/power/power-manager.cpp
+++ b/plugins/power/power-manager.cpp
@@ -188,7 +188,7 @@ void PowerManager::SetEventAction(gint32 event,
 
     switch (event)
     {
-    case PowerEvent::POWER_EVENT_PRESSED_POWEROFF:
+    case PowerEvent::POWER_EVENT_RELEASE_POWEROFF:
         result = this->power_settings_->set_enum(POWER_SCHEMA_BUTTON_POWER_ACTION, action);
         break;
     case PowerEvent::POWER_EVENT_PRESSED_SLEEP:
@@ -225,7 +225,7 @@ void PowerManager::GetEventAction(gint32 event,
 
     switch (event)
     {
-    case PowerEvent::POWER_EVENT_PRESSED_POWEROFF:
+    case PowerEvent::POWER_EVENT_RELEASE_POWEROFF:
         action = this->power_settings_->get_enum(POWER_SCHEMA_BUTTON_POWER_ACTION);
         break;
     case PowerEvent::POWER_EVENT_PRESSED_SLEEP:
@@ -428,7 +428,7 @@ void PowerManager::on_settings_changed(const Glib::ustring& key)
         this->EventActionChanged_signal.emit(PowerEvent::POWER_EVENT_PRESSED_HIBERNATE);
         break;
     case CONNECT(POWER_SCHEMA_BUTTON_POWER_ACTION, _hash):
-        this->EventActionChanged_signal.emit(PowerEvent::POWER_EVENT_PRESSED_POWEROFF);
+        this->EventActionChanged_signal.emit(PowerEvent::POWER_EVENT_RELEASE_POWEROFF);
         break;
     case CONNECT(POWER_SCHEMA_LID_CLOSED_ACTION, _hash):
         this->EventActionChanged_signal.emit(PowerEvent::POWER_EVENT_LID_CLOSED);
diff --git a/plugins/power/power-utils.cpp b/plugins/power/power-utils.cpp
index f04670e..a55b6df 100644
--- a/plugins/power/power-utils.cpp
+++ b/plugins/power/power-utils.cpp
@@ -77,8 +77,8 @@ std::string PowerUtils::event_enum2str(uint32_t event)
 {
     switch (event)
     {
-    case PowerEvent::POWER_EVENT_PRESSED_POWEROFF:
-        return "power off pressed";
+    case PowerEvent::POWER_EVENT_RELEASE_POWEROFF:
+        return "power off release";
     case PowerEvent::POWER_EVENT_PRESSED_SLEEP:
         return "sleep pressed";
     case PowerEvent::POWER_EVENT_PRESSED_SUSPEND:
-- 
2.27.0

