]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/screen.c
1 /* $NetBSD: screen.c,v 1.19 2004/01/27 20:30:30 jsm Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)screen.c 8.1 (Berkeley) 5/31/93
38 * Tetris screen control.
41 #include <sys/ioctl.h>
53 #define sigmask(s) (1 << ((s) - 1))
59 static cell curscreen
[B_SIZE
]; /* 1 => standout (or otherwise marked) */
61 static int isset
; /* true => terminal is in game mode */
62 static struct termios oldtt
;
63 static void (*tstp
)(int);
65 static void scr_stop(int);
66 static void stopset(int) __attribute__((__noreturn__
));
70 * Capabilities from TERMCAP.
75 *bcstr
, /* backspace char */
76 *CEstr
, /* clear to end of line */
77 *CLstr
, /* clear screen */
78 *CMstr
, /* cursor motion string */
80 *CRstr
, /* "\r" equivalent */
82 *HOstr
, /* cursor home */
83 *LLstr
, /* last line, first column */
84 *pcstr
, /* pad character */
85 *TEstr
, /* end cursor motion mode */
86 *TIstr
; /* begin cursor motion mode */
88 *SEstr
, /* end standout mode */
89 *SOstr
; /* begin standout mode */
91 COnum
, /* co# value */
92 LInum
, /* li# value */
93 MSflag
; /* can move in standout mode */
96 struct tcsinfo
{ /* termcap string info; some abbrevs above */
107 {"le", &BC
}, /* move cursor left one space */
113 {"up", &UP
}, /* cursor up */
117 /* This is where we will actually stuff the information */
119 static struct tinfo
*info
;
122 * Routine used by tputs().
133 * putstr() is for unpadded strings (either as in termcap(5) or
134 * simply literal strings); putpad() is for padded strings with
135 * count=1. (See screen.h for putpad().)
137 #define putstr(s) (void)fputs(s, stdout)
144 if (t_goto(info
, CMstr
, c
, r
, buf
, 255) == 0)
149 * Set up from termcap.
154 static int bsflag
, xsflag
, sgnum
;
159 static struct tcninfo
{ /* termcap numeric and flag info */
177 if ((term
= getenv("TERM")) == NULL
)
178 stop("you must set the TERM environment variable");
179 if (t_getent(&info
, term
) <= 0)
180 stop("cannot find your termcap");
184 for (p
= tcstrings
; p
->tcaddr
; p
++)
185 *p
->tcaddr
= t_agetstr(info
, p
->tcname
);
190 for (p
= tcflags
; p
->tcaddr
; p
++)
191 *p
->tcaddr
= t_getflag(info
, p
->tcname
);
192 for (p
= tcnums
; p
->tcaddr
; p
++)
193 *p
->tcaddr
= t_getnum(info
, p
->tcname
);
197 else if (BC
== NULL
&& bcstr
!= NULL
)
200 stop("cannot clear screen");
201 if (CMstr
== NULL
|| UP
== NULL
|| BC
== NULL
)
202 stop("cannot do random cursor positioning via tgoto()");
203 PC
= pcstr
? *pcstr
: 0;
204 if (sgnum
>= 0 || xsflag
)
205 SOstr
= SEstr
= NULL
;
209 else if (CRstr
== NULL
)
214 /* this foolery is needed to modify tty state `atomically' */
215 static jmp_buf scr_onstop
;
223 (void) signal(sig
, SIG_DFL
);
224 (void) kill(getpid(), sig
);
225 sigemptyset(&sigset
);
226 sigaddset(&sigset
, sig
);
227 (void) sigprocmask(SIG_UNBLOCK
, &sigset
, (sigset_t
*)0);
228 longjmp(scr_onstop
, 1);
238 (void) kill(getpid(), sig
);
239 sigemptyset(&sigset
);
240 sigaddset(&sigset
, sig
);
241 (void) sigprocmask(SIG_UNBLOCK
, &sigset
, (sigset_t
*)0);
247 * Set up screen mode.
253 struct termios newtt
;
254 sigset_t sigset
, osigset
;
257 sigemptyset(&sigset
);
258 sigaddset(&sigset
, SIGTSTP
);
259 sigaddset(&sigset
, SIGTTOU
);
260 (void) sigprocmask(SIG_BLOCK
, &sigset
, &osigset
);
261 if ((tstp
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
262 (void) signal(SIGTSTP
, SIG_IGN
);
263 if ((ttou
= signal(SIGTTOU
, stopset
)) == SIG_IGN
)
264 (void) signal(SIGTTOU
, SIG_IGN
);
266 * At last, we are ready to modify the tty state. If
267 * we stop while at it, stopset() above will longjmp back
268 * to the setjmp here and we will start over.
270 (void) setjmp(scr_onstop
);
271 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
273 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
281 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
282 (void) fprintf(stderr
,
283 "the screen is too small: must be at least %dx%d, ",
285 stop(""); /* stop() supplies \n */
287 if (tcgetattr(0, &oldtt
) < 0)
288 stop("tcgetattr() fails");
290 newtt
.c_lflag
&= ~(ICANON
|ECHO
);
291 newtt
.c_oflag
&= ~OXTABS
;
292 if (tcsetattr(0, TCSADRAIN
, &newtt
) < 0)
293 stop("tcsetattr() fails");
294 ospeed
= cfgetospeed(&newtt
);
295 (void) sigprocmask(SIG_BLOCK
, &sigset
, &osigset
);
298 * We made it. We are now in screen mode, modulo TIstr
299 * (which we will fix immediately).
302 putstr(TIstr
); /* termcap(5) says this is not padded */
304 (void) signal(SIGTSTP
, scr_stop
);
306 (void) signal(SIGTTOU
, ttou
);
309 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
319 sigset_t sigset
, osigset
;
321 sigemptyset(&sigset
);
322 sigaddset(&sigset
, SIGTSTP
);
323 sigaddset(&sigset
, SIGTTOU
);
324 (void) sigprocmask(SIG_BLOCK
, &sigset
, &osigset
);
325 /* move cursor to last line */
327 putstr(LLstr
); /* termcap(5) says this is not padded */
330 /* exit screen mode */
332 putstr(TEstr
); /* termcap(5) says this is not padded */
333 (void) fflush(stdout
);
334 (void) tcsetattr(0, TCSADRAIN
, &oldtt
);
336 /* restore signals */
337 (void) signal(SIGTSTP
, tstp
);
338 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
348 (void) fprintf(stderr
, "aborting: %s\n", why
);
353 * Clear the screen, forgetting the current contents in the process.
361 memset((char *)curscreen
, 0, sizeof(curscreen
));
365 typedef int regcell
; /* pcc is bad at `register char', etc */
367 typedef cell regcell
;
377 regcell so
, cur_so
= 0;
379 sigset_t sigset
, osigset
;
380 static const struct shape
*lastshape
;
382 sigemptyset(&sigset
);
383 sigaddset(&sigset
, SIGTSTP
);
384 (void) sigprocmask(SIG_BLOCK
, &sigset
, &osigset
);
386 /* always leave cursor after last displayed point */
387 curscreen
[D_LAST
* B_COLS
- 1] = -1;
389 if (score
!= curscore
) {
394 (void) printf("Score: %d", score
);
398 /* draw preview of nextpattern */
399 if (showpreview
&& (nextshape
!= lastshape
)) {
404 lastshape
= nextshape
;
408 moveto(r
-1, c
-1); putstr(" ");
409 moveto(r
, c
-1); putstr(" ");
410 moveto(r
+1, c
-1); putstr(" ");
411 moveto(r
+2, c
-1); putstr(" ");
414 putstr("Next shape:");
422 t
+= nextshape
->off
[i
];
433 bp
= &board
[D_FIRST
* B_COLS
];
434 sp
= &curscreen
[D_FIRST
* B_COLS
];
435 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
437 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
438 if (*sp
== (so
= *bp
))
442 if (cur_so
&& MSflag
) {
446 moveto(RTOD(j
), CTOD(i
));
450 putpad(so
? SOstr
: SEstr
);
455 putstr(so
? "XX" : " ");
458 * Look ahead a bit, to avoid extra motion if
459 * we will be redrawing the cell after the next.
460 * Motion probably takes four or more characters,
461 * so we save even if we rewrite two cells
462 * `unnecessarily'. Skip it all, though, if
463 * the next cell is a different color.
465 #define STOP (B_COLS - 3)
466 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
470 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
478 (void) fflush(stdout
);
479 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
483 * Write a message (set!=0), or clear the same message (set==0).
484 * (We need its length in case we have to overwrite with blanks.)
492 if (set
|| CEstr
== NULL
) {
495 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);