RK3588 Android13自定义一个按键实现长按短按

一、kernel修改

复制代码
diff --git a/arch/arm64/boot/dts/rockchip/rk3588-nvr-demo.dtsi b/arch/arm64/boot/dts/rockchip/rk3588-nvr-demo.dtsi
index 5aae5c613825..4cc1223f9cbf 100755
--- a/arch/arm64/boot/dts/rockchip/rk3588-nvr-demo.dtsi
+++ b/arch/arm64/boot/dts/rockchip/rk3588-nvr-demo.dtsi
+       
+       gpio-keys {
+               status = "okay";
+               compatible = "gpio-keys";
+               autorepeat;
+               userf1 {
+                               label = "GPIO user key";
+                               linux,code = <KEY_USERKEY>;
+                               gpios = <&gpio1 RK_PA3 GPIO_ACTIVE_LOW>;
+                               debounce-interval = <100>;
+               };
+               userf1_lp {
+                               label = "GPIO user key lp";
+                               linux,code = <KEY_USERKEY_LP>;
+                               gpios = <&gpio1 RK_PD0 GPIO_ACTIVE_LOW>;
+                               debounce-interval = <100>;
+               };
+       };
 };




diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
old mode 100644
new mode 100755
index f2d4e4daa818..9e95f3465620
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -29,6 +29,8 @@
 #include <linux/spinlock.h>
 #include <dt-bindings/input/gpio-keys.h>
 
+static unsigned long start_time = 0;
+
 struct gpio_button_data {
        const struct gpio_keys_button *button;
        struct input_dev *input;
@@ -359,7 +361,8 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
        struct input_dev *input = bdata->input;
        unsigned int type = button->type ?: EV_KEY;
        int state;
-
+       unsigned long press_time = 0;
+       
        state = gpiod_get_value_cansleep(bdata->gpiod);
        if (state < 0) {
                dev_err(input->dev.parent,
@@ -371,7 +374,27 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
                if (state)
                        input_event(input, type, button->code, button->value);
        } else {
-               input_event(input, type, *bdata->code, state);
+               if (*bdata->code == 744) {
+                       if (state) {
+                               start_time = jiffies;
+                       } else {
+                               press_time = jiffies_to_msecs(jiffies - start_time);
+                               printk(KERN_INFO "Button pressed for %lu milliseconds\n", press_time);
+                               if (press_time >= 5000) {
+                                       printk("factoryreset \n");
+                                       input_event(input, type, 745, 1);
+                                       input_sync(input);
+                                       input_event(input, type, 745, state);
+                               } else {
+                                       input_event(input, type, 744, 1);
+                                       input_sync(input);
+                                       input_event(input, type, 744, state);
+                               }
+                       }
+               } else {
+                       input_event(input, type, *bdata->code, state);
+               }
+               printk("gpio_keys_gpio_report_event input_event *bdata->code=%d,state = %d\n",*bdata->code, state);
        }
        input_sync(input);
 }
@@ -405,6 +428,7 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
                         * handler to run.
                         */
                        input_report_key(bdata->input, button->code, 1);
+                       printk("gpio_keys_gpio_isr input_report_key *bdata->code=%d\n",button->code);
                }
        }
 
@@ -424,6 +448,7 @@ static void gpio_keys_irq_timer(struct timer_list *t)
        spin_lock_irqsave(&bdata->lock, flags);
        if (bdata->key_pressed) {
                input_event(input, EV_KEY, *bdata->code, 0);
+               printk("gpio_keys_irq_timer input_event *bdata->code=%d\n",*bdata->code);
                input_sync(input);
                bdata->key_pressed = false;
        }
@@ -445,10 +470,12 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
                        pm_wakeup_event(bdata->input->dev.parent, 0);
 
                input_event(input, EV_KEY, *bdata->code, 1);
+               printk("gpio_keys_irq_isr input_event *bdata->code=%d\n",*bdata->code);
                input_sync(input);
 
                if (!bdata->release_delay) {
                        input_event(input, EV_KEY, *bdata->code, 0);
+                       printk("gpio_keys_irq_isr !bdata->release_delay input_event *bdata->code=%d\n",*bdata->code);
                        input_sync(input);
                        goto out;
                }





