Android Input Framework-InputReader&InputDispatcher
来源:互联网 发布:directx for windows 编辑:程序博客网 时间:2024/06/05 23:44
1 InputReader处理Input消息
在InputReaderThread继承于Thread中,读取RawEvent数据流程如下:
1)
2)
3)
4)
n
n
RawEvent结构体中有一个type变量,用这个变量来识别到底是什么事件:
n
n
n
n
代码如下:
void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
}
1.1 处理设备增加、删除和修改事件
1.1.1 增加设备
void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
}
调用createDeviceLocked()创建了一个InputDevice对象,并添加到mDevices列表中。下面看看createDeviceLocked是如何创建设备的。
先根据mContext, deviceId, name, classes创建一个InputDevice对象,它用于表示单个输入设备的状态。其中的classes为对应Device的classes成员,它用于表示设备类型,定义如下:
enum {
};
创建InputDevice对象之后,会根据classs类型,创建对应的InputManager对象,InputMapper对象主要负责将输入的原始数据进行处理,使其转换成标准的输入事件,每一个InputDevice至少有一个InputMapper,类图关系如下:
这张图,我们关注下mContext变量,InputReader::mContext在构造时用自己的指针初始化了mContext,从而mContext::mReader则为此InputReader实例。在InputReader::createDeviceLocked中创建InputDevice时,把自己的mContext作为参数传入,从而把它保存在InputDevice::mContext中;在创建InputMapper时,以InputDevice作为参数,且InputMapper把它保存在mDevice中,然后从把InputDevice中的mContext也保存在InputMapper的mContext中。
这里我们主要关注的是触摸屏消息:
INPUT_DEVICE_CLASS_TOUCH_MT
INPUT_DEVICE_CLASS_TOUCH
// Touchscreens and touchpad devices.
1.2触摸屏消息处理
1.2.1时序图(触摸消息)
1.2.2 消息处理
关注下两个变量:
n
InputReader::mContext在构造时用自己的指针初始化了mContext,从而mContext::mReader则为此InputReader实例。在InputReader::createDeviceLocked中创建InputDevice时,把自己的mContext作为参数传入,从而把它保存在InputDevice::mContext中;在创建InputMapper时,以InputDevice作为参数,且InputMapper把它保存在mDevice中,然后从把InputDevice中的mContext也保存在InputMapper的mContext中。
n
该变量是在InputReader构造函数中创建的QueuedListener对象,在QueuedListener的构造函数中,将InputDispatcher的实例作为参数赋给内部变量mInnerListener。
mContext->getListener()返回的就是mQueuedListener。
分析RawEvent结构体数据,
n
#define
#define
#define
#define
n
#define
#define
#define
#define
#define
#define
#define
#define
#define
n
对于多点触摸,消息发送方式如下:
ABS_MT_POSITION_X
ABS_MT_POSITION_Y
ABS_MT_TRACKING_ID
ABS_MT_TOUCH_MAJOR
ABS_MT_WIDTH_MAJOR
SYN_MT_REPORT
……
……
ABS_MT_POSITION_X
ABS_MT_POSITION_Y
ABS_MT_TRACKING_ID
ABS_MT_TOUCH_MAJOR
ABS_MT_WIDTH_MAJOR
SYN_MT_REPORT
SYN_REPORT
其中ABS_MT_POSITION_X\Y表示触摸消息的坐标值,ABS_MT_TRACKING_ID表示目前数据是几个手指触摸,SYN_MT_REPORT表示某个触摸信息已经上报完,SYN_REPORT表示一笔完整的多点触摸消息已经上报完成。
对于多点触摸消息,将在MultiTouchInputMapper对消息处理,看看process方法:
void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
}
void TouchInputMapper::process(const RawEvent* rawEvent) {
}
mCursorButtonAccumulator
mCursorScrollAccumulator
mTouchButtonAccumulator处理EV_KEY类型输入事件
mMultiTouchMotionAccumul
n
n
n
n
调用dispatchMotion
n
n
n
把传递过来的NotifyMotionArg参数复制一份,然后加入QueuedInputListener::mArgsQueue例表中。
此时触摸事件的数据已经都放在了mArgsQueue列表中,在InputReader::LoopOnce中,调用mQueuedListener->flush()开始把数据分发到InputDispatcher中,mInnerListener就是InputDispatcher的实例。
void QueuedInputListener::flush() {
}
调用链表中每个NotifyArgs的notify函数这么多类NotifyArgs,为描述方便,下面以NotifyMotionArgs为例,其代码为:
void NotifyMotionArgs::notify(const sp& listener) const {
}
1.2.3消息结构变化流程
2 InputDispatcher 消息分发
InputDispatcherThread开启了一个线程来接收InputReader发送过来的消息,当没有消息的时候,线程进行了休眠,当有消息的时候,mLooer->wake()唤醒线程。InputDispatcherThread主循环如下:
1)
2)
3)
4)
分发消息
5)
2.1 唤醒线程
void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) {
}
n
n
2.2 InputDispatcher线程工作
在5.1中,调用了mLooper->wake()唤醒InputDispatcher线程,分发工作主要是在dispatchOnceInnerLocked方法中。
n
n
n
n
n
n
- Android Input Framework-InputReader&InputDispatcher
- Android Input Framework(三)---InputReader&InputDispatcher
- Android Input Framework(三)---InputReader&InputDispatcher
- Android输入事件InputReader和InputDispatcher分析
- InputReader和InputDispatcher
- Android中InputManagerService里的InputReader和inputDispatcher
- Android input处理机制(一)InputReader
- Android input处理机制(三)InputDispatcher
- Android Input流程分析(三):InputReader
- Android Input流程分析(四):InputDispatcher
- Android 5.0输入系统分析之InputReader和InputDispatcher线程启动过程
- Linux/Android——Input系统之InputReader (七)
- Linux/Android——Input系统之InputReader (七)
- Android Input framework(一)
- android 4.0 framework input
- Android Input Framework(一)
- Android Input framework(一)
- Android Input Framework(一)
- IOC控制反转和DI依赖注入
- 线程学习笔记
- iOS开发:APP拨打电话、发短信、发邮件功能
- 分布式架构理论篇
- Laravel 实战视频教程(精品)
- Android Input Framework-InputReader&InputDispatcher
- 一款微信小程序项目的开发过程全解析
- java execute、executeQuery和executeUpdate之间的区别
- java对象的强引用,软引用,弱引用和虚引用
- 关于学习二叉搜索树的心得体会
- 微信小程序之https步骤一
- 《Intent相关的工具类》
- MySQL不能启动 Can't start server : Bind on unix socket: Permission denied
- 均匀分布(uniform distribution)期望的最大似然估计(maximum likelihood estimation)