首页 > 移动平台 > 详细

06day01input_app

时间:2017-02-28 00:59:56      阅读:226      评论:0      收藏:0      [点我收藏+]
  1 /*
  2 从应用层的角度去分析
  3 sysfs  proc  这两个节点下文件都是虚拟的文件系统
  4 凡是在sys  proc目录下文件都是在内存中
  5 
  6 可以通过 /proc/bus/input/devices 来查看当前的系统下面所有的输入设备的信息,包括绑定的设备节点
  7 
  8 */
  9 #include <sys/stat.h>
 10 #include <fcntl.h>
 11 #include <stdio.h>
 12 #include <unistd.h>
 13 
 14 ////////////////////////input 输入子系统////////////////////////////
 15 #include <linux/input.h>
 16 //  /user/include/linux/input.h
 17 /*
 18 在应用层处理一个
 19 struct input_event {
 20     struct timeval time;
 21     __u16 type;   事件的类型  Event types下定义了 事件的类型
 22     EV_SYN  事件结束
 23     EV_KEY  按键事件
 24     EV_REL  相对事件
 25     EV_ABS  绝对事件
 26 
 27     __u16 code;
 28     __s32 value;
 29 
 30     code和value  会根据 事件不同而代表不同的含义
 31 };
 32 结构体
 33 
 34 应用层 当接收input_event  然后进行解析
 35 */
 36 
 37 static void rel_handler(struct input_event * event)
 38 {
 39     //相对坐标事件
 40     //code 表示REL_X   REL_Y   REL_WHEEL
 41     //value 表示位移的距离
 42     switch(event->code) {
 43         case REL_X:
 44             printf("x=%d\n", event->value);
 45             break;
 46         case REL_Y:
 47             printf("y=%d\n", event->value);
 48             break;
 49         case REL_WHEEL:
 50             printf("wheel = %d\n",event->value );
 51             break;
 52         default:
 53             break;
 54     }
 55 
 56 }
 57 
 58 static void key_handler(struct input_event  * event)
 59 {
 60     //如果event type 是按键类型
 61     //code 表示按键键值
 62     //value 表示状态 0 松开  1按下
 63     printf("key: code = %#x\n",  event->code);
 64 
 65     if(event->value) {
 66         printf("按下\n");
 67     }else {
 68         printf("松开\n");
 69     }
 70 
 71 }
 72 
 73 //事件处理函数
 74 static void input_event_handler(struct input_event * event)
 75 {
 76     switch(event->type) {
 77         case EV_SYN: //结束事件
 78             printf("结束事件\n");
 79             break;
 80         case EV_KEY: //按键事件
 81             key_handler(event);
 82             break;
 83         case EV_REL: //相对坐标事件
 84             rel_handler(event);
 85             break;
 86         default:
 87             break;
 88     }
 89 
 90 }
 91 
 92 
 93 int main(int argc, char const *argv[])
 94 {
 95     if(argc  < 2) {
 96 //输入设备的节点在 /dev/input目录下 
 97 //可以通过查看 /proc/bus/input/devices   来找到对应的设备节点
 98 //
 99         printf("Usage:cmd <input devices  node>\n");
100         return -1;
101     }
102 
103     int fd = open(argv[1],  O_RDONLY);
104 
105     if(fd < 0) {
106         perror("open");
107         goto err_open;
108     }
109 
110     struct input_event  event = {0};
111 
112     //应用层是通过input_event 来获取底层的各种事件
113     //如果该设备有对应的事件发生,那么可以读取其中的值
114     while(read(fd, &event, sizeof(event))) {
115          input_event_handler(&event);
116     }
117 
118     return 0;
119 
120 err_open:
121     return -1;
122 }

 

06day01input_app

原文:http://www.cnblogs.com/baoshulin/p/6477024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!