当前位置: 首页 > news >正文

【安卓13 源码】Input子系统(3) - EventHub增加设备的流程

由前面的分析知道,在创建inputreader 线程的时候,会去循环执行 looponce 方法。主要的处理工作是:

  • 通过 getEvents() 从 EventHub 获取未处理的事件,这些事件分为两类:一类是原始输入事 件即从设备节点中读取出的原始事件;一类是设备事件即输入设备可用性变化事件
  • 通过 processEventsLocked() 对事件进行预处理

/frameworks/native/services/inputflinger/reader/InputReader.cpp

void InputReader::loopOnce() {int32_t oldGeneration;int32_t timeoutMillis;bool inputDevicesChanged = false;std::vector<InputDeviceInfo> inputDevices;{ // acquire lockstd::scoped_lock _l(mLock);oldGeneration = mGeneration;timeoutMillis = -1;uint32_t changes = mConfigurationChangesToRefresh;if (changes) {mConfigurationChangesToRefresh = 0;timeoutMillis = 0;refreshConfigurationLocked(changes);} else if (mNextTimeout != LLONG_MAX) {nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);}} // release lock// 1)首先去eventhub 中获取事件,缓存到 mEventBuffersize_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);{ // acquire lockstd::scoped_lock _l(mLock);mReaderIsAliveCondition.notify_all();if (count) {// 2)对 mEventBuffer 事件进行处理processEventsLocked(mEventBuffer, count);}

1)首先去eventhub 中获取事件,缓存到 mEventBuffer

 /frameworks/native/services/inputflinger/reader/EventHub.cpp

size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {ALOG_ASSERT(bufferSize >= 1);std::scoped_lock _l(mLock);。。。。
//  初始值 mNeedToScanDevices 是为true 的if (mNeedToScanDevices) {mNeedToScanDevices = false;
// 去扫描设备 scanDevicesLockedscanDevicesLocked();mNeedToSendFinishedDeviceScan = true;}
// 遍历所有的打开的设备mOpeningDeviceswhile (!mOpeningDevices.empty()) {
// 获取设备 Devicestd::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());mOpeningDevices.pop_back();ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str());event->when = now;event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;// 设置event事件类型是增加的DEVICE_ADDEDevent->type = DEVICE_ADDED;event += 1;// Try to find a matching video device by comparing device namesfor (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();it++) {std::unique_ptr<TouchVideoDevice>& videoDevice = *it;if (tryAddVideoDeviceLocked(*device, videoDevice)) {// videoDevice was transferred to 'device'it = mUnattachedVideoDevices.erase(it);break;}}// 然后将设备的id和设备保存到 mDevices 中auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));

 // 扫描设备 scanDevicesLocked


// 扫描设备,path 为:DEVICE_PATH = "/dev/input";
void EventHub::scanDevicesLocked() {status_t result = scanDirLocked(DEVICE_PATH);=======
status_t EventHub::scanDirLocked(const std::string& dirname) {for (const auto& entry : std::filesystem::directory_iterator(dirname)) {// 打开所有的设备openDeviceLocked(entry.path());}=======
void EventHub::openDeviceLocked(const std::string& devicePath) {ALOGV("Opening device: %s", devicePath.c_str());// 打开驱动设备int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);。。。。。// Allocate device.  (The device object takes ownership of the fd at this point.)int32_t deviceId = mNextDeviceId++;
// 创建 Device 对象std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier);。。。。device->configureFd();// 增肌 device,会打印下列logALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, ""configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(),device->classes.string().c_str(), device->configurationFile.c_str(),device->keyMap.keyLayoutFile.c_str(), device->keyMap.keyCharacterMapFile.c_str(),toString(mBuiltInKeyboardId == deviceId));// 将其增加到map 中addDeviceLocked(std::move(device));
}======
void EventHub::addDeviceLocked(std::unique_ptr<Device> device) {reportDeviceAddedForStatisticsLocked(device->identifier, device->classes);// 将其保存到已经打开的设备map 中mOpeningDevicesmOpeningDevices.push_back(std::move(device));
}

eventhub 将打开的设备保存到了 mOpeningDevices 中,在 InputReader 调用eventhub getevents的时候,去获取 mOpeningDevices先去eventhub 中获取事件,缓存到 mEventBuffer

2)对 mEventBuffer 事件进行处理

设置事件类型是增加的DEVICE_ADDED ,然后在inputreader 线程中处理 processEventsLocked

/frameworks/native/services/inputflinger/reader/InputReader.cpp

void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {for (const RawEvent* rawEvent = rawEvents; count;) {int32_t type = rawEvent->type;size_t batchSize = 1;if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {int32_t deviceId = rawEvent->deviceId;while (batchSize < count) {if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT ||rawEvent[batchSize].deviceId != deviceId) {break;}batchSize += 1;}
#if DEBUG_RAW_EVENTSALOGD("BatchSize: %zu Count: %zu", batchSize, count);
#endifprocessEventsForDeviceLocked(deviceId, rawEvent, batchSize);} else {switch (rawEvent->type) {case EventHubInterface::DEVICE_ADDED:// 处理设备增加的消息addDeviceLocked(rawEvent->when, rawEvent->deviceId);break;case EventHubInterface::DEVICE_REMOVED:removeDeviceLocked(rawEvent->when, rawEvent->deviceId);break;case EventHubInterface::FINISHED_DEVICE_SCAN:handleConfigurationChangedLocked(rawEvent->when);break;default:ALOG_ASSERT(false); // can't happenbreak;}}count -= batchSize;rawEvent += batchSize;}
}
void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {if (mDevices.find(eventHubId) != mDevices.end()) {ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);return;}InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);// 依据设备的id 创建了 inputdevice 对象std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);device->configure(when, &mConfig, 0);device->reset(when);if (device->isIgnored()) {ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' ""(ignored non-input device)",device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());} else {// 会打印下列log 增加了设备ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),device->getSources());}// 将id 和对应的device 增加到了 mDevices 中mDevices.emplace(eventHubId, device);// Add device to device to EventHub ids map.const auto mapIt = mDeviceToEventHubIdsMap.find(device);if (mapIt == mDeviceToEventHubIdsMap.end()) {std::vector<int32_t> ids = {eventHubId};mDeviceToEventHubIdsMap.emplace(device, ids);} else {mapIt->second.push_back(eventHubId);}bumpGenerationLocked();

// 依据设备的id 创建了 inputdevice 对象

std::shared_ptr<InputDevice> InputReader::createDeviceLocked(int32_t eventHubId, const InputDeviceIdentifier& identifier) {auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&devicePair.second->getDescriptor() == identifier.descriptor;});std::shared_ptr<InputDevice> device;if (deviceIt != mDevices.end()) {device = deviceIt->second;} else {int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();// 创建了 InputDevice 对象device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),identifier);}// 调用方法 addEventHubDevicedevice->addEventHubDevice(eventHubId);return device;
}

// 调用方法 addEventHubDevice

/frameworks/native/services/inputflinger/reader/InputDevice.cpp

New device: id=2, fd=246, path='/dev/input/event4', name='adaptive_ts', classes=TOUCH | TOUCH_MT 

void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {if (mDevices.find(eventHubId) != mDevices.end()) {return;}/ / 创建了 InputDeviceContext 对象,传入this 对象和 eventHubIdstd::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();std::vector<std::unique_ptr<InputMapper>> mappers;// Check if we should skip populationif (!populateMappers) {mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});return;}// 然后依据不同的设备创建不同的 mapper:SwitchInputMapper
// 将其放到 mappers 中// Switch-like devices.if (classes.test(InputDeviceClass::SWITCH)) {mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));}// 下列是多点触控和单触控的// Touchscreens and touchpad devices.if (classes.test(InputDeviceClass::TOUCH_MT)) {mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));} else if (classes.test(InputDeviceClass::TOUCH)) {mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));}。。。。// insert the context into the devices set
// 最后将其保存到 mDevices 中mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});

MultiTouchInputMapper 多点触控是继承了 TouchInputMapper

class MultiTouchInputMapper : public TouchInputMapper

/frameworks/native/services/inputflinger/reader/mapper/InputMapper.h

47      inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
48      inline const std::string getDeviceName() { return mDeviceContext.getName(); }
49      inline InputReaderContext* getContext() { return mDeviceContext.getContext(); }
50      inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); }// getContext 获取的值是 inputreader::contextimpl 对象,在获取getListener,则是
51      inline InputListenerInterface* getListener() { return getContext()->getListener(); }=======
/frameworks/native/services/inputflinger/reader/InputReader.cppInputListenerInterface* InputReader::ContextImpl::getListener() {// 获取的是  mQueuedListenerreturn mReader->mQueuedListener.get();
}

相关文章:

【安卓13 源码】Input子系统(3) - EventHub增加设备的流程

由前面的分析知道&#xff0c;在创建inputreader 线程的时候&#xff0c;会去循环执行 looponce 方法。主要的处理工作是&#xff1a; 通过 getEvents() 从 EventHub 获取未处理的事件&#xff0c;这些事件分为两类&#xff1a;一类是原始输入事 件即从设备节点中读取出的原始…...

基于JAVA+SpringBoot+Vue的网上商城系统的设计与实现

基于JAVASpringBootVue的网上商城系统的设计与实现 前言 ✌全网粉丝20W,csdn特邀作者、博客专家、CSDN[新星计划]导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末附源码下载链接&#x1…...

Mysql基础练习题 1729.求关注者的数量 (力扣)

编写解决方案&#xff0c;对于每一个用户&#xff0c;返回该用户的关注者数量。 #按 user_id 的顺序返回结果表 题目链接&#xff1a; https://leetcode.cn/problems/find-followers-count/description/ 建表插入语句&#xff1a; Create table If Not Exists Followers(us…...

【鸿蒙HarmonyOS NEXT】页面和自定义组件生命周期

【鸿蒙HarmonyOS NEXT】页面和自定义组件生命周期 一、环境说明二、页面和自定义组件生命周期三、示例代码加以说明四、小结 一、环境说明 DevEco Studio 版本&#xff1a; API版本&#xff1a;以12为主 二、页面和自定义组件生命周期 需要明确几个概念&#xff1a; 页面…...

Node.js Express 框架

Node.js Express 框架 介绍 Express 是一个快速、开放、极简的 Node.js Web 框架。它为构建 Web 应用程序和服务提供了一个强大的工具集,使得开发过程更加高效和便捷。Express 的设计哲学是提供一个最小的 API,让开发者可以轻松地构建自定义的 Web 应用程序。它被广泛用于构…...

生日贺卡录放音芯片,多段音频录音ic生产厂商,NVF04M-32minute

可以录音播放的生日贺卡与传统的纸质贺卡相比&#xff0c;它有着创意以及个性的特点&#xff0c;仅需少量的电子元器件&#xff0c;即可实现录音功能&#xff0c;搭配上文字&#xff0c;让声音存储在生日贺卡里&#xff0c;让贺卡也变得有温度&#xff0c;祝福我想亲口对TA说。…...

电影《西施新传》首映礼,九月金秋全国正式公映

2024年9月1日&#xff0c;古装谋略情感影片《西施新传》在无锡大世界影城中山路IMAX激光店举办首映礼。电影《西施新传》根据作家沈雅琴、笔名一蝶的同名小说改编&#xff0c;以家喻户晓四大美人之首的西施为主人公&#xff0c;讲述了春秋末期吴越战争的故事。越国败于吴国&…...

【H2O2|全栈】关于CSS(1)CSS基础(一)

目录 CSS基础知识 前言 准备工作 啥是CSS&#xff1f; 如何引用CSS&#xff1f; 选择器 通配符选择器 类名&#xff08;class&#xff09;选择器 id选择器 CSS解析顺序&#xff08;优先级&#xff09; 常见CSS标签&#xff08;一&#xff09; 字体属性 font-style…...

动态规划算法之背包问题详细解读(附带Java代码解读)

动态规划中的背包问题&#xff08;Knapsack Problem&#xff09;是经典问题之一&#xff0c;通常用来解决选择一组物品放入背包使得背包的价值最大化的问题。根据问题条件的不同&#xff0c;背包问题有很多种变体&#xff0c;如0-1背包问题、完全背包问题、多重背包问题等。这里…...

Vue3+TypeScript二次封装axios

安装如下 npm install axios 第一步&#xff1a;创建config配置文件&#xff0c;用于存放请求后端的ip地址&#xff0c;用于后期打包后便于修改ip地址。 注&#xff1a;typescript要求参数要有类型。&#xff08;ES6 定义对象 属性 类型 修改的是属性的值&#xff09; inte…...

华为 HCIP-Datacom H12-821 题库 (16)

有需要题库的可以加下方Q群 V群进行学习交流 1. OSPF 邻居关系建立出现故障&#xff0c;通过 display ospf error 命令来检查&#xff0c;输出结果如图所示&#xff0c;根据图中内容分析&#xff0c;邻居建立失败的原因可能是以下哪一项&#xff1f; A、Process ID 不一致 B、…...

【论文分享精炼版】 sNPU: Trusted Execution Environments on Integrated NPUs

今天在COMPASS分享了之前写的一个博客&#xff0c;做了进一步的提炼总结&#xff0c;大家可以看看原文~ 今天分享的论文《sNPU: Trusted Execution Environments on Integrated NPUs》来自2024年ISCA&#xff0c;共同一作为Erhu Feng以及Dahu Feng。并且&#xff0c; 这两位作…...

MyBatis 入门之动态 SQL

文章目录 一、什么是动态 SQL二、MyBatis 中的动态 SQL 标签三、动态 SQL 的使用示例四、总结 在 Java 开发中&#xff0c;MyBatis 是一个非常流行的持久层框架&#xff0c;它提供了强大的 SQL 映射功能和动态 SQL 支持。动态 SQL 可以根据不同的条件动态地生成 SQL 语句&#…...

软工大二学生待办事项:

该文章会常年更新!坚持! 2024.9.10 学习打包部署 记录睡眠 开始刷一个算法 巩固Git版本控制工具的使用 巩固利用Idea使用版本管理工具,SQl编写 抓紧时间了解公司营业执照 坚持到周末再打瓦!...

MongoDB延迟查询

在 MongoDB 中&#xff0c;查看副本集成员之间的副本延迟可以通过以下步骤进行&#xff1a; 使用 rs.status() 命令&#xff1a; 这个命令提供了副本集的详细状态信息&#xff0c;包括每个成员的延迟情况。在 MongoDB shell 中&#xff0c;你可以执行以下命令&#xff1a; rs.s…...

python如何获取html中的所有链接

在Python中&#xff0c;获取HTML页面中的所有链接通常可以通过使用第三方库如BeautifulSoup或lxml来完成。这里&#xff0c;我将提供一个使用BeautifulSoup库的示例&#xff0c;因为它简单易用且功能强大。 首先&#xff0c;你需要安装BeautifulSoup和requests库&#xff08;如…...

79-java static修饰的类能不能被继承

Java中的类可以被final关键字修饰&#xff0c;表示这个类不能被继承。如果一个类被final修饰&#xff0c;那么这个类不能被继承&#xff0c;也就是说&#xff0c;final类不能被继承。 另一方面&#xff0c;static关键字可以用来修饰内部类&#xff0c;这样的内部类是静态内部类…...

MacOS wine中文乱码问题

安装wine 1、brew update 执行失败&#xff0c;提示安装如下 2、git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow 3、git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow 3、brew update 4、brew in…...

基于Springboot的鲜花销售网站的设计与实现

项目描述 这是一款基于Springboot的鲜花销售网站的系统 模块描述 鲜花销售系统 1、用户 登录 在线注册 浏览商品 鲜花搜索 订购商品 查询商品详情 水果分类查看 水果加购物车 下单结算 填写收货地址 2、管理员 登录 用户管理 商品管理 订单管理 账户管理 截图...

安卓玩机工具-----适合安卓机型的“搞机工具箱” 功能齐全 玩机推荐

搞机工具箱最新版是一款相当出色的电脑端手机工具箱软件&#xff0c;搞机工具箱正式版功能强劲&#xff0c;可以帮助用户不需要root就能够直接对手机进行调节&#xff0c;方便对手机进行更加全面的掌控&#xff0c;搞机工具箱便捷好用&#xff0c;只需要根据文字提示及自己的需…...

消费增值积分单边上扬软件源码开发

消费增值积分单边上扬系统开发要点消费增值积分单边上扬系统是一种通过消费行为累积积分&#xff0c;并确保积分价值稳定上升的商业模式。以下是开发此类系统的关键要点&#xff1a;系统架构设计 采用微服务架构分离核心模块&#xff0c;积分管理模块独立部署确保高可用性。数据…...

编写程序实现智能户外帐篷湿检测,内部结露时,提示“通风除湿”。

智能户外帐篷湿度检测系统&#xff1a;从原理到实现一、实际应用场景描述在户外露营场景中&#xff0c;帐篷内部湿度受外界环境&#xff08;如雨天、清晨露水&#xff09;和人体活动&#xff08;呼吸、汗液蒸发&#xff09;影响显著。当帐篷内湿度超过70%时&#xff0c;空气中的…...

C++的动态内存管理(new/delete的用法,malloc和new的区别,内存的具体分布)

C的动态内存管理允许程序在运行是根据需要分配内存和释放内存&#xff0c;主要通过new和delete运算符来完成。与静态内存分配相比&#xff0c;动态内存分配更具有灵活性&#xff0c;但它需要手动管理来避免内存泄漏。一C/C中内存的具体分布先来了解一下内存的几个区域&#xff…...

CLIP损失函数实战:从零实现到避坑指南(附HuggingFace源码解析)

CLIP损失函数实战&#xff1a;从零实现到避坑指南&#xff08;附HuggingFace源码解析&#xff09; 在探索多模态模型的世界里&#xff0c;CLIP&#xff08;Contrastive Language-Image Pretraining&#xff09;无疑是一颗耀眼的明星。这个由OpenAI提出的模型&#xff0c;通过对…...

从零搭建高安全低代码表单系统,手把手实现JWT动态权限校验+防CSRF提交+审计日志闭环,7天交付标准SaaS组件

第一章&#xff1a;从零构建高安全低代码表单系统概览 高安全低代码表单系统并非传统表单引擎的简单封装&#xff0c;而是融合身份鉴权、字段级加密、动态权限策略与不可篡改审计能力的一体化平台。其核心目标是在显著降低前端开发门槛的同时&#xff0c;满足金融、政务等强监管…...

Windows钉钉防撤回终极指南:免费开源工具完整使用教程

Windows钉钉防撤回终极指南&#xff1a;免费开源工具完整使用教程 【免费下载链接】DingTalk_Assistant 钉钉助手&#xff0c;主要功能包括&#xff1a;聊天消息防撤回、程序多开、屏蔽频繁升级等。 项目地址: https://gitcode.com/gh_mirrors/di/DingTalk_Assistant 在…...

无人机APM实战:从串口调试到多协议通信配置

1. 无人机APM串口通信基础入门 第一次接触APM飞控的串口通信时&#xff0c;我完全被各种专业术语搞懵了。后来才发现&#xff0c;串口其实就是飞控与外部设备"对话"的通道&#xff0c;就像两个人用对讲机交流一样简单。以Nora飞控为例&#xff0c;它的每个串口都有特…...

终极指南:如何用Unpaywall一键免费获取学术论文PDF

终极指南&#xff1a;如何用Unpaywall一键免费获取学术论文PDF 【免费下载链接】unpaywall-extension Firefox/Chrome extension that gives you a link to a free PDF when you view scholarly articles 项目地址: https://gitcode.com/gh_mirrors/un/unpaywall-extension …...

Multisim仿真NE555驱动NMOS总报错?手把手教你修改仿真参数搞定PWM调光电路

Multisim仿真NE555驱动NMOS报错全解析&#xff1a;从参数调优到实战调光 当你在Multisim中搭建NE555 PWM调光电路时&#xff0c;是否遇到过一接上NMOS就仿真崩溃的尴尬&#xff1f;那个刺眼的"瞬态分析无法收敛"报错窗口&#xff0c;仿佛在嘲笑你连基础电路都搞不定。…...

AI全身全息感知快速体验:5步完成从部署到生成你的第一张骨骼图

AI全身全息感知快速体验&#xff1a;5步完成从部署到生成你的第一张骨骼图 1. 引言&#xff1a;开启你的全息感知之旅 想象一下&#xff0c;你有一张照片&#xff0c;里面的人正在跳舞、打拳&#xff0c;或者只是摆了一个有趣的姿势。现在&#xff0c;你只需要点几下鼠标&…...