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