Skip to content

Commit 49ccac5

Browse files
author
Jeff Brown
committed
Refactor key code mapping.
Added handling for EV_MSC / MSC_SCAN which typically reports the HID usage associated with a key. This will enable key maps to map keys with HID usages that Linux does not natively recognize. Removed keyCode and flags fields from EventHub RawEvent since they don't necessarily make sense in isolation now that we pay attention to HID usage codes too. Removed the fallback code for mapping keys and axes. In practice, an input device should be self-sufficient. We should not ever need to look at the built-in keyboard's key map. In fact, there usually isn't a built-in keyboard anyhow. This code was originally working around a problem where we weren't loading the key map for touch screens with virtual keys, which has long since been fixed. Change-Id: I0a319bdec44be9514f795526347397e94d53a127
1 parent db13a6b commit 49ccac5

File tree

7 files changed

+349
-232
lines changed

7 files changed

+349
-232
lines changed

include/androidfw/KeyLayoutMap.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class KeyLayoutMap : public RefBase {
6464
public:
6565
static status_t load(const String8& filename, sp<KeyLayoutMap>* outMap);
6666

67-
status_t mapKey(int32_t scanCode, int32_t* keyCode, uint32_t* flags) const;
67+
status_t mapKey(int32_t scanCode, int32_t usageCode,
68+
int32_t* outKeyCode, uint32_t* outFlags) const;
6869
status_t findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const;
6970

7071
status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const;
@@ -78,11 +79,14 @@ class KeyLayoutMap : public RefBase {
7879
uint32_t flags;
7980
};
8081

81-
KeyedVector<int32_t, Key> mKeys;
82+
KeyedVector<int32_t, Key> mKeysByScanCode;
83+
KeyedVector<int32_t, Key> mKeysByUsageCode;
8284
KeyedVector<int32_t, AxisInfo> mAxes;
8385

8486
KeyLayoutMap();
8587

88+
const Key* getKey(int32_t scanCode, int32_t usageCode) const;
89+
8690
class Parser {
8791
KeyLayoutMap* mMap;
8892
Tokenizer* mTokenizer;

libs/androidfw/KeyLayoutMap.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,49 @@ status_t KeyLayoutMap::load(const String8& filename, sp<KeyLayoutMap>* outMap) {
8080
return status;
8181
}
8282

83-
status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t* keyCode, uint32_t* flags) const {
84-
ssize_t index = mKeys.indexOfKey(scanCode);
85-
if (index < 0) {
83+
status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
84+
int32_t* outKeyCode, uint32_t* outFlags) const {
85+
const Key* key = getKey(scanCode, usageCode);
86+
if (!key) {
8687
#if DEBUG_MAPPING
87-
ALOGD("mapKey: scanCode=%d ~ Failed.", scanCode);
88+
ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode);
8889
#endif
89-
*keyCode = AKEYCODE_UNKNOWN;
90-
*flags = 0;
90+
*outKeyCode = AKEYCODE_UNKNOWN;
91+
*outFlags = 0;
9192
return NAME_NOT_FOUND;
9293
}
9394

94-
const Key& k = mKeys.valueAt(index);
95-
*keyCode = k.keyCode;
96-
*flags = k.flags;
95+
*outKeyCode = key->keyCode;
96+
*outFlags = key->flags;
9797

9898
#if DEBUG_MAPPING
99-
ALOGD("mapKey: scanCode=%d ~ Result keyCode=%d, flags=0x%08x.", scanCode, *keyCode, *flags);
99+
ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
100+
scanCode, usageCode, *outKeyCode, *outFlags);
100101
#endif
101102
return NO_ERROR;
102103
}
103104

105+
const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
106+
if (scanCode) {
107+
ssize_t index = mKeysByScanCode.indexOfKey(scanCode);
108+
if (index >= 0) {
109+
return &mKeysByScanCode.valueAt(index);
110+
}
111+
}
112+
if (usageCode) {
113+
ssize_t index = mKeysByUsageCode.indexOfKey(usageCode);
114+
if (index >= 0) {
115+
return &mKeysByUsageCode.valueAt(index);
116+
}
117+
}
118+
return NULL;
119+
}
120+
104121
status_t KeyLayoutMap::findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const {
105-
const size_t N = mKeys.size();
122+
const size_t N = mKeysByScanCode.size();
106123
for (size_t i=0; i<N; i++) {
107-
if (mKeys.valueAt(i).keyCode == keyCode) {
108-
outScanCodes->add(mKeys.keyAt(i));
124+
if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
125+
outScanCodes->add(mKeysByScanCode.keyAt(i));
109126
}
110127
}
111128
return NO_ERROR;
@@ -190,7 +207,7 @@ status_t KeyLayoutMap::Parser::parseKey() {
190207
scanCodeToken.string());
191208
return BAD_VALUE;
192209
}
193-
if (mMap->mKeys.indexOfKey(scanCode) >= 0) {
210+
if (mMap->mKeysByScanCode.indexOfKey(scanCode) >= 0) {
194211
ALOGE("%s: Duplicate entry for key scan code '%s'.", mTokenizer->getLocation().string(),
195212
scanCodeToken.string());
196213
return BAD_VALUE;
@@ -231,7 +248,7 @@ status_t KeyLayoutMap::Parser::parseKey() {
231248
Key key;
232249
key.keyCode = keyCode;
233250
key.flags = flags;
234-
mMap->mKeys.add(scanCode, key);
251+
mMap->mKeysByScanCode.add(scanCode, key);
235252
return NO_ERROR;
236253
}
237254

services/input/EventHub.cpp

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ static void setDescriptor(InputDeviceIdentifier& identifier) {
117117
}
118118
}
119119
identifier.descriptor = sha1(rawDescriptor);
120+
ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
121+
identifier.descriptor.string());
120122
}
121123

