From 69256622020faad287372bb038e8267872e748e6 Mon Sep 17 00:00:00 2001
From: meizhigang <meizhigang@kylinsec.com.cn>
Date: Mon, 29 Jan 2024 15:22:21 +0800
Subject: [PATCH] fix(keybinding):Fix shortcut grab in system and conf saved in
 custom
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 - 修复系统快捷键抓取和自定义快捷键配置保存

 Related #25039
---
 plugins/keybinding/custom-shortcut.cpp    | 14 ++++++++++++
 plugins/keybinding/media-keys-manager.cpp | 28 +++++++++++++++++++----
 plugins/keybinding/media-keys-manager.h   |  2 ++
 plugins/keybinding/shortcut-helper.h      |  1 -
 4 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/plugins/keybinding/custom-shortcut.cpp b/plugins/keybinding/custom-shortcut.cpp
index 1325946..4f8267a 100644
--- a/plugins/keybinding/custom-shortcut.cpp
+++ b/plugins/keybinding/custom-shortcut.cpp
@@ -16,11 +16,13 @@
 
 #include <gdk/gdkkeysyms.h>
 #include <gdk/gdkx.h>
+#include "config.h"
 
 #include "plugins/keybinding/shortcut-helper.h"
 
 namespace Kiran
 {
+#define KEYBINDING_CONF_DIR "kylinsec/" PROJECT_NAME "/keybinding"
 #define CUSTOM_SHORTCUT_FILE "custom_shortcut.ini"
 #define CUSTOM_KEYFILE_NAME "name"
 #define CUSTOM_KEYFILE_ACTION "action"
@@ -253,6 +255,18 @@ void CustomShortCuts::change_and_save(std::shared_ptr<CustomShortCut> shortcut,
 
 bool CustomShortCuts::save_to_file()
 {
+    // 文件不存在则先尝试创建对应的目录
+    if (!Glib::file_test(this->conf_file_path_, Glib::FILE_TEST_EXISTS))
+    {
+        auto dirname = Glib::path_get_dirname(this->conf_file_path_);
+        if (g_mkdir_with_parents(dirname.c_str(),
+                                 0775) != 0)
+        {
+            KLOG_WARNING_KEYBINDING("Failed to create directory %s.", dirname.c_str());
+            return false;
+        }
+    }
+
     this->keyfile_.save_to_file(this->conf_file_path_);
     return false;
 }
diff --git a/plugins/keybinding/media-keys-manager.cpp b/plugins/keybinding/media-keys-manager.cpp
index 135ae6d..f701695 100644
--- a/plugins/keybinding/media-keys-manager.cpp
+++ b/plugins/keybinding/media-keys-manager.cpp
@@ -74,10 +74,7 @@ void MediaKeysManager::init_grab_keys()
 
     for (auto &shortcut : system_shortcuts)
     {
-        if (shortcut.second->settings->property_schema_id() != MEDIAKEYS_SCHEMA_ID)
-        {
-            continue;
-        }
+        CONTINUE_IF_FALSE(this->is_media_keys_shortcut(shortcut.second))
 
         KeyState key_state = ShortCutHelper::get_keystate(shortcut.second->key_combination);
 
@@ -113,6 +110,23 @@ void MediaKeysManager::init_grab_keys()
     }
 }
 
+bool MediaKeysManager::is_media_keys_shortcut(std::shared_ptr<SystemShortCut> system_shortcut)
+{
+    if (!system_shortcut->settings)
+    {
+        return false;
+    }
+
+    if (system_shortcut->settings->property_schema_id() == MEDIAKEYS_SCHEMA_ID)
+    {
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
+
 bool MediaKeysManager::is_valid_key_event(XEvent *xev)
 {
     if (xev->type != KeyPress && xev->type != KeyRelease)
@@ -184,6 +198,8 @@ GdkFilterReturn MediaKeysManager::window_event(GdkXEvent *gdk_event, GdkEvent *e
 
 void MediaKeysManager::system_shortcut_added(std::shared_ptr<SystemShortCut> system_shortcut)
 {
+    RETURN_IF_FALSE(this->is_media_keys_shortcut(system_shortcut));
+
     auto iter = this->shortcuts_.find(system_shortcut->uid);
     if (iter != this->shortcuts_.end())
     {
@@ -220,6 +236,8 @@ void MediaKeysManager::system_shortcut_added(std::shared_ptr<SystemShortCut> sys
 
 void MediaKeysManager::system_shortcut_deleted(std::shared_ptr<SystemShortCut> system_shortcut)
 {
+    RETURN_IF_FALSE(this->is_media_keys_shortcut(system_shortcut));
+
     auto iter = this->shortcuts_.find(system_shortcut->uid);
     if (iter == this->shortcuts_.end())
     {
@@ -246,6 +264,8 @@ void MediaKeysManager::system_shortcut_deleted(std::shared_ptr<SystemShortCut> s
 
 void MediaKeysManager::system_shortcut_changed(std::shared_ptr<SystemShortCut> system_shortcut)
 {
+    RETURN_IF_FALSE(this->is_media_keys_shortcut(system_shortcut));
+
     auto iter = this->shortcuts_.find(system_shortcut->uid);
     if (iter == this->shortcuts_.end())
     {
diff --git a/plugins/keybinding/media-keys-manager.h b/plugins/keybinding/media-keys-manager.h
index aa6d154..ce08842 100644
--- a/plugins/keybinding/media-keys-manager.h
+++ b/plugins/keybinding/media-keys-manager.h
@@ -51,6 +51,8 @@ private:
 
     void init_grab_keys();
 
+    bool is_media_keys_shortcut(std::shared_ptr<SystemShortCut> system_shortcut);
+
     bool is_valid_key_event(XEvent *xev);
 
     void system_shortcut_added(std::shared_ptr<SystemShortCut> system_shortcut);
diff --git a/plugins/keybinding/shortcut-helper.h b/plugins/keybinding/shortcut-helper.h
index 1c4a727..0519b70 100644
--- a/plugins/keybinding/shortcut-helper.h
+++ b/plugins/keybinding/shortcut-helper.h
@@ -26,7 +26,6 @@ namespace Kiran
 #define NULL_KEYSTATE KeyState()
 #define SHORTCUT_KIND_CUSTOM "Custom"
 #define SHORTCUT_KEYCOMB_DISABLE "disabled"
-#define KEYBINDING_CONF_DIR "kylinsec/kiran/session-daemon/keybinding"
 
 struct KeyState
 {
-- 
2.27.0

