Home | History | Annotate | Line # | Download | only in tetris
screen.c revision 1.5
      1 /*	$NetBSD: screen.c,v 1.5 1997/10/12 02:03:45 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Chris Torek and Darren F. Provine.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)screen.c	8.1 (Berkeley) 5/31/93
     39  */
     40 
     41 /*
     42  * Tetris screen control.
     43  */
     44 
     45 #include <sys/ioctl.h>
     46 
     47 #include <setjmp.h>
     48 #include <signal.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <termios.h>
     53 #include <unistd.h>
     54 
     55 #ifndef sigmask
     56 #define sigmask(s) (1 << ((s) - 1))
     57 #endif
     58 
     59 #include "screen.h"
     60 #include "tetris.h"
     61 
     62 /*
     63  * XXX - need a <termcap.h>
     64  */
     65 int	tgetent __P((char *, const char *));
     66 int	tgetflag __P((const char *));
     67 int	tgetnum __P((const char *));
     68 int	tputs __P((const char *, int, int (*)(int)));
     69 
     70 static cell curscreen[B_SIZE];	/* 1 => standout (or otherwise marked) */
     71 static int curscore;
     72 static int isset;		/* true => terminal is in game mode */
     73 static struct termios oldtt;
     74 static void (*tstp) __P((int));
     75 
     76 static	void	scr_stop __P((int));
     77 static	void	stopset __P((int));
     78 extern	char   *tgetstr __P((char *, char **));
     79 extern	char   *tgoto __P((char *, int, int));
     80 
     81 
     82 /*
     83  * Capabilities from TERMCAP.
     84  */
     85 char	PC, *BC, *UP;		/* tgoto requires globals: ugh! */
     86 speed_t	ospeed;
     87 
     88 static char
     89 	*bcstr,			/* backspace char */
     90 	*CEstr,			/* clear to end of line */
     91 	*CLstr,			/* clear screen */
     92 	*CMstr,			/* cursor motion string */
     93 #ifdef unneeded
     94 	*CRstr,			/* "\r" equivalent */
     95 #endif
     96 	*HOstr,			/* cursor home */
     97 	*LLstr,			/* last line, first column */
     98 	*pcstr,			/* pad character */
     99 	*TEstr,			/* end cursor motion mode */
    100 	*TIstr;			/* begin cursor motion mode */
    101 char
    102 	*SEstr,			/* end standout mode */
    103 	*SOstr;			/* begin standout mode */
    104 static int
    105 	COnum,			/* co# value */
    106 	LInum,			/* li# value */
    107 	MSflag;			/* can move in standout mode */
    108 
    109 
    110 struct tcsinfo {	/* termcap string info; some abbrevs above */
    111 	char tcname[3];
    112 	char **tcaddr;
    113 } tcstrings[] = {
    114 	{"bc", &bcstr},
    115 	{"ce", &CEstr},
    116 	{"cl", &CLstr},
    117 	{"cm", &CMstr},
    118 #ifdef unneeded
    119 	{"cr", &CRstr},
    120 #endif
    121 	{"le", &BC},		/* move cursor left one space */
    122 	{"pc", &pcstr},
    123 	{"se", &SEstr},
    124 	{"so", &SOstr},
    125 	{"te", &TEstr},
    126 	{"ti", &TIstr},
    127 	{"up", &UP},		/* cursor up */
    128 	{ {0}, NULL}
    129 };
    130 
    131 /* This is where we will actually stuff the information */
    132 
    133 static char combuf[1024], tbuf[1024];
    134 
    135 
    136 /*
    137  * Routine used by tputs().
    138  */
    139 int
    140 put(c)
    141 	int c;
    142 {
    143 
    144 	return (putchar(c));
    145 }
    146 
    147 /*
    148  * putstr() is for unpadded strings (either as in termcap(5) or
    149  * simply literal strings); putpad() is for padded strings with
    150  * count=1.  (See screen.h for putpad().)
    151  */
    152 #define	putstr(s)	(void)fputs(s, stdout)
    153 #define	moveto(r, c)	putpad(tgoto(CMstr, c, r))
    154 
    155 /*
    156  * Set up from termcap.
    157  */
    158 void
    159 scr_init()
    160 {
    161 	static int bsflag, xsflag, sgnum;
    162 #ifdef unneeded
    163 	static int ncflag;
    164 #endif
    165 	char *term, *fill;
    166 	static struct tcninfo {	/* termcap numeric and flag info */
    167 		char tcname[3];
    168 		int *tcaddr;
    169 	} tcflags[] = {
    170 		{"bs", &bsflag},
    171 		{"ms", &MSflag},
    172 #ifdef unneeded
    173 		{"nc", &ncflag},
    174 #endif
    175 		{"xs", &xsflag},
    176 		{ {0}, NULL}
    177 	}, tcnums[] = {
    178 		{"co", &COnum},
    179 		{"li", &LInum},
    180 		{"sg", &sgnum},
    181 		{ {0}, NULL}
    182 	};
    183 
    184 	if ((term = getenv("TERM")) == NULL)
    185 		stop("you must set the TERM environment variable");
    186 	if (tgetent(tbuf, term) <= 0)
    187 		stop("cannot find your termcap");
    188 	fill = combuf;
    189 	{
    190 		register struct tcsinfo *p;
    191 
    192 		for (p = tcstrings; p->tcaddr; p++)
    193 			*p->tcaddr = tgetstr(p->tcname, &fill);
    194 	}
    195 	{
    196 		register struct tcninfo *p;
    197 
    198 		for (p = tcflags; p->tcaddr; p++)
    199 			*p->tcaddr = tgetflag(p->tcname);
    200 		for (p = tcnums; p->tcaddr; p++)
    201 			*p->tcaddr = tgetnum(p->tcname);
    202 	}
    203 	if (bsflag)
    204 		BC = "\b";
    205 	else if (BC == NULL && bcstr != NULL)
    206 		BC = bcstr;
    207 	if (CLstr == NULL)
    208 		stop("cannot clear screen");
    209 	if (CMstr == NULL || UP == NULL || BC == NULL)
    210 		stop("cannot do random cursor positioning via tgoto()");
    211 	PC = pcstr ? *pcstr : 0;
    212 	if (sgnum >= 0 || xsflag)
    213 		SOstr = SEstr = NULL;
    214 #ifdef unneeded
    215 	if (ncflag)
    216 		CRstr = NULL;
    217 	else if (CRstr == NULL)
    218 		CRstr = "\r";
    219 #endif
    220 }
    221 
    222 /* this foolery is needed to modify tty state `atomically' */
    223 static jmp_buf scr_onstop;
    224 
    225 static void
    226 stopset(sig)
    227 	int sig;
    228 {
    229 	sigset_t sigset;
    230 
    231 	(void) signal(sig, SIG_DFL);
    232 	(void) kill(getpid(), sig);
    233 	sigemptyset(&sigset);
    234 	sigaddset(&sigset, sig);
    235 	(void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0);
    236 	longjmp(scr_onstop, 1);
    237 }
    238 
    239 static void
    240 scr_stop(sig)
    241 	int sig;
    242 {
    243 	sigset_t sigset;
    244 
    245 	scr_end();
    246 	(void) kill(getpid(), sig);
    247 	sigemptyset(&sigset);
    248 	sigaddset(&sigset, sig);
    249 	(void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0);
    250 	scr_set();
    251 	scr_msg(key_msg, 1);
    252 }
    253 
    254 /*
    255  * Set up screen mode.
    256  */
    257 void
    258 scr_set()
    259 {
    260 	struct winsize ws;
    261 	struct termios newtt;
    262 	sigset_t sigset, osigset;
    263 	void (*ttou) __P((int));
    264 
    265 	sigemptyset(&sigset);
    266 	sigaddset(&sigset, SIGTSTP);
    267 	sigaddset(&sigset, SIGTTOU);
    268 	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
    269 	if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
    270 		(void) signal(SIGTSTP, SIG_IGN);
    271 	if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN)
    272 		(void) signal(SIGTTOU, SIG_IGN);
    273 	/*
    274 	 * At last, we are ready to modify the tty state.  If
    275 	 * we stop while at it, stopset() above will longjmp back
    276 	 * to the setjmp here and we will start over.
    277 	 */
    278 	(void) setjmp(scr_onstop);
    279 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    280 	Rows = 0, Cols = 0;
    281 	if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
    282 		Rows = ws.ws_row;
    283 		Cols = ws.ws_col;
    284 	}
    285 	if (Rows == 0)
    286 		Rows = LInum;
    287 	if (Cols == 0)
    288 	Cols = COnum;
    289 	if (Rows < MINROWS || Cols < MINCOLS) {
    290 		(void) fprintf(stderr,
    291 		    "the screen is too small: must be at least %d x %d",
    292 		    MINROWS, MINCOLS);
    293 		stop("");	/* stop() supplies \n */
    294 	}
    295 	if (tcgetattr(0, &oldtt) < 0)
    296 		stop("tcgetattr() fails");
    297 	newtt = oldtt;
    298 	newtt.c_lflag &= ~(ICANON|ECHO);
    299 	newtt.c_oflag &= ~OXTABS;
    300 	if (tcsetattr(0, TCSADRAIN, &newtt) < 0)
    301 		stop("tcsetattr() fails");
    302 	ospeed = cfgetospeed(&newtt);
    303 	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
    304 
    305 	/*
    306 	 * We made it.  We are now in screen mode, modulo TIstr
    307 	 * (which we will fix immediately).
    308 	 */
    309 	if (TIstr)
    310 		putstr(TIstr);	/* termcap(5) says this is not padded */
    311 	if (tstp != SIG_IGN)
    312 		(void) signal(SIGTSTP, scr_stop);
    313 	if (ttou != SIG_IGN)
    314 		(void) signal(SIGTTOU, ttou);
    315 
    316 	isset = 1;
    317 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    318 	scr_clear();
    319 }
    320 
    321 /*
    322  * End screen mode.
    323  */
    324 void
    325 scr_end()
    326 {
    327 	sigset_t sigset, osigset;
    328 
    329 	sigemptyset(&sigset);
    330 	sigaddset(&sigset, SIGTSTP);
    331 	sigaddset(&sigset, SIGTTOU);
    332 	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
    333 	/* move cursor to last line */
    334 	if (LLstr)
    335 		putstr(LLstr);	/* termcap(5) says this is not padded */
    336 	else
    337 		moveto(Rows - 1, 0);
    338 	/* exit screen mode */
    339 	if (TEstr)
    340 		putstr(TEstr);	/* termcap(5) says this is not padded */
    341 	(void) fflush(stdout);
    342 	(void) tcsetattr(0, TCSADRAIN, &oldtt);
    343 	isset = 0;
    344 	/* restore signals */
    345 	(void) signal(SIGTSTP, tstp);
    346 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    347 }
    348 
    349 void
    350 stop(why)
    351 	char *why;
    352 {
    353 
    354 	if (isset)
    355 		scr_end();
    356 	(void) fprintf(stderr, "aborting: %s\n", why);
    357 	exit(1);
    358 }
    359 
    360 /*
    361  * Clear the screen, forgetting the current contents in the process.
    362  */
    363 void
    364 scr_clear()
    365 {
    366 
    367 	putpad(CLstr);
    368 	curscore = -1;
    369 	bzero((char *)curscreen, sizeof(curscreen));
    370 }
    371 
    372 #if vax && !__GNUC__
    373 typedef int regcell;	/* pcc is bad at `register char', etc */
    374 #else
    375 typedef cell regcell;
    376 #endif
    377 
    378 /*
    379  * Update the screen.
    380  */
    381 void
    382 scr_update()
    383 {
    384 	register cell *bp, *sp;
    385 	register regcell so, cur_so = 0;
    386 	register int i, ccol, j;
    387 	sigset_t sigset, osigset;
    388 
    389 	sigemptyset(&sigset);
    390 	sigaddset(&sigset, SIGTSTP);
    391 	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
    392 
    393 	/* always leave cursor after last displayed point */
    394 	curscreen[D_LAST * B_COLS - 1] = -1;
    395 
    396 	if (score != curscore) {
    397 		if (HOstr)
    398 			putpad(HOstr);
    399 		else
    400 			moveto(0, 0);
    401 		(void) printf("%d", score);
    402 		curscore = score;
    403 	}
    404 
    405 	bp = &board[D_FIRST * B_COLS];
    406 	sp = &curscreen[D_FIRST * B_COLS];
    407 	for (j = D_FIRST; j < D_LAST; j++) {
    408 		ccol = -1;
    409 		for (i = 0; i < B_COLS; bp++, sp++, i++) {
    410 			if (*sp == (so = *bp))
    411 				continue;
    412 			*sp = so;
    413 			if (i != ccol) {
    414 				if (cur_so && MSflag) {
    415 					putpad(SEstr);
    416 					cur_so = 0;
    417 				}
    418 				moveto(RTOD(j), CTOD(i));
    419 			}
    420 			if (SOstr) {
    421 				if (so != cur_so) {
    422 					putpad(so ? SOstr : SEstr);
    423 					cur_so = so;
    424 				}
    425 				putstr("  ");
    426 			} else
    427 				putstr(so ? "XX" : "  ");
    428 			ccol = i + 1;
    429 			/*
    430 			 * Look ahead a bit, to avoid extra motion if
    431 			 * we will be redrawing the cell after the next.
    432 			 * Motion probably takes four or more characters,
    433 			 * so we save even if we rewrite two cells
    434 			 * `unnecessarily'.  Skip it all, though, if
    435 			 * the next cell is a different color.
    436 			 */
    437 #define	STOP (B_COLS - 3)
    438 			if (i > STOP || sp[1] != bp[1] || so != bp[1])
    439 				continue;
    440 			if (sp[2] != bp[2])
    441 				sp[1] = -1;
    442 			else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
    443 				sp[2] = -1;
    444 				sp[1] = -1;
    445 			}
    446 		}
    447 	}
    448 	if (cur_so)
    449 		putpad(SEstr);
    450 	(void) fflush(stdout);
    451 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    452 }
    453 
    454 /*
    455  * Write a message (set!=0), or clear the same message (set==0).
    456  * (We need its length in case we have to overwrite with blanks.)
    457  */
    458 void
    459 scr_msg(s, set)
    460 	register char *s;
    461 	int set;
    462 {
    463 
    464 	if (set || CEstr == NULL) {
    465 		register int l = strlen(s);
    466 
    467 		moveto(Rows - 2, ((Cols - l) >> 1) - 1);
    468 		if (set)
    469 			putstr(s);
    470 		else
    471 			while (--l >= 0)
    472 				(void) putchar(' ');
    473 	} else {
    474 		moveto(Rows - 2, 0);
    475 		putpad(CEstr);
    476 	}
    477 }
    478