Android 虛擬鍵 生成與派發(fā)
- 本文以Android8.1源碼為分析
1.kl解析
- 虛擬鍵在Android TP (Touch Panel)中得到了廣泛的應(yīng)用,而在TP的driver中,報(bào)虛擬按鍵的方法常用的有兩種
- 直接傳鍵值(kernel的,不清楚,只是知道有這么回事)
- 還是將touch傳過(guò)來(lái),但是我們可以根據(jù)坐標(biāo)通過(guò)kl文件的解析判定是那個(gè)key.
- 在Android中我們用的是第二種做法,kl文件存放在設(shè)備/sys/board_properties/目錄下,一般名字是virtualkeys.設(shè)備名,打開(kāi)看大概如下所示:
0x01:158:128:670:160:100
- 0X01是標(biāo)志位,然后分別是scanCode,中心點(diǎn)X跪但,中心點(diǎn)Y,寬度和高度衩藤,其實(shí)就是一個(gè)矩形區(qū)域啤贩,以X,Y為中心點(diǎn)诞帐,分別延伸 w/2, h/2砂心。
- 在源碼VirtualKeyMap.cpp(Android/frameworks/native/libs/input)中,代碼如下:
//parse會(huì)在EventHub的openDeviceLocked調(diào)用VirtualKeyMap的load函數(shù)是調(diào)用
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;
}
//最關(guān)鍵代碼,kl的解析
VirtualKeyDefinition defn;
bool success = parseNextIntField(&defn.scanCode)
&& parseNextIntField(&defn.centerX)
&& parseNextIntField(&defn.centerY)
&& parseNextIntField(&defn.width)
&& parseNextIntField(&defn.height);
if (!success) {
//kl文件格式不對(duì)
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
//將每一個(gè)virtualkey解析出來(lái),以便去使用
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;
}
- 實(shí)際的范圍是[x-w/2,x+w/2] ,[y-h/2,y+h/2].
- 實(shí)際使用的地方是InputReader.cpp的TouchInputMapper::configureVirtualKeysLocked()方法中會(huì)計(jì)算出每個(gè)虛擬按鍵對(duì)應(yīng)的矩形范圍介却,并將scancode轉(zhuǎn)換成keycode讨韭。
2.獲取virtual key的規(guī)定矩形區(qū)域
touch事件整個(gè)派發(fā)在此就不再多說(shuō),直接從configureVirtualKeys開(kāi)始說(shuō)起,touch事件派發(fā)到此處后,在configureVirtualKeys中被處理是否去轉(zhuǎn)換為virtual key.源碼如下:
void TouchInputMapper::configureVirtualKeys() {
//獲取kl文件中解析出來(lái)的關(guān)于virtual key的內(nèi)容
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;
//根據(jù)scanCode轉(zhuǎn)換得到相應(yīng)的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的矩形區(qū)域
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的生成和派發(fā)
在void TouchInputMapper::cookAndDispatch(nsecs_t when)中會(huì)去調(diào)用consumeRawTouches進(jìn)行派發(fā)
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
//判斷區(qū)域是否被擊中
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且派發(fā)的地方
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的坐標(biāo),看是否在此區(qū)域,是的話,直接轉(zhuǎn)化為相應(yīng)的virtual key down,進(jìn)行派發(fā)
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);
}
}
}
....
....
}
//派發(fā)
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轉(zhuǎn)virtual key的過(guò)程就結(jié)束了.