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