]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/screen.c
1 /* $NetBSD: screen.c,v 1.23 2009/05/25 04:33:53 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().
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 */
176 static char backspace
[] = "\b";
178 if ((term
= getenv("TERM")) == NULL
)
179 stop("you must set the TERM environment variable");
180 if (t_getent(&info
, term
) <= 0)
181 stop("cannot find your termcap");
185 for (p
= tcstrings
; p
->tcaddr
; p
++)
186 *p
->tcaddr
= t_agetstr(info
, p
->tcname
);
191 for (p
= tcflags
; p
->tcaddr
; p
++)
192 *p
->tcaddr
= t_getflag(info
, p
->tcname
);
193 for (p
= tcnums
; p
->tcaddr
; p
++)
194 *p
->tcaddr
= t_getnum(info
, p
->tcname
);
198 else if (BC
== NULL
&& bcstr
!= NULL
)
201 stop("cannot clear screen");
202 if (CMstr
== NULL
|| UP
== NULL
|| BC
== NULL
)
203 stop("cannot do random cursor positioning via tgoto()");
204 PC
= pcstr
? *pcstr
: 0;
205 if (sgnum
>= 0 || xsflag
)
206 SOstr
= SEstr
= NULL
;
210 else if (CRstr
== NULL
)
215 /* this foolery is needed to modify tty state `atomically' */
216 static jmp_buf scr_onstop
;
223 (void) signal(sig
, SIG_DFL
);
224 (void) kill(getpid(), sig
);
226 sigaddset(&set
, sig
);
227 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
228 longjmp(scr_onstop
, 1);
237 (void) kill(getpid(), sig
);
239 sigaddset(&set
, sig
);
240 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
246 * Set up screen mode.
252 struct termios newtt
;
253 sigset_t nsigset
, osigset
;
256 sigemptyset(&nsigset
);
257 sigaddset(&nsigset
, SIGTSTP
);
258 sigaddset(&nsigset
, SIGTTOU
);
259 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
260 if ((tstp
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
261 (void) signal(SIGTSTP
, SIG_IGN
);
262 if ((ttou
= signal(SIGTTOU
, stopset
)) == SIG_IGN
)
263 (void) signal(SIGTTOU
, SIG_IGN
);
265 * At last, we are ready to modify the tty state. If
266 * we stop while at it, stopset() above will longjmp back
267 * to the setjmp here and we will start over.
269 (void) setjmp(scr_onstop
);
270 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
272 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
280 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
281 (void) fprintf(stderr
,
282 "the screen is too small: must be at least %dx%d, ",
284 stop(""); /* stop() supplies \n */
286 if (tcgetattr(0, &oldtt
) < 0)
287 stop("tcgetattr() fails");
289 newtt
.c_lflag
&= ~(ICANON
|ECHO
);
290 newtt
.c_oflag
&= ~OXTABS
;
291 if (tcsetattr(0, TCSADRAIN
, &newtt
) < 0)
292 stop("tcsetattr() fails");
293 ospeed
= cfgetospeed(&newtt
);
294 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
297 * We made it. We are now in screen mode, modulo TIstr
298 * (which we will fix immediately).
301 putstr(TIstr
); /* termcap(5) says this is not padded */
303 (void) signal(SIGTSTP
, scr_stop
);
305 (void) signal(SIGTTOU
, ttou
);
308 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
318 sigset_t nsigset
, osigset
;
320 sigemptyset(&nsigset
);
321 sigaddset(&nsigset
, SIGTSTP
);
322 sigaddset(&nsigset
, SIGTTOU
);
323 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
324 /* move cursor to last line */
326 putstr(LLstr
); /* termcap(5) says this is not padded */
329 /* exit screen mode */
331 putstr(TEstr
); /* termcap(5) says this is not padded */
332 (void) fflush(stdout
);
333 (void) tcsetattr(0, TCSADRAIN
, &oldtt
);
335 /* restore signals */
336 (void) signal(SIGTSTP
, tstp
);
337 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
341 stop(const char *why
)
346 (void) fprintf(stderr
, "aborting: %s\n", why
);
351 * Clear the screen, forgetting the current contents in the process.
359 memset((char *)curscreen
, 0, sizeof(curscreen
));
363 typedef int regcell
; /* pcc is bad at `register char', etc */
365 typedef cell regcell
;
375 regcell so
, cur_so
= 0;
377 sigset_t nsigset
, osigset
;
378 static const struct shape
*lastshape
;
380 sigemptyset(&nsigset
);
381 sigaddset(&nsigset
, SIGTSTP
);
382 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
384 /* always leave cursor after last displayed point */
385 curscreen
[D_LAST
* B_COLS
- 1] = -1;
387 if (score
!= curscore
) {
392 (void) printf("Score: %d", score
);
396 /* draw preview of nextpattern */
397 if (showpreview
&& (nextshape
!= lastshape
)) {
401 lastshape
= nextshape
;
405 moveto(r
-1, c
-1); putstr(" ");
406 moveto(r
, c
-1); putstr(" ");
407 moveto(r
+1, c
-1); putstr(" ");
408 moveto(r
+2, c
-1); putstr(" ");
411 putstr("Next shape:");
419 t
+= nextshape
->off
[i
];
430 bp
= &board
[D_FIRST
* B_COLS
];
431 sp
= &curscreen
[D_FIRST
* B_COLS
];
432 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
434 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
435 if (*sp
== (so
= *bp
))
439 if (cur_so
&& MSflag
) {
443 moveto(RTOD(j
), CTOD(i
));
447 putpad(so
? SOstr
: SEstr
);
452 putstr(so
? "XX" : " ");
455 * Look ahead a bit, to avoid extra motion if
456 * we will be redrawing the cell after the next.
457 * Motion probably takes four or more characters,
458 * so we save even if we rewrite two cells
459 * `unnecessarily'. Skip it all, though, if
460 * the next cell is a different color.
462 #define STOP (B_COLS - 3)
463 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
467 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
475 (void) fflush(stdout
);
476 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
480 * Write a message (set!=0), or clear the same message (set==0).
481 * (We need its length in case we have to overwrite with blanks.)
484 scr_msg(char *s
, int set
)
487 if (set
|| CEstr
== NULL
) {
490 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);