]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/input.c
1 /* $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)input.c 8.1 (Berkeley) 5/31/93
45 #include <sys/types.h>
54 /* return true iff the given timeval is positive */
56 ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
58 /* subtract timeval `sub' from `res' */
59 #define TV_SUB(res, sub) \
60 (res)->tv_sec -= (sub)->tv_sec; \
61 (res)->tv_usec -= (sub)->tv_usec; \
62 if ((res)->tv_usec < 0) { \
63 (res)->tv_usec += 1000000; \
68 * Do a `read wait': select for reading from stdin, with timeout *tvp.
69 * On return, modify *tvp to reflect the amount of time spent waiting.
70 * It will be positive only if input appeared before the time ran out;
71 * otherwise it will be zero or perhaps negative.
73 * If tvp is nil, wait forever, but return if select is interrupted.
75 * Return 0 => no input, 1 => can read() from stdin
79 register struct timeval
*tvp
;
82 struct timeval starttv
, endtv
, *s
;
83 #define NILTZ ((struct timezone *)0)
86 * Someday, select() will do this for us.
87 * Just in case that day is now, and no one has
88 * changed this, we use a temporary.
91 (void) gettimeofday(&starttv
, NILTZ
);
98 switch (select(1, (fd_set
*)&i
, (fd_set
*)0, (fd_set
*)0, s
)) {
105 stop("select failed, help");
108 case 0: /* timed out */
114 /* since there is input, we may not have timed out */
115 (void) gettimeofday(&endtv
, NILTZ
);
116 TV_SUB(&endtv
, &starttv
);
117 TV_SUB(tvp
, &endtv
); /* adjust *tvp by elapsed time */
123 * `sleep' for the current turn time (using select).
124 * Eat any input that might be available.
133 tv
.tv_usec
= fallrate
;
135 if (rwait(&tv
) && read(0, &c
, 1) != 1)
140 * Eat up any input (used at end of game).
149 tv
.tv_sec
= tv
.tv_usec
= 0;
150 } while (rwait(&tv
) && read(0, &c
, 1) == 1);
154 * getchar with timeout.
159 static struct timeval timeleft
;
163 * Reset timeleft to fallrate whenever it is not positive.
164 * In any case, wait to see if there is any input. If so,
165 * take it, and update timeleft so that the next call to
166 * tgetchar() will not wait as long. If there is no input,
167 * make timeleft zero or negative, and return -1.
169 * Most of the hard work is done by rwait().
171 if (!TV_POS(&timeleft
)) {
172 faster(); /* go faster */
174 timeleft
.tv_usec
= fallrate
;
176 if (!rwait(&timeleft
))
178 if (read(0, &c
, 1) != 1)
179 stop("end of file, help");
180 return ((int)(unsigned char)c
);