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