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

124 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

这两天在搞嵌入式设备下的触摸屏,有一个需求是动态获取触摸设备,废话不说直接上代码: ` #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;

}`