Home | History | Annotate | Line # | Download | only in tetris
input.c revision 1.6
      1  1.6  mycroft /*	$NetBSD: input.c,v 1.6 2002/09/19 21:12:10 mycroft Exp $	*/
      2  1.2      cgd 
      3  1.1      cgd /*-
      4  1.1      cgd  * Copyright (c) 1992, 1993
      5  1.1      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * This code is derived from software contributed to Berkeley by
      8  1.1      cgd  * Chris Torek and Darren F. Provine.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  *
     38  1.1      cgd  *	@(#)input.c	8.1 (Berkeley) 5/31/93
     39  1.1      cgd  */
     40  1.1      cgd 
     41  1.1      cgd /*
     42  1.1      cgd  * Tetris input.
     43  1.1      cgd  */
     44  1.1      cgd 
     45  1.1      cgd #include <sys/types.h>
     46  1.1      cgd #include <sys/time.h>
     47  1.6  mycroft #include <sys/poll.h>
     48  1.1      cgd 
     49  1.1      cgd #include <errno.h>
     50  1.1      cgd #include <unistd.h>
     51  1.1      cgd 
     52  1.1      cgd #include "input.h"
     53  1.1      cgd #include "tetris.h"
     54  1.1      cgd 
     55  1.1      cgd /* return true iff the given timeval is positive */
     56  1.1      cgd #define	TV_POS(tv) \
     57  1.1      cgd 	((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
     58  1.1      cgd 
     59  1.1      cgd /* subtract timeval `sub' from `res' */
     60  1.1      cgd #define	TV_SUB(res, sub) \
     61  1.1      cgd 	(res)->tv_sec -= (sub)->tv_sec; \
     62  1.1      cgd 	(res)->tv_usec -= (sub)->tv_usec; \
     63  1.1      cgd 	if ((res)->tv_usec < 0) { \
     64  1.1      cgd 		(res)->tv_usec += 1000000; \
     65  1.1      cgd 		(res)->tv_sec--; \
     66  1.1      cgd 	}
     67  1.1      cgd 
     68  1.1      cgd /*
     69  1.1      cgd  * Do a `read wait': select for reading from stdin, with timeout *tvp.
     70  1.1      cgd  * On return, modify *tvp to reflect the amount of time spent waiting.
     71  1.1      cgd  * It will be positive only if input appeared before the time ran out;
     72  1.1      cgd  * otherwise it will be zero or perhaps negative.
     73  1.1      cgd  *
     74  1.1      cgd  * If tvp is nil, wait forever, but return if select is interrupted.
     75  1.1      cgd  *
     76  1.1      cgd  * Return 0 => no input, 1 => can read() from stdin
     77  1.1      cgd  */
     78  1.1      cgd int
     79  1.1      cgd rwait(tvp)
     80  1.5      wiz 	struct timeval *tvp;
     81  1.1      cgd {
     82  1.6  mycroft 	struct pollfd set[1];
     83  1.1      cgd 	struct timeval starttv, endtv, *s;
     84  1.1      cgd #define	NILTZ ((struct timezone *)0)
     85  1.1      cgd 
     86  1.1      cgd 	/*
     87  1.1      cgd 	 * Someday, select() will do this for us.
     88  1.1      cgd 	 * Just in case that day is now, and no one has
     89  1.1      cgd 	 * changed this, we use a temporary.
     90  1.1      cgd 	 */
     91  1.1      cgd 	if (tvp) {
     92  1.1      cgd 		(void) gettimeofday(&starttv, NILTZ);
     93  1.1      cgd 		endtv = *tvp;
     94  1.1      cgd 		s = &endtv;
     95  1.1      cgd 	} else
     96  1.1      cgd 		s = 0;
     97  1.1      cgd again:
     98  1.6  mycroft 	set[0].fd = STDIN_FILENO;
     99  1.6  mycroft 	set[0].events = POLLIN;
    100  1.6  mycroft 	switch (poll(set, 1, s->tv_sec * 1000 + s->tv_usec / 1000)) {
    101  1.1      cgd 
    102  1.1      cgd 	case -1:
    103  1.1      cgd 		if (tvp == 0)
    104  1.1      cgd 			return (-1);
    105  1.1      cgd 		if (errno == EINTR)
    106  1.1      cgd 			goto again;
    107  1.1      cgd 		stop("select failed, help");
    108  1.1      cgd 		/* NOTREACHED */
    109  1.1      cgd 
    110  1.1      cgd 	case 0:	/* timed out */
    111  1.1      cgd 		tvp->tv_sec = 0;
    112  1.1      cgd 		tvp->tv_usec = 0;
    113  1.1      cgd 		return (0);
    114  1.1      cgd 	}
    115  1.1      cgd 	if (tvp) {
    116  1.1      cgd 		/* since there is input, we may not have timed out */
    117  1.1      cgd 		(void) gettimeofday(&endtv, NILTZ);
    118  1.1      cgd 		TV_SUB(&endtv, &starttv);
    119  1.1      cgd 		TV_SUB(tvp, &endtv);	/* adjust *tvp by elapsed time */
    120  1.1      cgd 	}
    121  1.1      cgd 	return (1);
    122  1.1      cgd }
    123  1.1      cgd 
    124  1.1      cgd /*
    125  1.1      cgd  * `sleep' for the current turn time (using select).
    126  1.1      cgd  * Eat any input that might be available.
    127  1.1      cgd  */
    128  1.1      cgd void
    129  1.1      cgd tsleep()
    130  1.1      cgd {
    131  1.1      cgd 	struct timeval tv;
    132  1.1      cgd 	char c;
    133  1.1      cgd 
    134  1.1      cgd 	tv.tv_sec = 0;
    135  1.1      cgd 	tv.tv_usec = fallrate;
    136  1.1      cgd 	while (TV_POS(&tv))
    137  1.1      cgd 		if (rwait(&tv) && read(0, &c, 1) != 1)
    138  1.1      cgd 			break;
    139  1.1      cgd }
    140  1.1      cgd 
    141  1.1      cgd /*
    142  1.1      cgd  * getchar with timeout.
    143  1.1      cgd  */
    144  1.1      cgd int
    145  1.1      cgd tgetchar()
    146  1.1      cgd {
    147  1.1      cgd 	static struct timeval timeleft;
    148  1.1      cgd 	char c;
    149  1.1      cgd 
    150  1.1      cgd 	/*
    151  1.1      cgd 	 * Reset timeleft to fallrate whenever it is not positive.
    152  1.1      cgd 	 * In any case, wait to see if there is any input.  If so,
    153  1.1      cgd 	 * take it, and update timeleft so that the next call to
    154  1.1      cgd 	 * tgetchar() will not wait as long.  If there is no input,
    155  1.1      cgd 	 * make timeleft zero or negative, and return -1.
    156  1.1      cgd 	 *
    157  1.1      cgd 	 * Most of the hard work is done by rwait().
    158  1.1      cgd 	 */
    159  1.1      cgd 	if (!TV_POS(&timeleft)) {
    160  1.1      cgd 		faster();	/* go faster */
    161  1.1      cgd 		timeleft.tv_sec = 0;
    162  1.1      cgd 		timeleft.tv_usec = fallrate;
    163  1.1      cgd 	}
    164  1.1      cgd 	if (!rwait(&timeleft))
    165  1.1      cgd 		return (-1);
    166  1.1      cgd 	if (read(0, &c, 1) != 1)
    167  1.1      cgd 		stop("end of file, help");
    168  1.1      cgd 	return ((int)(unsigned char)c);
    169  1.1      cgd }
    170