linux下input子框架之触摸

1.input子系统将硬件输入设备和用户空间通过内核空间联系起来,输入设备都有:鼠标,键盘,触摸,按键等下面以触摸举例:

2。触摸协议分为type A和type B:
type A:直接上报原始的触摸数据,由系统来区别具体属于哪个触摸点。

重要宏信息:
ABS_MT_TOOL_X                                       input_report_abs上报触摸点坐标信息
ABS_MT_TOOL_Y
SYNC_MT_REPORT                                    
SYNC_REPORT                                           input_sync所有点上报

type B协议:使用slot来区分触摸点,且slot用ABS_MT_TRACKING_ID来增加,替换,删除触摸点信息。每个slot关联一个id,触摸点通过ABS_MT(适用多点触摸)事件上报内核。

函数解析:
static inline void input_mt_slot(struct input_dev *dev, int slot)
{
 	input_event(dev, EV_ABS, ABS_MT_SLOT, slot);
}
参数1:input设备
参数2:指定当前上报的是哪一个触摸点的信息
该函数会产生ABS_MT_SLOT事件,type b设备驱动对每个识别的触摸点分配一个slot,后续由slot来上报信息,且可以根据ABS_MT_TRACKING_ID 来增加替换删除触摸点。
重要宏信息:                                               调用函数:
ABS_MT_SLOT                                             input_mt_slot 区分触摸点的信息,触发该事件
ABS_MT_TRACKING_ID                                      input_mt_report_slot_state添加或删除替换
ABS_MT_TOOL_X                                           input_report_abs上报触摸点坐标信息
ABS_MT_TOOL_Y											input_report_abs上报触摸点坐标信息
SYNC_REPORT                                             input_sync多点触摸信息传输结束
input子系统之数据上报流程:
1.设备和驱动匹配成功运行probe函数
2.input设备申请,例如devm_input_allocate_device(dev);
3.inpu设备参数和能力声明:
__set_bit(EV_KEY, inputdev->evbit)       产生的按键事件
__set_bit(EV_REP, inputdev->evbit)      产生的重复事件
__set_bit(KEY_0, inputdev->keybit)       产生哪些按键值
或者采用:input_set_abs_params(struct input_dev *dev, unsigned int axis,int min, int max, int fuzz, int flat)函数
 /* Single touch */
        input_set_abs_params(input, ABS_X, 0, xmax, 0, 0);
        input_set_abs_params(input, ABS_Y, 0, ymax, 0, 0);

/* Multi touch */
        input_mt_init_slots(input, MAX_TOUCHES, 0);
        input_set_abs_params(input, ABS_MT_POSITION_X, 0, xmax, 0, 0);
        input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ymax, 0, 0);
        
4.判断是单点触摸还是多点触摸,包括是否产生中断,来执行中断回调函数(事件上报)
5.注册到input类设备,input_register_device(input);

事件上报需要获取具体的输入值,对于按键就是在按键中断处理函数处理:
gpio_keys_gpio_report_event
input_report_key  //按键上报
inpt_report_abs函数原型为
input_event(struct input_dev*dev, unsigned int type, unsigned int code, int value)
上报完成后使用input_sync同步,即input_event(dev, EV_SYNC, SYNC_REPORT, 0)

3.如何从应用层读取input设备的事件类型,键值等

/*
apt-get install aarch64-linux-gnu-gcc
产生的gcc编译器来编译上述.c文件
struct input_event {
        struct timeval time;               时间(秒和微秒)      32位表示秒和微秒
        __u16 type;                          事件类型16位表示<EV_KEY>
        __u16 code;                         模拟成哪种按键,16位事件码 <KEY_0>
        __s32 value;                        按下还是释放,32位具体键值<1/0>
};
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <linux/input.h>
int main(void)
{
	int fd;
	int i = 0;
	int reads;

	struct input_event key;
	fd = open("/dev/input/event1", O_RDWR);  /*event1为产生的节点*/
	if (fd < 0) {
		perror("open device error");
		exit(1);
	}

	for (;;) {
		reads= read(fd, &key, sizeof(struct input_event));
		
		for(i=0; i<(int)reads/sizeof(struct input_event); i++)
			if(EV_KEY == key.type)
				printf("key_type:%d, key_code:%d, key_value:%d\n", key.type, key.code, key.value);
		    if(EV_ABS == key.type)
				printf("key_type:%d, key_code:%d, key_value:%d\n", key.type, key.code, key.value);
			if(EV_SYN == key.type)
				printf("syn event\n");

	}
	close(fd);
	
	return 0;
}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值