inputreader是如何将事件传递到inputdispath::notifykey里面去的?在inputdispatcher按键的派发的step6中遗留下了这个问题,现在回过头来学习下:
STEP6:
voidKeyboardInputMapper::processKey(nsecs_twhen, bool down, int32_t keyCode,
int32_tscanCode, uint32_t policyFlags) {
。。。。。。
NotifyKeyArgsargs(when, getDeviceId(), mSource, policyFlags,
down? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
AKEY_EVENT_FLAG_FROM_SYSTEM,keyCode, scanCode, newMetaState, downTime);
getListener()->notifyKey(&args);
//这个notifyKey 应该是对应InputDispatcher.cpp中的notifykey(),具体如何通讯的,待学习。
ALOGD_READER("notifyKey - eventTime=%lld,deviceId=%d, source=0x%x,policyFlags=0x%x, action=0x%x, "
"flags=0x%x, keyCode=0x%x,scanCode=0x%x, metaState=0x%x,downTime=%lld",
args.eventTime, args.deviceId,args.source, args.policyFlags,
args.action, args.flags,args.keyCode, args.scanCode,
args.metaState, args.downTime);
}
从getListener()->notifyKey(&args);这里我们继续往里面看看。
STEP 1:
\frameworks\native\services\inputflinger\ InputReader.cpp
voidKeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
int32_t scanCode, uint32_t policyFlags) {
if (down) {
// Rotate key codes according to orientation if needed.
if (mParameters.orientationAware && mParameters.hasAssociatedDisplay){
keyCode = rotateKeyCode(keyCode, mOrientation);
}
// Add key down.
ssize_t keyDownIndex = findKeyDown(scanCode);
if (keyDownIndex >= 0) {
// key repeat, be sure to use same keycode as before in case of rotation
keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
} else {
// key down
if ((policyFlags & POLICY_FLAG_VIRTUAL)
&&mContext->shouldDropVirtualKey(when,
getDevice(),keyCode, scanCode)) {
return;
}
if (policyFlags & POLICY_FLAG_GESTURE) {
mDevice->cancelTouch(when);
}
mKeyDowns.push();
KeyDown& keyDown = mKeyDowns.editTop();
keyDown.keyCode = keyCode;
keyDown.scanCode = scanCode;
}
mDownTime = when;
}else {
// Remove key down.
ssize_t keyDownIndex = findKeyDown(scanCode);
if (keyDownIndex >= 0) {
// key up, be sure to use same keycode as before in case of rotation
keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
mKeyDowns.removeAt(size_t(keyDownIndex));
} else {
// key was not actually down
ALOGI("Dropping key up from device %s because the key was notdown. "
"keyCode=%d,scanCode=%d",
getDeviceName().string(),keyCode, scanCode);
return;
}
}
int32_t oldMetaState = mMetaState;
int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
bool metaStateChanged = oldMetaState != newMetaState;
if (metaStateChanged) {
mMetaState = newMetaState;
updateLedState(false);
}
nsecs_t downTime = mDownTime;
// Key down on external an keyboard should wake the device.
// We don't do this for internal keyboards to prevent them from wakingup in your pocket.
// For internal keyboards, the key layout file should specify the policyflags for
// each wake key individually.
// TODO: Use the input device configuration to control this behaviormore finely.
if (down && getDevice()->isExternal()) {
policyFlags |= POLICY_FLAG_WAKE;
}
if (mParameters.handlesKeyRepeat) {
policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
}
if (metaStateChanged) {
getContext()->updateGlobalMetaState();
}
if (down && !isMetaKey(keyCode)) {
getContext()->fadePointer();
}
NotifyKeyArgsargs(when, getDeviceId(), mSource, policyFlags,
down ? AKEY_EVENT_ACTION_DOWN :AKEY_EVENT_ACTION_UP,
AKEY_EVENT_FLAG_FROM_SYSTEM,keyCode, scanCode, newMetaState, downTime);
getListener()->notifyKey(&args);/*主要是把这个getListener 搞清楚,起对应的notifykey就知道是哪里了。*/
ALOGD_READER("notifyKey - eventTime=%lld, deviceId=%d, source=0x%x,policyFlags=0x%x, action=0x%x, "
"flags=0x%x, keyCode=0x%x,scanCode=0x%x, metaState=0x%x, downTime=%lld",
args.eventTime, args.deviceId,args.source, args.policyFlags,
args.action, args.flags,args.keyCode, args.scanCode,
args.metaState, args.downTime);
}
STEP 2:
\frameworks\native\services\inputflinger\ InputReader.cpp
InputListenerInterface*InputReader::ContextImpl::getListener() {
return mReader->mQueuedListener.get();
}
/*
mQueuedListener是从哪里来的呢?在mReader的构造函数中,通过参数的形式传进来。
*/
STEP 3:
InputReader(const sp<EventHubInterface>& eventHub,
const sp<InputReaderPolicyInterface>& policy,
constsp<InputListenerInterface>& listener);
/*
mReader创建的的时候,通过参数的形式传递进来的。
*/
STEP 4:
\frameworks\native\services\inputflinger\ InputManager.cpp
/*mReader的创建*/
InputManager::InputManager(
constsp<EventHubInterface>& eventHub,
const sp<InputReaderPolicyInterface>& readerPolicy,
const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
mDispatcher = new InputDispatcher(dispatcherPolicy);
mReader = new InputReader(eventHub,readerPolicy, mDispatcher);/*就是这里传进去的*/
initialize();
}
/*
所以这个mQueuedListener就是mDispatcher。自然也就是调用的是mDispatcher的notifykey了。后面的就不继续分析了。这里只是说明标题的问题“inputreader是如何将事件传递到inputdispath::notifykey里面去的”
*/