From 0cc9b20c15460213e488bf5e70963b941482f628 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Tue, 14 Jan 2025 16:06:02 -0600 Subject: Add source. --- sdk/dev/inkey.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 sdk/dev/inkey.c (limited to 'sdk/dev/inkey.c') 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 +#include +#include + +#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 -- cgit v1.2.3-54-g00ecf