diff options
Diffstat (limited to 'sdk/dev/inkey.c')
-rw-r--r-- | sdk/dev/inkey.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/sdk/dev/inkey.c b/sdk/dev/inkey.c new file mode 100644 index 0000000..1003905 --- /dev/null +++ b/sdk/dev/inkey.c @@ -0,0 +1,76 @@ +/* + * Wait and read single byte input from device. + * $Id$ + * Copyright (c) 1997 by Tycho Softworks. + * For conditions of distribution and reuse see product license. + */ + +#include <dev/tty.h> +#include <std/select.h> +#include <std/poll.h> + +#ifndef SYS_POLL_H_MISSING +#define USE_POLL +#endif + +#ifndef POLL_H_MISSING +#define USE_POLL +#endif + +#ifdef USE_POLL + +int inkey(fd_t fd, int timeout) +{ + struct pollfd poller; + uchar buf; + + poller.fd = fd; + poller.revents = 0; + poller.events = POLLIN ; + + if(!poll(&poller, 1, timeout)) + return -1; + + if(poller.revents & (POLLERR | POLLHUP)) + return -1; + + if(read(fd, &buf, 1) != 1) + return -1; + + return buf; +} + +#else + +int inkey(fd_t fd, int timeout) +{ + fd_set inp,out,exc; + struct timeval timer; + uchar buf; + + FD_ZERO(&inp); + FD_ZERO(&out); + FD_ZERO(&exc); + + FD_SET(fd, &inp); + FD_SET(fd, &exc); + + timer.tv_sec = timeout / 1000; + timer.tv_usec = timeout % 1000; + + if(!select(fd + 1, &inp, &out, &exc, &timer)) + return -1; + + if(FD_ISSET(fd, &exc)) + return -1; + + if(FD_ISSET(fd, &inp)) + { + if(read(fd, &buf, 1) != 1) + return -1; + return buf; + } + return -1; +} + +#endif |