此文章只分析EventHub获取输入事件的getevents函数的具体实现
首先在EventHub的构造函数中,将以下变量进行初始化,以下初始化变量在getevents中会用到
mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
mOpeningDevices(0), mClosingDevices(0),mNeedToSendFinishedDeviceScan(false),
mNeedToReopenDevices(false), mNeedToScanDevices(true),
mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false)
下面来分析getevents:
size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize)
1.定义一个 input_event 结构的 readBuffer[buffersize] 来存储从 /dev/input/event*读取到的数据
2.定义一个 RawEvent的指针,指向 getEvents方法的参数 buffer指针中,返回给调用者InputReader对象的loopOnce方法
3.定义一个size_t capacity = bufferSize;
4.bool awoken = false;
5.进入到一个fo循环中
5.1 获取当前系统的时间,单位为ns
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
5.2 判断变量 mNeedToReopenDevices,来判断是否重新打开设备,mNeedToReopenDevices变量在EventHub的构造函数中初始化false,所以此条件并不成立,不需要重新ReopenDevices,关于mNeedToReopenDevices变量的调用过程详细分析参见
https://blog.csdn.net/qq_30025621/article/details/79944825
if (mNeedToReopenDevices) {
mNeedToReopenDevices = false;
ALOGI("Reopening all input devices due to a configuration change.");
closeAllDevicesLocked();
mNeedToScanDevices = true;
break; // return to the caller before we actually rescan
}
5.3 如果mClosingDevices存在,那么将用mClosingDevices中的每一个对象构造RawEvent对象,并将
mNeedToSendFinishedDeviceScan设置为true,mClosingDevices详细分析参考如下:
android input子系统--InputReader EventHub::getevents之mClosingDevices调用过程详细分析
while (mClosingDevices) {
Device* device = mClosingDevices;
ALOGV("Reporting device closed: id=%d, name=%s\n",
device->id, device->path.string());
mClosingDevices = device->next;
event->when = now;
event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
event->type = DEVICE_REMOVED;
event += 1;
delete device;
mNeedToSendFinishedDeviceScan = true;
if (--capacity == 0) {
break;
}
}
5.4 判断mNeedToScanDevices变量,确定是否需要进行设备的扫描(是否有设备发生变化),在系统开始初始化的时候,
mNeedToScanDevices=true,所以此处成立,当mNeedToReopenDevices=true的时候,mNeedToScanDevices
也为true,关于mNeedToScanDevices变量的详细分析,参考:
android input子系统--InputReader EventHub::getevents之mNeedToScanDevices变量详细分析
if (mNeedToScanDevices) {
mNeedToScanDevices = false;
scanDevicesLocked();
mNeedToSendFinishedDeviceScan = true;
}
5.5