LinuxAndroid: 旋转编码器input输入事件适配(旋转输入)

news2025/6/25 13:10:50

rk3588s: 旋转编码器input输入事件适配

基于Android 12 + kernel-5.10版本

参考文档:
https://blog.csdn.net/szembed/article/details/131551950
Linux 输入设备调试详解(零基础开发)Rotary_Encoder旋转编码器驱动 通用GPIO为例 挂载input输入子系统

https://source.android.google.cn/docs/core/interaction/input?hl=zh-cn
https://developer.android.google.cn/reference/android/support/wearable/input/RotaryEncoder
https://developer.android.google.cn/training/wearables/user-input/rotary-input?hl=zh-cn
旋转输入
某些 Wear OS 设备包含实体侧面旋钮。当用户旋转此类旋钮时,应用的当前视图会向上或向下滚动。此类输入称为“旋转输入”。

1,驱动层配置
配置设备树,使用已有的rotary_encoder.c驱动代码。

linux驱动设备树配置参考:
https://elixir.bootlin.com/linux/latest/source/drivers/input/misc/rotary_encoder.c
https://elixir.bootlin.com/linux/latest/source/arch/arm64/boot/dts/freescale/imx8mn-dimonoff-gateway-evk.dts
	rotary: rotary-encoder {
		compatible = "rotary-encoder";
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_rotary>;
		gpios = <&gpio5 12 GPIO_ACTIVE_LOW>, /* A */
			<&gpio5 13 GPIO_ACTIVE_LOW>; /* B */
		linux,axis = <0>; /* REL_X */
		rotary-encoder,relative-axis;
	};

	pinctrl_rotary: rotarygrp {
		fsl,pins = <
			MX8MN_IOMUXC_ECSPI2_MISO_GPIO5_IO12	0x00000156
			MX8MN_IOMUXC_ECSPI2_SS0_GPIO5_IO13	0x00000156
		>;
	};

2,framework层适配
上面驱动层配置好设备树后,通过getevent能看到rotary encoder事件。
但是,应用App层却收不到。
旋转编码器input输入事件和鼠标滚轮类似,设备上鼠标滚轮事件是正常的。
于是,先看鼠标滚轮事件。

鼠标滚轮:
$ adb shell getevent -lpi
add device 2: /dev/input/event2
  bus:      0003
  vendor    093a
  product   2533
  version   0111
  name:     "Gaming Mouse"
  location: "usb-fc840000.usb-1/input0"
  id:       ""
  version:  1.0.1
  events:
    KEY (0001): BTN_MOUSE             BTN_RIGHT             BTN_MIDDLE            BTN_SIDE             
                BTN_EXTRA            
    REL (0002): REL_X                 REL_Y                 REL_WHEEL             REL_WHEEL_HI_RES     
    MSC (0004): MSC_SCAN             
  input props:
    <none>

adb shell dumpsys input
    2: Gaming Mouse
      Classes: CURSOR | EXTERNAL
      Path: /dev/input/event2
      Enabled: true
      Descriptor: 922b2be403d5734c3dacd1c480566209f0f39e80
      Location: usb-fc840000.usb-1/input0
      ControllerNumber: 0
      UniqueId: 
      Identifier: bus=0x0003, vendor=0x093a, product=0x2533, version=0x0111
      KeyLayoutFile: 
      KeyCharacterMapFile: 
      ConfigurationFile: 
      VideoDevice: <none>

01-11 03:13:33.710   569   663 I EventHub: New device: id=6, fd=181, path='/dev/input/event2', name='Gaming Mouse', classes=CURSOR | EXTERNAL, configuration='', keyLayout='', keyCharacterMap='', builtinKeyboard=false, 
01-11 03:13:33.714   569   663 I InputReader: Device added: id=5, eventHubId=6, name='Gaming Mouse', descriptor='922b2be403d5734c3dacd1c480566209f0f39e80',sources=0x00002002

rotary encoder事件信息

rotary encoder事件信息
$ adb shell getevent -lpi
add device 2: /dev/input/event0
  bus:      0019
  vendor    0000
  product   0000
  version   0000
  name:     "rotary"  // 设备名是rotary
  location: ""
  id:       ""
  version:  1.0.1
  events:
    REL (0002): REL_X                
  input props:
    <none>

添加rotary.idc文件,用于framework层识别rotary encoder设备

