您现在的位置是:首页 > 文章详情

Linux/Android——input_handler之evdev (四) 【转】

日期:2017-05-22点击:362

转自:http://blog.csdn.net/u013491946/article/details/72638919

目录(?)[+]

    在前文Linux/Android——input子系统核心 (三) 中概括了总体的结构,以及介绍了input核心的职责,其中有说道注册input设备时会去匹配已有的事件处理器handler,

而这个handler也是存放在一个链表里面的,这里介绍下input子系统中的事件处理input_handler机制.


                                              撰写不易,转载需注明出处:http://blog.csdn.net/jscese/article/details/42238377#t6


evdev:

  /kernel/drivers/input下众多事件处理器handler其中的一个,可以看下源码/kernel/drivers/input/evdev.c中的模块init:

 

[objc]  view plain  copy
 
 
  1. staticintvoid return }  


这个初始化就是往input核心中注册一个input_handler类型的evdev_handler,调用的是input.c提供的接口,input_handler结构前文有介绍,看下evdev_handler的赋值:

 

 

[objc]  view plain  copy
 
 
  1. staticstruct       = evdev_event,  
  2.     = evdev_connect,  
  3.  = evdev_disconnect,  
  4.        = &evdev_fops,  
  5.       = EVDEV_MINOR_BASE,  
  6.        = ,  
  7.    = evdev_ids,  
  8. };  


赋值各个函数指针!

 



input_register_handler:

 可以看到上面的evdev handler 就是调用这个接口注册到input核心中的,同样evdev.c同目录下也还有其它的handler,有兴趣可以看看它们的init函数,都是会调用到这个接口去注册的.

 

[objc]  view plain  copy
 
 
  1. /** 
  2.  * input_register_handler - register a new input handler 
  3.  * @handler: handler to be registered 
  4.  * 
  5.  * This function registers a new input handler (interface) for input 
  6.  * devices in the system and attaches it to all input devices that 
  7.  * are compatible with the handler. 
  8.  */ intstructinput_handler structinput_dev int if return ifNULL if]) {  
  9. goto ] = handler;   
  10.   
  11.   
  12.  out return }  


 

input核心中保存的handler数组:

 

[objc]  view plain  copy
 
 
  1. staticstructinput_handler];  


这是保存注册到input核心中的handler数组,因为在之前input注册的时候注册的字符设备主设备号为13.字符设备的次设备号为0~255,可以有256个设备,

这里后面会看到一个handler可以connect处理32个input设备,所以input体系中,最多拥有8个handler


这个匹配过程和上一篇中的过程是一样的,最后匹配上的话会调用匹配上的handler 中connect指针指向的函数.


另外可以注意的是evdev是匹配所有设备的,因为:

 

[objc]  view plain  copy
 
 
  1. staticconststruct  =  },     
  2.   
  3. };  


如果没有特定的handler添加进handler链表,那么在匹配的时候,只要有这个evdev的handler,最后都会匹配到evdev,这个具体可以去看看上篇的匹配过程.

 

我这边调试的是usb触摸屏,所以用的是evdev的handler,下面看下evdev的connect.


evdev_connect:

 注册的evdev_handler中connect指向的函数为evdev_connect:

[objc]  view plain  copy
 
 
  1. /* 
  2.  * Create new evdev device. Note that input core serializes calls 
  3.  * to connect and disconnect so we don't need to lock evdev_table here. 
  4.  */ staticintstructinput_handlerstructinput_dev conststructinput_device_idid structevdev int int for; minor < EVDEV_MINORS; minor++)  
  5. if break if return // 可以看到这里evdev handler匹配连接好的设备都以evdev 类型存在这个evdev_table数组的,这个数组大小为32个,这就是我上面说到的,为什么只有8个handler //这里是判断evdev的32个位置中是否有空 sizeofstruct  
  6. if return   
  7. , minor);    
  8. true   
  9.  = input_get_device(dev);    
  10.  = dev_name(&evdev->dev);  
  11.  = handler;  
  12.  = evdev;  
  13.  = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);  
  14.  = &input_class;  
  15.  = &dev->dev;  
  16.  = evdev_free;  
  17.   
  18. if goto   
  19. if goto   
  20. if goto return;  
  21.  err_cleanup_evdev  err_unregister_handle  err_free_evdev return }  



evdev:

这里的evdev变量的结构如下:

 

[objc]  view plain  copy
 
 
  1. struct int  
  2. int  
  3. struct  
  4.   
  5. struct__rcu  
  6. struct  
  7.   
  8. struct struct bool };  



关于这个结构变量我的理解是抽象出来一个设备,代表一个input_dev与其匹配好的handler的组合(handle),可以看作提供给事件处理层的一个封装.

 


input_handle:

这个代表一个匹配成功的input dev和 handler组合,定义在input.h中,每个evdev中包含一个input_handle,并且注册到input核心中:

 

[objc]  view plain  copy
 
 
  1. /** 
  2.  * struct input_handle - links input device with an input handler 
  3.  * @private: handler-specific data 
  4.  * @open: counter showing whether the handle is 'open', i.e. should deliver 
  5.  *    events from its device 
  6.  * @name: name given to the handle by handler that created it 
  7.  * @dev: input device the handle is attached to 
  8.  * @handler: handler that works with the device through this handle 
  9.  * @d_node: used to put the handle on device's list of attached handles 
  10.  * @h_node: used to put the handle on handler's list of handles from which 
  11.  *    it gets events 
  12.  */ struct voidvoidprivate  
  13. int constcharchar structinput_dev  
  14. structinput_handler  
  15. struct  
  16. struct };  

 

input_register_handle:

 看看这个handle的注册,不要和handler搞混淆了,这不是一个概念~

[objc]  view plain  copy
 
 
  1. /** 
  2.  * input_register_handle - register a new input handle 
  3.  * @handle: handle to register 
  4.  * 
  5.  * This function puts a new input handle onto device's 
  6.  * and handler's lists so that events can flow through 
  7.  * it once it is opened using input_open_device(). 
  8.  * 
  9.  * This function is supposed to be called from handler's 
  10.  * connect() method. 
  11.  */ intstructinput_handle structinput_handler structinput_dev  
  12.      * Filters go to the head of the list, normal handlers 
  13.      * to the tail. 
  14.      */ if else //把这个handle的d_node 加到对应input_dev的h_list链表里面 //把这个handle的h_node 加到对应input_handler的h_list链表里面 }  


这个注册是把handle 本身的链表加入到它自己的input_dev 以及 input_handler的h_list链表中,这样以后就可以通过h_list遍历到这个handle,

这样就实现了三者的绑定联系.



另外在evdev中还有个结构:


[objc]  view plain  copy
 
 
  1. struct int  
  2. int  
  3. int  
  4.   
  5. struct bool char8 structfasync_struct  
  6. structevdev  
  7. struct  
  8. int struct  
  9. };  


 

这个结构会在evdev被打开的时候 创建,这里关于evdev的初始以及在input系统中承接作用暂时介绍到这里,

前文 Linux/Android——输入子系统input_event传递 (二) 中有记录从设备驱动传递上来的event是怎么到input核心,然后接着往上传递的,接下来就是用到evdev传递了.下篇介绍.

【作者】 张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接:https://yq.aliyun.com/articles/374940
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章