diff --git a/include/dt-bindings/input/rk-input.h b/include/dt-bindings/input/rk-input.h
index 00b412927890..06c66d7c6bd4 100644
--- a/include/dt-bindings/input/rk-input.h
+++ b/include/dt-bindings/input/rk-input.h
@@ -578,6 +578,9 @@
 #define KEY_BRIGHTNESS_MIN             0x250   /* Set Brightness to Minimum */
 #define KEY_BRIGHTNESS_MAX             0x251   /* Set Brightness to Maximum */
 
+#define KEY_USERKEY     0x2e8
+#define KEY_USERKEY_LP  0x2e9
+
 #define BTN_TRIGGER_HAPPY              0x2c0
 #define BTN_TRIGGER_HAPPY1             0x2c0
 #define BTN_TRIGGER_HAPPY2             0x2c1
diff --git a/include/uapi/linux/input-event-codes.h b/include/uapi/linux/input-event-codes.h
index 7989d9483ea7..1064395e72c3 100644
--- a/include/uapi/linux/input-event-codes.h
+++ b/include/uapi/linux/input-event-codes.h
@@ -778,6 +778,8 @@
 #define BTN_TRIGGER_HAPPY38            0x2e5
 #define BTN_TRIGGER_HAPPY39            0x2e6
 #define BTN_TRIGGER_HAPPY40            0x2e7
+#define KEY_USERKEY     0x2e8
+#define KEY_USERKEY_LP  0x2e9

二、framework修改

