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