122124
// --- Global Functions ---
@@ -434,58 +436,35 @@ bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
434436
return false;
435437
}
436438

437-
status_t EventHub::mapKey(int32_t deviceId, int scancode,
438-
int32_t* outKeycode, uint32_t* outFlags) const
439-
{
439+
status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
440+
int32_t* outKeycode, uint32_t* outFlags) const {
440441
AutoMutex _l(mLock);
441442
Device* device = getDeviceLocked(deviceId);
442-
443+
443444
if (device && device->keyMap.haveKeyLayout()) {
444-
status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
445+
status_t err = device->keyMap.keyLayoutMap->mapKey(
446+
scanCode, usageCode, outKeycode, outFlags);
445447
if (err == NO_ERROR) {
446448
return NO_ERROR;
447449
}
448450
}
449-
450-
if (mBuiltInKeyboardId != NO_BUILT_IN_KEYBOARD) {
451-
device = getDeviceLocked(mBuiltInKeyboardId);
452-
453-
if (device && device->keyMap.haveKeyLayout()) {
454-
status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
455-
if (err == NO_ERROR) {
456-
return NO_ERROR;
457-
}
458-
}
459-
}
460-
451+
461452
*outKeycode = 0;
462453
*outFlags = 0;
463454
return NAME_NOT_FOUND;
464455
}
465456

466-
status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
467-
{
457+
status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
468458
AutoMutex _l(mLock);
469459
Device* device = getDeviceLocked(deviceId);
470460

471461
if (device && device->keyMap.haveKeyLayout()) {
472-
status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
462+
status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
473463
if (err == NO_ERROR) {
474464
return NO_ERROR;
475465
}
476466
}
477467

478-
if (mBuiltInKeyboardId != NO_BUILT_IN_KEYBOARD) {
479-
device = getDeviceLocked(mBuiltInKeyboardId);
480-
481-
if (device && device->keyMap.haveKeyLayout()) {
482-
status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
483-
if (err == NO_ERROR) {
484-
return NO_ERROR;
485-
}
486-
}
487-
}
488-
489468
return NAME_NOT_FOUND;
490469
}
491470

@@ -729,16 +708,8 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
729708
#endif
730709
event->deviceId = deviceId;
731710
event->type = iev.type;
732-
event->scanCode = iev.code;
711+
event->code = iev.code;
733712
event->value = iev.value;
734-
event->keyCode = AKEYCODE_UNKNOWN;
735-
event->flags = 0;
736-
if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
737-
status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
738-
&event->keyCode, &event->flags);
739-
ALOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
740-
iev.code, event->keyCode, event->flags, err);
741-
}
742713
event += 1;
743714
}
744715
capacity -= count;
@@ -960,7 +931,7 @@ status_t EventHub::openDeviceLocked(const char *devicePath) {
960931
ALOGV(" name: \"%s\"\n", identifier.name.string());
961932
ALOGV(" location: \"%s\"\n", identifier.location.string());
962933
ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string());
963-
ALOGV(" descriptor: \"%s\" (%s)\n", identifier.descriptor.string(), rawDescriptor.string());
934+
ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string());
964935
ALOGV(" driver: v%d.%d.%d\n",
965936
driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
966937

services/input/EventHub.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
/* Convenience constants. */
4141

42-
#define BTN_FIRST 0x100 // first button scancode
43-
#define BTN_LAST 0x15f // last button scancode
42+
#define BTN_FIRST 0x100 // first button code
43+
#define BTN_LAST 0x15f // last button code
4444

4545
namespace android {
4646

@@ -58,10 +58,8 @@ struct RawEvent {
5858
nsecs_t when;
5959
int32_t deviceId;
6060
int32_t type;
61-
int32_t scanCode;
62-
int32_t keyCode;
61+
int32_t code;
6362
int32_t value;
64-
uint32_t flags;
6563
};
6664

6765
/* Describes an absolute axis. */
@@ -173,10 +171,10 @@ class EventHubInterface : public virtual RefBase {
173171

174172
virtual bool hasInputProperty(int32_t deviceId, int property) const = 0;
175173

176-
virtual status_t mapKey(int32_t deviceId, int scancode,
174+
virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
177175
int32_t* outKeycode, uint32_t* outFlags) const = 0;
178176

179-
virtual status_t mapAxis(int32_t deviceId, int scancode,
177+
virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
180178
AxisInfo* outAxisInfo) const = 0;
181179

182180
// Sets devices that are excluded from opening.
@@ -252,10 +250,10 @@ class EventHub : public EventHubInterface
252250

253251
virtual bool hasInputProperty(int32_t deviceId, int property) const;
254252

255-
virtual status_t mapKey(int32_t deviceId, int scancode,
253+
virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
256254
int32_t* outKeycode, uint32_t* outFlags) const;
257255

258-
virtual status_t mapAxis(int32_t deviceId, int scancode,
256+
virtual status_t mapAxis(int32_t deviceId, int32_t scanCode,
259257
AxisInfo* outAxisInfo) const;
260258

261259
virtual void setExcludedDevices(const Vector<String8>& devices);

0 commit comments

Comments
 (0)