获取嵌入式Linux设备下的触摸设备节点

这两天在搞嵌入式设备下的触摸屏,有一个需求是动态获取触摸设备,废话不说直接上代码:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>


/* this macro is used to tell if "bit" is set in "array"
 * it selects a byte from the array, and does a boolean AND
 * operation with a byte that only has the relevant bit set.
 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
 */
#define CHECK_FOR_BIT(bit, array)    (array[bit/8] & (1<<(bit%8)))


int is_touch_device(const char *device_path) 
{
    int ret = -1;
    unsigned char abs_bitmask[(ABS_MAX + 1) / 8] = { 0 };
    unsigned char key_bitmask[(KEY_MAX + 1) / 8] = { 0 };

	int fd = open(device_path, O_RDWR | O_CLOEXEC);
    if (fd < 0) 
	{
        printf("[%s:%d] could not open %s, %s\n", __FUNCTION__, __LINE__, device_path, strerror(errno));
        return -1;
    }
	
    ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
    ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);

    // See if this is a touch pad.
    // Is this a new modern multi-touch driver?
    if (CHECK_FOR_BIT(ABS_MT_POSITION_X, abs_bitmask) && CHECK_FOR_BIT(ABS_MT_POSITION_Y, abs_bitmask)) 
	{
        if (CHECK_FOR_BIT(BTN_TOUCH, key_bitmask)) 
		{
            printf("[%s:%d] %s is multi-touch\n", __FUNCTION__, __LINE__, device_path);
            ret = 0;
        }
    } 
	else if (CHECK_FOR_BIT(BTN_TOUCH, key_bitmask) && CHECK_FOR_BIT(ABS_X, abs_bitmask) && CHECK_FOR_BIT(ABS_Y, abs_bitmask)) // Is this an old style single-touch driver?
    {
        printf("[%s:%d] %s is single-touch\n", __FUNCTION__, __LINE__, device_path);
        ret = 0;
    } 
    else 
    {
        printf("[%s:%d] %s isn't touch device\n", __FUNCTION__, __LINE__, device_path);
    }
    
	close(fd);
	
    return ret;
}


int scan_touch_panel(const char *dirname, char *touch_device_name, unsigned int len)
{
    int ret = -1;
    char devname[256] = { 0 };
    char *filename = NULL;
    DIR *dir = NULL;
    struct dirent *dent = NULL;
	
    dir = opendir(dirname);
    if (dir == NULL)
	{   
        printf("[%s:%d] opendir %s failed\n", __FUNCTION__, __LINE__, dirname);
		return -1;
	}
	
    strcpy(devname, dirname);
    filename = devname + strlen(devname);
    *filename++ = '/';
	
    while((dent = readdir(dir))) 
	{
        if(dent->d_name[0] == '.' && (dent->d_name[1] == '\0' || (dent->d_name[1] == '.' && dent->d_name[2] == '\0')))
		{
            continue;
		}
		
        strcpy(filename, dent->d_name);
        
		if (0 == is_touch_device(devname)) 
		{
            memset(touch_device_name, 0, len);
            strcpy(touch_device_name, devname);
            ret = 0;
            break;
        }
    }
	
    closedir(dir);
	
    return ret;
}


int main(int argc, char *argv[])
{
    static const char *DEVICE_PATH = "/dev/input";
    char touch_device_name[128] = { 0 };

	int res = scan_touch_panel(DEVICE_PATH, touch_device_name, sizeof(touch_device_name)/sizeof(touch_device_name[0]));
    if (res < 0) 
	{
        printf("not found touch panel\n");
    } 
	else 
	{
        printf("touchPanel device path is %s\n", touch_device_name);
    }

    return 0;
}

参考文献:android 是如何找到触摸屏设备节点

android 触摸屏路径

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值