Rev 4055 | Rev 4420 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4055 | Rev 4156 | ||
---|---|---|---|
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 | /** @addtogroup tetris |
38 | /** @addtogroup tetris |
39 | * @{ |
39 | * @{ |
40 | */ |
40 | */ |
41 | /** @file |
41 | /** @file |
42 | */ |
42 | */ |
43 | 43 | ||
44 | /* |
44 | /* |
45 | * Tetris input. |
45 | * Tetris input. |
46 | */ |
46 | */ |
47 | 47 | ||
48 | #include <sys/types.h> |
48 | #include <sys/types.h> |
49 | #include <sys/time.h> |
49 | #include <sys/time.h> |
50 | #include <stdio.h> |
50 | #include <stdio.h> |
51 | 51 | ||
52 | #include <errno.h> |
52 | #include <errno.h> |
53 | #include <unistd.h> |
53 | #include <unistd.h> |
54 | #include <string.h> |
54 | #include <string.h> |
55 | 55 | ||
56 | #include "input.h" |
56 | #include "input.h" |
57 | #include "tetris.h" |
57 | #include "tetris.h" |
58 | 58 | ||
59 | #include <async.h> |
59 | #include <async.h> |
60 | #include <ipc/console.h> |
60 | #include <ipc/console.h> |
- | 61 | #include <console.h> |
|
61 | #include <kbd/kbd.h> |
62 | #include <kbd/kbd.h> |
62 | 63 | ||
63 | /* return true iff the given timeval is positive */ |
64 | /* return true iff the given timeval is positive */ |
64 | #define TV_POS(tv) \ |
65 | #define TV_POS(tv) \ |
65 | ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0)) |
66 | ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0)) |
66 | 67 | ||
67 | /* subtract timeval `sub' from `res' */ |
68 | /* subtract timeval `sub' from `res' */ |
68 | #define TV_SUB(res, sub) \ |
69 | #define TV_SUB(res, sub) \ |
69 | (res)->tv_sec -= (sub)->tv_sec; \ |
70 | (res)->tv_sec -= (sub)->tv_sec; \ |
70 | (res)->tv_usec -= (sub)->tv_usec; \ |
71 | (res)->tv_usec -= (sub)->tv_usec; \ |
71 | if ((res)->tv_usec < 0) { \ |
72 | if ((res)->tv_usec < 0) { \ |
72 | (res)->tv_usec += 1000000; \ |
73 | (res)->tv_usec += 1000000; \ |
73 | (res)->tv_sec--; \ |
74 | (res)->tv_sec--; \ |
74 | } |
75 | } |
75 | 76 | ||
76 | /* We will use a hack here - if lastchar is non-zero, it is |
77 | /* We will use a hack here - if lastchar is non-zero, it is |
77 | * the last character read. We will somehow simulate the select |
78 | * the last character read. We will somehow simulate the select |
78 | * semantics. |
79 | * semantics. |
79 | */ |
80 | */ |
80 | static aid_t getchar_inprog = 0; |
81 | static aid_t getchar_inprog = 0; |
81 | static char lastchar = '\0'; |
82 | static char lastchar = '\0'; |
82 | 83 | ||
83 | /* |
84 | /* |
84 | * Do a `read wait': select for reading from stdin, with timeout *tvp. |
85 | * Do a `read wait': select for reading from stdin, with timeout *tvp. |
85 | * On return, modify *tvp to reflect the amount of time spent waiting. |
86 | * On return, modify *tvp to reflect the amount of time spent waiting. |
86 | * It will be positive only if input appeared before the time ran out; |
87 | * It will be positive only if input appeared before the time ran out; |
87 | * otherwise it will be zero or perhaps negative. |
88 | * otherwise it will be zero or perhaps negative. |
88 | * |
89 | * |
89 | * If tvp is nil, wait forever, but return if select is interrupted. |
90 | * If tvp is nil, wait forever, but return if select is interrupted. |
90 | * |
91 | * |
91 | * Return 0 => no input, 1 => can read() from stdin |
92 | * Return 0 => no input, 1 => can read() from stdin |
92 | * |
93 | * |
93 | */ |
94 | */ |
94 | int |
95 | int |
95 | rwait(struct timeval *tvp) |
96 | rwait(struct timeval *tvp) |
96 | { |
97 | { |
97 | struct timeval starttv, endtv, *s; |
98 | struct timeval starttv, endtv, *s; |
98 | static ipc_call_t charcall; |
99 | static ipc_call_t charcall; |
99 | ipcarg_t rc; |
100 | ipcarg_t rc; |
100 | int cons_phone; |
101 | int cons_phone; |
101 | 102 | ||
102 | /* |
103 | /* |
103 | * Someday, select() will do this for us. |
104 | * Someday, select() will do this for us. |
104 | * Just in case that day is now, and no one has |
105 | * Just in case that day is now, and no one has |
105 | * changed this, we use a temporary. |
106 | * changed this, we use a temporary. |
106 | */ |
107 | */ |
107 | if (tvp) { |
108 | if (tvp) { |
108 | (void) gettimeofday(&starttv, NULL); |
109 | (void) gettimeofday(&starttv, NULL); |
109 | endtv = *tvp; |
110 | endtv = *tvp; |
110 | s = &endtv; |
111 | s = &endtv; |
111 | } else |
112 | } else |
112 | s = NULL; |
113 | s = NULL; |
113 | 114 | ||
114 | if (!lastchar) { |
115 | if (!lastchar) { |
115 | again: |
116 | again: |
116 | if (!getchar_inprog) { |
117 | if (!getchar_inprog) { |
117 | cons_phone = get_console_phone(); |
118 | cons_phone = console_phone_get(); |
118 | getchar_inprog = async_send_2(cons_phone, |
119 | getchar_inprog = async_send_2(cons_phone, |
119 | CONSOLE_GETKEY, 0, 0, &charcall); |
120 | CONSOLE_GETKEY, 0, 0, &charcall); |
120 | } |
121 | } |
121 | if (!s) |
122 | if (!s) |
122 | async_wait_for(getchar_inprog, &rc); |
123 | async_wait_for(getchar_inprog, &rc); |
123 | else if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) { |
124 | else if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) { |
124 | tvp->tv_sec = 0; |
125 | tvp->tv_sec = 0; |
125 | tvp->tv_usec = 0; |
126 | tvp->tv_usec = 0; |
126 | return (0); |
127 | return (0); |
127 | } |
128 | } |
128 | getchar_inprog = 0; |
129 | getchar_inprog = 0; |
129 | if (rc) { |
130 | if (rc) { |
130 | stop("end of file, help"); |
131 | stop("end of file, help"); |
131 | } |
132 | } |
132 | if (IPC_GET_ARG1(charcall) == KE_RELEASE) |
133 | if (IPC_GET_ARG1(charcall) == KE_RELEASE) |
133 | goto again; |
134 | goto again; |
134 | 135 | ||
135 | lastchar = IPC_GET_ARG4(charcall); |
136 | lastchar = IPC_GET_ARG4(charcall); |
136 | } |
137 | } |
137 | if (tvp) { |
138 | if (tvp) { |
138 | /* since there is input, we may not have timed out */ |
139 | /* since there is input, we may not have timed out */ |
139 | (void) gettimeofday(&endtv, NULL); |
140 | (void) gettimeofday(&endtv, NULL); |
140 | TV_SUB(&endtv, &starttv); |
141 | TV_SUB(&endtv, &starttv); |
141 | TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */ |
142 | TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */ |
142 | } |
143 | } |
143 | return (1); |
144 | return (1); |
144 | } |
145 | } |
145 | 146 | ||
146 | /* |
147 | /* |
147 | * `sleep' for the current turn time (using select). |
148 | * `sleep' for the current turn time (using select). |
148 | * Eat any input that might be available. |
149 | * Eat any input that might be available. |
149 | */ |
150 | */ |
150 | void |
151 | void |
151 | tsleep(void) |
152 | tsleep(void) |
152 | { |
153 | { |
153 | struct timeval tv; |
154 | struct timeval tv; |
154 | 155 | ||
155 | tv.tv_sec = 0; |
156 | tv.tv_sec = 0; |
156 | tv.tv_usec = fallrate; |
157 | tv.tv_usec = fallrate; |
157 | while (TV_POS(&tv)) |
158 | while (TV_POS(&tv)) |
158 | if (rwait(&tv)) { |
159 | if (rwait(&tv)) { |
159 | lastchar = '\0'; |
160 | lastchar = '\0'; |
160 | } else |
161 | } else |
161 | break; |
162 | break; |
162 | } |
163 | } |
163 | 164 | ||
164 | /* |
165 | /* |
165 | * getchar with timeout. |
166 | * getchar with timeout. |
166 | */ |
167 | */ |
167 | int |
168 | int |
168 | tgetchar(void) |
169 | tgetchar(void) |
169 | { |
170 | { |
170 | static struct timeval timeleft; |
171 | static struct timeval timeleft; |
171 | char c; |
172 | char c; |
172 | 173 | ||
173 | /* |
174 | /* |
174 | * Reset timeleft to fallrate whenever it is not positive. |
175 | * Reset timeleft to fallrate whenever it is not positive. |
175 | * In any case, wait to see if there is any input. If so, |
176 | * In any case, wait to see if there is any input. If so, |
176 | * take it, and update timeleft so that the next call to |
177 | * take it, and update timeleft so that the next call to |
177 | * tgetchar() will not wait as long. If there is no input, |
178 | * tgetchar() will not wait as long. If there is no input, |
178 | * make timeleft zero or negative, and return -1. |
179 | * make timeleft zero or negative, and return -1. |
179 | * |
180 | * |
180 | * Most of the hard work is done by rwait(). |
181 | * Most of the hard work is done by rwait(). |
181 | */ |
182 | */ |
182 | if (!TV_POS(&timeleft)) { |
183 | if (!TV_POS(&timeleft)) { |
183 | faster(); /* go faster */ |
184 | faster(); /* go faster */ |
184 | timeleft.tv_sec = 0; |
185 | timeleft.tv_sec = 0; |
185 | timeleft.tv_usec = fallrate; |
186 | timeleft.tv_usec = fallrate; |
186 | } |
187 | } |
187 | if (!rwait(&timeleft)) |
188 | if (!rwait(&timeleft)) |
188 | return (-1); |
189 | return (-1); |
189 | c = lastchar; |
190 | c = lastchar; |
190 | lastchar = '\0'; |
191 | lastchar = '\0'; |
191 | return ((int)(unsigned char)c); |
192 | return ((int)(unsigned char)c); |
192 | } |
193 | } |
193 | 194 | ||
194 | /** @} |
195 | /** @} |
195 | */ |
196 | */ |
196 | 197 | ||
197 | 198 |