input 输入系统
input是Android中重要的系统服务之一,专用于处理输入设备的各种外界输入信号。首先输入设备包括:屏幕,触摸,键盘,鼠标,等等。其中最常见的应该就是触摸屏幕了,那么是怎么处理的呢?这些硬件被操作后,会触发对应的硬件驱动,硬件驱动收到事件后,会将相应的事件写入到对应的输入设备节点,Input系统会去从设备节点读取事件,再一层一层往上传递,一直传递到对应的Activity。下面该图是Input输入系统和WMS以应用进程之间的调用关系图。
该图参考:https://blog.csdn.net/liuwg1226/article/details/113570829
// Check whether this device is an accelerometer.
if (device->propBitmask.test(INPUT_PROP_ACCELEROMETER)) {
device->classes |= InputDeviceClass::SENSOR;
}
// Check whether this device has switches.
for (int i = 0; i <= SW_MAX; i++) {
if (device->swBitmask.test(i)) {
device->classes |= InputDeviceClass::SWITCH;
break;
}
}
Input的基本组成部分:
InputManagerService:Android系统服务之一,分为java和native两层,java层级负责与wms进行交互,native包括InputReader与InputDispatcher,这两个服务用于接收处理下面的事件,往java层级传递。
输入设备节点:在/dev/input下面会有event0,event1,event2等多个文件,这就是输入设备节点,在每个输入系统的硬件驱动加载的时候会创建对应的节点。内核就是往该节点写事件,供用户空间去读取。
EventHub: 是Input系统的一部分,专用于直接和内核驱动进行交互,用于读取设备节点事件信息。如getEvent定义了和驱动交互的流程。
InputReader: 通过调用EventHub的接口,负责管理设备的添加与删除,并进行事件的加工处理。其内部会不断的通过EventHub的getEvents轮询驱动的事件,并最终将事件发送给InputDispatcher进行派发。
InputDispatcher: InputDispatcher保存了WMS的窗口信息,在收到了来自InputReader的事件后,会在其窗口信息中找到一个合适的窗口,并将事件派发到此窗口。
WMS: 每次新建窗口时,WMS会将窗口信息更新到InputDispatcher中,这样InputDispatcher才能找到对应的窗口。
ViewRootImpl:当窗口接收到事件后,窗口的ViewRootImpl会沿着控件树将事件派发给感兴趣的控件。控件再做出响应。
InputManagerService
在InputManagerService初始化时,会通过com_android_server_input_InputManagerService jni层,调用InputManager进行初始化,InputManager在初始化时,会创建InputDispatcher与InputReader线程对象。等InputManagerService创建好并启动时,InputReader与InputDispatcher也会被开启进行工作。
InputManager::InputManager(
const sp<InputReaderPolicyInterface>& readerPolicy,
const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
mDispatcher = createInputDispatcher(dispatcherPolicy);
mClassifier = new InputClassifier(mDispatcher);
mReader = createInputReader(readerPolicy, mClassifier);
}
status_t InputManager::start() {
status_t result = mDispatcher->start();
if (result) {
ALOGE("Could not start InputDispatcher thread due to error %d.", result);
return result;
}
result = mReader->start();
if (result) {
ALOGE("Could not start InputReader due to error %d.", result);
mDispatcher->stop();
return result;
}
return OK;
}
InputReader
InputReader在启动之后,会通过loopOnce轮询event输入事件,内部过程就是通过EventHub的接口mEventHub->getEvents进行事件获取。获取完之后,通过processEventsLocked对事件进行处理,这里包括两种情况:
- 设备事件:设备添加、删除等相关事件,其中在系统开机时,会进行驱动加载,进行设备添加,对应就会上报添加设备事件。
- 输入事件:通过设备节点读取的输入事件
void InputReader::loopOnce() {
int32_t oldGeneration;
int32_t timeoutMillis;
bool inputDevicesChanged = false;
std::vector<InputDeviceInfo> inputDevices;
......
size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
{ // acquire lock
std::scoped_lock _l(mLock);
mReaderIsAliveCondition.notify_all();
if (count) {
processEventsLocked(mEventBuffer, count);
}
......
// Flush queued events out to the listener.
// This must happen outside of the lock because the listener could potentially call
// back into the InputReader's methods, such as getScanCodeState, or become blocked
// on another thread similarly waiting to acquire the InputReader lock thereby
// resulting in a deadlock. This situation is actually quite plausible because the
// listener is actually the input dispatcher, which calls into the window manager,
// which occasionally calls into the input reader.
mQueuedListener->flush();
}
processEventsLocked方法内部会根据事件类别进行判断处理。
- type < EventHubInterface::FIRST_SYNTHETIC_EVENT:该部分过程处理输入事件,通过processEventsForDeviceLocked最后将输入事件处理添加到mQueuedListener中,最后通过flush操作,一次性将事件交给InputDispatcher。
- 设备处理事件:进行设备添加/删除/配置
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_EVENTS
ALOGD("BatchSize: %zu Count: %zu", batchSize, count);
#endif
processEventsForDeviceLocked(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 happen
break;
}
}
count -= batchSize;
rawEvent += batchSize;
}
}
设备添加
在接收到添加事件后,InputReader会进行设备添加,共包括下面两个过程:
- 通过createDeviceLocked创建对应的InputDevice,在该方法内会根据前面创建的设备的描述符与当前设备的描述符对比,看是否存在,如果存在,则直接复用之前的InputDevice,否则重新根据当前的描述符创建一个InputDevice。(ps: 双屏异显的两个触摸驱动的描述符一定不能一样,否则后面无法实现触摸input与display的正常绑定)
- 通过
device->configure
进行设备配置
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);
std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
device->configure(when, &mConfig, 0);
device->reset(when);
......
}
创建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();
device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
identifier);
}
device->addEventHubDevice(eventHubId);
return device;
}
进行设备配置,最终调用mapper.configure(when, config, changes);
对设备进行配置。
void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
uint32_t changes) {
mSources = 0;
mClasses = Flags<InputDeviceClass>(0);
mControllerNumber = 0;
for_each_subdevice([this](InputDeviceContext& context) {
mClasses |= context.getDeviceClasses();
int32_t controllerNumber = context.getDeviceControllerNumber();
if (controllerNumber > 0) {
if (mControllerNumber && mControllerNumber != controllerNumber) {
ALOGW("InputDevice::configure(): composite device contains multiple unique "
"controller numbers");
}
mControllerNumber = controllerNumber;
}
});
mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
mHasMic = mClasses.test(InputDeviceClass::MIC);
if (!isIgnored()) {
if (!changes) { // first time only
mConfiguration.clear();
for_each_subdevice([this](InputDeviceContext& context) {
PropertyMap configuration;
context.getConfiguration(&configuration);
mConfiguration.addAll(&configuration);
});
}
if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
std::shared_ptr<KeyCharacterMap> keyboardLayout =
mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
bool shouldBumpGeneration = false;
for_each_subdevice(
[&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
shouldBumpGeneration = true;
}
});
if (shouldBumpGeneration) {
bumpGeneration();
}
}
}
if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
if (mAlias != alias) {
mAlias = alias;
bumpGeneration();
}
}
}
if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
auto it = config->disabledDevices.find(mId);
bool enabled = it == config->disabledDevices.end();
setEnabled(enabled, when);
}
if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
// In most situations, no port or name will be specified.
mAssociatedDisplayPort = std::nullopt;
mAssociatedDisplayUniqueId = std::nullopt;
mAssociatedViewport = std::nullopt;
// Find the display port that corresponds to the current input port.
const std::string& inputPort = mIdentifier.location;
if (!inputPort.empty()) {
const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
const auto& displayPort = ports.find(inputPort);
if (displayPort != ports.end()) {
mAssociatedDisplayPort = std::make_optional(displayPort->second);
}
}
const std::string& inputDeviceName = mIdentifier.name;
const std::unordered_map<std::string, std::string>& names =
config->uniqueIdAssociations;
const auto& displayUniqueId = names.find(inputDeviceName);
if (displayUniqueId != names.end()) {
mAssociatedDisplayUniqueId = displayUniqueId->second;
}
// If the device was explicitly disabled by the user, it would be present in the
// "disabledDevices" list. If it is associated with a specific display, and it was not
// explicitly disabled, then enable/disable the device based on whether we can find the
// corresponding viewport.
bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
if (mAssociatedDisplayPort) {
mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
if (!mAssociatedViewport) {
ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
"but the corresponding viewport is not found.",
getName().c_str(), *mAssociatedDisplayPort);
enabled = false;
}
} else if (mAssociatedDisplayUniqueId != std::nullopt) {
mAssociatedViewport =
config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
if (!mAssociatedViewport) {
ALOGW("Input device %s should be associated with display %s but the "
"corresponding viewport cannot be found",
inputDeviceName.c_str(), mAssociatedDisplayUniqueId->c_str());
enabled = false;
}
}
if (changes) {
// For first-time configuration, only allow device to be disabled after mappers have
// finished configuring. This is because we need to read some of the properties from
// the device's open fd.
setEnabled(enabled, when);
}
}
for_each_mapper([this, when, config, changes](InputMapper& mapper) {
mapper.configure(when, config, changes);
mSources |= mapper.getSources();
});
// If a device is just plugged but it might be disabled, we need to update some info like
// axis range of touch from each InputMapper first, then disable it.
if (!changes) {
setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
}
}
}
响应触摸事件
响应触摸事件,InputDispatcher就需要派上用场了,在InputManager中创建了InputDispatcherThread,并在InputManagerService服务启动的时候,该线程被同步启动。通过dispatchOnce进行轮询接收来自InputReader的消息,再通过dispatchOnceInnerLocked进行事件派发。
status_t InputDispatcher::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
return OK;
}
下面就是轮询方法,在被唤醒后会调用dispatchOnceInnerLocked
进行实际派发任务。下面的mLooper->pollOnce
就是等待InputReader发送事件。
void InputDispatcher::dispatchOnce() {
nsecs_t nextWakeupTime = LONG_LONG_MAX;
{ // acquire lock
std::scoped_lock _l(mLock);
mDispatcherIsAlive.notify_all();
// Run a dispatch loop if there are no pending commands.
// The dispatch loop might enqueue commands to run afterwards.
if (!haveCommandsLocked()) {
dispatchOnceInnerLocked(&nextWakeupTime);
}
// Run all pending commands if there are any.
// If any commands were run then force the next poll to wake up immediately.
if (runCommandsLockedInterruptible()) {
nextWakeupTime = LONG_LONG_MIN;
}
// If we are still waiting for ack on some events,
// we might have to wake up earlier to check if an app is anr'ing.
const nsecs_t nextAnrCheck = processAnrsLocked();
nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck);
// We are about to enter an infinitely long sleep, because we have no commands or
// pending or queued events
if (nextWakeupTime == LONG_LONG_MAX) {
mDispatcherEnteredIdle.notify_all();
}
} // release lock
// Wait for callback or timeout or wake. (make sure we round up, not down)
nsecs_t currentTime = now();
int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
mLooper->pollOnce(timeoutMillis);
}
在dispatchOnceInnerLocked
内部会根据事件类型做出不同的派发处理,比如最常见的MOTION
事件,会通过dispatchMotionLocked
进行派发。除此之外,还有其他类型的事件,如KEY
事件,RESET
事件等等。
void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
nsecs_t currentTime = now();
......
switch (mPendingEvent->type) {
case EventEntry::Type::CONFIGURATION_CHANGED: {
const ConfigurationChangedEntry& typedEntry =
static_cast<const ConfigurationChangedEntry&>(*mPendingEvent);
done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped
break;
}
case EventEntry::Type::DEVICE_RESET: {
const DeviceResetEntry& typedEntry =
static_cast<const DeviceResetEntry&>(*mPendingEvent);
done = dispatchDeviceResetLocked(currentTime, typedEntry);
dropReason = DropReason::NOT_DROPPED; // device resets are never dropped
break;
}
case EventEntry::Type::FOCUS: {
std::shared_ptr<FocusEntry> typedEntry =
std::static_pointer_cast<FocusEntry>(mPendingEvent);
dispatchFocusLocked(currentTime, typedEntry);
done = true;
dropReason = DropReason::NOT_DROPPED; // focus events are never dropped
break;
}
case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
const auto typedEntry =
std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent);
dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason);
done = true;
break;
}
case EventEntry::Type::DRAG: {
std::shared_ptr<DragEntry> typedEntry =
std::static_pointer_cast<DragEntry>(mPendingEvent);
dispatchDragLocked(currentTime, typedEntry);
done = true;
break;
}
case EventEntry::Type::KEY: {
std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent);
if (isAppSwitchDue) {
if (isAppSwitchKeyEvent(*keyEntry)) {
resetPendingAppSwitchLocked(true);
isAppSwitchDue = false;
} else if (dropReason == DropReason::NOT_DROPPED) {
dropReason = DropReason::APP_SWITCH;
}
}
if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) {
dropReason = DropReason::STALE;
}
if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
dropReason = DropReason::BLOCKED;
}
done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime);
break;
}
case EventEntry::Type::MOTION: {
std::shared_ptr<MotionEntry> motionEntry =
std::static_pointer_cast<MotionEntry>(mPendingEvent);
if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) {
dropReason = DropReason::APP_SWITCH;
}
if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) {
dropReason = DropReason::STALE;
}
if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
dropReason = DropReason::BLOCKED;
}
done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime);
break;
}
case EventEntry::Type::SENSOR: {
std::shared_ptr<SensorEntry> sensorEntry =
std::static_pointer_cast<SensorEntry>(mPendingEvent);
if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) {
dropReason = DropReason::APP_SWITCH;
}
// Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use
// 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead.
nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME);
if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) {
dropReason = DropReason::STALE;
}
dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime);
done = true;
break;
}
}
if (done) {
if (dropReason != DropReason::NOT_DROPPED) {
dropInboundEventLocked(*mPendingEvent, dropReason);
}
mLastDropReason = dropReason;
releasePendingEventLocked();
*nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
}
}
获取到MOTION
事件后,会根据事件寻找目标窗口,通过 findTouchedWindowTargetsLocked
寻找对应的窗口。寻找到目标窗口后通过dispatchEventLocked
将事件派发到对应窗口上。
bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry,
DropReason* dropReason, nsecs_t* nextWakeupTime) {
ATRACE_CALL();
......
bool conflictingPointerActions = false;
InputEventInjectionResult injectionResult;
if (isPointerEvent) {
// Pointer event. (eg. touchscreen)
injectionResult =
findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime,
&conflictingPointerActions);
} else {
// Non touch event. (eg. trackball)
injectionResult =
findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime);
}
......
//通过上面找到对应的窗口后,进行派发消息
dispatchEventLocked(currentTime, entry, inputTargets);
return true;
}
在findTouchedWindowAtLocked
方法内部先是根据屏幕的id获取对应屏幕的所有window,获取方法:getWindowHandlesLocked(displayId)
,将window 信息保存在windowHandles 的一个vector里。然后通过for循环,进行遍历,查找对应的window。当
sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x,
int32_t y, TouchState* touchState,
bool addOutsideTargets,
bool addPortalWindows,
bool ignoreDragWindow) {
if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) {
LOG_ALWAYS_FATAL(
"Must provide a valid touch state if adding portal windows or outside targets");
}
// Traverse windows from front to back to find touched window.
const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
//通过for循环遍历所有的窗口
for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) {
continue;
}
const WindowInfo* windowInfo = windowHandle->getInfo();
if (windowInfo->displayId == displayId) {
auto flags = windowInfo->flags;
if (windowInfo->visible) {
if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) {
bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) &&
!flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL);
//查找到对应的window
if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
int32_t portalToDisplayId = windowInfo->portalToDisplayId;
if (portalToDisplayId != ADISPLAY_ID_NONE &&
portalToDisplayId != displayId) {
if (addPortalWindows) {
// For the monitoring channels of the display.
touchState->addPortalWindow(windowHandle);
}
return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState,
addOutsideTargets, addPortalWindows);
......
return nullptr;
}
在下面往窗口window派发事件过程中,调用prepareDispatchCycleLocked继续进行事件的派发。prepareDispatchCycleLocked会继续调用
void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
std::shared_ptr<EventEntry> eventEntry,
const std::vector<InputTarget>& inputTargets) {
ATRACE_CALL();
#if DEBUG_DISPATCH_CYCLE
ALOGD("dispatchEventToCurrentInputTargets");
#endif
updateInteractionTokensLocked(*eventEntry, inputTargets);
ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
pokeUserActivityLocked(*eventEntry);
for (const InputTarget& inputTarget : inputTargets) {
sp<Connection> connection =
getConnectionLocked(inputTarget.inputChannel->getConnectionToken());
if (connection != nullptr) {
prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget);
} else {
if (DEBUG_FOCUS) {
ALOGD("Dropping event delivery to target with channel '%s' because it "
"is no longer registered with the input dispatcher.",
inputTarget.inputChannel->getName().c_str());
}
}
}
}
prepareDispatchCycleLocked内部继续调用enqueueDispatchEntriesLocked。
void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
const sp<Connection>& connection,
std::shared_ptr<EventEntry> eventEntry,
const InputTarget& inputTarget) {
......
enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry),
inputTarget);
return;
}
}
// Not splitting. Enqueue dispatch entries for the event as is.
enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget);
}
enqueueDispatchEntriesLocked内部继续调用startDispatchCycleLocked,
void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime,
const sp<Connection>& connection,
std::shared_ptr<EventEntry> eventEntry,
const InputTarget& inputTarget) {
......
// If the outbound queue was previously empty, start the dispatch cycle going.
if (wasEmpty && !connection->outboundQueue.empty()) {
startDispatchCycleLocked(currentTime, connection);
}
}
startDispatchCycleLocked内部有继续调用connection->inputPublisher.publishMotionEvent
。
void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
const sp<Connection>& connection) {
......
case EventEntry::Type::MOTION: {
......
// Publish the motion event.
status = connection->inputPublisher
.publishMotionEvent(dispatchEntry->seq,
dispatchEntry->resolvedEventId,
......
}
......
}
通过InputPublisher调用InputChannel sendMessage往window发送消息。
status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
int32_t source, int32_t displayId,
std::array<uint8_t, 32> hmac, int32_t action,
int32_t flags, int32_t keyCode, int32_t scanCode,
int32_t metaState, int32_t repeatCount, nsecs_t downTime,
nsecs_t eventTime) {
InputMessage msg;
msg.header.type = InputMessage::Type::KEY;
msg.header.seq = seq;
msg.body.key.eventId = eventId;
msg.body.key.deviceId = deviceId;
msg.body.key.source = source;
msg.body.key.displayId = displayId;
msg.body.key.hmac = std::move(hmac);
msg.body.key.action = action;
msg.body.key.flags = flags;
msg.body.key.keyCode = keyCode;
msg.body.key.scanCode = scanCode;
msg.body.key.metaState = metaState;
msg.body.key.repeatCount = repeatCount;
msg.body.key.downTime = downTime;
msg.body.key.eventTime = eventTime;
return mChannel->sendMessage(&msg);
}
通过socket接口::send往window发送消息。消息发送完成后,下面就是APP进程进行下一步View更新了。
status_t InputChannel::sendMessage(const InputMessage* msg) {
const size_t msgLength = msg->size();
InputMessage cleanMsg;
msg->getSanitizedCopy(&cleanMsg);
ssize_t nWrite;
do {
nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
} while (nWrite == -1 && errno == EINTR);
......
return OK;
}
EventHub
EventHub主要负责和驱动进行交互,获得驱动设备的信息和上报设备节点的事件信息。getEvents给InputReader提供了获取设备和事件的功能。在开机时,会进行设备扫描,通过scanDevicesLocked获取设备信息。在这个过程中,会创建Device对象,并将Device添加。值得一提的是Android12与Android10在添加设备有些区别。Android12仅仅添加了mOpeningDevices中,而Android10中还添加到了mDevices中。针对该差异,会导致一个问题。该问题就是双屏场景下,触摸无响应。
分析:在双屏异显场景下,双屏的驱动信息都是一样的,EventHub获取到的Idenfier信息都是一样的,这样在调用assignDescriptorLocked(identifier);
生成descriptor时(通过vendor和product),会给双屏驱动生成一样的descriptor。但是谷歌做了容错处理,识别到如果生成的descriptor和前面某台设备是一样的,会在此基础上继续+1
保证生成另外一个不同的descriptor。这个在Android10上是可以实现的。因为每次打开设备过程中,最后addDeviceLocked时,都会将mDevices添加到设备中。这样后面在生成描述符后会判断一下前面打开的设备中是否已经有相同descriptor的设备了。但是,在Android12上虽然这个过程虽然也执行了,但是没有生效,因为在判断过程中,遍历设备的时候,mDevices还是空的,所以无法进行判断,而对比代码差异来看,就是Android12上在addDeviceLocked中,没有将打开的设备添加到mDevices列表中。通过getEvent流程来看,Android12上对mDevices进行设备添加的时机是扫描完所有的设备了,才将设备一次性添加进去。
size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
ALOG_ASSERT(bufferSize >= 1);
......
for (;;) {
......
if (mNeedToScanDevices) {
mNeedToScanDevices = false;
//扫描设备
scanDevicesLocked();
mNeedToSendFinishedDeviceScan = true;
}
//扫描完设备后,对mOpeningDevices进行遍历,将设备插入到mDevices中
while (!mOpeningDevices.empty()) {
std::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->type = DEVICE_ADDED;
event += 1;
auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));
}
void EventHub::scanDevicesLocked() {
status_t result;
std::error_code errorCode;
//判断设备路径/dev/input存在,然后扫描设备路径下的设备
if (std::filesystem::exists(DEVICE_INPUT_PATH, errorCode)) {
result = scanDirLocked(DEVICE_INPUT_PATH);
if (result < 0) {
ALOGE("scan dir failed for %s", DEVICE_INPUT_PATH);
}
}
status_t EventHub::scanDirLocked(const std::string& dirname) {
//依次打开设备,获取设备信息
for (const auto& entry : std::filesystem::directory_iterator(dirname)) {
openDeviceLocked(entry.path());
}
return 0;
}
void EventHub::openDeviceLocked(const std::string& devicePath) {
char buffer[80];
ALOGV("Opening device: %s", devicePath.c_str());
//打开设备
int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
if (fd < 0) {
ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno));
return;
}
InputDeviceIdentifier identifier;
// Get device name.
if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno));
} else {
buffer[sizeof(buffer) - 1] = '\0';
identifier.name = buffer;
}
// Check to see if the device is on our excluded list
for (size_t i = 0; i < mExcludedDevices.size(); i++) {
const std::string& item = mExcludedDevices[i];
if (identifier.name == item) {
ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str());
close(fd);
return;
}
}
//通过ioctl与驱动进行交互,获取驱动版本
// Get device driver version.
int driverVersion;
if (ioctl(fd, EVIOCGVERSION, &driverVersion)) {
ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno));
close(fd);
return;
}
//获取驱动的identifier信息
// Get device identifier.
struct input_id inputId;
if (ioctl(fd, EVIOCGID, &inputId)) {
ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno));
close(fd);
return;
}
identifier.bus = inputId.bustype;
identifier.product = inputId.product;
identifier.vendor = inputId.vendor;
identifier.version = inputId.version;
//获取驱动设备的物理位置
// Get device physical location.
if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
// fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
} else {
buffer[sizeof(buffer) - 1] = '\0';
identifier.location = buffer;
}
//获取设备的id
// Get device unique id.
if (ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
// fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
} else {
buffer[sizeof(buffer) - 1] = '\0';
identifier.uniqueId = buffer;
}
//根据前面获得的idenfier生成description
// Fill in the descriptor.
assignDescriptorLocked(identifier);
//根据上面获取的驱动设备信息,生成对应的一个Device对象,该对象后面生成InputDevice会使用到.
// Allocate device. (The device object takes ownership of the fd at this point.)
int32_t deviceId = mNextDeviceId++;
std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier);
ALOGV("add device %d: %s\n", deviceId, devicePath.c_str());
ALOGV(" bus: %04x\n"
" vendor %04x\n"
" product %04x\n"
" version %04x\n",
identifier.bus, identifier.vendor, identifier.product, identifier.version);
ALOGV(" name: \"%s\"\n", identifier.name.c_str());
ALOGV(" location: \"%s\"\n", identifier.location.c_str());
ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.c_str());
ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.c_str());
ALOGV(" driver: v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff,
driverVersion & 0xff);
// Load the configuration file for the device.
device->loadConfigurationLocked();
......
//添加设备到mOpeningDevices列表中
addDeviceLocked(std::move(device));
}
//Android12
void EventHub::addDeviceLocked(std::unique_ptr<Device> device) {
reportDeviceAddedForStatisticsLocked(device->identifier, device->classes);
mOpeningDevices.push_back(std::move(device));
}
//Android10
void EventHub::addDeviceLocked(Device* device) {
mDevices.add(device->id, device);
device->next = mOpeningDevices;
mOpeningDevices = device;
}
//生成descriptor
void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
identifier.nonce = 0;
std::string rawDescriptor = generateDescriptor(identifier);
if (identifier.uniqueId.empty()) {
// If it didn't have a unique id check for conflicts and enforce
// uniqueness if necessary.
//对设备遍历,如果不为null,则identifier.nonce++; 重新一个生成一个Descriptor
while(getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) {
identifier.nonce++;
rawDescriptor = generateDescriptor(identifier);
}
}
ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(),
identifier.descriptor.c_str());
}
//通过descriptor看是否存在相同的descriptor的设备
EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const {
size_t size = mDevices.size();
for (size_t i = 0; i < size; i++) {
Device* device = mDevices.valueAt(i);
if (descriptor == device->identifier.descriptor) {
return device;
}
}
return nullptr;
}
为什么说一定要生成不同的描述符呢?因为在InputReader中生成InputDevice时,会使用这个mDevices,在创建一个新的InputDevices时,会判断是否已经有相同descriptor的设备了,如果有,则直接复用,共用一个InputDevice。但是,双屏异显场景下,则肯定那个不能使用一个InputDevice,因为使用一个InputDevice,导致触摸设备无法与display显示设备进行绑定,所以无法实现触摸事件上报到对应的显示屏幕上。
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();
device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
identifier);
}
device->addEventHubDevice(eventHubId);
return device;
}