复制代码
diff --git a/core/api/current.txt b/core/api/current.txt
index 1d610ab7536b..15e5b6487d13 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -48634,6 +48634,8 @@ package android.view {
     field public static final int KEYCODE_TV_ZOOM_MODE = 255; // 0xff
     field public static final int KEYCODE_U = 49; // 0x31
     field public static final int KEYCODE_UNKNOWN = 0; // 0x0
+    field public static final int KEYCODE_USERKEY = 305; // 0x131
+    field public static final int KEYCODE_USERKEY_LP = 306; // 0x132
     field public static final int KEYCODE_V = 50; // 0x32
     field public static final int KEYCODE_VIDEO_APP_1 = 289; // 0x121
     field public static final int KEYCODE_VIDEO_APP_2 = 290; // 0x122
diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java
index c3a638c4c36a..e5a21e3f3e06 100644
--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -866,6 +866,8 @@ public class KeyEvent extends InputEvent implements Parcelable {
     public static final int KEYCODE_DEMO_APP_3 = 303;
     /** Key code constant: Demo Application key #4. */
     public static final int KEYCODE_DEMO_APP_4 = 304;
+    public static final int KEYCODE_USERKEY = 305;
+    public static final int KEYCODE_USERKEY_LP = 306;
 
    /**
      * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index d86aa1122d3a..fb3bccc7fd80 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -2004,7 +2004,9 @@
         <enum name="KEYCODE_DEMO_APP_1" value="301" />
         <enum name="KEYCODE_DEMO_APP_2" value="302" />
         <enum name="KEYCODE_DEMO_APP_3" value="303" />
-        <enum name="KEYCODE_DEMO_APP_4" value="304" />
+       <enum name="KEYCODE_DEMO_APP_4" value="304" />
+       <enum name="KEYCODE_USERKEY" value="305" />
+       <enum name="KEYCODE_USERKEY_LP" value="306" />
     </attr>
 
     <!-- ***************************************************************** -->
diff --git a/data/keyboards/Generic.kl b/data/keyboards/Generic.kl
index c81473ddcfc6..0096266539a4 100644
--- a/data/keyboards/Generic.kl
+++ b/data/keyboards/Generic.kl
@@ -410,6 +410,8 @@ key 580   APP_SWITCH
 key 582   VOICE_ASSIST
 # Linux KEY_ASSISTANT
 key 583   ASSIST
+key 744   USERKEY
+key 745   USERKEY_LP
 
 # Keys defined by HID usages
 key usage 0x0c0067 WINDOW
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
old mode 100644
new mode 100755
index 40643f638ac0..3f573a8a52de
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -182,6 +182,8 @@ import android.view.accessibility.AccessibilityManager;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.view.autofill.AutofillManagerInternal;
+import android.os.UserManager;
+import android.os.RecoverySystem;
 
 import com.android.internal.R;
 import com.android.internal.accessibility.AccessibilityShortcutController;
@@ -3031,6 +3033,27 @@ public class PhoneWindowManager implements WindowManagerPolicy {
             case KeyEvent.KEYCODE_DEMO_APP_4:
                 Slog.wtf(TAG, "KEYCODE_APP_X should be handled in interceptKeyBeforeQueueing");
                 return key_consumed;
+                       case KeyEvent.KEYCODE_USERKEY:
+                 //do nothing
+                                PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+                                pm.reboot(null);
+                                Slog.wtf(TAG, "KeyEvent.KEYCODE_USERKEY in interceptKeyBeforeQueueing");
+                               break;
+                       case KeyEvent.KEYCODE_USERKEY_LP:
+                           // 检查是否允许当前用户执行恢复出厂设置操作
+                               UserManager um = mContext.getSystemService(UserManager.class);
+                               if (um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
+                                       throw new SecurityException("Factory reset is not allowed for this user.");
+                               }
+
+                               // 执行恢复出厂设置操作
+                               try {
+                                       RecoverySystem.rebootWipeUserData(mContext);
+                               } catch (IOException e) {
+                                       // 处理异常
+                               }
+                               Slog.wtf(TAG, "KeyEvent.KEYCODE_USERKEY_LP in interceptKeyBeforeQueueing");
+                               break;
             case KeyEvent.KEYCODE_BRIGHTNESS_UP:
             case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
                 if (down) {




diff --git a/include/android/keycodes.h b/include/android/keycodes.h
index 3357660f5c..52750f400e 100644
--- a/include/android/keycodes.h
+++ b/include/android/keycodes.h
@@ -809,7 +809,8 @@ enum {
     AKEYCODE_DEMO_APP_3 = 303,
     /** Demo Application key #4. */
     AKEYCODE_DEMO_APP_4 = 304,
-
+    AKEYCODE_USERKEY = 305,
+    AKEYCODE_USERKEY_LP = 306,
     // NOTE: If you add a new keycode here you must also add it to several other files.
     //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
 };
diff --git a/libs/input/InputEventLabels.cpp b/libs/input/InputEventLabels.cpp
index 2d768ce573..9cd92a7bef 100644
--- a/libs/input/InputEventLabels.cpp
+++ b/libs/input/InputEventLabels.cpp
@@ -330,7 +330,9 @@ namespace android {
     DEFINE_KEYCODE(DEMO_APP_1), \
     DEFINE_KEYCODE(DEMO_APP_2), \
     DEFINE_KEYCODE(DEMO_APP_3), \
-    DEFINE_KEYCODE(DEMO_APP_4)
+    DEFINE_KEYCODE(DEMO_APP_4), \
+    DEFINE_KEYCODE(USERKEY), \
+    DEFINE_KEYCODE(USERKEY_LP)
 
 // NOTE: If you add a new axis here you must also add it to several other files.
相关推荐
小米里的大麦3 小时前
022 基础 IO —— 文件
linux
Xの哲學3 小时前
Perf使用详解
linux·网络·网络协议·算法·架构
门前灯3 小时前
Linux系统之iprconfig 命令详解
linux·运维·服务器·iprconfig
tb_first3 小时前
k8sday09
linux·云原生·容器·kubernetes
忧郁的橙子.3 小时前
三、k8s 1.29 之 安装2
linux·运维·服务器
huangyuchi.4 小时前
【Linux系统】动静态库的制作
linux·运维·服务器·动态库·静态库·库的简单制作
jim写博客4 小时前
Linux进程概念(四)环境地址变量
linux·运维·服务器
稚辉君.MCA_P8_Java4 小时前
豆包 Java的23种设计模式
java·linux·jvm·设计模式·kubernetes
Nie_Xun5 小时前
ubuntu网络共享
linux·运维·ubuntu
花小璇学linux6 小时前
imx6ull-驱动开发篇22——Linux 时间管理和内核定时器
linux·运维·驱动开发