framework层代码流程分析:
frameworks/native/services/inputflinger/reader/EventHub.cpp
    // Load the configuration file for the device.
    device->loadConfigurationLocked();
 

    // 要想rotary encoder旋转编码器被framework层识别到需要的条件:要有configuration文件且device.type为rotaryEncoder
    // See if this is a rotary encoder type device.
    String8 deviceType = String8();
    if (device->configuration &&
        device->configuration->tryGetProperty(String8("device.type"), deviceType)) {
        if (!deviceType.compare(String8("rotaryEncoder"))) {
            device->classes |= InputDeviceClass::ROTARY_ENCODER;
        }
    }


    // 根据设备名找configuration配置文件,adb shell getevent -lpi 查看到设备名是name:     "rotary"
    // Try device name.
    return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName(), type);  

先在android源代码device目录grep -r "rotaryEncoder"搜索看看是否有类似配置。
搜索到virtio_input_rotary.idc,所以,执行如下操作验证framework层就能识别到旋转编码器设备了,
cp device/generic/goldfish/input/virtio_input_rotary.idc rotary.idc
adb push rotary.idc /system/usr/idc/

添加rotary.idc文件,虽然framework层识别到了rotary encoder设备,但是事件还是报不到App层。继续分析。(原因是:rotary encoder报的事件是 EV_REL REL_X,而RotaryEncoderInputMapper没有解析REL_X事件。因此,需要适配解析REL_X事件)

打开DEBUG_INBOUND_EVENT_DETAILS log开关后,验证旋转编码器的input事件,
能看到log时,说明App层就能收到事件,如果打印不出该log,则App层收不到事件。
void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) {
#if DEBUG_INBOUND_EVENT_DETAILS
    ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, "
          "displayId=%" PRId32 ", policyFlags=0x%x, "
          "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, "
          "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, "
          "yCursorPosition=%f, downTime=%" PRId64,
          args->id, args->eventTime, args->deviceId, args->source, args->displayId,
          args->policyFlags, args->action, args->actionButton, args->flags, args->metaState,
          args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision,
          args->xCursorPosition, args->yCursorPosition, args->downTime);
    for (uint32_t i = 0; i < args->pointerCount; i++) {
        ALOGD("  Pointer %d: id=%d, toolType=%d, "
              "x=%f, y=%f, pressure=%f, size=%f, "
              "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
              "orientation=%f",
              i, args->pointerProperties[i].id, args->pointerProperties[i].toolType,
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
              args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
    }
#endif

RotaryEncoderInputMapper解析旋转编码器的input事件数据

RotaryEncoderInputMapper::sync() 函数解析旋转编码器的input事件
$ adb shell getevent -l
add device 1: /dev/input/event0
  name:     "rotary"

/dev/input/event0: EV_REL       REL_X                00000001            
/dev/input/event0: EV_SYN       SYN_REPORT           00000000            

/dev/input/event0: EV_REL       REL_X                ffffffff            
/dev/input/event0: EV_SYN       SYN_REPORT           00000000
 
86  void RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
87      mRotaryEncoderScrollAccumulator.process(rawEvent);
88  
89      if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
90          sync(rawEvent->when, rawEvent->readTime);
91      }
92  }
93  
94  void RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
95      PointerCoords pointerCoords;
96      pointerCoords.clear();
97  
98      PointerProperties pointerProperties;
99      pointerProperties.clear();
100      pointerProperties.id = 0;
101      pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
102  
+    ALOGI("RotaryEncoderInputMapper::sync");
		 // scroll 返回的是0,导致下面notifyMotion走不到。需要在getRelativeVWheel函数里适配
103      float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
104      bool scrolled = scroll != 0;
105  
106      // This is not a pointer, so it's not associated with a display.
107      int32_t displayId = ADISPLAY_ID_NONE;
108  
109      // Moving the rotary encoder should wake the device (if specified).
110      uint32_t policyFlags = 0;
111      if (scrolled && getDeviceContext().isExternal()) {
112          policyFlags |= POLICY_FLAG_WAKE;
113      }
114  
115      if (mOrientation == DISPLAY_ORIENTATION_180) {
116          scroll = -scroll;
117      }
118  
119      // Send motion event.
120      if (scrolled) {
121          int32_t metaState = getContext()->getGlobalMetaState();
122          pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
         // 添加x的值,否则,无论正向旋转还是反向旋转,x值都是0,导致应用App无法识别方向
+        pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, scroll);
123  
124          NotifyMotionArgs scrollArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
125                                      mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0,
126                                      0, metaState, /* buttonState */ 0, MotionClassification::NONE,
127                                      AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
128                                      &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
129                                      AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {});
130          getListener()->notifyMotion(&scrollArgs);
+    ALOGI("RotaryEncoderInputMapper::sync notifyMotion");
131      }
132  
133      mRotaryEncoderScrollAccumulator.finishSync();
134  }


