1.查询 阻塞机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int ret;
struct input_id id;
int len;
unsigned int evbit[2];
unsigned char byte;
int i;
int bit;
char read_buff[100] = {'\0'};
struct input_event event;
char *ev_names[] = {
"EV_SYN ",
"EV_KEY ",
"EV_REL ",
"EV_ABS ",
"EV_MSC ",
"EV_SW ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"EV_LED ",
"EV_SND ",
"NULL ",
"EV_REP ",
"EV_FF ",
"EV_PWR ",
};
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR ); /*| O_NONBLOCK*/
if (fd < 0)
{
return -1;
}
ret = ioctl(fd, EVIOCGID, &id);
if (ret == 0)
{
printf("bustype = 0x%x\n", id.bustype );
printf("vendor = 0x%x\n", id.vendor );
printf("product = 0x%x\n", id.product );
printf("version = 0x%x\n", id.version );
}
len = ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
if (len > 0)
{
printf("support ev type:");
for (i = 0; i < len; i++)
{
byte = ((unsigned char *)evbit)[i];
for (bit = 0; bit < 8; bit++)
{
if (byte & (1 << bit))
{
printf("%s ", ev_names[i * 8 + bit]);
}
}
}
printf("\n");
}
while (1)
{
len = read(fd, &event, sizeof(event));
if (len == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
else
{
printf("read err.\n");
}
}
return 0;
}
2.poll机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
int main(int argc, char **argv)
{
int fd;
int ret;
struct input_event event;
struct pollfd fds[1];
nfds_t nfds = 1;
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR ); /*| O_NONBLOCK*/
if (fd < 0)
{
return -1;
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1)
{
fds[0].revents = 0;
ret = poll(fds, nfds, 3000);
if (ret > 0)
{
if (fds[0].revents == POLLIN)
{
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
else
{
printf("read error.\n");
}
}
else if(ret == 0)
{
printf("time out\n");
}
else
{
printf("poll err.\n");
}
}
return 0;
}
3.select机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
int main(int argc, char **argv)
{
int fd;
int ret;
struct input_event event;
fd_set readfd;
unsigned int nfds = 1;
struct timeval time_val;
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd < 0)
{
return -1;
}
nfds = fd +1;
FD_ZERO(&readfd);
while (1)
{
time_val.tv_sec = 3;
time_val.tv_usec = 0;
FD_SET(fd, &readfd);
ret = select(nfds, &readfd, NULL, NULL, &time_val);
if (ret > 0)
{
if (FD_ISSET(fd, &readfd))
{
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
else
{
printf("read error.\n");
}
}
else if(ret == 0)
{
printf("time out\n");
}
else
{
printf("select err.\n");
}
}
return 0;
}
poll和select在open的时候,要使用NONBLOCK, 即非阻塞。原因可以查看这里。
4.异步机制
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
int fd;
void my_sig_process(int sig)
{
struct input_event event;
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
int main(int argc, char **argv)
{
int ret = 0;
int cnt = 0;
if (argc != 2)
{
printf("Usage: %s <dev>\ne.g., %s /dev/input/event0\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd < 0)
{
return -1;
}
signal(SIGIO, my_sig_process);
fcntl(fd, F_SETOWN, getpid());
ret = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, ret | FASYNC);
while (1)
{
printf("main loop count = %d\n", cnt++);
sleep(1);
}
return 0;
}