我是靠谱客的博主 含糊小伙,这篇文章主要介绍Linux input 输入子系统,现在分享给大家,希望可以做个参考。

        输入子系统:主要为输入设备弄的一套框架,比如键盘、鼠标、触摸屏等,这些设备有数据后就上报给子系统,然后子系统再根据类型再做出处理,然后再发送到应用层。

使用input子系统的流程:

        1、定义一个 struct input_dev   *idev; 结构体

        2、实现代码流程,在input子系统中不需要注册 file_operations结构体(也就是不需要编写 read 、write那些函数),这些都会在input子系统中自动完成

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
static void inputdev_timer_function(unsigned long arg) { struct input_key_dev *dev = (struct input_key_dev *)arg; static int status = 0; if(status == 0){ /* 向 input子系统上报数据 */ input_report_key(inputdev.idev,KEY_0,1); input_sync(inputdev.idev); /* 每次上报完数据后都要一个同步信号 */ } if(status == 1){ /* 向 input子系统上报数 */ input_report_key(inputdev.idev,KEY_0,0); input_sync(inputdev.idev); } status = !status; mod_timer(&inputdev.timer,jiffies + msecs_to_jiffies(5000)); } static int __init inputdev_init(void) { int ret = 0; init_timer(&inputdev.timer); inputdev.timer.function = inputdev_timer_function; inputdev.idev = input_allocate_device(); /* 分配内存 */ inputdev.idev->name = "key0_input"; /* 设置事件和事件号的方法有三种 这是其中的一种 */ __set_bit(EV_KEY, inputdev.idev->evbit); /* 设置产生按键事件 */ __set_bit(EV_REP, inputdev.idev->evbit); /* 重复事件,比如按下去不放开,就会一直输出信息 */ /* 初始化input_dev,设置产生哪些按键 */ __set_bit(KEY_0, inputdev.idev->keybit); /* 注册输入设备 */ ret = input_register_device(inputdev.idev); if (ret) { printk("register input device failed!rn"); goto err_free_input_dev; //return ret; } mod_timer(&inputdev.timer,jiffies + msecs_to_jiffies(5000)); /* 以5秒的方式上报数据 */ return 0; } static void __exit inputdev_exit(void) { del_timer_sync(&inputdev.timer); /* 删除定时器 */ /* 释放input_dev */ input_unregister_device(inputdev.idev); input_free_device(inputdev.idev); }

3、insmod 加载 input 设备后,会在 /dev/input/下生成 event设备(带编号),应用层就可以open对应的设备文件了。

4、测试驱动能不能工作:用 hexdump 命令

测试后的数据是以小端模式显示出来

 5、应用层测试代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <errno.h> #include <linux/input.h> /* struct input_event { struct timeval time; __u16 type; __u16 code; __s32 value; }; */ int main(int argc, char **argv) { int fd; char *file; int len = 0; file = argv[1]; struct input_event event; fd = open(file,O_RDWR); if(fd < 0){ perror("open error:"); } while(1){ len = read(fd,&event,sizeof(struct input_event)); if(len < 0){ perror("read error:"); continue; } switch(event.type){ case EV_KEY: if(event.code == KEY_0){ printf("key value %d %sn", event.value, event.value ? "pressed" : "released"); } break; case EV_SYN: printf("synn"); break; default: printf("unknown eventn"); } } close(fd); return 0; }

最后

以上就是含糊小伙最近收集整理的关于Linux input 输入子系统的全部内容,更多相关Linux内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部