1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/select.h>
#include "input.h"
/* ------------------------------------------------------------------ */
static void show_events(int nr, int timeout, int grab)
{
struct input_event event;
struct timeval tv;
int fd, quit;
fd_set set;
fd = device_open(nr,1);
if (-1 == fd)
return;
device_info(fd);
if (grab) {
if (-1 == ioctl(fd,EVIOCGRAB,1)) {
perror("ioctl EVIOCGRAB(1)");
close(fd);
return;
}
printf("grabbed input device\n");
}
printf("waiting for events\n");
quit = 0;
for (;;) {
/* wait for input */
FD_ZERO(&set);
FD_SET(fd,&set);
tv.tv_sec = timeout;
tv.tv_usec = 0;
switch (select(fd+1,&set,NULL,NULL,&tv)) {
case -1:
perror("select");
quit = 1;
break;
case 0:
printf("timeout, quitting\n");
quit = 1;
break;
}
if (quit)
break;
/* read input */
if (FD_ISSET(fd,&set)) {
switch (read(fd,&event,sizeof(event))) {
case -1:
perror("read");
quit = 1;
break;
case 0:
printf("EOF\n");
quit = 1;
break;
default:
print_event(&event);
break;
}
}
if (quit)
break;
}
if (grab) {
if (-1 == ioctl(fd,EVIOCGRAB,0)) {
perror("ioctl EVIOCGRAB(0)");
close(fd);
return;
}
printf("released input device\n");
}
close(fd);
}
static int usage(char *prog, int error)
{
fprintf(error ? stderr : stdout,
"usage: %s"
" [ -g ] [ -t <sec> ]"
" devnr\n",
prog);
exit(error);
}
int main(int argc, char *argv[])
{
int timeout = 10;
int grab = 0;
int c,devnr;
for (;;) {
if (-1 == (c = getopt(argc, argv, "hgt:")))
break;
switch (c) {
case 't':
timeout = atoi(optarg);
break;
case 'g':
grab = 1;
break;
case 'h':
usage(argv[0],0);
default:
usage(argv[0],1);
}
}
if (optind == argc)
usage(argv[0],1);
devnr = atoi(argv[optind]);
show_events(devnr,timeout,grab);
return 0;
}
/* ---------------------------------------------------------------------
* Local variables:
* c-basic-offset: 8
* End:
*/
|