xref: /openbsd-src/games/tetris/input.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: input.c,v 1.18 2016/08/27 02:02:44 guenther 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/time.h>
43 #include <errno.h>
44 #include <poll.h>
45 #include <unistd.h>
46 
47 #include "input.h"
48 #include "tetris.h"
49 
50 /* return true iff the given timeval is positive */
51 #define	TV_POS(tv) \
52 	((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
53 
54 /* subtract timeval `sub' from `res' */
55 #define	TV_SUB(res, sub) \
56 	(res)->tv_sec -= (sub)->tv_sec; \
57 	(res)->tv_usec -= (sub)->tv_usec; \
58 	if ((res)->tv_usec < 0) { \
59 		(res)->tv_usec += 1000000; \
60 		(res)->tv_sec--; \
61 	}
62 
63 /*
64  * Do a `read wait': poll for reading from stdin, with timeout *tvp.
65  * On return, modify *tvp to reflect the amount of time spent waiting.
66  * It will be positive only if input appeared before the time ran out;
67  * otherwise it will be zero or perhaps negative.
68  *
69  * If tvp is nil, wait forever, but return if poll is interrupted.
70  *
71  * Return 0 => no input, 1 => can read() from stdin
72  */
73 int
74 rwait(struct timeval *tvp)
75 {
76 	int	timo = INFTIM;
77 	struct timeval starttv, endtv;
78 	struct pollfd pfd[1];
79 
80 #define	NILTZ ((struct timezone *)0)
81 
82 	if (tvp) {
83 		(void) gettimeofday(&starttv, NILTZ);
84 		endtv = *tvp;
85 		timo = endtv.tv_sec * 1000 + endtv.tv_usec / 1000;
86 	}
87 again:
88 	pfd[0].fd = STDIN_FILENO;
89 	pfd[0].events = POLLIN;
90 	switch (poll(pfd, 1, timo)) {
91 	case -1:
92 		if (tvp == 0)
93 			return (-1);
94 		if (errno == EINTR)
95 			goto again;
96 		stop("poll failed, help");
97 
98 	case 0:	/* timed out */
99 		tvp->tv_sec = 0;
100 		tvp->tv_usec = 0;
101 		return (0);
102 	}
103 	if (tvp) {
104 		/* since there is input, we may not have timed out */
105 		(void) gettimeofday(&endtv, NILTZ);
106 		TV_SUB(&endtv, &starttv);
107 		TV_SUB(tvp, &endtv);	/* adjust *tvp by elapsed time */
108 	}
109 	return (1);
110 }
111 
112 /*
113  * `sleep' for the current turn time (using poll).
114  * Eat any input that might be available.
115  */
116 void
117 tsleep(void)
118 {
119 	struct timeval tv;
120 	char c;
121 
122 	tv.tv_sec = 0;
123 	tv.tv_usec = fallrate;
124 	while (TV_POS(&tv))
125 		if (rwait(&tv) && read(STDIN_FILENO, &c, 1) != 1)
126 			break;
127 }
128 
129 /*
130  * getchar with timeout.
131  */
132 int
133 tgetchar(void)
134 {
135 	static struct timeval timeleft;
136 	char c;
137 
138 	/*
139 	 * Reset timeleft to fallrate whenever it is not positive.
140 	 * In any case, wait to see if there is any input.  If so,
141 	 * take it, and update timeleft so that the next call to
142 	 * tgetchar() will not wait as long.  If there is no input,
143 	 * make timeleft zero or negative, and return -1.
144 	 *
145 	 * Most of the hard work is done by rwait().
146 	 */
147 	if (!TV_POS(&timeleft)) {
148 		faster();	/* go faster */
149 		timeleft.tv_sec = 0;
150 		timeleft.tv_usec = fallrate;
151 	}
152 	if (!rwait(&timeleft))
153 		return (-1);
154 	if (read(STDIN_FILENO, &c, 1) != 1)
155 		stop("end of file, help");
156 	return ((int)(unsigned char)c);
157 }
158