Android Input子系统-含实例源码
1 Input子系統作用
Android很多外設都是用到輸入輸出設備,比如touchscreen,鍵盤,音量鍵等,輸入
設備對應Android 框架是Android?input子系統,像我們定制類比較多的,很多?
需要用到輸入子系統,比如一鍵打開相機,一鍵喚醒,實體按鍵等。
2 Android input子系統框架
Android Input框架講解最好的一篇文章?
https://blog.csdn.net/mmmccc000/article/details/49756059?
Android input框架主要分為三個部分,一個是kernel,一個是kl文件,kl也可?
以劃分為框架層,Framework層主要做監聽、過濾、分發的工作,Android App部?
分主要是接收Input子系統發上來的鍵值做對應的操作。
?
??
3 kernel input框架
Kernel主要是涉及Input設備節點的生成,proc文件系統的生成
?
??
有一個文章寫Kernel框架非常好的,我就不再講了,給幾個鏈接?
https://blog.csdn.net/u013604527/article/details/53432623/?
https://blog.csdn.net/jscese/article/details/42123673?
https://blog.csdn.net/xubin341719/article/details/7881735
4 如何在kernel添加一個新的鍵值
4.1 先在頭文件里面添加
找個共用的頭文件添加要的key值,正常是在include下面的,我的代碼是在這個位?
置
4.2 在kernel對應的位置注冊和發送鍵值
注冊的位置
/*包含頭文件*/#include <linux/input.h>/*聲明input設備*/struct input_dev *button_dev;/*注冊input 子設備*/ printk(KERN_ERR " xxxx_wake_init\n"); if (!(button_dev= input_allocate_device())) { printk(KERN_ERR "input_dev: not enough memory\n"); return -ENOMEM; } button_dev->name = "xxxx"; button_dev->phys = "xxxx/event0"; button_dev->id.bustype = BUS_HOST; button_dev->id.vendor = 0x0001; button_dev->id.product = 0x0002; button_dev->id.version = 0x0100; button_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_SYN); set_bit(KEY_WEIQIFA_TEST,button_dev->keybit); err = input_register_device(button_dev); if (err) { printk(KERN_ERR " fail to input_register_device\n"); input_free_device(button_dev); return err; } printk(KERN_ERR "xxxx_wake_init success!!!\n"); return 0;發送鍵值的位置
input_report_key(button_dev, KEY_WEIQIFA_TEST, 1);input_sync(button_dev);input_report_key(button_dev, KEY_WEIQIFA_TEST, 0); input_sync(button_dev);4.3 寫一個對應這個kl文件
kl文件簡單介紹
Kl文件主要是起到了承上啟下的作用,kernel發過來的鍵值主要通過kl文件來映?
射,我們來看看kl文件里面的內容把。左邊的是對應kernel上報上來的鍵值,后?
面是對應Android Framework上的鍵值。
kl文件哪里來的呢
我們看項目配置下面,有很多kl文件,那么是每個input子系統對應一個kl文件,?
還是所有的項目共用一個kl文件??
kl源碼位置在
在運行的設備里,它的位置如下
rk3399_mid:/ # busybox find / -iname *.kl /system/usr/keylayout/Generic.kl /system/usr/keylayout/Vendor_0079_Product_0011.kl /system/usr/keylayout/qwerty.kl /system/usr/keylayout/rk29-keypad.kl /system/usr/keylayout/rtkbt_virtual_hid.kl ... rk3399_mid:/ #kl是跟input設備如何對應起來的呢
我們可以用這個命令來看看?cat /proc/bus/input/devices?
kl文件里面的vendor和product分別對應下面的vendor和product
添加我們對應的kl文件?
文件命名跟你申請的vendor和product對應 上面的input設備對應的kl文件應該是:Vendor_0001_Product_0002.kl?
里面的內容如下,當然如果你沒有配置kl文件,默認會去Generic.kl找鍵值映?
射,如果里面也沒有,就會去其他kl文件繼續找。
4.4 mtk平臺的一個input 子系統驅動例子
上面寫的是在rockchip平臺做的例子,下面給出一個mtk平臺的例子?
[mkt平臺的input子系統例子]?
(https://blog.csdn.net/weiqifa0/article/details/50387504)
5 Framework input系統介紹
Framework 主要做的事情是監聽底層的input,然后分發給上層?
下面的這個說的框架非常好,可以看看,我介紹完主要說明如何添加一個新的key?
到系統里面?
https://blog.csdn.net/wangkaiblog/article/details/12085183?
https://blog.csdn.net/mmmccc000/article/details/49756059
?
??
?
6 添加一個新的鍵值
主要涉及到幾個文件位置?
下面列出需要修改的文件還有修改的差異點?
base/api/current.txt?
base/api/system-current.txt?
base/core/java/android/view/KeyEvent.java?
base/core/res/res/values/attrs.xml?
/Generic.kl這里也可以根據驅動添加/?
base/data/keyboards/Generic.kl?
base/policy/src/com/android/internal/policy/impl/PhoneFallbackEventHandler.java?
native/include/android/keycodes.h?
native/include/input/InputEventLabels.h
如果出現以下編譯錯誤
out/target/common/obj/PACKAGING/test-api.txt:41961: error 5: Added public field android.view.KeyEvent.KEYCODE_WEIQIFA****************************** You have tried to change the API from what has been previously approved. To make these errors go away, you have two choices: 1) You can add "@hide" javadoc comments to the methods, etc. listed in the errors above. 2) You can update current.txt by executing the following command: make update-api To submit the revised current.txt to the main Android repository, you will need approval. ******************************提示你運行make update-api,但是要先回退current.txt 和system-current.txt,因為這兩個文件就是make update-api生成出來的
git checkout api/current.txt git checkout api/system-current.txt make update-api關于make update-api詳細看這個https://blog.csdn.net/u010229714/article/details/73840014
如果需要調試,可以打開下面的調試文件
--- a/services/core/java/com/android/server/input/InputManagerService.java +++ b/services/core/java/com/android/server/input/InputManagerService.java @@ -125,7 +125,7 @@ import libcore.util.Objects; public class InputManagerService extends IInputManager.Stub implements Watchdog.Monitor { static final String TAG = "InputManager"; - static final boolean DEBUG = false; + static final boolean DEBUG = true; private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml"; --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -172,7 +172,7 @@ public class PhoneWindowManager implements WindowManagerPolicy { static final String TAG = "WindowManager"; static final boolean DEBUG = false; static final boolean localLOGV = false; - static final boolean DEBUG_INPUT = false; + static final boolean DEBUG_INPUT = true; static final boolean DEBUG_KEYGUARD = false; static final boolean DEBUG_LAYOUT = false; static final boolean DEBUG_STARTING_WINDOW = false; --- a/services/inputflinger/InputDispatcher.cpp +++ b/services/inputflinger/InputDispatcher.cpp @@ -17,31 +17,31 @@ #define LOG_TAG "InputDispatcher" #define ATRACE_TAG ATRACE_TAG_INPUT -//#define LOG_NDEBUG 0 +#define LOG_NDEBUG 1 // Log detailed debug messages about each inbound event notification to the dispatcher. -#define DEBUG_INBOUND_EVENT_DETAILS 0 +#define DEBUG_INBOUND_EVENT_DETAILS 1 // Log detailed debug messages about each outbound event processed by the dispatcher. -#define DEBUG_OUTBOUND_EVENT_DETAILS 0 +#define DEBUG_OUTBOUND_EVENT_DETAILS 1 // Log debug messages about the dispatch cycle. -#define DEBUG_DISPATCH_CYCLE 0 +#define DEBUG_DISPATCH_CYCLE 1 // Log debug messages about registrations. -#define DEBUG_REGISTRATION 0 +#define DEBUG_REGISTRATION 1 // Log debug messages about input event injection. -#define DEBUG_INJECTION 0 +#define DEBUG_INJECTION 1 // Log debug messages about input focus tracking. -#define DEBUG_FOCUS 0 +#define DEBUG_FOCUS 1 // Log debug messages about the app switch latency optimization. -#define DEBUG_APP_SWITCH 0 +#define DEBUG_APP_SWITCH 1 // Log debug messages about hover events. -#define DEBUG_HOVER 0 +#define DEBUG_HOVER 1 #include "InputDispatcher.h" diff --git a/services/inputflinger/InputReader.cpp b/services/inputflinger/InputReader.cpp index c8dc454..3a622fc 100644 --- a/services/inputflinger/InputReader.cpp +++ b/services/inputflinger/InputReader.cpp @@ -16,10 +16,10 @@ #define LOG_TAG "InputReader" -//#define LOG_NDEBUG 0 +#define LOG_NDEBUG 1 // Log debug messages for each raw event received from the EventHub. -#define DEBUG_RAW_EVENTS 0 +#define DEBUG_RAW_EVENTS 1 // Log debug messages about touch screen filtering hacks. #define DEBUG_HACKS 0 diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp index 2dff4e0..977aa22 100644 --- a/libs/input/InputTransport.cpp +++ b/libs/input/InputTransport.cpp @@ -5,19 +5,19 @@ // #define LOG_TAG "InputTransport" -//#define LOG_NDEBUG 0 +#define LOG_NDEBUG 1 // Log debug messages about channel messages (send message, receive message) -#define DEBUG_CHANNEL_MESSAGES 0 +#define DEBUG_CHANNEL_MESSAGES 1 // Log debug messages whenever InputChannel objects are created/destroyed -#define DEBUG_CHANNEL_LIFECYCLE 0 +#define DEBUG_CHANNEL_LIFECYCLE 1 // Log debug messages about transport actions -#define DEBUG_TRANSPORT_ACTIONS 0 +#define DEBUG_TRANSPORT_ACTIONS 1 // Log debug messages about touch event resampling -#define DEBUG_RESAMPLING 0 +#define DEBUG_RESAMPLING 17 App接收鍵值
主要參照這個鏈接來寫?
https://www.cnblogs.com/zhujiabin/p/5915802.html?
例程Android studio源碼?
鏈接:https://pan.baidu.com/s/1hzhQ-dLSRnAbg2Q8w5qKPw 密碼:944g
8 命令調試
8.1 測試上報時間
觸發Input上報的時間點
rk3399_mid:/ $ logcat -s "CAE_LOG"|grep -E CAEIvwCb 09-06 14:10:13.502 327 436 D CAE_LOG : CAEIvwCb angle = 23 ,beam = 0 CMScore = 1643APK接收到鍵值的時間點
09-06 14:10:13.518 5693-5693/com.hu.audiotest D/audiotest_MainActivity: KeyEvent.KEYCODE_ENTER KeyCode0 09-06 14:10:13.522 5693-5693/com.hu.audiotest D/audiotest_MainActivity: KeyEvent.KEYCODE_ENTER KeyCode0消耗的時間大概需要16MS,但是觸發是通過HAL層觸發的,如果從Kerne觸發事間會更短一些
8.2 設備文件調試
我們kernel下面正常用 getevent 來調試?
getevent -l?
cat /proc/bus/input/devices
8.3 adb 上報鍵值調試
我們正常用adb shell input keyevent xx xx代表鍵值?
可以用input來觸發touchscreen也是可以的?
有個比較好的文章?
https://blog.csdn.net/jlminghui/article/details/39268419?
大部分支持的鍵值如下
總結
以上是生活随笔為你收集整理的Android Input子系统-含实例源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 各种快捷键
- 下一篇: x线计算机体层成像设备教案,医学影像设备