Android系統(tǒng)移植-按鍵移植
(1)Android使用標(biāo)準(zhǔn)的linux輸入事件設(shè)備(/dev/input目錄下)和驅(qū)動(dòng),按鍵定義在內(nèi)核include/linux/input.h文件中,
按鍵定義形式如下:
#define KEY_ESC??? ??? ??? 1
#define KEY_1??? ??? ??? 2
#define KEY_2??? ??? ??? 3
(2)內(nèi)核中(我的平臺(tái)是arch/arm/mach-mmp/merlin.c文件)中按鍵的定義如下形式:
static struct gpio_keys_button btn_button_table[] = {
??? [0] = {
??? ??? .code??? ??? ??? =??? KEY_F1,
??? ??? .gpio??? ??? ??? =??? MFP_PIN_GPIO2,
??? ??? .active_low??? ??? =??? 1,??? ??? /* 0 for down 0, up 1; 1 for down 1, up 0 */
??? ??? .desc??? ??? ??? =??? "H_BTN button",
??? ??? .type??? ??? ??? =??? EV_KEY,
??? ??? /* .wakeup??? ??? ??? = */
??? ??? .debounce_interval??? =??? 10,??? ??? /* 10 msec jitter elimination */
??? },
??? [1] = {
??? ??? .code??? ??? ??? =??? KEY_F2,
??? ??? .gpio??? ??? ??? =??? MFP_PIN_GPIO3,
??? ??? .active_low??? ??? =??? 1,??? ??? /* 0 for down 0, up 1; 1 for down 1, up 0 */
??? ??? .desc??? ??? ??? =??? "O_BTN button",
??? ??? .type??? ??? ??? =??? EV_KEY,
??? ??? /* .wakeup??? ??? ??? = */
??? ??? .debounce_interval??? =??? 10,??? ??? /* 10 msec jitter elimination */
??? },
??? [2] = {
??? ??? .code??? ??? ??? =??? KEY_F4,
??? ??? .gpio??? ??? ??? =??? MFP_PIN_GPIO1,
??? ??? .active_low??? ??? =??? 1,??? ??? /* 0 for down 0, up 1; 1 for down 1, up 0 */
??? ??? .desc??? ??? ??? =??? "S_BTN button",
??? ??? .type??? ??? ??? =??? EV_KEY,
??? ??? /* .wakeup??? ??? ??? = */
??? ??? .debounce_interval??? =??? 10,??? ??? /* 10 msec jitter elimination */
??? },
};
static struct gpio_keys_platform_data gpio_keys_data = {
??? .buttons? = btn_button_table,
??? .nbuttons = ARRAY_SIZE(btn_button_table),
};
static struct platform_device gpio_keys = {
??? .name = "gpio-keys",
??? .dev? = {
??? ??? .platform_data = &gpio_keys_data,
??? },
??? .id?? = -1,
};
上面定義是將MFP_PIN_GPIO2這個(gè)GPIO口的按鍵映射到Linux的KEY_F1按鍵,MPF_PIN_GPIO3映射到KEY_F2,MFP_PIN_GPIO1映射到KEY_F4
(3)上面(2)步實(shí)現(xiàn)了從硬件GPIO口到內(nèi)核標(biāo)準(zhǔn)按鍵的映射,但是android并沒有直接使用映射后的鍵值,而且對(duì)其再進(jìn)行了一次映射,從內(nèi)核標(biāo)準(zhǔn)鍵值
到android所用鍵值的映射表定義在android文件系統(tǒng)的/system/usr/keylayout目錄下。標(biāo)準(zhǔn)的映射文件為qwerty.kl,定義如下:
key 399?? GRAVE
key 2???? 1
key 3???? 2
key 4???? 3
key 5???? 4
key 6???? 5
key 7???? 6
key 8???? 7
key 9???? 8
key 10??? 9
key 11??? 0
key 158?? BACK????????????? WAKE_DROPPED
key 230?? SOFT_RIGHT??????? WAKE
key 60??? SOFT_RIGHT??????? WAKE
key 107?? ENDCALL?????????? WAKE_DROPPED
key 62??? ENDCALL?????????? WAKE_DROPPED
key 229?? MENU????????????? WAKE_DROPPED
key 139?? MENU????????????? WAKE_DROPPED
key 59??? MENU????????????? WAKE_DROPPED
key 127?? SEARCH??????????? WAKE_DROPPED
key 217?? SEARCH??????????? WAKE_DROPPED
key 228?? POUND
key 227?? STAR
key 231?? CALL????????????? WAKE_DROPPED
key 61??? CALL????????????? WAKE_DROPPED
key 232?? DPAD_CENTER?????? WAKE_DROPPED
key 108?? DPAD_DOWN???????? WAKE_DROPPED
key 103?? DPAD_UP?????????? WAKE_DROPPED
key 102?? HOME????????????? WAKE
key 105?? DPAD_LEFT???????? WAKE_DROPPED
key 106?? DPAD_RIGHT??????? WAKE_DROPPED
key 115?? VOLUME_UP
key 114?? VOLUME_DOWN
key 116?? POWER???????????? WAKE
key 212?? CAMERA
key 16??? Q
key 17??? W
key 18??? E
key 19??? R
key 20??? T
key 21??? Y
key 22??? U
key 23??? I
key 24??? O
key 25??? P
key 26??? LEFT_BRACKET
key 27??? RIGHT_BRACKET
key 43??? BACKSLASH
key 30??? A
key 31??? S
key 32??? D
key 33??? F
key 34??? G
key 35??? H
key 36??? J
key 37??? K
key 38??? L
key 39??? SEMICOLON
key 40??? APOSTROPHE
key 14??? DEL
???????
key 44??? Z
key 45??? X
key 46??? C
key 47??? V
key 48??? B
key 49??? N
key 50??? M
key 51??? COMMA
key 52??? PERIOD
key 53??? SLASH
key 28??? ENTER
???????
key 56??? ALT_LEFT
key 100?? ALT_RIGHT
key 42??? SHIFT_LEFT
key 54??? SHIFT_RIGHT
key 15??? TAB
key 57??? SPACE
key 150?? EXPLORER
key 155?? ENVELOPE???????
key 12??? MINUS
key 13??? EQUALS
key 215?? AT
(4)android對(duì)底層按鍵的處理方法
android按鍵的處理是Window Manager負(fù)責(zé),主要的映射轉(zhuǎn)換實(shí)現(xiàn)在android源代碼frameworks/base/libs/ui/EventHub.cpp
此文件處理來自底層的所有輸入事件,并根據(jù)來源對(duì)事件進(jìn)行分類處理,對(duì)于按鍵事件,處理過程如下:
(a)記錄驅(qū)動(dòng)名稱為
(b)獲取環(huán)境變量ANDROID_ROOT為系統(tǒng)路徑(默認(rèn)是/system,定義在android源代碼/system/core/rootdir/init.rc文件中)
(c)查找路徑為"系統(tǒng)路徑/usr/keylayout/驅(qū)動(dòng)名稱.kl"的按鍵映射文件,如果不存在則默認(rèn)用路徑為"系統(tǒng)路徑/usr/keylayout/qwerty.kl"
這個(gè)默認(rèn)的按鍵映射文件,映射完成后再把經(jīng)映射得到的android按鍵碼值發(fā)給上層應(yīng)用程序。
所以我們可以在內(nèi)核中定義多個(gè)按鍵設(shè)備,然后為每個(gè)設(shè)備設(shè)定不同的按鍵映射文件,不定義則會(huì)默認(rèn)用qwerty.kl
(5)舉例
上面(2)步我們?cè)趦?nèi)核中聲明了一個(gè)名為"gpio-keys"的按鍵設(shè)備,此設(shè)備定義在內(nèi)核drivers/input/keyboard/gpio_keys.c文件中
然后我們?cè)趦?nèi)核啟動(dòng)過程中注冊(cè)此設(shè)備:? platform_device_register(&gpio_keys);
然后我們可以自己定義一個(gè)名為gpio-keys.kl的android按鍵映射文件,此文件的定義可以參考querty.kl的內(nèi)容,比如說我們想將MPF_PIN_GPIO3
對(duì)應(yīng)的按鍵作android中的MENU鍵用,首先我們?cè)趦?nèi)核中將MPF_PIN_GPIO3映射到KEY_F2,在內(nèi)核include/linux/input.h中查找KEY_F2發(fā)現(xiàn)
#define KEY_F2??? ??? ??? 60
參照KEY_F2的值我們?cè)趃pio-keys.kl中加入如下映射即可
key 60??? MENU????????????? WAKE
其它按鍵也照此添加,完成后將按鍵表放置到/system/usr/keylayout目錄下即可。
補(bǔ)充:
(1)android按鍵設(shè)備的映射關(guān)系可以在logcat開機(jī)日志中找的到(查找EventHub即可)
(2)android按鍵設(shè)備由Window Manager負(fù)責(zé),Window Manager從按鍵驅(qū)動(dòng)讀取內(nèi)核按鍵碼,然后將內(nèi)核按鍵碼轉(zhuǎn)換成android按鍵碼,轉(zhuǎn)換完成
后Window Manager會(huì)將內(nèi)核按鍵碼和android按鍵碼一起發(fā)給應(yīng)用程序來使用,這一點(diǎn)一定要注意。