42  void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
43      if (rawEvent->type == EV_REL) {
44          switch (rawEvent->code) {
45              case REL_WHEEL:
46                  mRelWheel = rawEvent->value;
47                  break;
48              case REL_HWHEEL:
49                  mRelHWheel = rawEvent->value;
50                  break;
+            case REL_X:  // 由于自己的旋转编码器报的事件是REL_X,所以,需要添加该类型解析
+                mRelWheel = rawEvent->value;
+                break;
51          }
52      }
53  }
log: 

旋转编码器正向旋转:x=1.000000   SOURCE_ROTARY_ENCODER = 0x00400000  <==> source=0x400000
04-07 05:47:32.041   575   667 D InputDispatcher: notifyMotion - id=b0eb158 eventTime=1947954629000, deviceId=4, source=0x400000, displayId=-1, policyFlags=0x0, action=0x8, actionButton=0x0, flags=0x0, metaState=0x0, buttonState=0x0, edgeFlags=0x0, xPrecision=0.000000, yPrecision=0.000000, xCursorPosition=nan, yCursorPosition=nan, downTime=0
04-07 05:47:32.041   575   667 D InputDispatcher:   Pointer 0: id=0, toolType=0, x=1.000000, y=0.000000, pressure=0.000000, size=0.000000, touchMajor=0.000000, touchMinor=0.000000, toolMajor=0.000000, toolMinor=0.000000, orientation=0.000000
04-07 05:47:32.041   575   666 D InputDispatcher: dispatchMotion - eventTime=1947954629000, deviceId=4, source=0x400000, displayId=-1, policyFlags=0x62000000, action=0x8, actionButton=0x0, flags=0x0, metaState=0x0, buttonState=0x0,edgeFlags=0x0, xPrecision=0.000000, yPrecision=0.000000, downTime=0
04-07 05:47:32.041   575   666 D InputDispatcher:   Pointer 0: id=0, toolType=0, x=1.000000, y=0.000000, pressure=0.000000, size=0.000000, touchMajor=0.000000, touchMinor=0.000000, toolMajor=0.000000, toolMinor=0.000000, orientation=0.000000

旋转编码器反向旋转:x=-1.000000  SOURCE_ROTARY_ENCODER = 0x00400000  <==> source=0x400000
04-07 05:47:35.923   575   667 I InputReader: lqy111 RotaryEncoderInputMapper::sync
04-07 05:47:35.923   575   667 I InputReader: lqy111 RotaryEncoderInputMapper::sync: scroll:-1.000000
04-07 05:47:35.923   575   667 I InputReader: lqy111 RotaryEncoderInputMapper::sync notifyMotion
04-07 05:47:35.923   575   667 D InputDispatcher: notifyMotion - id=45c55f eventTime=1951836828000, deviceId=4, source=0x400000, displayId=-1, policyFlags=0x0, action=0x8, actionButton=0x0, flags=0x0, metaState=0x0, buttonState=0x0, edgeFlags=0x0, xPrecision=0.000000, yPrecision=0.000000, xCursorPosition=nan, yCursorPosition=nan, downTime=0
04-07 05:47:35.923   575   667 D InputDispatcher:   Pointer 0: id=0, toolType=0, x=-1.000000, y=0.000000, pressure=0.000000, size=0.000000, touchMajor=0.000000, touchMinor=0.000000, toolMajor=0.000000, toolMinor=0.000000, orientation=0.000000
04-07 05:47:35.923   575   666 D InputDispatcher: dispatchMotion - eventTime=1951836828000, deviceId=4, source=0x400000, displayId=-1, policyFlags=0x62000000, action=0x8, actionButton=0x0, flags=0x0, metaState=0x0, buttonState=0x0,edgeFlags=0x0, xPrecision=0.000000, yPrecision=0.000000, downTime=0
04-07 05:47:35.923   575   666 D InputDispatcher:   Pointer 0: id=0, toolType=0, x=-1.000000, y=0.000000, pressure=0.000000, size=0.000000, touchMajor=0.000000, touchMinor=0.000000, toolMajor=0.000000, toolMinor=0.000000, orientation=0.000000

