2012年10月15日 星期一

Linux - How to open device of type of input ?

0. Include file
#include

1. Get device name :
int GetDevices()
{
int fd = -1;
char name[80];

fd = open("/dev/input/event1", O_RDONLY); // open "dev/input/event1" and read only

if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1)
{
    name[0] = '\0';
    DBGMSG("ioctl fail, path = %s \n",devname);
    close(fd);
    return fd;
}
DBGMSG("ioctl OK, name = %s \n",name);

close(fd);
return fd;
}

2. Read data from devices

int fd = -1;
fd = GetDevices();
if (fd <0 devices="devices" err="%d\n" errno="errno" fail="fail" nbsp="nbsp" open="open" p="p">
struct input_event ev;
ret = read(fd, &ev, sizeof(struct input_event));

if (ret < 0)
{
DBGMSG("read data from devices fail, err = %d\n",errno);
}
else
{
DBGMSG("ev.type = %d\n",ev.type);
DBGMSG("ev.code = %d\n",ev.code);
DBGMSG("ev.value = %d\n",ev.value);
}

close(fd);
return fd;
}


沒有留言: