Home | History | Annotate | Line # | Download | only in tetris
input.c revision 1.2
      1  1.2  cgd /*	$NetBSD: input.c,v 1.2 1995/04/22 07:42:34 cgd 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.1  cgd 
     48  1.1  cgd #include <errno.h>
     49  1.1  cgd #include <unistd.h>
     50  1.1  cgd 
     51  1.1  cgd #include "input.h"
     52  1.1  cgd #include "tetris.h"
     53  1.1  cgd 
     54  1.1  cgd /* return true iff the given timeval is positive */
     55  1.1  cgd #define	TV_POS(tv) \
     56  1.1  cgd 	((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
     57  1.1  cgd 
     58  1.1  cgd /* subtract timeval `sub' from `res' */
     59  1.1  cgd #define	TV_SUB(res, sub) \
     60  1.1  cgd 	(res)->tv_sec -= (sub)->tv_sec; \
     61  1.1  cgd 	(res)->tv_usec -= (sub)->tv_usec; \
     62  1.1  cgd 	if ((res)->tv_usec < 0) { \
     63  1.1  cgd 		(res)->tv_usec += 1000000; \
     64  1.1  cgd 		(res)->tv_sec--; \
     65  1.1  cgd 	}
     66  1.1  cgd 
     67  1.1  cgd /*
     68  1.1  cgd  * Do a `read wait': select for reading from stdin, with timeout *tvp.
     69  1.1  cgd  * On return, modify *tvp to reflect the amount of time spent waiting.
     70  1.1  cgd  * It will be positive only if input appeared before the time ran out;
     71  1.1  cgd  * otherwise it will be zero or perhaps negative.
     72  1.1  cgd  *
     73  1.1  cgd  * If tvp is nil, wait forever, but return if select is interrupted.
     74  1.1  cgd  *
     75  1.1  cgd  * Return 0 => no input, 1 => can read() from stdin
     76  1.1  cgd  */
     77  1.1  cgd int
     78  1.1  cgd rwait(tvp)
     79  1.1  cgd 	register struct timeval *tvp;
     80  1.1  cgd {
     81  1.1  cgd 	int i;
     82  1.1  cgd 	struct timeval starttv, endtv, *s;
     83  1.1  cgd 	extern int errno;
     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.1  cgd 	i = 1;
     99  1.1  cgd 	switch (select(1, (fd_set *)&i, (fd_set *)0, (fd_set *)0, s)) {
    100  1.1  cgd 
    101  1.1  cgd 	case -1:
    102  1.1  cgd 		if (tvp == 0)
    103  1.1  cgd 			return (-1);
    104  1.1  cgd 		if (errno == EINTR)
    105  1.1  cgd 			goto again;
    106  1.1  cgd 		stop("select failed, help");
    107  1.1  cgd 		/* NOTREACHED */
    108  1.1  cgd 
    109  1.1  cgd 	case 0:	/* timed out */
    110  1.1  cgd 		tvp->tv_sec = 0;
    111  1.1  cgd 		tvp->tv_usec = 0;
    112  1.1  cgd 		return (0);
    113  1.1  cgd 	}
    114  1.1  cgd 	if (tvp) {
    115  1.1  cgd 		/* since there is input, we may not have timed out */
    116  1.1  cgd 		(void) gettimeofday(&endtv, NILTZ);
    117  1.1  cgd 		TV_SUB(&endtv, &starttv);
    118  1.1  cgd 		TV_SUB(tvp, &endtv);	/* adjust *tvp by elapsed time */
    119  1.1  cgd 	}
    120  1.1  cgd 	return (1);
    121  1.1  cgd }
    122  1.1  cgd 
    123  1.1  cgd /*
    124  1.1  cgd  * `sleep' for the current turn time (using select).
    125  1.1  cgd  * Eat any input that might be available.
    126  1.1  cgd  */
    127  1.1  cgd void
    128  1.1  cgd tsleep()
    129  1.1  cgd {
    130  1.1  cgd 	struct timeval tv;
    131  1.1  cgd 	char c;
    132  1.1  cgd 
    133  1.1  cgd 	tv.tv_sec = 0;
    134  1.1  cgd 	tv.tv_usec = fallrate;
    135  1.1  cgd 	while (TV_POS(&tv))
    136  1.1  cgd 		if (rwait(&tv) && read(0, &c, 1) != 1)
    137  1.1  cgd 			break;
    138  1.1  cgd }
    139  1.1  cgd 
    140  1.1  cgd /*
    141  1.1  cgd  * Eat up any input (used at end of game).
    142  1.1  cgd  */
    143  1.1  cgd void
    144  1.1  cgd eat_input()
    145  1.1  cgd {
    146  1.1  cgd 	struct timeval tv;
    147  1.1  cgd 	char c;
    148  1.1  cgd 
    149  1.1  cgd 	do {
    150  1.1  cgd 		tv.tv_sec = tv.tv_usec = 0;
    151  1.1  cgd 	} while (rwait(&tv) && read(0, &c, 1) == 1);
    152  1.1  cgd }
    153  1.1  cgd 
    154  1.1  cgd /*
    155  1.1  cgd  * getchar with timeout.
    156  1.1  cgd  */
    157  1.1  cgd int
    158  1.1  cgd tgetchar()
    159  1.1  cgd {
    160  1.1  cgd 	static struct timeval timeleft;
    161  1.1  cgd 	char c;
    162  1.1  cgd 
    163  1.1  cgd 	/*
    164  1.1  cgd 	 * Reset timeleft to fallrate whenever it is not positive.
    165  1.1  cgd 	 * In any case, wait to see if there is any input.  If so,
    166  1.1  cgd 	 * take it, and update timeleft so that the next call to
    167  1.1  cgd 	 * tgetchar() will not wait as long.  If there is no input,
    168  1.1  cgd 	 * make timeleft zero or negative, and return -1.
    169  1.1  cgd 	 *
    170  1.1  cgd 	 * Most of the hard work is done by rwait().
    171  1.1  cgd 	 */
    172  1.1  cgd 	if (!TV_POS(&timeleft)) {
    173  1.1  cgd 		faster();	/* go faster */
    174  1.1  cgd 		timeleft.tv_sec = 0;
    175  1.1  cgd 		timeleft.tv_usec = fallrate;
    176  1.1  cgd 	}
    177  1.1  cgd 	if (!rwait(&timeleft))
    178  1.1  cgd 		return (-1);
    179  1.1  cgd 	if (read(0, &c, 1) != 1)
    180  1.1  cgd 		stop("end of file, help");
    181  1.1  cgd 	return ((int)(unsigned char)c);
    182  1.1  cgd }
    183