core/java/android/view/InputDevice.java:    public static final int SOURCE_ROTARY_ENCODER = 0x00400000 | SOURCE_CLASS_NONE; <==> source=0x400000

App层监听旋转输入事件
在这里插入图片描述

App层监听旋转输入事件:
developer.android.google.cn/training/wearables/user-input/rotary-input
myView.setOnGenericMotionListener
onGenercMotion
或者 在Activity也可以。

framework层监听旋转输入事件:
在NativeInputManager::interceptMotionBeforeQueueing()添加适配代码。
com_android_server_input_InputManagerService.cpp
void NativeInputManager::interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
        uint32_t& policyFlags) {

调试总结

make libinputreader -j3
make libinputflinger -j3 && make libinputflinger_base -j3

while true; do echo "######$(date)######";adb logcat -b all | grep -i -E "EventHub|InputDispatcher|InputReader|WindowManager"; done

adb shell getevent
adb shell getevent -l
adb shell getevent -lip
adb shell dumpsys input

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1584291.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【mT5多语言翻译】之一——实战项目总览

[1] 总览 【mT5多语言翻译】系列共六篇文章&#xff1a; 【mT5多语言翻译】之一——实战项目总览   【mT5多语言翻译】之二——模型&#xff1a;T5模型与mT5模型与前置知识   【mT5多语言翻译】之三——数据集&#xff1a;多语言翻译数据集与预处理   【mT5多语言翻译】之…

Java使用aspose-words实现word文档转pdf

Java使用aspose-words实现word文档转pdf 1.获取转换jar文件并安装到本地maven仓库 aspose-words-15.8.0-jdk16.jar包下载地址&#xff1a;https://zhouquanquan.lanzn.com/b00g257yja 密码:965f 下载aspose-words-15.8.0-jdk16.jar包后&#xff0c;通过maven命令手动安装到本…

报修小程序怎么建立?维修服务行业的智能化升级

在这个数字化飞速发展的时代&#xff0c;维修服务行业也在经历着前所未有的变革。消费者对于服务的期待不再局限于传统的电话预约或线下等待&#xff0c;而是希望能够通过更加智能、便捷的途径解决日常生活中的维修问题。在这样的背景下&#xff0c;报修小程序应运而生&#xf…

SAAS医院管理系统总结

时间很久了&#xff0c;颗粒归仓的重要性 再次体现&#xff0c;经历即成长 兼职也能学到东西 boot web mybatis-plus dynamic-datasource druid pagehelper必须的啦 shiro devtools没必要 软件供应商 给客户提供服务的形式&#xff1a; SAAS&#xff1a;软件即服务&#xf…

Github Benefits 学生认证/学生包 新版申请指南

本教程适用于2024年之后的Github学生认证申请&#xff0c;因为现在的认证流程改变了很多&#xff0c;所以重新进行了总结这方面的指南。 目录 验证教育邮箱修改个人资料制作认证文件图片转换Base64提交验证 验证教育邮箱 进入Email settings&#xff0c;找到Add email address…

kafka(五)——消费者流程分析(c++)

概念 ​ 消费者组&#xff08;Consumer Group&#xff09;&#xff1a;由多个consumer组成。消费者组内每个消费者负责消费不同分区的数据&#xff0c;一个分区只能由一个组内消费者消费&#xff1b;消费者组之间互不影响。所有的消费者都属于某个消费者组&#xff0c;即消费者…

219 基于matlab的汽车悬架(钢板弹簧,减震器)设计程序GUI

基于matlab的汽车悬架&#xff08;钢板弹簧&#xff0c;减震器&#xff09;设计程序&#xff27;&#xff35;&#xff29;。根据需求输入设计参数&#xff0c;包括前桥负荷、簧下质量、弹簧刚度、阻尼等&#xff0c;输出钢板弹簧、减震器结果。程序已调通&#xff0c;可直接运…

数据结构-----链表

目录 1.顺序表经典算法 &#xff08;1&#xff09;移除元素 &#xff08;2&#xff09;合并数组 2.链表的创建 &#xff08;1&#xff09;准备工作 &#xff08;2&#xff09;建结构体 &#xff08;3&#xff09;链表打印 &#xff08;4&#xff09;尾插数据 &#xff…

电介质材料(三)

本篇为西安交通大学本科课程《电气材料基础》的笔记。 本篇为这一单元的第三篇笔记&#xff0c;上一篇传送门&#xff0c;下一篇传送门。 固体电介质材料 分为有机绝缘材料和无机绝缘材料。有机绝缘材料包括塑料、橡胶、纤维等&#xff1b;无机绝缘材料包括玻璃、陶瓷、云母…

Spring Boot统一功能处理之拦截器

本篇主要介绍Spring Boot的统一功能处理中的拦截器。 目录 一、拦截器的基本使用 二、拦截器实操 三、浅尝源码 初始化DispatcherServerlet 处理请求&#xff08;doDispatch) 四、适配器模式 一、拦截器的基本使用 在一般的学校或者社区门口&#xff0c;通常会安排几个…

Harmony鸿蒙南向驱动开发-UART

UART指异步收发传输器&#xff08;Universal Asynchronous Receiver/Transmitter&#xff09;&#xff0c;是通用串行数据总线&#xff0c;用于异步通信。该总线双向通信&#xff0c;可以实现全双工传输。 两个UART设备的连接示意图如下&#xff0c;UART与其他模块一般用2线&a…

关于Keil 的编译选项 LTO 代码优化 你必须知道的事

LTO 这个 听起来很好&#xff0c;功能强大 但是一定要慎用 平时不要用&#xff0c; 功能开发完成前不要用&#xff0c; 等代码运行稳定后在开启优化师可以的 不然 掉坑里 你很久会爬不出来 这个选项 会让很多高级自定义功能失效&#xff0c;甚至函数报错&#xff0c;奇怪的异…

算法打卡day31

今日任务&#xff1a; 1&#xff09;435.无重叠区间 2&#xff09;763.划分字母区间 3&#xff09;56.合并区间 435.无重叠区间 题目链接&#xff1a;435. 无重叠区间 - 力扣&#xff08;LeetCode&#xff09; 给定一个区间的集合&#xff0c;找到需要移除区间的最小数量&…

sqoop:错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster(已解决)

1 报错信息 错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster 说明&#xff1a; 操作将数据库中的数据导入到HDFS中 执行sqoop import --connect jdbc:mysql://aaa01:3306/mysql --username root --password root --table test 时报了以下错误 2 报…

【MIT6.S081】Lab1: Xv6 and Unix utilities(详细解答版)

实验内容网址&#xff1a;https://xv6.dgs.zone/labs/requirements/lab1.html Sleep 关键点&#xff1a;函数参数判断、系统函数调用 思路&#xff1a; 通过argc来判断函数参数是否正确&#xff0c;通过atoi函数来讲字符串转化为整型&#xff0c;调用sleep函数后退出程序。 代…

Harmony鸿蒙南向驱动开发-MIPI DSI

功能简介 DSI&#xff08;Display Serial Interface&#xff09;是由移动行业处理器接口联盟&#xff08;Mobile Industry Processor Interface (MIPI) Alliance&#xff09;制定的规范&#xff0c;旨在降低移动设备中显示控制器的成本。它以串行的方式发送像素数据或指令给外…

5.2 配置静态路由

5.2.1 实验1&#xff1a;配置IPv4静态路由 1、实验目的 通过本实验可以掌握&#xff1a; 配置带下一跳地址的IPv4静态路由的方法。配置带送出接口的IPv4静态路由的方法。配置总结IPv4静态路由的方法。配置浮动IPv4静态路由的方法。代理 ARP的作用。路由表的含义。扩展ping命…

铸造大型基础平板的结构应该怎样设计

设计大型基础平板的结构时&#xff0c;需要考虑以下几个方面&#xff1a; 地质条件&#xff1a;首先要了解工程所在地的地质条件&#xff0c;包括土质、地下水位、地震状况等。根据地质条件来选择合适的基础类型&#xff0c;如浅基、深基或地下连续墙等。 荷载分析&#xff1a…

使用docker制作Android镜像(实操可用)

一、安装包准备 1、准备jdk 下载地址&#xff1a;Java Downloads | Oracle 注意版本&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 我下载的jdk17&#xff0c;不然后面构建镜像报错&#xff0c;就是版本不对 2、准备安装的工具包 ttps://dev…

大话设计模式之迭代器模式

迭代器模式是一种行为设计模式&#xff0c;它允许客户端逐个访问集合中的元素&#xff0c;而不暴露集合的底层表示。这种模式提供了一种方法来访问聚合对象中的各个元素&#xff0c;而不需要暴露其内部结构。 迭代器模式由以下几个关键角色组成&#xff1a; 迭代器&#xff08…