aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/dev/inkey.c
diff options
context:
space:
mode:
authorWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
committerWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
commit0cc9b20c15460213e488bf5e70963b941482f628 (patch)
treebb0143245583ec846630f39bfa2258dba640ccd7 /sdk/dev/inkey.c
parent0e084ade5069756d487b5c948c48b777e37c00c9 (diff)
Add source.
Diffstat (limited to 'sdk/dev/inkey.c')
-rw-r--r--sdk/dev/inkey.c76
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