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 #ifndef INFTIM
52 # define INFTIM (-1)
53 #endif
55 /* return true iff the given timeval is positive */
56 #define TV_POS(tv) \
57 ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
59 /* subtract timeval `sub' from `res' */
60 #define TV_SUB(res, sub) \
61 (res)->tv_sec -= (sub)->tv_sec; \
62 (res)->tv_usec -= (sub)->tv_usec; \
63 if ((res)->tv_usec < 0) { \
64 (res)->tv_usec += 1000000; \
65 (res)->tv_sec--; \
66 }
68 /*
69 * Do a `read wait': poll for reading from stdin, with timeout *tvp.
70 * On return, modify *tvp to reflect the amount of time spent waiting.
71 * It will be positive only if input appeared before the time ran out;
72 * otherwise it will be zero or perhaps negative.
73 *
74 * If tvp is nil, wait forever, but return if poll is interrupted.
75 *
76 * Return 0 => no input, 1 => can read() from stdin
77 */
78 int
79 rwait(struct timeval *tvp)
80 {
81 struct pollfd set[1];
82 struct timeval starttv, endtv;
83 int timeout;
84 #define NILTZ ((struct timezone *)0)
86 if (tvp) {
87 (void) gettimeofday(&starttv, NILTZ);
88 endtv = *tvp;
89 timeout = tvp->tv_sec * 1000 + tvp->tv_usec / 1000;
90 } else
91 timeout = INFTIM;
92 again:
93 set[0].fd = STDIN_FILENO;
94 set[0].events = POLLIN;
95 switch (poll(set, 1, timeout)) {
97 case -1:
98 if (tvp == 0)
99 return (-1);
100 if (errno == EINTR)
101 goto again;
102 stop("poll failed, help");
103 /* NOTREACHED */
105 case 0: /* timed out */
106 if (tvp) {
107 tvp->tv_sec = 0;
108 tvp->tv_usec = 0;
110 return (0);
112 if (tvp) {
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 */
118 return (1);
121 /*
122 * `sleep' for the current turn time.
123 * Eat any input that might be available.
124 */
125 void
126 tsleep(void)
128 struct timeval tv;
129 char c;
131 tv.tv_sec = 0;
132 tv.tv_usec = fallrate;
133 while (TV_POS(&tv))
134 if (rwait(&tv) && read(0, &c, 1) != 1)
135 break;
138 /*
139 * getchar with timeout.
140 */
141 int
142 tgetchar(void)
144 static struct timeval timeleft;
145 char c;
147 /*
148 * Reset timeleft to fallrate whenever it is not positive.
149 * In any case, wait to see if there is any input. If so,
150 * take it, and update timeleft so that the next call to
151 * tgetchar() will not wait as long. If there is no input,
152 * make timeleft zero or negative, and return -1.
154 * Most of the hard work is done by rwait().
155 */
156 if (!TV_POS(&timeleft)) {
157 faster(); /* go faster */
158 timeleft.tv_sec = 0;
159 timeleft.tv_usec = fallrate;
161 if (!rwait(&timeleft))
162 return (-1);
163 if (read(0, &c, 1) != 1)
164 stop("end of file, help");
165 return ((int)(unsigned char)c);