close, sleep, and reopen on errors.

master
Eric Messick 2013-10-17 15:24:06 -07:00
rodzic 349e059596
commit b081e1b999
1 zmienionych plików z 27 dodań i 17 usunięć

Wyświetl plik

@ -294,6 +294,7 @@ main(int argc, char **argv)
int nread; int nread;
char *dev_name; char *dev_name;
int fd; int fd;
int first_time = 1;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "usage: shuttlepro <device>\n" ); fprintf(stderr, "usage: shuttlepro <device>\n" );
@ -301,30 +302,39 @@ main(int argc, char **argv)
} }
dev_name = argv[1]; dev_name = argv[1];
fd = open(dev_name, O_RDONLY);
if (fd < 0) {
perror(dev_name);
exit(1);
}
// Flag it as exclusive access
if(ioctl( fd, EVIOCGRAB, 1 ) < 0) {
perror( "evgrab ioctl" );
exit(1);
}
initdisplay(); initdisplay();
while (1) { while (1) {
nread = read(fd, &ev, sizeof(ev)); fd = open(dev_name, O_RDONLY);
if (nread == sizeof(ev)) { if (fd < 0) {
handle_event(ev); perror(dev_name);
if (first_time) {
exit(1);
}
} else { } else {
if (nread < 0) { // Flag it as exclusive access
perror("read event"); if(ioctl( fd, EVIOCGRAB, 1 ) < 0) {
perror( "evgrab ioctl" );
} else { } else {
fprintf(stderr, "short read: %d\n", nread); first_time = 0;
while (1) {
nread = read(fd, &ev, sizeof(ev));
if (nread == sizeof(ev)) {
handle_event(ev);
} else {
if (nread < 0) {
perror("read event");
break;
} else {
fprintf(stderr, "short read: %d\n", nread);
break;
}
}
}
} }
} }
close(fd);
sleep(1);
} }
} }