mirror of
https://github.com/vattam/BSDGames.git
synced 2025-12-21 03:14:50 +00:00
copy in from cvs; cvs2svn fucked up big time
git-svn-id: file:///srv/svn/joey/trunk/src/packages/bsdgames@9775 a4a2c43b-8ac3-0310-8836-e0e880c912e2
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $ */
|
||||
/* $NetBSD: input.c,v 1.8 2002/12/29 15:12:17 kristerw Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
@@ -44,6 +44,7 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/poll.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
@@ -65,44 +66,41 @@
|
||||
}
|
||||
|
||||
/*
|
||||
* Do a `read wait': select for reading from stdin, with timeout *tvp.
|
||||
* Do a `read wait': poll for reading from stdin, with timeout *tvp.
|
||||
* On return, modify *tvp to reflect the amount of time spent waiting.
|
||||
* It will be positive only if input appeared before the time ran out;
|
||||
* otherwise it will be zero or perhaps negative.
|
||||
*
|
||||
* If tvp is nil, wait forever, but return if select is interrupted.
|
||||
* If tvp is nil, wait forever, but return if poll is interrupted.
|
||||
*
|
||||
* Return 0 => no input, 1 => can read() from stdin
|
||||
*/
|
||||
int
|
||||
rwait(tvp)
|
||||
register struct timeval *tvp;
|
||||
struct timeval *tvp;
|
||||
{
|
||||
int i;
|
||||
struct timeval starttv, endtv, *s;
|
||||
struct pollfd set[1];
|
||||
struct timeval starttv, endtv;
|
||||
int timeout;
|
||||
#define NILTZ ((struct timezone *)0)
|
||||
|
||||
/*
|
||||
* Someday, select() will do this for us.
|
||||
* Just in case that day is now, and no one has
|
||||
* changed this, we use a temporary.
|
||||
*/
|
||||
if (tvp) {
|
||||
(void) gettimeofday(&starttv, NILTZ);
|
||||
endtv = *tvp;
|
||||
s = &endtv;
|
||||
timeout = tvp->tv_sec * 1000 + tvp->tv_usec / 1000;
|
||||
} else
|
||||
s = 0;
|
||||
timeout = INFTIM;
|
||||
again:
|
||||
i = 1;
|
||||
switch (select(1, (fd_set *)&i, (fd_set *)0, (fd_set *)0, s)) {
|
||||
set[0].fd = STDIN_FILENO;
|
||||
set[0].events = POLLIN;
|
||||
switch (poll(set, 1, timeout)) {
|
||||
|
||||
case -1:
|
||||
if (tvp == 0)
|
||||
return (-1);
|
||||
if (errno == EINTR)
|
||||
goto again;
|
||||
stop("select failed, help");
|
||||
stop("poll failed, help");
|
||||
/* NOTREACHED */
|
||||
|
||||
case 0: /* timed out */
|
||||
@@ -120,7 +118,7 @@ again:
|
||||
}
|
||||
|
||||
/*
|
||||
* `sleep' for the current turn time (using select).
|
||||
* `sleep' for the current turn time.
|
||||
* Eat any input that might be available.
|
||||
*/
|
||||
void
|
||||
@@ -136,20 +134,6 @@ tsleep()
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Eat up any input (used at end of game).
|
||||
*/
|
||||
void
|
||||
eat_input()
|
||||
{
|
||||
struct timeval tv;
|
||||
char c;
|
||||
|
||||
do {
|
||||
tv.tv_sec = tv.tv_usec = 0;
|
||||
} while (rwait(&tv) && read(0, &c, 1) == 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* getchar with timeout.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user