Home | History | Annotate | Line # | Download | only in tetris
screen.c revision 1.28
      1  1.28  christos /*	$NetBSD: screen.c,v 1.28 2014/06/11 16:47:39 christos Exp $	*/
      2   1.2       cgd 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1992, 1993
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Chris Torek and Darren F. Provine.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.18       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  *
     34   1.1       cgd  *	@(#)screen.c	8.1 (Berkeley) 5/31/93
     35   1.1       cgd  */
     36   1.1       cgd 
     37   1.1       cgd /*
     38   1.1       cgd  * Tetris screen control.
     39   1.1       cgd  */
     40   1.1       cgd 
     41  1.20     perry #include <sys/cdefs.h>
     42   1.1       cgd #include <sys/ioctl.h>
     43   1.1       cgd 
     44   1.1       cgd #include <setjmp.h>
     45   1.1       cgd #include <signal.h>
     46   1.1       cgd #include <stdio.h>
     47   1.1       cgd #include <stdlib.h>
     48   1.1       cgd #include <string.h>
     49  1.26       roy #include <term.h>
     50   1.3   mycroft #include <termios.h>
     51   1.1       cgd #include <unistd.h>
     52   1.1       cgd 
     53   1.1       cgd #ifndef sigmask
     54   1.1       cgd #define sigmask(s) (1 << ((s) - 1))
     55   1.1       cgd #endif
     56   1.1       cgd 
     57   1.1       cgd #include "screen.h"
     58   1.1       cgd #include "tetris.h"
     59   1.1       cgd 
     60   1.1       cgd static cell curscreen[B_SIZE];	/* 1 => standout (or otherwise marked) */
     61   1.1       cgd static int curscore;
     62   1.1       cgd static int isset;		/* true => terminal is in game mode */
     63   1.3   mycroft static struct termios oldtt;
     64  1.19       jsm static void (*tstp)(int);
     65   1.1       cgd 
     66  1.19       jsm static	void	scr_stop(int);
     67  1.21     perry static	void	stopset(int) __dead;
     68   1.1       cgd 
     69   1.1       cgd 
     70   1.1       cgd /*
     71   1.1       cgd  * Routine used by tputs().
     72   1.1       cgd  */
     73  1.13     lukem int
     74  1.23  dholland put(int c)
     75   1.1       cgd {
     76   1.1       cgd 
     77  1.13     lukem 	return (putchar(c));
     78   1.1       cgd }
     79   1.1       cgd 
     80   1.1       cgd /*
     81   1.1       cgd  * putstr() is for unpadded strings (either as in termcap(5) or
     82   1.1       cgd  * simply literal strings); putpad() is for padded strings with
     83   1.1       cgd  * count=1.  (See screen.h for putpad().)
     84   1.1       cgd  */
     85   1.1       cgd #define	putstr(s)	(void)fputs(s, stdout)
     86  1.14     blymn 
     87  1.24  dholland static void
     88  1.14     blymn moveto(int r, int c)
     89  1.14     blymn {
     90  1.26       roy 	char *buf;
     91  1.14     blymn 
     92  1.27       roy 	buf = tiparm(cursor_address, r, c);
     93  1.26       roy 	if (buf != NULL)
     94  1.14     blymn 		putpad(buf);
     95  1.14     blymn }
     96   1.1       cgd 
     97  1.28  christos static void
     98  1.28  christos setcolor(int c)
     99  1.28  christos {
    100  1.28  christos 	char *buf;
    101  1.28  christos 	if (set_a_foreground == NULL)
    102  1.28  christos 		return;
    103  1.28  christos 
    104  1.28  christos 	buf = tiparm(set_a_foreground, c == 7 ? 0 : c);
    105  1.28  christos 	if (buf != NULL)
    106  1.28  christos 		putpad(buf);
    107  1.28  christos }
    108  1.28  christos 
    109   1.1       cgd /*
    110   1.1       cgd  * Set up from termcap.
    111   1.1       cgd  */
    112   1.1       cgd void
    113  1.23  dholland scr_init(void)
    114   1.1       cgd {
    115   1.1       cgd 
    116  1.26       roy 	setupterm(NULL, 0, NULL);
    117  1.26       roy 	if (clear_screen == NULL)
    118   1.1       cgd 		stop("cannot clear screen");
    119  1.26       roy 	if (cursor_address == NULL || cursor_up == NULL)
    120  1.26       roy 		stop("cannot do random cursor positioning");
    121   1.1       cgd }
    122   1.1       cgd 
    123   1.1       cgd /* this foolery is needed to modify tty state `atomically' */
    124   1.1       cgd static jmp_buf scr_onstop;
    125   1.1       cgd 
    126   1.1       cgd static void
    127  1.23  dholland stopset(int sig)
    128   1.1       cgd {
    129  1.22  dholland 	sigset_t set;
    130   1.3   mycroft 
    131   1.1       cgd 	(void) signal(sig, SIG_DFL);
    132   1.1       cgd 	(void) kill(getpid(), sig);
    133  1.22  dholland 	sigemptyset(&set);
    134  1.22  dholland 	sigaddset(&set, sig);
    135  1.22  dholland 	(void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
    136   1.1       cgd 	longjmp(scr_onstop, 1);
    137   1.1       cgd }
    138   1.1       cgd 
    139   1.1       cgd static void
    140  1.23  dholland scr_stop(int sig)
    141   1.1       cgd {
    142  1.22  dholland 	sigset_t set;
    143   1.3   mycroft 
    144   1.1       cgd 	scr_end();
    145   1.3   mycroft 	(void) kill(getpid(), sig);
    146  1.22  dholland 	sigemptyset(&set);
    147  1.22  dholland 	sigaddset(&set, sig);
    148  1.22  dholland 	(void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
    149   1.1       cgd 	scr_set();
    150   1.1       cgd 	scr_msg(key_msg, 1);
    151   1.1       cgd }
    152   1.1       cgd 
    153   1.1       cgd /*
    154   1.1       cgd  * Set up screen mode.
    155   1.1       cgd  */
    156   1.1       cgd void
    157  1.23  dholland scr_set(void)
    158   1.1       cgd {
    159   1.1       cgd 	struct winsize ws;
    160   1.3   mycroft 	struct termios newtt;
    161  1.22  dholland 	sigset_t nsigset, osigset;
    162  1.19       jsm 	void (*ttou)(int);
    163   1.1       cgd 
    164  1.22  dholland 	sigemptyset(&nsigset);
    165  1.22  dholland 	sigaddset(&nsigset, SIGTSTP);
    166  1.22  dholland 	sigaddset(&nsigset, SIGTTOU);
    167  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    168   1.1       cgd 	if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
    169   1.1       cgd 		(void) signal(SIGTSTP, SIG_IGN);
    170   1.3   mycroft 	if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN)
    171   1.3   mycroft 		(void) signal(SIGTTOU, SIG_IGN);
    172   1.1       cgd 	/*
    173   1.1       cgd 	 * At last, we are ready to modify the tty state.  If
    174   1.1       cgd 	 * we stop while at it, stopset() above will longjmp back
    175   1.1       cgd 	 * to the setjmp here and we will start over.
    176   1.1       cgd 	 */
    177   1.1       cgd 	(void) setjmp(scr_onstop);
    178   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    179   1.1       cgd 	Rows = 0, Cols = 0;
    180   1.1       cgd 	if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
    181   1.1       cgd 		Rows = ws.ws_row;
    182   1.1       cgd 		Cols = ws.ws_col;
    183   1.1       cgd 	}
    184   1.1       cgd 	if (Rows == 0)
    185  1.26       roy 		Rows = lines;
    186   1.1       cgd 	if (Cols == 0)
    187  1.26       roy 	    Cols = columns;
    188   1.1       cgd 	if (Rows < MINROWS || Cols < MINCOLS) {
    189   1.1       cgd 		(void) fprintf(stderr,
    190   1.8   hubertf 		    "the screen is too small: must be at least %dx%d, ",
    191   1.8   hubertf 		    MINCOLS, MINROWS);
    192   1.1       cgd 		stop("");	/* stop() supplies \n */
    193   1.1       cgd 	}
    194   1.3   mycroft 	if (tcgetattr(0, &oldtt) < 0)
    195   1.3   mycroft 		stop("tcgetattr() fails");
    196   1.1       cgd 	newtt = oldtt;
    197   1.3   mycroft 	newtt.c_lflag &= ~(ICANON|ECHO);
    198   1.3   mycroft 	newtt.c_oflag &= ~OXTABS;
    199   1.3   mycroft 	if (tcsetattr(0, TCSADRAIN, &newtt) < 0)
    200   1.3   mycroft 		stop("tcsetattr() fails");
    201   1.3   mycroft 	ospeed = cfgetospeed(&newtt);
    202  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    203   1.1       cgd 
    204   1.1       cgd 	/*
    205   1.1       cgd 	 * We made it.  We are now in screen mode, modulo TIstr
    206   1.1       cgd 	 * (which we will fix immediately).
    207   1.1       cgd 	 */
    208  1.26       roy 	if (enter_ca_mode)
    209  1.26       roy 		putstr(enter_ca_mode);
    210  1.26       roy 	if (cursor_invisible)
    211  1.26       roy 		putstr(cursor_invisible);
    212   1.1       cgd 	if (tstp != SIG_IGN)
    213   1.1       cgd 		(void) signal(SIGTSTP, scr_stop);
    214   1.3   mycroft 	if (ttou != SIG_IGN)
    215   1.3   mycroft 		(void) signal(SIGTTOU, ttou);
    216   1.1       cgd 
    217   1.1       cgd 	isset = 1;
    218   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    219   1.1       cgd 	scr_clear();
    220   1.1       cgd }
    221   1.1       cgd 
    222   1.1       cgd /*
    223   1.1       cgd  * End screen mode.
    224   1.1       cgd  */
    225   1.1       cgd void
    226  1.23  dholland scr_end(void)
    227   1.1       cgd {
    228  1.22  dholland 	sigset_t nsigset, osigset;
    229   1.1       cgd 
    230  1.22  dholland 	sigemptyset(&nsigset);
    231  1.22  dholland 	sigaddset(&nsigset, SIGTSTP);
    232  1.22  dholland 	sigaddset(&nsigset, SIGTTOU);
    233  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    234   1.1       cgd 	/* move cursor to last line */
    235  1.26       roy 	if (cursor_to_ll)
    236  1.26       roy 		putstr(cursor_to_ll);
    237   1.1       cgd 	else
    238   1.1       cgd 		moveto(Rows - 1, 0);
    239   1.1       cgd 	/* exit screen mode */
    240  1.26       roy 	if (exit_ca_mode)
    241  1.26       roy 		putstr(exit_ca_mode);
    242  1.26       roy 	if (cursor_normal)
    243  1.26       roy 		putstr(cursor_normal);
    244   1.1       cgd 	(void) fflush(stdout);
    245   1.3   mycroft 	(void) tcsetattr(0, TCSADRAIN, &oldtt);
    246   1.1       cgd 	isset = 0;
    247   1.1       cgd 	/* restore signals */
    248   1.1       cgd 	(void) signal(SIGTSTP, tstp);
    249   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    250   1.1       cgd }
    251   1.1       cgd 
    252   1.1       cgd void
    253  1.23  dholland stop(const char *why)
    254   1.1       cgd {
    255   1.1       cgd 
    256   1.1       cgd 	if (isset)
    257   1.1       cgd 		scr_end();
    258   1.1       cgd 	(void) fprintf(stderr, "aborting: %s\n", why);
    259   1.1       cgd 	exit(1);
    260   1.1       cgd }
    261   1.1       cgd 
    262   1.1       cgd /*
    263   1.1       cgd  * Clear the screen, forgetting the current contents in the process.
    264   1.1       cgd  */
    265   1.1       cgd void
    266  1.23  dholland scr_clear(void)
    267   1.1       cgd {
    268   1.1       cgd 
    269  1.26       roy 	putpad(clear_screen);
    270   1.1       cgd 	curscore = -1;
    271   1.7     perry 	memset((char *)curscreen, 0, sizeof(curscreen));
    272   1.1       cgd }
    273   1.1       cgd 
    274   1.1       cgd #if vax && !__GNUC__
    275   1.1       cgd typedef int regcell;	/* pcc is bad at `register char', etc */
    276   1.1       cgd #else
    277   1.1       cgd typedef cell regcell;
    278   1.1       cgd #endif
    279   1.1       cgd 
    280   1.1       cgd /*
    281   1.1       cgd  * Update the screen.
    282   1.1       cgd  */
    283   1.1       cgd void
    284  1.23  dholland scr_update(void)
    285   1.1       cgd {
    286  1.17       wiz 	cell *bp, *sp;
    287  1.17       wiz 	regcell so, cur_so = 0;
    288  1.17       wiz 	int i, ccol, j;
    289  1.22  dholland 	sigset_t nsigset, osigset;
    290  1.11       jsm 	static const struct shape *lastshape;
    291   1.3   mycroft 
    292  1.22  dholland 	sigemptyset(&nsigset);
    293  1.22  dholland 	sigaddset(&nsigset, SIGTSTP);
    294  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    295   1.1       cgd 
    296   1.1       cgd 	/* always leave cursor after last displayed point */
    297   1.1       cgd 	curscreen[D_LAST * B_COLS - 1] = -1;
    298   1.1       cgd 
    299   1.1       cgd 	if (score != curscore) {
    300  1.26       roy 		if (cursor_home)
    301  1.26       roy 			putpad(cursor_home);
    302   1.1       cgd 		else
    303   1.1       cgd 			moveto(0, 0);
    304   1.8   hubertf 		(void) printf("Score: %d", score);
    305   1.1       cgd 		curscore = score;
    306   1.1       cgd 	}
    307   1.1       cgd 
    308   1.8   hubertf 	/* draw preview of nextpattern */
    309   1.9   hubertf 	if (showpreview && (nextshape != lastshape)) {
    310   1.8   hubertf 		static int r=5, c=2;
    311   1.8   hubertf 		int tr, tc, t;
    312   1.8   hubertf 
    313   1.8   hubertf 		lastshape = nextshape;
    314   1.8   hubertf 
    315   1.8   hubertf 		/* clean */
    316  1.26       roy 		putpad(exit_standout_mode);
    317   1.8   hubertf 		moveto(r-1, c-1); putstr("          ");
    318   1.8   hubertf 		moveto(r,   c-1); putstr("          ");
    319   1.8   hubertf 		moveto(r+1, c-1); putstr("          ");
    320   1.8   hubertf 		moveto(r+2, c-1); putstr("          ");
    321   1.8   hubertf 
    322   1.8   hubertf 		moveto(r-3, c-2);
    323   1.8   hubertf 		putstr("Next shape:");
    324   1.8   hubertf 
    325   1.8   hubertf 		/* draw */
    326  1.26       roy 		putpad(enter_standout_mode);
    327  1.28  christos 		setcolor(nextshape->color);
    328   1.8   hubertf 		moveto(r, 2*c);
    329   1.8   hubertf 		putstr("  ");
    330   1.8   hubertf 		for(i=0; i<3; i++) {
    331   1.8   hubertf 			t = c + r*B_COLS;
    332   1.8   hubertf 			t += nextshape->off[i];
    333   1.8   hubertf 
    334   1.8   hubertf 			tr = t / B_COLS;
    335   1.8   hubertf 			tc = t % B_COLS;
    336   1.8   hubertf 
    337   1.8   hubertf 			moveto(tr, 2*tc);
    338   1.8   hubertf 			putstr("  ");
    339   1.8   hubertf 		}
    340  1.26       roy 		putpad(exit_standout_mode);
    341   1.8   hubertf 	}
    342   1.8   hubertf 
    343   1.1       cgd 	bp = &board[D_FIRST * B_COLS];
    344   1.1       cgd 	sp = &curscreen[D_FIRST * B_COLS];
    345   1.1       cgd 	for (j = D_FIRST; j < D_LAST; j++) {
    346   1.1       cgd 		ccol = -1;
    347   1.1       cgd 		for (i = 0; i < B_COLS; bp++, sp++, i++) {
    348   1.1       cgd 			if (*sp == (so = *bp))
    349   1.1       cgd 				continue;
    350   1.1       cgd 			*sp = so;
    351   1.1       cgd 			if (i != ccol) {
    352  1.26       roy 				if (cur_so && move_standout_mode) {
    353  1.26       roy 					putpad(exit_standout_mode);
    354   1.1       cgd 					cur_so = 0;
    355   1.1       cgd 				}
    356   1.1       cgd 				moveto(RTOD(j), CTOD(i));
    357   1.1       cgd 			}
    358  1.26       roy 			if (enter_standout_mode) {
    359   1.1       cgd 				if (so != cur_so) {
    360  1.26       roy 					putpad(so ?
    361  1.26       roy 					    enter_standout_mode :
    362  1.26       roy 					    exit_standout_mode);
    363   1.1       cgd 					cur_so = so;
    364   1.1       cgd 				}
    365  1.28  christos 				setcolor(so);
    366  1.28  christos #ifdef DEBUG
    367  1.28  christos 				char buf[3];
    368  1.28  christos 				snprintf(buf, sizeof(buf), "%d%d", so, so);
    369  1.28  christos 				putstr(buf);
    370  1.28  christos #else
    371   1.1       cgd 				putstr("  ");
    372  1.28  christos #endif
    373   1.1       cgd 			} else
    374   1.1       cgd 				putstr(so ? "XX" : "  ");
    375   1.1       cgd 			ccol = i + 1;
    376   1.1       cgd 			/*
    377   1.1       cgd 			 * Look ahead a bit, to avoid extra motion if
    378   1.1       cgd 			 * we will be redrawing the cell after the next.
    379   1.1       cgd 			 * Motion probably takes four or more characters,
    380   1.1       cgd 			 * so we save even if we rewrite two cells
    381   1.1       cgd 			 * `unnecessarily'.  Skip it all, though, if
    382   1.1       cgd 			 * the next cell is a different color.
    383   1.1       cgd 			 */
    384   1.1       cgd #define	STOP (B_COLS - 3)
    385   1.1       cgd 			if (i > STOP || sp[1] != bp[1] || so != bp[1])
    386   1.1       cgd 				continue;
    387   1.1       cgd 			if (sp[2] != bp[2])
    388   1.1       cgd 				sp[1] = -1;
    389   1.1       cgd 			else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
    390   1.1       cgd 				sp[2] = -1;
    391   1.1       cgd 				sp[1] = -1;
    392   1.1       cgd 			}
    393   1.1       cgd 		}
    394   1.1       cgd 	}
    395   1.1       cgd 	if (cur_so)
    396  1.26       roy 		putpad(exit_standout_mode);
    397   1.1       cgd 	(void) fflush(stdout);
    398   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    399   1.1       cgd }
    400   1.1       cgd 
    401   1.1       cgd /*
    402   1.1       cgd  * Write a message (set!=0), or clear the same message (set==0).
    403   1.1       cgd  * (We need its length in case we have to overwrite with blanks.)
    404   1.1       cgd  */
    405   1.1       cgd void
    406  1.23  dholland scr_msg(char *s, int set)
    407   1.1       cgd {
    408   1.1       cgd 
    409  1.26       roy 	if (set || clr_eol == NULL) {
    410  1.17       wiz 		int l = strlen(s);
    411   1.1       cgd 
    412   1.1       cgd 		moveto(Rows - 2, ((Cols - l) >> 1) - 1);
    413   1.1       cgd 		if (set)
    414   1.1       cgd 			putstr(s);
    415   1.1       cgd 		else
    416   1.1       cgd 			while (--l >= 0)
    417   1.1       cgd 				(void) putchar(' ');
    418   1.1       cgd 	} else {
    419   1.1       cgd 		moveto(Rows - 2, 0);
    420  1.26       roy 		putpad(clr_eol);
    421   1.1       cgd 	}
    422   1.1       cgd }
    423