scan code到keycode的转换过程

时间:2021-03-03 09:18:15
/*******************************************************************************************************************/
转换的过程就是:从驱动层上报的input_event:的code,已powerkey为例:
116,从对应的xxx.kl文件找到对应的行:
key 116   POWER
116对应的 key是POWER
与POWER对应的InputEventLabel是 {POWER, AKEYCODE_POWER}
AKEYCODE_POWER是个枚举类型的变量,对应的值是26.


转换的代码是:
frameworks/native/libs/input/KeyLayoutMap.cpp
从这个代码可以学到cpp是怎样分析文件,从文件中提取信息的。
通过下面的log可以看出:
status_t KeyLayoutMap::Parser::parseKey() ->
 int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string());


frameworks/native/include/input/InputEventLabels.h
static int32_t getKeyCodeByLabel(const char* label) {
    return int32_t(lookupValueByLabel(label, KEYCODES));
}


frameworks/native/include/input/InputEventLabels.h
#define DEFINE_KEYCODE(key) { #key, AKEYCODE_##key }
struct InputEventLabel {
    const char *literal;
    int value;
};
static const InputEventLabel KEYCODES[] = {
    // NOTE: If you add a new keycode here you must also add it to several other files.
    //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
    DEFINE_KEYCODE(UNKNOWN),
    DEFINE_KEYCODE(SOFT_LEFT),
    DEFINE_KEYCODE(SOFT_RIGHT),
}
从这个包含的头文件#include <android/keycodes.h>找到,字符串对应的数值,就是所说的keyCode:


frameworks/native/include/android/keycodes.h
/*
 * Key codes.
 */
enum {
    AKEYCODE_UNKNOWN         = 0,
    AKEYCODE_SOFT_LEFT       = 1,
    AKEYCODE_SOFT_RIGHT      = 2,
    AKEYCODE_HOME            = 3,
    AKEYCODE_BACK            = 4,
    AKEYCODE_CALL            = 5,
    AKEYCODE_POWER           = 26,
}


这个文件和java层的文件:frameworks/base/core/java/android/view/KeyEvent.java对应
    /** Key code constant: Power key. */
    public static final int KEYCODE_POWER           = 26;
/*******************************************************************************************************************/
打开debug开关,可得如下的log:下面的log是创建一个输入设备的过程


08-12 14:19:40.010  1271  2703 E EventHub: Opening device: /dev/input/event2
08-12 14:19:40.011  1271  2703 E EventHub: Created descriptor: raw=:0000:0000:name:qpnp_pon, cooked=fb60d4f4370f5dbe8267b63d38dea852987571ab
08-12 14:19:40.011  1271  2703 E EventHub: add device 3: /dev/input/event2
08-12 14:19:40.011  1271  2703 E EventHub:   bus:        0000
08-12 14:19:40.011  1271  2703 E EventHub:   vendor      0000
08-12 14:19:40.011  1271  2703 E EventHub:   product     0000
08-12 14:19:40.011  1271  2703 E EventHub:   version     0000
08-12 14:19:40.011  1271  2703 E EventHub:   name:       "qpnp_pon"
08-12 14:19:40.011  1271  2703 E EventHub:   location:   "qpnp_pon/input0"
08-12 14:19:40.011  1271  2703 E EventHub:   unique id:  ""
08-12 14:19:40.011  1271  2703 E EventHub:   descriptor: "fb60d4f4370f5dbe8267b63d38dea852987571ab"
08-12 14:19:40.011  1271  2703 E EventHub:   driver:     v1.0.1
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system provided input device configuration file: path='/system/usr/idc/qpnp_pon.idc'
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system user input device configuration file: path='/data/system/devices/idc/qpnp_pon.idc'
08-12 14:19:40.011  1271  2703 E InputDevice: Probe failed to find input device configuration file: name='qpnp_pon', type=0
08-12 14:19:40.011  1271  2703 D EventHub: No input device configuration file found for device 'qpnp_pon'.
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system provided input device configuration file: path='/system/usr/keylayout/qpnp_pon.kl'
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system user input device configuration file: path='/data/system/devices/keylayout/qpnp_pon.kl'
08-12 14:19:40.011  1271  2703 E InputDevice: Probe failed to find input device configuration file: name='qpnp_pon', type=1
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system provided input device configuration file: path='/system/usr/keychars/qpnp_pon.kcm'
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system user input device configuration file: path='/data/system/devices/keychars/qpnp_pon.kcm'
08-12 14:19:40.011  1271  2703 E InputDevice: Probe failed to find input device configuration file: name='qpnp_pon', type=2
08-12 14:19:40.011  1271  2703 E InputDevice: Probing for system provided input device configuration file: path='/system/usr/keylayout/Generic.kl'


08-12 14:19:40.011  1271  2703 E InputDevice: Found


08-12 14:19:40.014  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:25: 'key 3     2'.
08-12 14:19:40.014  1271  2703 D KeyLayoutMap: Parsed key scan code: code=3, keyCode=9, flags=0x00000000.
08-12 14:19:40.014  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:26: 'key 4     3'.
08-12 14:19:40.014  1271  2703 D KeyLayoutMap: Parsed key scan code: code=4, keyCode=10, flags=0x00000000.
08-12 14:
08-12 14:19:40.015  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:37: 'key 15    TAB'.
08-12 14:19:40.015  1271  2703 D KeyLayoutMap: Parsed key scan code: code=15, keyCode=61, flags=0x00000000.




08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:437: 'led 0x06 SUSPEND'.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsed led scan code: code=6, ledCode=6.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:438: 'led 0x07 MUTE'.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsed led scan code: code=7, ledCode=7.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:439: 'led 0x08 MISC'.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsed led scan code: code=8, ledCode=8.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:440: 'led 0x09 MAIL'.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsed led scan code: code=9, ledCode=9.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsing /system/usr/keylayout/Generic.kl:441: 'led 0x0a CHARGING'.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsed led scan code: code=10, ledCode=10.
08-12 14:19:40.036  1271  2703 D KeyLayoutMap: Parsed key layout map file '/system/usr/keylayout/Generic.kl' 442 lines in 24.518ms.
08-12 14:19:40.036  1271  2703 E InputDevice: Probing for system provided input device configuration file: path='/system/usr/keychars/Generic.kcm'
08-12 14:19:40.036  1271  2703 E InputDevice: Found
08-12 14:19:40.036  1271  1271 I SystemServiceManager: Starting com.android.server.MountService$Lifecycle
08-12 14:19:40.037  1271  2703 W EventHub: Unable to disable kernel key repeat for /dev/input/event2: Function not implemented


08-12 14:19:40.037  1271  2703 I EventHub: New device: id=3, fd=123, path='/dev/input/event2', name='qpnp_pon', classes=0x1, configuration='', keyLayout='/system/usr/keylayout/Generic.kl', keyCharacterMap='/system/usr/keychars/Generic.kcm', builtinKeyboard=false, wakeMechanism=EPOLLWAKEUP, usingClockIoctl=true