screen.c revision 1.1 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1992, 1993
3 1.1 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Chris Torek and Darren F. Provine.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.1 cgd * @(#)screen.c 8.1 (Berkeley) 5/31/93
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd /*
40 1.1 cgd * Tetris screen control.
41 1.1 cgd */
42 1.1 cgd
43 1.1 cgd #include <sgtty.h>
44 1.1 cgd #include <sys/ioctl.h>
45 1.1 cgd
46 1.1 cgd #include <setjmp.h>
47 1.1 cgd #include <signal.h>
48 1.1 cgd #include <stdio.h>
49 1.1 cgd #include <stdlib.h>
50 1.1 cgd #include <string.h>
51 1.1 cgd #include <unistd.h>
52 1.1 cgd
53 1.1 cgd #ifndef sigmask
54 1.1 cgd #define sigmask(s) (1 << ((s) - 1))
55 1.1 cgd #endif
56 1.1 cgd
57 1.1 cgd #include "screen.h"
58 1.1 cgd #include "tetris.h"
59 1.1 cgd
60 1.1 cgd /*
61 1.1 cgd * XXX - need a <termcap.h>
62 1.1 cgd */
63 1.1 cgd int tgetent __P((char *, const char *));
64 1.1 cgd int tgetflag __P((const char *));
65 1.1 cgd int tgetnum __P((const char *));
66 1.1 cgd int tputs __P((const char *, int, int (*)(int)));
67 1.1 cgd
68 1.1 cgd static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */
69 1.1 cgd static int curscore;
70 1.1 cgd static int isset; /* true => terminal is in game mode */
71 1.1 cgd static struct sgttyb oldtt;
72 1.1 cgd static void (*tstp)();
73 1.1 cgd
74 1.1 cgd char *tgetstr(), *tgoto();
75 1.1 cgd
76 1.1 cgd
77 1.1 cgd /*
78 1.1 cgd * Capabilities from TERMCAP.
79 1.1 cgd */
80 1.1 cgd char PC, *BC, *UP; /* tgoto requires globals: ugh! */
81 1.1 cgd short ospeed;
82 1.1 cgd
83 1.1 cgd static char
84 1.1 cgd *bcstr, /* backspace char */
85 1.1 cgd *CEstr, /* clear to end of line */
86 1.1 cgd *CLstr, /* clear screen */
87 1.1 cgd *CMstr, /* cursor motion string */
88 1.1 cgd #ifdef unneeded
89 1.1 cgd *CRstr, /* "\r" equivalent */
90 1.1 cgd #endif
91 1.1 cgd *HOstr, /* cursor home */
92 1.1 cgd *LLstr, /* last line, first column */
93 1.1 cgd *pcstr, /* pad character */
94 1.1 cgd *TEstr, /* end cursor motion mode */
95 1.1 cgd *TIstr; /* begin cursor motion mode */
96 1.1 cgd char
97 1.1 cgd *SEstr, /* end standout mode */
98 1.1 cgd *SOstr; /* begin standout mode */
99 1.1 cgd static int
100 1.1 cgd COnum, /* co# value */
101 1.1 cgd LInum, /* li# value */
102 1.1 cgd MSflag; /* can move in standout mode */
103 1.1 cgd
104 1.1 cgd
105 1.1 cgd struct tcsinfo { /* termcap string info; some abbrevs above */
106 1.1 cgd char tcname[3];
107 1.1 cgd char **tcaddr;
108 1.1 cgd } tcstrings[] = {
109 1.1 cgd "bc", &bcstr,
110 1.1 cgd "ce", &CEstr,
111 1.1 cgd "cl", &CLstr,
112 1.1 cgd "cm", &CMstr,
113 1.1 cgd #ifdef unneeded
114 1.1 cgd "cr", &CRstr,
115 1.1 cgd #endif
116 1.1 cgd "le", &BC, /* move cursor left one space */
117 1.1 cgd "pc", &pcstr,
118 1.1 cgd "se", &SEstr,
119 1.1 cgd "so", &SOstr,
120 1.1 cgd "te", &TEstr,
121 1.1 cgd "ti", &TIstr,
122 1.1 cgd "up", &UP, /* cursor up */
123 1.1 cgd 0
124 1.1 cgd };
125 1.1 cgd
126 1.1 cgd /* This is where we will actually stuff the information */
127 1.1 cgd
128 1.1 cgd static char combuf[1024], tbuf[1024];
129 1.1 cgd
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * Routine used by tputs().
133 1.1 cgd */
134 1.1 cgd int
135 1.1 cgd put(c)
136 1.1 cgd int c;
137 1.1 cgd {
138 1.1 cgd
139 1.1 cgd return (putchar(c));
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd /*
143 1.1 cgd * putstr() is for unpadded strings (either as in termcap(5) or
144 1.1 cgd * simply literal strings); putpad() is for padded strings with
145 1.1 cgd * count=1. (See screen.h for putpad().)
146 1.1 cgd */
147 1.1 cgd #define putstr(s) (void)fputs(s, stdout)
148 1.1 cgd #define moveto(r, c) putpad(tgoto(CMstr, c, r))
149 1.1 cgd
150 1.1 cgd /*
151 1.1 cgd * Set up from termcap.
152 1.1 cgd */
153 1.1 cgd void
154 1.1 cgd scr_init()
155 1.1 cgd {
156 1.1 cgd static int bsflag, xsflag, sgnum;
157 1.1 cgd #ifdef unneeded
158 1.1 cgd static int ncflag;
159 1.1 cgd #endif
160 1.1 cgd char *term, *fill;
161 1.1 cgd static struct tcninfo { /* termcap numeric and flag info */
162 1.1 cgd char tcname[3];
163 1.1 cgd int *tcaddr;
164 1.1 cgd } tcflags[] = {
165 1.1 cgd "bs", &bsflag,
166 1.1 cgd "ms", &MSflag,
167 1.1 cgd #ifdef unneeded
168 1.1 cgd "nc", &ncflag,
169 1.1 cgd #endif
170 1.1 cgd "xs", &xsflag,
171 1.1 cgd 0
172 1.1 cgd }, tcnums[] = {
173 1.1 cgd "co", &COnum,
174 1.1 cgd "li", &LInum,
175 1.1 cgd "sg", &sgnum,
176 1.1 cgd 0
177 1.1 cgd };
178 1.1 cgd
179 1.1 cgd if ((term = getenv("TERM")) == NULL)
180 1.1 cgd stop("you must set the TERM environment variable");
181 1.1 cgd if (tgetent(tbuf, term) <= 0)
182 1.1 cgd stop("cannot find your termcap");
183 1.1 cgd fill = combuf;
184 1.1 cgd {
185 1.1 cgd register struct tcsinfo *p;
186 1.1 cgd
187 1.1 cgd for (p = tcstrings; p->tcaddr; p++)
188 1.1 cgd *p->tcaddr = tgetstr(p->tcname, &fill);
189 1.1 cgd }
190 1.1 cgd {
191 1.1 cgd register struct tcninfo *p;
192 1.1 cgd
193 1.1 cgd for (p = tcflags; p->tcaddr; p++)
194 1.1 cgd *p->tcaddr = tgetflag(p->tcname);
195 1.1 cgd for (p = tcnums; p->tcaddr; p++)
196 1.1 cgd *p->tcaddr = tgetnum(p->tcname);
197 1.1 cgd }
198 1.1 cgd if (bsflag)
199 1.1 cgd BC = "\b";
200 1.1 cgd else if (BC == NULL && bcstr != NULL)
201 1.1 cgd BC = bcstr;
202 1.1 cgd if (CLstr == NULL)
203 1.1 cgd stop("cannot clear screen");
204 1.1 cgd if (CMstr == NULL || UP == NULL || BC == NULL)
205 1.1 cgd stop("cannot do random cursor positioning via tgoto()");
206 1.1 cgd PC = pcstr ? *pcstr : 0;
207 1.1 cgd if (sgnum >= 0 || xsflag)
208 1.1 cgd SOstr = SEstr = NULL;
209 1.1 cgd #ifdef unneeded
210 1.1 cgd if (ncflag)
211 1.1 cgd CRstr = NULL;
212 1.1 cgd else if (CRstr == NULL)
213 1.1 cgd CRstr = "\r";
214 1.1 cgd #endif
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd /* this foolery is needed to modify tty state `atomically' */
218 1.1 cgd static jmp_buf scr_onstop;
219 1.1 cgd
220 1.1 cgd #define sigunblock(mask) sigsetmask(sigblock(0) & ~(mask))
221 1.1 cgd
222 1.1 cgd static void
223 1.1 cgd stopset(sig)
224 1.1 cgd int sig;
225 1.1 cgd {
226 1.1 cgd (void) signal(sig, SIG_DFL);
227 1.1 cgd (void) kill(getpid(), sig);
228 1.1 cgd (void) sigunblock(sigmask(sig));
229 1.1 cgd longjmp(scr_onstop, 1);
230 1.1 cgd }
231 1.1 cgd
232 1.1 cgd static void
233 1.1 cgd scr_stop()
234 1.1 cgd {
235 1.1 cgd scr_end();
236 1.1 cgd (void) kill(getpid(), SIGTSTP);
237 1.1 cgd (void) sigunblock(sigmask(SIGTSTP));
238 1.1 cgd scr_set();
239 1.1 cgd scr_msg(key_msg, 1);
240 1.1 cgd }
241 1.1 cgd
242 1.1 cgd /*
243 1.1 cgd * Set up screen mode.
244 1.1 cgd */
245 1.1 cgd void
246 1.1 cgd scr_set()
247 1.1 cgd {
248 1.1 cgd struct winsize ws;
249 1.1 cgd struct sgttyb newtt;
250 1.1 cgd volatile int omask;
251 1.1 cgd void (*ttou)();
252 1.1 cgd
253 1.1 cgd omask = sigblock(sigmask(SIGTSTP) | sigmask(SIGTTOU));
254 1.1 cgd if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
255 1.1 cgd (void) signal(SIGTSTP, SIG_IGN);
256 1.1 cgd if ((ttou = signal(SIGTSTP, stopset)) == SIG_IGN)
257 1.1 cgd (void) signal(SIGTSTP, SIG_IGN);
258 1.1 cgd /*
259 1.1 cgd * At last, we are ready to modify the tty state. If
260 1.1 cgd * we stop while at it, stopset() above will longjmp back
261 1.1 cgd * to the setjmp here and we will start over.
262 1.1 cgd */
263 1.1 cgd (void) setjmp(scr_onstop);
264 1.1 cgd (void) sigsetmask(omask);
265 1.1 cgd Rows = 0, Cols = 0;
266 1.1 cgd if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
267 1.1 cgd Rows = ws.ws_row;
268 1.1 cgd Cols = ws.ws_col;
269 1.1 cgd }
270 1.1 cgd if (Rows == 0)
271 1.1 cgd Rows = LInum;
272 1.1 cgd if (Cols == 0)
273 1.1 cgd Cols = COnum;
274 1.1 cgd if (Rows < MINROWS || Cols < MINCOLS) {
275 1.1 cgd (void) fprintf(stderr,
276 1.1 cgd "the screen is too small: must be at least %d x %d",
277 1.1 cgd MINROWS, MINCOLS);
278 1.1 cgd stop(""); /* stop() supplies \n */
279 1.1 cgd }
280 1.1 cgd if (ioctl(0, TIOCGETP, &oldtt))
281 1.1 cgd stop("ioctl(TIOCGETP) fails");
282 1.1 cgd newtt = oldtt;
283 1.1 cgd newtt.sg_flags = (newtt.sg_flags | CBREAK) & ~(CRMOD | ECHO);
284 1.1 cgd if ((newtt.sg_flags & TBDELAY) == XTABS)
285 1.1 cgd newtt.sg_flags &= ~TBDELAY;
286 1.1 cgd if (ioctl(0, TIOCSETN, &newtt))
287 1.1 cgd stop("ioctl(TIOCSETN) fails");
288 1.1 cgd ospeed = newtt.sg_ospeed;
289 1.1 cgd omask = sigblock(sigmask(SIGTSTP) | sigmask(SIGTTOU));
290 1.1 cgd
291 1.1 cgd /*
292 1.1 cgd * We made it. We are now in screen mode, modulo TIstr
293 1.1 cgd * (which we will fix immediately).
294 1.1 cgd */
295 1.1 cgd if (TIstr)
296 1.1 cgd putstr(TIstr); /* termcap(5) says this is not padded */
297 1.1 cgd if (tstp != SIG_IGN)
298 1.1 cgd (void) signal(SIGTSTP, scr_stop);
299 1.1 cgd (void) signal(SIGTTOU, ttou);
300 1.1 cgd
301 1.1 cgd isset = 1;
302 1.1 cgd (void) sigsetmask(omask);
303 1.1 cgd scr_clear();
304 1.1 cgd }
305 1.1 cgd
306 1.1 cgd /*
307 1.1 cgd * End screen mode.
308 1.1 cgd */
309 1.1 cgd void
310 1.1 cgd scr_end()
311 1.1 cgd {
312 1.1 cgd int omask = sigblock(sigmask(SIGTSTP) | sigmask(SIGTTOU));
313 1.1 cgd
314 1.1 cgd /* move cursor to last line */
315 1.1 cgd if (LLstr)
316 1.1 cgd putstr(LLstr); /* termcap(5) says this is not padded */
317 1.1 cgd else
318 1.1 cgd moveto(Rows - 1, 0);
319 1.1 cgd /* exit screen mode */
320 1.1 cgd if (TEstr)
321 1.1 cgd putstr(TEstr); /* termcap(5) says this is not padded */
322 1.1 cgd (void) fflush(stdout);
323 1.1 cgd (void) ioctl(0, TIOCSETN, &oldtt);
324 1.1 cgd isset = 0;
325 1.1 cgd /* restore signals */
326 1.1 cgd (void) signal(SIGTSTP, tstp);
327 1.1 cgd (void) sigsetmask(omask);
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd void
331 1.1 cgd stop(why)
332 1.1 cgd char *why;
333 1.1 cgd {
334 1.1 cgd
335 1.1 cgd if (isset)
336 1.1 cgd scr_end();
337 1.1 cgd (void) fprintf(stderr, "aborting: %s\n", why);
338 1.1 cgd exit(1);
339 1.1 cgd }
340 1.1 cgd
341 1.1 cgd /*
342 1.1 cgd * Clear the screen, forgetting the current contents in the process.
343 1.1 cgd */
344 1.1 cgd void
345 1.1 cgd scr_clear()
346 1.1 cgd {
347 1.1 cgd
348 1.1 cgd putpad(CLstr);
349 1.1 cgd curscore = -1;
350 1.1 cgd bzero((char *)curscreen, sizeof(curscreen));
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd #if vax && !__GNUC__
354 1.1 cgd typedef int regcell; /* pcc is bad at `register char', etc */
355 1.1 cgd #else
356 1.1 cgd typedef cell regcell;
357 1.1 cgd #endif
358 1.1 cgd
359 1.1 cgd /*
360 1.1 cgd * Update the screen.
361 1.1 cgd */
362 1.1 cgd void
363 1.1 cgd scr_update()
364 1.1 cgd {
365 1.1 cgd register cell *bp, *sp;
366 1.1 cgd register regcell so, cur_so = 0;
367 1.1 cgd register int i, ccol, j;
368 1.1 cgd int omask = sigblock(sigmask(SIGTSTP));
369 1.1 cgd
370 1.1 cgd /* always leave cursor after last displayed point */
371 1.1 cgd curscreen[D_LAST * B_COLS - 1] = -1;
372 1.1 cgd
373 1.1 cgd if (score != curscore) {
374 1.1 cgd if (HOstr)
375 1.1 cgd putpad(HOstr);
376 1.1 cgd else
377 1.1 cgd moveto(0, 0);
378 1.1 cgd (void) printf("%d", score);
379 1.1 cgd curscore = score;
380 1.1 cgd }
381 1.1 cgd
382 1.1 cgd bp = &board[D_FIRST * B_COLS];
383 1.1 cgd sp = &curscreen[D_FIRST * B_COLS];
384 1.1 cgd for (j = D_FIRST; j < D_LAST; j++) {
385 1.1 cgd ccol = -1;
386 1.1 cgd for (i = 0; i < B_COLS; bp++, sp++, i++) {
387 1.1 cgd if (*sp == (so = *bp))
388 1.1 cgd continue;
389 1.1 cgd *sp = so;
390 1.1 cgd if (i != ccol) {
391 1.1 cgd if (cur_so && MSflag) {
392 1.1 cgd putpad(SEstr);
393 1.1 cgd cur_so = 0;
394 1.1 cgd }
395 1.1 cgd moveto(RTOD(j), CTOD(i));
396 1.1 cgd }
397 1.1 cgd if (SOstr) {
398 1.1 cgd if (so != cur_so) {
399 1.1 cgd putpad(so ? SOstr : SEstr);
400 1.1 cgd cur_so = so;
401 1.1 cgd }
402 1.1 cgd putstr(" ");
403 1.1 cgd } else
404 1.1 cgd putstr(so ? "XX" : " ");
405 1.1 cgd ccol = i + 1;
406 1.1 cgd /*
407 1.1 cgd * Look ahead a bit, to avoid extra motion if
408 1.1 cgd * we will be redrawing the cell after the next.
409 1.1 cgd * Motion probably takes four or more characters,
410 1.1 cgd * so we save even if we rewrite two cells
411 1.1 cgd * `unnecessarily'. Skip it all, though, if
412 1.1 cgd * the next cell is a different color.
413 1.1 cgd */
414 1.1 cgd #define STOP (B_COLS - 3)
415 1.1 cgd if (i > STOP || sp[1] != bp[1] || so != bp[1])
416 1.1 cgd continue;
417 1.1 cgd if (sp[2] != bp[2])
418 1.1 cgd sp[1] = -1;
419 1.1 cgd else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
420 1.1 cgd sp[2] = -1;
421 1.1 cgd sp[1] = -1;
422 1.1 cgd }
423 1.1 cgd }
424 1.1 cgd }
425 1.1 cgd if (cur_so)
426 1.1 cgd putpad(SEstr);
427 1.1 cgd (void) fflush(stdout);
428 1.1 cgd (void) sigsetmask(omask);
429 1.1 cgd }
430 1.1 cgd
431 1.1 cgd /*
432 1.1 cgd * Write a message (set!=0), or clear the same message (set==0).
433 1.1 cgd * (We need its length in case we have to overwrite with blanks.)
434 1.1 cgd */
435 1.1 cgd void
436 1.1 cgd scr_msg(s, set)
437 1.1 cgd register char *s;
438 1.1 cgd int set;
439 1.1 cgd {
440 1.1 cgd
441 1.1 cgd if (set || CEstr == NULL) {
442 1.1 cgd register int l = strlen(s);
443 1.1 cgd
444 1.1 cgd moveto(Rows - 2, ((Cols - l) >> 1) - 1);
445 1.1 cgd if (set)
446 1.1 cgd putstr(s);
447 1.1 cgd else
448 1.1 cgd while (--l >= 0)
449 1.1 cgd (void) putchar(' ');
450 1.1 cgd } else {
451 1.1 cgd moveto(Rows - 2, 0);
452 1.1 cgd putpad(CEstr);
453 1.1 cgd }
454 1.1 cgd }
455