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