本文主要是介绍android input设备event处理以及hotplug检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://wb127.blog.51cto.com/2232662/793116android平台2.3.4,发现插上usb鼠标和键盘开机,那么都能正常使用,一旦拔出以后再插回去,就不能使用了。
首先检测/dev/input下的设备节点是否正常,发现拔出和插入设备,节点文件都能正常删除和创建。
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 2012 ./
drwxrwxrwx 7 default default 4096 Feb 29 2012 ../
crw-r----- 1 root root 13, 63 Feb 28 2012 mice
插入鼠标:
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 2012 ./
drwxrwxrwx 7 default default 4096 Feb 29 2012 ../
crw-r----- 1 root root 13, 64 Feb 29 2012 event0
crw-r----- 1 root root 13, 63 Feb 28 2012 mice
crw-r----- 1 root root 13, 32 Feb 29 2012 mouse0
再插入键盘:
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 2012 ./
drwxrwxrwx 7 default default 4096 Feb 29 2012 ../
crw-r----- 1 root root 13, 64 Feb 29 2012 event0
crw-r----- 1 root root 13, 65 Feb 29 2012 event1
crw-r----- 1 root root 13, 63 Feb 28 2012 mice
crw-r----- 1 root root 13, 32 Feb 29 2012 mouse0
直接cat /dev/input/event0,然后晃动鼠标,能看见有接收到消息。说明kernel这一层的input系统工作正常。问题应该在android内部。
打开logcat,查看拔插消息,发现如下打印:
如果鼠标正常时拔除:
E/EventHub( 1202): remove device: /dev/input/mouse0 not found
I/EventHub( 1202): Removed device: path=/dev/input/event0 name=Logitech USB Optical Mouse id=0x10001 (of 0x2) index=3 fd=76 classes=0x8
I/InputReader( 1202): Device removed: id=0x10001, name=Logitech USB Optical Mouse, sources=00010004
如果已经拔出过,再次插入拔出时:
E/EventHub( 1202): remove device: /dev/input/mouse0 not found
E/EventHub( 1202): remove device: /dev/input/event0 not found
插入鼠标时:
E/EventHub( 1202): could not open /dev/input/mouse0, Permission denied
E/EventHub( 1202): could not open /dev/input/event0, Permission denied
android的设备检测由两部分来合作:
1.Init - system/core/init/负责处理uevent消息并在/dev下建立相关节点文件
system/core/init/devices.c
2.EventHub - frameworks/base/libs/ui/处理/dev/input/下的节点文件,监测是否有文件新建(IN_CREATE)
frameworks/base/libs/ui/EventHub.cpp
当init使用mknod()在/dev下建立节点文件,文件的owner和group都是root。然后根据需要使用chown()改变起ower和group属性。对于/dev/input/下的节点文件,group变为input。但是EventHub随时随刻都在监测/dev/input/event*,在init创建节点但是还没有执行chown时,此时EventHub没有权限去打开。
为了验证这个理论,你可以自己写一个模块,在其初始化函数里面在/dev/input使用evdev创建节点文件event*,然后insmod这个模块,你会在logcat里面看见对应的event*无法打开,和我们开头的出错信息一样:
E/EventHub( 953): could not open /dev/input/event3, Permission denied
以下是修改内容:
--- old/system/init/devices.c |
+++ new/system/init/devices.c |
@@ -209,8 +209,10 @@ static void make_device(const char *path, int block, int major, int minor) |
mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); |
dev = makedev(major, minor); |
- mknod(path, mode, dev); |
- chown(path, uid, -1); |
+ unlink("/dev/.initdev"); |
+ mknod("/dev/.initdev", mode, dev); |
+ chown("/dev/.initdev", uid, -1); |
+ rename("/dev/.initdev", path); |
} --- old/frameworks/base/libs/ui/EventHub_old.cpp 2012-02-29 23:48:42.000000000 +0800 |
这篇关于android input设备event处理以及hotplug检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!