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

相关文章

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti