Rev 1472 | Rev 1524 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1472 | Rev 1519 | ||
---|---|---|---|
Line 37... | Line 37... | ||
37 | 37 | ||
38 | /* |
38 | /* |
39 | * Tetris screen control. |
39 | * Tetris screen control. |
40 | */ |
40 | */ |
41 | 41 | ||
42 | #include <sys/ioctl.h> |
- | |
43 | - | ||
44 | #include <err.h> |
42 | #include <err.h> |
45 | //#include <setjmp.h> |
- | |
46 | //#include <signal.h> |
- | |
47 | #include <stdio.h> |
43 | #include <stdio.h> |
48 | #include <stdlib.h> |
44 | #include <stdlib.h> |
49 | #include <string.h> |
45 | #include <string.h> |
50 | #include <term.h> |
- | |
51 | #include <termios.h> |
- | |
52 | #include <unistd.h> |
46 | #include <unistd.h> |
- | 47 | #include <io/stream.h> |
|
53 | 48 | ||
54 | #include "screen.h" |
49 | #include "screen.h" |
55 | #include "tetris.h" |
50 | #include "tetris.h" |
56 | 51 | ||
57 | static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */ |
52 | static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */ |
Line 132... | Line 127... | ||
132 | * count=1. (See screen.h for putpad().) |
127 | * count=1. (See screen.h for putpad().) |
133 | */ |
128 | */ |
134 | #define putstr(s) (void)fputs(s, stdout) |
129 | #define putstr(s) (void)fputs(s, stdout) |
135 | #define moveto(r, c) putpad(tgoto(CMstr, c, r)) |
130 | #define moveto(r, c) putpad(tgoto(CMstr, c, r)) |
136 | 131 | ||
- | 132 | static int con_phone; |
|
- | 133 | ||
137 | /* |
134 | /* |
138 | * Set up from termcap. |
135 | * Set up from termcap. |
139 | */ |
136 | */ |
140 | void |
137 | void |
141 | scr_init(void) |
138 | scr_init(void) |
142 | { |
139 | { |
143 | static int bsflag, xsflag, sgnum; |
- | |
144 | #ifdef unneeded |
- | |
145 | static int ncflag; |
- | |
146 | #endif |
- | |
147 | char *term, *fill; |
- | |
148 | static struct tcninfo { /* termcap numeric and flag info */ |
- | |
149 | char tcname[3]; |
- | |
150 | int *tcaddr; |
- | |
151 | } tcflags[] = { |
- | |
152 | {"bs", &bsflag}, |
- | |
153 | {"ms", &MSflag}, |
- | |
154 | #ifdef unneeded |
- | |
155 | {"nc", &ncflag}, |
- | |
156 | #endif |
- | |
157 | {"xs", &xsflag}, |
- | |
158 | { {0}, NULL} |
- | |
159 | }, tcnums[] = { |
- | |
160 | {"co", &COnum}, |
- | |
161 | {"li", &LInum}, |
- | |
162 | {"sg", &sgnum}, |
- | |
163 | { {0}, NULL} |
- | |
164 | }; |
- | |
165 | - | ||
166 | if ((term = getenv("TERM")) == NULL) |
- | |
167 | stop("you must set the TERM environment variable"); |
- | |
168 | if (tgetent(tbuf, term) <= 0) |
- | |
169 | stop("cannot find your termcap"); |
- | |
170 | fill = combuf; |
- | |
171 | { |
- | |
172 | struct tcsinfo *p; |
- | |
173 | - | ||
174 | for (p = tcstrings; p->tcaddr; p++) |
- | |
175 | *p->tcaddr = tgetstr(p->tcname, &fill); |
- | |
176 | } |
- | |
177 | if (classic) |
- | |
178 | SOstr = SEstr = NULL; |
- | |
179 | { |
- | |
180 | struct tcninfo *p; |
- | |
181 | - | ||
182 | for (p = tcflags; p->tcaddr; p++) |
- | |
183 | *p->tcaddr = tgetflag(p->tcname); |
- | |
184 | for (p = tcnums; p->tcaddr; p++) |
- | |
185 | *p->tcaddr = tgetnum(p->tcname); |
140 | con_phone = get_fd_phone(1); |
186 | } |
- | |
187 | if (bsflag) |
- | |
188 | BC = "\b"; |
- | |
189 | else if (BC == NULL && bcstr != NULL) |
- | |
190 | BC = bcstr; |
- | |
191 | if (CLstr == NULL) |
- | |
192 | stop("cannot clear screen"); |
- | |
193 | if (CMstr == NULL || UP == NULL || BC == NULL) |
- | |
194 | stop("cannot do random cursor positioning via tgoto()"); |
- | |
195 | PC = pcstr ? *pcstr : 0; |
- | |
196 | if (sgnum > 0 || xsflag) |
- | |
197 | SOstr = SEstr = NULL; |
- | |
198 | #ifdef unneeded |
- | |
199 | if (ncflag) |
- | |
200 | CRstr = NULL; |
- | |
201 | else if (CRstr == NULL) |
- | |
202 | CRstr = "\r"; |
- | |
203 | #endif |
- | |
204 | } |
141 | } |
205 | 142 | ||
206 | /* this foolery is needed to modify tty state `atomically' */ |
- | |
207 | //static jmp_buf scr_onstop; |
- | |
208 | - | ||
209 | /* static void */ |
- | |
210 | /* stopset(int sig) */ |
- | |
211 | /* { */ |
- | |
212 | /* sigset_t sigset; */ |
- | |
213 | - | ||
214 | /* (void) signal(sig, SIG_DFL); */ |
- | |
215 | /* (void) kill(getpid(), sig); */ |
- | |
216 | /* sigemptyset(&sigset); */ |
- | |
217 | /* sigaddset(&sigset, sig); */ |
- | |
218 | /* (void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0); */ |
- | |
219 | /* longjmp(scr_onstop, 1); */ |
- | |
220 | /* } */ |
- | |
221 | 143 | ||
222 | static void |
144 | static void |
223 | scr_stop(int sig) |
145 | scr_stop(int sig) |
224 | { |
146 | { |
225 | // sigset_t sigset; |
- | |
226 | 147 | ||
227 | scr_end(); |
148 | scr_end(); |
228 | /* (void) kill(getpid(), sig); */ |
- | |
229 | /* sigemptyset(&sigset); */ |
- | |
230 | /* sigaddset(&sigset, sig); */ |
- | |
231 | /* (void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0); */ |
- | |
232 | scr_set(); |
149 | scr_set(); |
233 | scr_msg(key_msg, 1); |
150 | scr_msg(key_msg, 1); |
234 | } |
151 | } |
235 | 152 | ||
236 | /* |
153 | /* |
Line 239... | Line 156... | ||
239 | void |
156 | void |
240 | scr_set(void) |
157 | scr_set(void) |
241 | { |
158 | { |
242 | struct winsize ws; |
159 | struct winsize ws; |
243 | struct termios newtt; |
160 | struct termios newtt; |
244 | // sigset_t sigset, osigset; |
- | |
245 | void (*ttou)(int); |
161 | void (*ttou)(int); |
246 | 162 | ||
247 | /* sigemptyset(&sigset); */ |
- | |
248 | /* sigaddset(&sigset, SIGTSTP); */ |
- | |
249 | /* sigaddset(&sigset, SIGTTOU); */ |
- | |
250 | /* (void) sigprocmask(SIG_BLOCK, &sigset, &osigset); */ |
- | |
251 | /* if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN) */ |
- | |
252 | /* (void) signal(SIGTSTP, SIG_IGN); */ |
- | |
253 | /* if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN) */ |
- | |
254 | /* (void) signal(SIGTTOU, SIG_IGN); */ |
- | |
255 | /* /\* */ |
- | |
256 | /* * At last, we are ready to modify the tty state. If */ |
- | |
257 | /* * we stop while at it, stopset() above will longjmp back */ |
- | |
258 | /* * to the setjmp here and we will start over. */ |
- | |
259 | /* *\/ */ |
- | |
260 | /* (void) setjmp(scr_onstop); */ |
- | |
261 | /* (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); */ |
- | |
262 | Rows = 0, Cols = 0; |
163 | Rows = 0, Cols = 0; |
263 | if (ioctl(0, TIOCGWINSZ, &ws) == 0) { |
164 | if (ioctl(0, TIOCGWINSZ, &ws) == 0) { |
264 | Rows = ws.ws_row; |
165 | Rows = ws.ws_row; |
265 | Cols = ws.ws_col; |
166 | Cols = ws.ws_col; |
266 | } |
167 | } |
Line 281... | Line 182... | ||
281 | newtt = oldtt; |
182 | newtt = oldtt; |
282 | newtt.c_lflag &= ~(ICANON|ECHO); |
183 | newtt.c_lflag &= ~(ICANON|ECHO); |
283 | newtt.c_oflag &= ~OXTABS; |
184 | newtt.c_oflag &= ~OXTABS; |
284 | if (tcsetattr(0, TCSADRAIN, &newtt) < 0) |
185 | if (tcsetattr(0, TCSADRAIN, &newtt) < 0) |
285 | stop("tcsetattr() fails"); |
186 | stop("tcsetattr() fails"); |
286 | /* (void) sigprocmask(SIG_BLOCK, &sigset, &osigset); */ |
- | |
287 | 187 | ||
288 | /* |
188 | /* |
289 | * We made it. We are now in screen mode, modulo TIstr |
189 | * We made it. We are now in screen mode, modulo TIstr |
290 | * (which we will fix immediately). |
190 | * (which we will fix immediately). |
291 | */ |
191 | */ |
292 | if (TIstr) |
192 | if (TIstr) |
293 | putstr(TIstr); /* termcap(5) says this is not padded */ |
193 | putstr(TIstr); /* termcap(5) says this is not padded */ |
294 | /* if (tstp != SIG_IGN) */ |
- | |
295 | /* (void) signal(SIGTSTP, scr_stop); */ |
- | |
296 | /* if (ttou != SIG_IGN) */ |
- | |
297 | /* (void) signal(SIGTTOU, ttou); */ |
- | |
298 | 194 | ||
299 | isset = 1; |
195 | isset = 1; |
300 | // (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); |
- | |
- | 196 | ||
301 | scr_clear(); |
197 | scr_clear(); |
302 | } |
198 | } |
303 | 199 | ||
304 | /* |
200 | /* |
305 | * End screen mode. |
201 | * End screen mode. |
306 | */ |
202 | */ |
307 | void |
203 | void |
308 | scr_end(void) |
204 | scr_end(void) |
309 | { |
205 | { |
310 | // sigset_t sigset, osigset; |
- | |
311 | - | ||
312 | /* sigemptyset(&sigset); */ |
- | |
313 | /* sigaddset(&sigset, SIGTSTP); */ |
- | |
314 | /* sigaddset(&sigset, SIGTTOU); */ |
- | |
315 | /* (void) sigprocmask(SIG_BLOCK, &sigset, &osigset); */ |
- | |
316 | /* move cursor to last line */ |
- | |
317 | if (LLstr) |
- | |
318 | putstr(LLstr); /* termcap(5) says this is not padded */ |
- | |
319 | else |
- | |
320 | moveto(Rows - 1, 0); |
- | |
321 | /* exit screen mode */ |
- | |
322 | if (TEstr) |
- | |
323 | putstr(TEstr); /* termcap(5) says this is not padded */ |
- | |
324 | // (void) fflush(stdout); |
- | |
325 | (void) tcsetattr(0, TCSADRAIN, &oldtt); |
- | |
326 | isset = 0; |
- | |
327 | /* restore signals */ |
- | |
328 | /* (void) signal(SIGTSTP, tstp); */ |
- | |
329 | /* (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0); */ |
- | |
330 | } |
206 | } |
331 | 207 | ||
332 | void |
208 | void |
333 | stop(char *why) |
209 | stop(char *why) |
334 | { |
210 | { |
Line 363... | Line 239... | ||
363 | scr_update(void) |
239 | scr_update(void) |
364 | { |
240 | { |
365 | cell *bp, *sp; |
241 | cell *bp, *sp; |
366 | regcell so, cur_so = 0; |
242 | regcell so, cur_so = 0; |
367 | int i, ccol, j; |
243 | int i, ccol, j; |
368 | // sigset_t sigset, osigset; |
- | |
369 | static const struct shape *lastshape; |
244 | static const struct shape *lastshape; |
370 | 245 | ||
371 | /* sigemptyset(&sigset); */ |
- | |
372 | /* sigaddset(&sigset, SIGTSTP); */ |
- | |
373 | /* (void) sigprocmask(SIG_BLOCK, &sigset, &osigset); */ |
- | |
374 | - | ||
375 | /* always leave cursor after last displayed point */ |
246 | /* always leave cursor after last displayed point */ |
376 | curscreen[D_LAST * B_COLS - 1] = -1; |
247 | curscreen[D_LAST * B_COLS - 1] = -1; |
377 | 248 | ||
378 | if (score != curscore) { |
249 | if (score != curscore) { |
379 | if (HOstr) |
250 | if (HOstr) |