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