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

2024-06-05 07:28

本文主要是介绍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; // 0xfffield public static final int KEYCODE_U = 49; // 0x31field 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; // 0x132field public static final int KEYCODE_V = 50; // 0x32field public static final int KEYCODE_VIDEO_APP_1 = 289; // 0x121field 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_SWITCHkey 582   VOICE_ASSIST# Linux KEY_ASSISTANTkey 583   ASSIST
+key 744   USERKEY
+key 745   USERKEY_LP# Keys defined by HID usageskey 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.

这篇关于RK3588 Android13自定义一个按键实现长按短按的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1032441

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (