1 /* $NetBSD: screen.c,v 1.24 2009/08/12 08:51:21 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 */
88 *VIstr
, /* make cursor invisible */
89 *VEstr
; /* make cursor appear normal */
91 *SEstr
, /* end standout mode */
92 *SOstr
; /* begin standout mode */
94 COnum
, /* co# value */
95 LInum
, /* li# value */
96 MSflag
; /* can move in standout mode */
99 static struct tcsinfo
{ /* termcap string info; some abbrevs above */
110 {"le", &BC
}, /* move cursor left one space */
118 {"up", &UP
}, /* cursor up */
122 /* This is where we will actually stuff the information */
124 static struct tinfo
*info
;
127 * Routine used by tputs().
137 * putstr() is for unpadded strings (either as in termcap(5) or
138 * simply literal strings); putpad() is for padded strings with
139 * count=1. (See screen.h for putpad().)
141 #define putstr(s) (void)fputs(s, stdout)
148 if (t_goto(info
, CMstr
, c
, r
, buf
, 255) == 0)
153 * Set up from termcap.
158 static int bsflag
, xsflag
, sgnum
;
163 static struct tcninfo
{ /* termcap numeric and flag info */
180 static char backspace
[] = "\b";
182 if ((term
= getenv("TERM")) == NULL
)
183 stop("you must set the TERM environment variable");
184 if (t_getent(&info
, term
) <= 0)
185 stop("cannot find your termcap");
189 for (p
= tcstrings
; p
->tcaddr
; p
++)
190 *p
->tcaddr
= t_agetstr(info
, p
->tcname
);
195 for (p
= tcflags
; p
->tcaddr
; p
++)
196 *p
->tcaddr
= t_getflag(info
, p
->tcname
);
197 for (p
= tcnums
; p
->tcaddr
; p
++)
198 *p
->tcaddr
= t_getnum(info
, p
->tcname
);
202 else if (BC
== NULL
&& bcstr
!= NULL
)
205 stop("cannot clear screen");
206 if (CMstr
== NULL
|| UP
== NULL
|| BC
== NULL
)
207 stop("cannot do random cursor positioning via tgoto()");
208 PC
= pcstr
? *pcstr
: 0;
209 if (sgnum
>= 0 || xsflag
)
210 SOstr
= SEstr
= NULL
;
214 else if (CRstr
== NULL
)
219 /* this foolery is needed to modify tty state `atomically' */
220 static jmp_buf scr_onstop
;
227 (void) signal(sig
, SIG_DFL
);
228 (void) kill(getpid(), sig
);
230 sigaddset(&set
, sig
);
231 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
232 longjmp(scr_onstop
, 1);
241 (void) kill(getpid(), sig
);
243 sigaddset(&set
, sig
);
244 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
250 * Set up screen mode.
256 struct termios newtt
;
257 sigset_t nsigset
, osigset
;
260 sigemptyset(&nsigset
);
261 sigaddset(&nsigset
, SIGTSTP
);
262 sigaddset(&nsigset
, SIGTTOU
);
263 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
264 if ((tstp
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
265 (void) signal(SIGTSTP
, SIG_IGN
);
266 if ((ttou
= signal(SIGTTOU
, stopset
)) == SIG_IGN
)
267 (void) signal(SIGTTOU
, SIG_IGN
);
269 * At last, we are ready to modify the tty state. If
270 * we stop while at it, stopset() above will longjmp back
271 * to the setjmp here and we will start over.
273 (void) setjmp(scr_onstop
);
274 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
276 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
284 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
285 (void) fprintf(stderr
,
286 "the screen is too small: must be at least %dx%d, ",
288 stop(""); /* stop() supplies \n */
290 if (tcgetattr(0, &oldtt
) < 0)
291 stop("tcgetattr() fails");
293 newtt
.c_lflag
&= ~(ICANON
|ECHO
);
294 newtt
.c_oflag
&= ~OXTABS
;
295 if (tcsetattr(0, TCSADRAIN
, &newtt
) < 0)
296 stop("tcsetattr() fails");
297 ospeed
= cfgetospeed(&newtt
);
298 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
301 * We made it. We are now in screen mode, modulo TIstr
302 * (which we will fix immediately).
305 putstr(TIstr
); /* termcap(5) says this is not padded */
307 putstr(VIstr
); /* termcap(5) says this is not padded */
309 (void) signal(SIGTSTP
, scr_stop
);
311 (void) signal(SIGTTOU
, ttou
);
314 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
324 sigset_t nsigset
, osigset
;
326 sigemptyset(&nsigset
);
327 sigaddset(&nsigset
, SIGTSTP
);
328 sigaddset(&nsigset
, SIGTTOU
);
329 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
330 /* move cursor to last line */
332 putstr(LLstr
); /* termcap(5) says this is not padded */
335 /* exit screen mode */
337 putstr(TEstr
); /* termcap(5) says this is not padded */
339 putstr(VEstr
); /* termcap(5) says this is not padded */
340 (void) fflush(stdout
);
341 (void) tcsetattr(0, TCSADRAIN
, &oldtt
);
343 /* restore signals */
344 (void) signal(SIGTSTP
, tstp
);
345 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
349 stop(const char *why
)
354 (void) fprintf(stderr
, "aborting: %s\n", why
);
359 * Clear the screen, forgetting the current contents in the process.
367 memset((char *)curscreen
, 0, sizeof(curscreen
));
371 typedef int regcell
; /* pcc is bad at `register char', etc */
373 typedef cell regcell
;
383 regcell so
, cur_so
= 0;
385 sigset_t nsigset
, osigset
;
386 static const struct shape
*lastshape
;
388 sigemptyset(&nsigset
);
389 sigaddset(&nsigset
, SIGTSTP
);
390 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
392 /* always leave cursor after last displayed point */
393 curscreen
[D_LAST
* B_COLS
- 1] = -1;
395 if (score
!= curscore
) {
400 (void) printf("Score: %d", score
);
404 /* draw preview of nextpattern */
405 if (showpreview
&& (nextshape
!= lastshape
)) {
409 lastshape
= nextshape
;
413 moveto(r
-1, c
-1); putstr(" ");
414 moveto(r
, c
-1); putstr(" ");
415 moveto(r
+1, c
-1); putstr(" ");
416 moveto(r
+2, c
-1); putstr(" ");
419 putstr("Next shape:");
427 t
+= nextshape
->off
[i
];
438 bp
= &board
[D_FIRST
* B_COLS
];
439 sp
= &curscreen
[D_FIRST
* B_COLS
];
440 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
442 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
443 if (*sp
== (so
= *bp
))
447 if (cur_so
&& MSflag
) {
451 moveto(RTOD(j
), CTOD(i
));
455 putpad(so
? SOstr
: SEstr
);
460 putstr(so
? "XX" : " ");
463 * Look ahead a bit, to avoid extra motion if
464 * we will be redrawing the cell after the next.
465 * Motion probably takes four or more characters,
466 * so we save even if we rewrite two cells
467 * `unnecessarily'. Skip it all, though, if
468 * the next cell is a different color.
470 #define STOP (B_COLS - 3)
471 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
475 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
483 (void) fflush(stdout
);
484 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
488 * Write a message (set!=0), or clear the same message (set==0).
489 * (We need its length in case we have to overwrite with blanks.)
492 scr_msg(char *s
, int set
)
495 if (set
|| CEstr
== NULL
) {
498 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);