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

相关文章

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用

Java实现Excel与HTML互转

《Java实现Excel与HTML互转》Excel是一种电子表格格式,而HTM则是一种用于创建网页的标记语言,虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,下面我们就来看看... Excel是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言。虽然两

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

NFS实现多服务器文件的共享的方法步骤

《NFS实现多服务器文件的共享的方法步骤》NFS允许网络中的计算机之间共享资源,客户端可以透明地读写远端NFS服务器上的文件,本文就来介绍一下NFS实现多服务器文件的共享的方法步骤,感兴趣的可以了解一... 目录一、简介二、部署1、准备1、服务端和客户端:安装nfs-utils2、服务端:创建共享目录3、服

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭

Python实现高效地读写大型文件

《Python实现高效地读写大型文件》Python如何读写的是大型文件,有没有什么方法来提高效率呢,这篇文章就来和大家聊聊如何在Python中高效地读写大型文件,需要的可以了解下... 目录一、逐行读取大型文件二、分块读取大型文件三、使用 mmap 模块进行内存映射文件操作(适用于大文件)四、使用 pand

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一