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