diff options
author | kraxel <kraxel> | 2004-04-21 11:55:44 +0000 |
---|---|---|
committer | kraxel <kraxel> | 2004-04-21 11:55:44 +0000 |
commit | c4d7dacb41039e087d20b8889a4d13bd4c9928f2 (patch) | |
tree | 0aef1f53f2e8cd55c4db8915d9d9a1265dd97adc /input-events.c | |
download | input-c4d7dacb41039e087d20b8889a4d13bd4c9928f2.tar.gz |
Initial revision
Diffstat (limited to 'input-events.c')
-rw-r--r-- | input-events.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/input-events.c b/input-events.c new file mode 100644 index 0000000..c525757 --- /dev/null +++ b/input-events.c @@ -0,0 +1,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; + } + fprintf(stderr,"grabbed input device\n"); + } + + fprintf(stderr,"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: + fprintf(stderr,"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: + fprintf(stderr,"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; + } + fprintf(stderr,"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: + */ |