Blob


1 /* $NetBSD: input.c,v 1.11 2009/05/25 04:33:53 dholland Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)input.c 8.1 (Berkeley) 5/31/93
35 */
37 /*
38 * Tetris input.
39 */
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <sys/poll.h>
45 #include <errno.h>
46 #include <unistd.h>
48 #include "input.h"
49 #include "tetris.h"
51 /* return true iff the given timeval is positive */
52 #define TV_POS(tv) \
53 ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
55 /* subtract timeval `sub' from `res' */
56 #define TV_SUB(res, sub) \
57 (res)->tv_sec -= (sub)->tv_sec; \
58 (res)->tv_usec -= (sub)->tv_usec; \
59 if ((res)->tv_usec < 0) { \
60 (res)->tv_usec += 1000000; \
61 (res)->tv_sec--; \
62 }
64 /*
65 * Do a `read wait': poll for reading from stdin, with timeout *tvp.
66 * On return, modify *tvp to reflect the amount of time spent waiting.
67 * It will be positive only if input appeared before the time ran out;
68 * otherwise it will be zero or perhaps negative.
69 *
70 * If tvp is nil, wait forever, but return if poll is interrupted.
71 *
72 * Return 0 => no input, 1 => can read() from stdin
73 */
74 int
75 rwait(struct timeval *tvp)
76 {
77 struct pollfd set[1];
78 struct timeval starttv, endtv;
79 int timeout;
80 #define NILTZ ((struct timezone *)0)
82 if (tvp) {
83 (void) gettimeofday(&starttv, NILTZ);
84 endtv = *tvp;
85 timeout = tvp->tv_sec * 1000 + tvp->tv_usec / 1000;
86 } else
87 timeout = INFTIM;
88 again:
89 set[0].fd = STDIN_FILENO;
90 set[0].events = POLLIN;
91 switch (poll(set, 1, timeout)) {
93 case -1:
94 if (tvp == 0)
95 return (-1);
96 if (errno == EINTR)
97 goto again;
98 stop("poll failed, help");
99 /* NOTREACHED */
101 case 0: /* timed out */
102 if (tvp) {
103 tvp->tv_sec = 0;
104 tvp->tv_usec = 0;
106 return (0);
108 if (tvp) {
109 /* since there is input, we may not have timed out */
110 (void) gettimeofday(&endtv, NILTZ);
111 TV_SUB(&endtv, &starttv);
112 TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */
114 return (1);
117 /*
118 * `sleep' for the current turn time.
119 * Eat any input that might be available.
120 */
121 void
122 tsleep(void)
124 struct timeval tv;
125 char c;
127 tv.tv_sec = 0;
128 tv.tv_usec = fallrate;
129 while (TV_POS(&tv))
130 if (rwait(&tv) && read(0, &c, 1) != 1)
131 break;
134 /*
135 * getchar with timeout.
136 */
137 int
138 tgetchar(void)
140 static struct timeval timeleft;
141 char c;
143 /*
144 * Reset timeleft to fallrate whenever it is not positive.
145 * In any case, wait to see if there is any input. If so,
146 * take it, and update timeleft so that the next call to
147 * tgetchar() will not wait as long. If there is no input,
148 * make timeleft zero or negative, and return -1.
150 * Most of the hard work is done by rwait().
151 */
152 if (!TV_POS(&timeleft)) {
153 faster(); /* go faster */
154 timeleft.tv_sec = 0;
155 timeleft.tv_usec = fallrate;
157 if (!rwait(&timeleft))
158 return (-1);
159 if (read(0, &c, 1) != 1)
160 stop("end of file, help");
161 return ((int)(unsigned char)c);