]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/screen.c
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek and Darren F. Provine.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)screen.c 8.1 (Berkeley) 5/31/93
40 * Tetris screen control.
44 #include <sys/ioctl.h>
54 #define sigmask(s) (1 << ((s) - 1))
61 * XXX - need a <termcap.h>
63 int tgetent
__P((char *, const char *));
64 int tgetflag
__P((const char *));
65 int tgetnum
__P((const char *));
66 int tputs
__P((const char *, int, int (*)(int)));
68 static cell curscreen
[B_SIZE
]; /* 1 => standout (or otherwise marked) */
70 static int isset
; /* true => terminal is in game mode */
71 static struct sgttyb oldtt
;
72 static void (*tstp
)();
74 char *tgetstr(), *tgoto();
78 * Capabilities from TERMCAP.
80 char PC
, *BC
, *UP
; /* tgoto requires globals: ugh! */
84 *bcstr
, /* backspace char */
85 *CEstr
, /* clear to end of line */
86 *CLstr
, /* clear screen */
87 *CMstr
, /* cursor motion string */
89 *CRstr
, /* "\r" equivalent */
91 *HOstr
, /* cursor home */
92 *LLstr
, /* last line, first column */
93 *pcstr
, /* pad character */
94 *TEstr
, /* end cursor motion mode */
95 *TIstr
; /* begin cursor motion mode */
97 *SEstr
, /* end standout mode */
98 *SOstr
; /* begin standout mode */
100 COnum
, /* co# value */
101 LInum
, /* li# value */
102 MSflag
; /* can move in standout mode */
105 struct tcsinfo
{ /* termcap string info; some abbrevs above */
116 "le", &BC
, /* move cursor left one space */
122 "up", &UP
, /* cursor up */
126 /* This is where we will actually stuff the information */
128 static char combuf
[1024], tbuf
[1024];
132 * Routine used by tputs().
143 * putstr() is for unpadded strings (either as in termcap(5) or
144 * simply literal strings); putpad() is for padded strings with
145 * count=1. (See screen.h for putpad().)
147 #define putstr(s) (void)fputs(s, stdout)
148 #define moveto(r, c) putpad(tgoto(CMstr, c, r))
151 * Set up from termcap.
156 static int bsflag
, xsflag
, sgnum
;
161 static struct tcninfo
{ /* termcap numeric and flag info */
179 if ((term
= getenv("TERM")) == NULL
)
180 stop("you must set the TERM environment variable");
181 if (tgetent(tbuf
, term
) <= 0)
182 stop("cannot find your termcap");
185 register struct tcsinfo
*p
;
187 for (p
= tcstrings
; p
->tcaddr
; p
++)
188 *p
->tcaddr
= tgetstr(p
->tcname
, &fill
);
191 register struct tcninfo
*p
;
193 for (p
= tcflags
; p
->tcaddr
; p
++)
194 *p
->tcaddr
= tgetflag(p
->tcname
);
195 for (p
= tcnums
; p
->tcaddr
; p
++)
196 *p
->tcaddr
= tgetnum(p
->tcname
);
200 else if (BC
== NULL
&& bcstr
!= NULL
)
203 stop("cannot clear screen");
204 if (CMstr
== NULL
|| UP
== NULL
|| BC
== NULL
)
205 stop("cannot do random cursor positioning via tgoto()");
206 PC
= pcstr
? *pcstr
: 0;
207 if (sgnum
>= 0 || xsflag
)
208 SOstr
= SEstr
= NULL
;
212 else if (CRstr
== NULL
)
217 /* this foolery is needed to modify tty state `atomically' */
218 static jmp_buf scr_onstop
;
220 #define sigunblock(mask) sigsetmask(sigblock(0) & ~(mask))
226 (void) signal(sig
, SIG_DFL
);
227 (void) kill(getpid(), sig
);
228 (void) sigunblock(sigmask(sig
));
229 longjmp(scr_onstop
, 1);
236 (void) kill(getpid(), SIGTSTP
);
237 (void) sigunblock(sigmask(SIGTSTP
));
243 * Set up screen mode.
253 omask
= sigblock(sigmask(SIGTSTP
) | sigmask(SIGTTOU
));
254 if ((tstp
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
255 (void) signal(SIGTSTP
, SIG_IGN
);
256 if ((ttou
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
257 (void) signal(SIGTSTP
, SIG_IGN
);
259 * At last, we are ready to modify the tty state. If
260 * we stop while at it, stopset() above will longjmp back
261 * to the setjmp here and we will start over.
263 (void) setjmp(scr_onstop
);
264 (void) sigsetmask(omask
);
266 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
274 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
275 (void) fprintf(stderr
,
276 "the screen is too small: must be at least %d x %d",
278 stop(""); /* stop() supplies \n */
280 if (ioctl(0, TIOCGETP
, &oldtt
))
281 stop("ioctl(TIOCGETP) fails");
283 newtt
.sg_flags
= (newtt
.sg_flags
| CBREAK
) & ~(CRMOD
| ECHO
);
284 if ((newtt
.sg_flags
& TBDELAY
) == XTABS
)
285 newtt
.sg_flags
&= ~TBDELAY
;
286 if (ioctl(0, TIOCSETN
, &newtt
))
287 stop("ioctl(TIOCSETN) fails");
288 ospeed
= newtt
.sg_ospeed
;
289 omask
= sigblock(sigmask(SIGTSTP
) | sigmask(SIGTTOU
));
292 * We made it. We are now in screen mode, modulo TIstr
293 * (which we will fix immediately).
296 putstr(TIstr
); /* termcap(5) says this is not padded */
298 (void) signal(SIGTSTP
, scr_stop
);
299 (void) signal(SIGTTOU
, ttou
);
302 (void) sigsetmask(omask
);
312 int omask
= sigblock(sigmask(SIGTSTP
) | sigmask(SIGTTOU
));
314 /* move cursor to last line */
316 putstr(LLstr
); /* termcap(5) says this is not padded */
319 /* exit screen mode */
321 putstr(TEstr
); /* termcap(5) says this is not padded */
322 (void) fflush(stdout
);
323 (void) ioctl(0, TIOCSETN
, &oldtt
);
325 /* restore signals */
326 (void) signal(SIGTSTP
, tstp
);
327 (void) sigsetmask(omask
);
337 (void) fprintf(stderr
, "aborting: %s\n", why
);
342 * Clear the screen, forgetting the current contents in the process.
350 bzero((char *)curscreen
, sizeof(curscreen
));
354 typedef int regcell
; /* pcc is bad at `register char', etc */
356 typedef cell regcell
;
365 register cell
*bp
, *sp
;
366 register regcell so
, cur_so
= 0;
367 register int i
, ccol
, j
;
368 int omask
= sigblock(sigmask(SIGTSTP
));
370 /* always leave cursor after last displayed point */
371 curscreen
[D_LAST
* B_COLS
- 1] = -1;
373 if (score
!= curscore
) {
378 (void) printf("%d", score
);
382 bp
= &board
[D_FIRST
* B_COLS
];
383 sp
= &curscreen
[D_FIRST
* B_COLS
];
384 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
386 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
387 if (*sp
== (so
= *bp
))
391 if (cur_so
&& MSflag
) {
395 moveto(RTOD(j
), CTOD(i
));
399 putpad(so
? SOstr
: SEstr
);
404 putstr(so
? "XX" : " ");
407 * Look ahead a bit, to avoid extra motion if
408 * we will be redrawing the cell after the next.
409 * Motion probably takes four or more characters,
410 * so we save even if we rewrite two cells
411 * `unnecessarily'. Skip it all, though, if
412 * the next cell is a different color.
414 #define STOP (B_COLS - 3)
415 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
419 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
427 (void) fflush(stdout
);
428 (void) sigsetmask(omask
);
432 * Write a message (set!=0), or clear the same message (set==0).
433 * (We need its length in case we have to overwrite with blanks.)
441 if (set
|| CEstr
== NULL
) {
442 register int l
= strlen(s
);
444 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);