Android 虚拟键 生成与派发
- 本文以Android8.1源码为分析
1.kl解析
- 虚拟键在Android TP (Touch Panel)中得到了广泛的应用,而在TP的driver中,报虚拟按键的方法常用的有两种
- 直接传键值(kernel的,不清楚,只是知道有这么回事)
- 还是将touch传过来,但是我们可以根据坐标通过kl文件的解析判定是那个key.
- 在Android中我们用的是第二种做法,kl文件存放在设备/sys/board_properties/目录下,一般名字是virtualkeys.设备名,打开看大概如下所示:
0x01:158:128:670:160:100
- 0X01是标志位,然后分别是scanCode,中心点X,中心点Y,宽度和高度,其实就是一个矩形区域,以X,Y为中心点,分别延伸 w/2, h/2。
- 在源码VirtualKeyMap.cpp(Android/frameworks/native/libs/input)中,代码如下:
//parse会在EventHub的openDeviceLocked调用VirtualKeyMap的load函数是调用
status_t VirtualKeyMap::Parser::parse() {
while (!mTokenizer->isEof()) {
#if DEBUG_PARSER
ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
mTokenizer->peekRemainderOfLine().string());
#endif
mTokenizer->skipDelimiters(WHITESPACE);
if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
// Multiple keys can appear on one line or they can be broken up across multiple lines.
do {
String8 token = mTokenizer->nextToken(WHITESPACE_OR_FIELD_DELIMITER);
if (token != "0x01") {
ALOGE("%s: Unknown virtual key type, expected 0x01.",
mTokenizer->getLocation().string());
return BAD_VALUE;
}
//最关键代码,kl的解析
VirtualKeyDefinition defn;
bool success = parseNextIntField(&defn.scanCode)
&& parseNextIntField(&defn.centerX)
&& parseNextIntField(&defn.centerY)
&& parseNextIntField(&defn.width)
&& parseNextIntField(&defn.height);
if (!success) {
//kl文件格式不对
ALOGE("%s: Expected 5 colon-delimited integers in virtual key definition.",
mTokenizer->getLocation().string());
return BAD_VALUE;
}
#if DEBUG_PARSER
ALOGD("Parsed virtual key: scanCode=%d, centerX=%d, centerY=%d, "
"width=%d, height=%d",
defn.scanCode, defn.centerX, defn.centerY, defn.width, defn.height);
#endif
//将每一个virtualkey解析出来,以便去使用
mMap->mVirtualKeys.push(defn);
} while (consumeFieldDelimiterAndSkipWhitespace());
if (!mTokenizer->isEol()) {
ALOGE("%s: Expected end of line, got '%s'.",
mTokenizer->getLocation().string(),
mTokenizer->peekRemainderOfLine().string());
return BAD_VALUE;
}
}
mTokenizer->nextLine();
}
return NO_ERROR;
}
- 实际的范围是[x-w/2,x+w/2] ,[y-h/2,y+h/2].
- 实际使用的地方是InputReader.cpp的TouchInputMapper::configureVirtualKeysLocked()方法中会计算出每个虚拟按键对应的矩形范围,并将scancode转换成keycode。
2.获取virtual key的规定矩形区域
touch事件整个派发在此就不再多说,直接从configureVirtualKeys开始说起,touch事件派发到此处后,在configureVirtualKeys中被处理是否去转换为virtual key.源码如下:
void TouchInputMapper::configureVirtualKeys() {
//获取kl文件中解析出来的关于virtual key的内容
Vector<VirtualKeyDefinition> virtualKeyDefinitions;
getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
mVirtualKeys.clear();
if (virtualKeyDefinitions.size() == 0) {
return;
}
mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
//获取屏幕范围
int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
int32_t touchScreenTop = mRawPointerAxes.y.minValue;
int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
const VirtualKeyDefinition& virtualKeyDefinition =
virtualKeyDefinitions[i];
mVirtualKeys.add();
VirtualKey& virtualKey = mVirtualKeys.editTop();
virtualKey.scanCode = virtualKeyDefinition.scanCode;
int32_t keyCode;
int32_t dummyKeyMetaState;
uint32_t flags;
//根据scanCode转换得到相应的keyCode
if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, 0,
&keyCode, &dummyKeyMetaState, &flags)) {
ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
virtualKey.scanCode);
mVirtualKeys.pop(); // drop the key
continue;
}
virtualKey.keyCode = keyCode;
virtualKey.flags = flags;
// convert the key definition's display coordinates into touch coordinates for a hit box
int32_t halfWidth = virtualKeyDefinition.width / 2;
int32_t halfHeight = virtualKeyDefinition.height / 2;
//得到scanCodec的virtual key的矩形区域
virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
* touchScreenWidth / mSurfaceWidth + touchScreenLeft;
virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
* touchScreenWidth / mSurfaceWidth + touchScreenLeft;
virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
* touchScreenHeight / mSurfaceHeight + touchScreenTop;
virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
* touchScreenHeight / mSurfaceHeight + touchScreenTop;
}
}
3.virtual key的生成和派发
在void TouchInputMapper::cookAndDispatch(nsecs_t when)中会去调用consumeRawTouches进行派发
const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
int32_t x, int32_t y) {
size_t numVirtualKeys = mVirtualKeys.size();
for (size_t i = 0; i < numVirtualKeys; i++) {
const VirtualKey& virtualKey = mVirtualKeys[i];
#if DEBUG_VIRTUAL_KEYS
ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
"left=%d, top=%d, right=%d, bottom=%d",
x, y,
virtualKey.keyCode, virtualKey.scanCode,
virtualKey.hitLeft, virtualKey.hitTop,
virtualKey.hitRight, virtualKey.hitBottom);
#endif
//判断区域是否被击中
if (virtualKey.isHit(x, y)) {
return & virtualKey;
}
}
return NULL;
}
bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
....
....
//最主要的代码
if (mCurrentVirtualKey.down) {
if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
// Pointer went up while virtual key was down.
mCurrentVirtualKey.down = false;
if (!mCurrentVirtualKey.ignored) {
#if DEBUG_VIRTUAL_KEYS
ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
#endif
dispatchVirtualKey(when, policyFlags,
AKEY_EVENT_ACTION_UP,
AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
}
return true;
}
if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
const RawPointerData::Pointer& pointer =
mCurrentRawState.rawPointerData.pointerForId(id);
const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
// Pointer is still within the space of the virtual key.
return true;
}
}
// Pointer left virtual key area or another pointer also went down.
// Send key cancellation but do not consume the touch yet.
// This is useful when the user swipes through from the virtual key area
// into the main display surface.
mCurrentVirtualKey.down = false;
if (!mCurrentVirtualKey.ignored) {
#if DEBUG_VIRTUAL_KEYS
ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
#endif
//生成virtual key up且派发的地方
dispatchVirtualKey(when, policyFlags,
AKEY_EVENT_ACTION_UP,
AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
| AKEY_EVENT_FLAG_CANCELED);
}
}
....
....
if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
//检查touch的坐标,看是否在此区域,是的话,直接转化为相应的virtual key down,进行派发
const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
if (virtualKey) {
mCurrentVirtualKey.down = true;
mCurrentVirtualKey.downTime = when;
mCurrentVirtualKey.keyCode = virtualKey->keyCode;
mCurrentVirtualKey.scanCode = virtualKey->scanCode;
mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
if (!mCurrentVirtualKey.ignored) {
#if DEBUG_VIRTUAL_KEYS
ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
mCurrentVirtualKey.keyCode,
mCurrentVirtualKey.scanCode);
#endif
dispatchVirtualKey(when, policyFlags,
AKEY_EVENT_ACTION_DOWN,
AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
}
}
}
....
....
}
//派发
void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
int32_t keyEventAction, int32_t keyEventFlags) {
int32_t keyCode = mCurrentVirtualKey.keyCode;
int32_t scanCode = mCurrentVirtualKey.scanCode;
nsecs_t downTime = mCurrentVirtualKey.downTime;
int32_t metaState = mContext->getGlobalMetaState();
policyFlags |= POLICY_FLAG_VIRTUAL;
NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
getListener()->notifyKey(&args);
}
- 至此touch转virtual key的过程就结束了.