]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/screen.c
1 /* $NetBSD: screen.c,v 1.22 2008/01/28 01:38:59 dholland 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/cdefs.h>
42 #include <sys/ioctl.h>
54 #define sigmask(s) (1 << ((s) - 1))
60 static cell curscreen
[B_SIZE
]; /* 1 => standout (or otherwise marked) */
62 static int isset
; /* true => terminal is in game mode */
63 static struct termios oldtt
;
64 static void (*tstp
)(int);
66 static void scr_stop(int);
67 static void stopset(int) __dead
;
71 * Capabilities from TERMCAP.
76 *bcstr
, /* backspace char */
77 *CEstr
, /* clear to end of line */
78 *CLstr
, /* clear screen */
79 *CMstr
, /* cursor motion string */
81 *CRstr
, /* "\r" equivalent */
83 *HOstr
, /* cursor home */
84 *LLstr
, /* last line, first column */
85 *pcstr
, /* pad character */
86 *TEstr
, /* end cursor motion mode */
87 *TIstr
; /* begin cursor motion mode */
89 *SEstr
, /* end standout mode */
90 *SOstr
; /* begin standout mode */
92 COnum
, /* co# value */
93 LInum
, /* li# value */
94 MSflag
; /* can move in standout mode */
97 struct tcsinfo
{ /* termcap string info; some abbrevs above */
108 {"le", &BC
}, /* move cursor left one space */
114 {"up", &UP
}, /* cursor up */
118 /* This is where we will actually stuff the information */
120 static struct tinfo
*info
;
123 * Routine used by tputs().
134 * putstr() is for unpadded strings (either as in termcap(5) or
135 * simply literal strings); putpad() is for padded strings with
136 * count=1. (See screen.h for putpad().)
138 #define putstr(s) (void)fputs(s, stdout)
145 if (t_goto(info
, CMstr
, c
, r
, buf
, 255) == 0)
150 * Set up from termcap.
155 static int bsflag
, xsflag
, sgnum
;
160 static struct tcninfo
{ /* termcap numeric and flag info */
177 static char backspace
[] = "\b";
179 if ((term
= getenv("TERM")) == NULL
)
180 stop("you must set the TERM environment variable");
181 if (t_getent(&info
, term
) <= 0)
182 stop("cannot find your termcap");
186 for (p
= tcstrings
; p
->tcaddr
; p
++)
187 *p
->tcaddr
= t_agetstr(info
, p
->tcname
);
192 for (p
= tcflags
; p
->tcaddr
; p
++)
193 *p
->tcaddr
= t_getflag(info
, p
->tcname
);
194 for (p
= tcnums
; p
->tcaddr
; p
++)
195 *p
->tcaddr
= t_getnum(info
, p
->tcname
);
199 else if (BC
== NULL
&& bcstr
!= NULL
)
202 stop("cannot clear screen");
203 if (CMstr
== NULL
|| UP
== NULL
|| BC
== NULL
)
204 stop("cannot do random cursor positioning via tgoto()");
205 PC
= pcstr
? *pcstr
: 0;
206 if (sgnum
>= 0 || xsflag
)
207 SOstr
= SEstr
= NULL
;
211 else if (CRstr
== NULL
)
216 /* this foolery is needed to modify tty state `atomically' */
217 static jmp_buf scr_onstop
;
225 (void) signal(sig
, SIG_DFL
);
226 (void) kill(getpid(), sig
);
228 sigaddset(&set
, sig
);
229 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
230 longjmp(scr_onstop
, 1);
240 (void) kill(getpid(), sig
);
242 sigaddset(&set
, sig
);
243 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
249 * Set up screen mode.
255 struct termios newtt
;
256 sigset_t nsigset
, osigset
;
259 sigemptyset(&nsigset
);
260 sigaddset(&nsigset
, SIGTSTP
);
261 sigaddset(&nsigset
, SIGTTOU
);
262 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
263 if ((tstp
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
264 (void) signal(SIGTSTP
, SIG_IGN
);
265 if ((ttou
= signal(SIGTTOU
, stopset
)) == SIG_IGN
)
266 (void) signal(SIGTTOU
, SIG_IGN
);
268 * At last, we are ready to modify the tty state. If
269 * we stop while at it, stopset() above will longjmp back
270 * to the setjmp here and we will start over.
272 (void) setjmp(scr_onstop
);
273 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
275 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
283 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
284 (void) fprintf(stderr
,
285 "the screen is too small: must be at least %dx%d, ",
287 stop(""); /* stop() supplies \n */
289 if (tcgetattr(0, &oldtt
) < 0)
290 stop("tcgetattr() fails");
292 newtt
.c_lflag
&= ~(ICANON
|ECHO
);
293 newtt
.c_oflag
&= ~OXTABS
;
294 if (tcsetattr(0, TCSADRAIN
, &newtt
) < 0)
295 stop("tcsetattr() fails");
296 ospeed
= cfgetospeed(&newtt
);
297 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
300 * We made it. We are now in screen mode, modulo TIstr
301 * (which we will fix immediately).
304 putstr(TIstr
); /* termcap(5) says this is not padded */
306 (void) signal(SIGTSTP
, scr_stop
);
308 (void) signal(SIGTTOU
, ttou
);
311 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
321 sigset_t nsigset
, osigset
;
323 sigemptyset(&nsigset
);
324 sigaddset(&nsigset
, SIGTSTP
);
325 sigaddset(&nsigset
, SIGTTOU
);
326 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
327 /* move cursor to last line */
329 putstr(LLstr
); /* termcap(5) says this is not padded */
332 /* exit screen mode */
334 putstr(TEstr
); /* termcap(5) says this is not padded */
335 (void) fflush(stdout
);
336 (void) tcsetattr(0, TCSADRAIN
, &oldtt
);
338 /* restore signals */
339 (void) signal(SIGTSTP
, tstp
);
340 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
350 (void) fprintf(stderr
, "aborting: %s\n", why
);
355 * Clear the screen, forgetting the current contents in the process.
363 memset((char *)curscreen
, 0, sizeof(curscreen
));
367 typedef int regcell
; /* pcc is bad at `register char', etc */
369 typedef cell regcell
;
379 regcell so
, cur_so
= 0;
381 sigset_t nsigset
, osigset
;
382 static const struct shape
*lastshape
;
384 sigemptyset(&nsigset
);
385 sigaddset(&nsigset
, SIGTSTP
);
386 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
388 /* always leave cursor after last displayed point */
389 curscreen
[D_LAST
* B_COLS
- 1] = -1;
391 if (score
!= curscore
) {
396 (void) printf("Score: %d", score
);
400 /* draw preview of nextpattern */
401 if (showpreview
&& (nextshape
!= lastshape
)) {
405 lastshape
= nextshape
;
409 moveto(r
-1, c
-1); putstr(" ");
410 moveto(r
, c
-1); putstr(" ");
411 moveto(r
+1, c
-1); putstr(" ");
412 moveto(r
+2, c
-1); putstr(" ");
415 putstr("Next shape:");
423 t
+= nextshape
->off
[i
];
434 bp
= &board
[D_FIRST
* B_COLS
];
435 sp
= &curscreen
[D_FIRST
* B_COLS
];
436 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
438 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
439 if (*sp
== (so
= *bp
))
443 if (cur_so
&& MSflag
) {
447 moveto(RTOD(j
), CTOD(i
));
451 putpad(so
? SOstr
: SEstr
);
456 putstr(so
? "XX" : " ");
459 * Look ahead a bit, to avoid extra motion if
460 * we will be redrawing the cell after the next.
461 * Motion probably takes four or more characters,
462 * so we save even if we rewrite two cells
463 * `unnecessarily'. Skip it all, though, if
464 * the next cell is a different color.
466 #define STOP (B_COLS - 3)
467 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
471 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
479 (void) fflush(stdout
);
480 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
484 * Write a message (set!=0), or clear the same message (set==0).
485 * (We need its length in case we have to overwrite with blanks.)
493 if (set
|| CEstr
== NULL
) {
496 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);