Home | History | Annotate | Line # | Download | only in tetris
screen.c revision 1.29
      1  1.29  pgoyette /*	$NetBSD: screen.c,v 1.29 2014/07/13 16:23:55 pgoyette 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.29  pgoyette 	if (nocolor == 1)
    102  1.29  pgoyette 		return;
    103  1.28  christos 	if (set_a_foreground == NULL)
    104  1.28  christos 		return;
    105  1.28  christos 
    106  1.28  christos 	buf = tiparm(set_a_foreground, c == 7 ? 0 : c);
    107  1.28  christos 	if (buf != NULL)
    108  1.28  christos 		putpad(buf);
    109  1.28  christos }
    110  1.28  christos 
    111   1.1       cgd /*
    112   1.1       cgd  * Set up from termcap.
    113   1.1       cgd  */
    114   1.1       cgd void
    115  1.23  dholland scr_init(void)
    116   1.1       cgd {
    117   1.1       cgd 
    118  1.26       roy 	setupterm(NULL, 0, NULL);
    119  1.26       roy 	if (clear_screen == NULL)
    120   1.1       cgd 		stop("cannot clear screen");
    121  1.26       roy 	if (cursor_address == NULL || cursor_up == NULL)
    122  1.26       roy 		stop("cannot do random cursor positioning");
    123   1.1       cgd }
    124   1.1       cgd 
    125   1.1       cgd /* this foolery is needed to modify tty state `atomically' */
    126   1.1       cgd static jmp_buf scr_onstop;
    127   1.1       cgd 
    128   1.1       cgd static void
    129  1.23  dholland stopset(int sig)
    130   1.1       cgd {
    131  1.22  dholland 	sigset_t set;
    132   1.3   mycroft 
    133   1.1       cgd 	(void) signal(sig, SIG_DFL);
    134   1.1       cgd 	(void) kill(getpid(), sig);
    135  1.22  dholland 	sigemptyset(&set);
    136  1.22  dholland 	sigaddset(&set, sig);
    137  1.22  dholland 	(void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
    138   1.1       cgd 	longjmp(scr_onstop, 1);
    139   1.1       cgd }
    140   1.1       cgd 
    141   1.1       cgd static void
    142  1.23  dholland scr_stop(int sig)
    143   1.1       cgd {
    144  1.22  dholland 	sigset_t set;
    145   1.3   mycroft 
    146   1.1       cgd 	scr_end();
    147   1.3   mycroft 	(void) kill(getpid(), sig);
    148  1.22  dholland 	sigemptyset(&set);
    149  1.22  dholland 	sigaddset(&set, sig);
    150  1.22  dholland 	(void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
    151   1.1       cgd 	scr_set();
    152   1.1       cgd 	scr_msg(key_msg, 1);
    153   1.1       cgd }
    154   1.1       cgd 
    155   1.1       cgd /*
    156   1.1       cgd  * Set up screen mode.
    157   1.1       cgd  */
    158   1.1       cgd void
    159  1.23  dholland scr_set(void)
    160   1.1       cgd {
    161   1.1       cgd 	struct winsize ws;
    162   1.3   mycroft 	struct termios newtt;
    163  1.22  dholland 	sigset_t nsigset, osigset;
    164  1.19       jsm 	void (*ttou)(int);
    165   1.1       cgd 
    166  1.22  dholland 	sigemptyset(&nsigset);
    167  1.22  dholland 	sigaddset(&nsigset, SIGTSTP);
    168  1.22  dholland 	sigaddset(&nsigset, SIGTTOU);
    169  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    170   1.1       cgd 	if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
    171   1.1       cgd 		(void) signal(SIGTSTP, SIG_IGN);
    172   1.3   mycroft 	if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN)
    173   1.3   mycroft 		(void) signal(SIGTTOU, SIG_IGN);
    174   1.1       cgd 	/*
    175   1.1       cgd 	 * At last, we are ready to modify the tty state.  If
    176   1.1       cgd 	 * we stop while at it, stopset() above will longjmp back
    177   1.1       cgd 	 * to the setjmp here and we will start over.
    178   1.1       cgd 	 */
    179   1.1       cgd 	(void) setjmp(scr_onstop);
    180   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    181   1.1       cgd 	Rows = 0, Cols = 0;
    182   1.1       cgd 	if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
    183   1.1       cgd 		Rows = ws.ws_row;
    184   1.1       cgd 		Cols = ws.ws_col;
    185   1.1       cgd 	}
    186   1.1       cgd 	if (Rows == 0)
    187  1.26       roy 		Rows = lines;
    188   1.1       cgd 	if (Cols == 0)
    189  1.26       roy 	    Cols = columns;
    190   1.1       cgd 	if (Rows < MINROWS || Cols < MINCOLS) {
    191   1.1       cgd 		(void) fprintf(stderr,
    192   1.8   hubertf 		    "the screen is too small: must be at least %dx%d, ",
    193   1.8   hubertf 		    MINCOLS, MINROWS);
    194   1.1       cgd 		stop("");	/* stop() supplies \n */
    195   1.1       cgd 	}
    196   1.3   mycroft 	if (tcgetattr(0, &oldtt) < 0)
    197   1.3   mycroft 		stop("tcgetattr() fails");
    198   1.1       cgd 	newtt = oldtt;
    199   1.3   mycroft 	newtt.c_lflag &= ~(ICANON|ECHO);
    200   1.3   mycroft 	newtt.c_oflag &= ~OXTABS;
    201   1.3   mycroft 	if (tcsetattr(0, TCSADRAIN, &newtt) < 0)
    202   1.3   mycroft 		stop("tcsetattr() fails");
    203   1.3   mycroft 	ospeed = cfgetospeed(&newtt);
    204  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    205   1.1       cgd 
    206   1.1       cgd 	/*
    207   1.1       cgd 	 * We made it.  We are now in screen mode, modulo TIstr
    208   1.1       cgd 	 * (which we will fix immediately).
    209   1.1       cgd 	 */
    210  1.26       roy 	if (enter_ca_mode)
    211  1.26       roy 		putstr(enter_ca_mode);
    212  1.26       roy 	if (cursor_invisible)
    213  1.26       roy 		putstr(cursor_invisible);
    214   1.1       cgd 	if (tstp != SIG_IGN)
    215   1.1       cgd 		(void) signal(SIGTSTP, scr_stop);
    216   1.3   mycroft 	if (ttou != SIG_IGN)
    217   1.3   mycroft 		(void) signal(SIGTTOU, ttou);
    218   1.1       cgd 
    219   1.1       cgd 	isset = 1;
    220   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    221   1.1       cgd 	scr_clear();
    222   1.1       cgd }
    223   1.1       cgd 
    224   1.1       cgd /*
    225   1.1       cgd  * End screen mode.
    226   1.1       cgd  */
    227   1.1       cgd void
    228  1.23  dholland scr_end(void)
    229   1.1       cgd {
    230  1.22  dholland 	sigset_t nsigset, osigset;
    231   1.1       cgd 
    232  1.22  dholland 	sigemptyset(&nsigset);
    233  1.22  dholland 	sigaddset(&nsigset, SIGTSTP);
    234  1.22  dholland 	sigaddset(&nsigset, SIGTTOU);
    235  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    236   1.1       cgd 	/* move cursor to last line */
    237  1.26       roy 	if (cursor_to_ll)
    238  1.26       roy 		putstr(cursor_to_ll);
    239   1.1       cgd 	else
    240   1.1       cgd 		moveto(Rows - 1, 0);
    241   1.1       cgd 	/* exit screen mode */
    242  1.26       roy 	if (exit_ca_mode)
    243  1.26       roy 		putstr(exit_ca_mode);
    244  1.26       roy 	if (cursor_normal)
    245  1.26       roy 		putstr(cursor_normal);
    246   1.1       cgd 	(void) fflush(stdout);
    247   1.3   mycroft 	(void) tcsetattr(0, TCSADRAIN, &oldtt);
    248   1.1       cgd 	isset = 0;
    249   1.1       cgd 	/* restore signals */
    250   1.1       cgd 	(void) signal(SIGTSTP, tstp);
    251   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    252   1.1       cgd }
    253   1.1       cgd 
    254   1.1       cgd void
    255  1.23  dholland stop(const char *why)
    256   1.1       cgd {
    257   1.1       cgd 
    258   1.1       cgd 	if (isset)
    259   1.1       cgd 		scr_end();
    260   1.1       cgd 	(void) fprintf(stderr, "aborting: %s\n", why);
    261   1.1       cgd 	exit(1);
    262   1.1       cgd }
    263   1.1       cgd 
    264   1.1       cgd /*
    265   1.1       cgd  * Clear the screen, forgetting the current contents in the process.
    266   1.1       cgd  */
    267   1.1       cgd void
    268  1.23  dholland scr_clear(void)
    269   1.1       cgd {
    270   1.1       cgd 
    271  1.26       roy 	putpad(clear_screen);
    272   1.1       cgd 	curscore = -1;
    273   1.7     perry 	memset((char *)curscreen, 0, sizeof(curscreen));
    274   1.1       cgd }
    275   1.1       cgd 
    276   1.1       cgd #if vax && !__GNUC__
    277   1.1       cgd typedef int regcell;	/* pcc is bad at `register char', etc */
    278   1.1       cgd #else
    279   1.1       cgd typedef cell regcell;
    280   1.1       cgd #endif
    281   1.1       cgd 
    282   1.1       cgd /*
    283   1.1       cgd  * Update the screen.
    284   1.1       cgd  */
    285   1.1       cgd void
    286  1.23  dholland scr_update(void)
    287   1.1       cgd {
    288  1.17       wiz 	cell *bp, *sp;
    289  1.17       wiz 	regcell so, cur_so = 0;
    290  1.17       wiz 	int i, ccol, j;
    291  1.22  dholland 	sigset_t nsigset, osigset;
    292  1.11       jsm 	static const struct shape *lastshape;
    293   1.3   mycroft 
    294  1.22  dholland 	sigemptyset(&nsigset);
    295  1.22  dholland 	sigaddset(&nsigset, SIGTSTP);
    296  1.22  dholland 	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    297   1.1       cgd 
    298   1.1       cgd 	/* always leave cursor after last displayed point */
    299   1.1       cgd 	curscreen[D_LAST * B_COLS - 1] = -1;
    300   1.1       cgd 
    301   1.1       cgd 	if (score != curscore) {
    302  1.26       roy 		if (cursor_home)
    303  1.26       roy 			putpad(cursor_home);
    304   1.1       cgd 		else
    305   1.1       cgd 			moveto(0, 0);
    306   1.8   hubertf 		(void) printf("Score: %d", score);
    307   1.1       cgd 		curscore = score;
    308   1.1       cgd 	}
    309   1.1       cgd 
    310   1.8   hubertf 	/* draw preview of nextpattern */
    311   1.9   hubertf 	if (showpreview && (nextshape != lastshape)) {
    312   1.8   hubertf 		static int r=5, c=2;
    313   1.8   hubertf 		int tr, tc, t;
    314   1.8   hubertf 
    315   1.8   hubertf 		lastshape = nextshape;
    316   1.8   hubertf 
    317   1.8   hubertf 		/* clean */
    318  1.26       roy 		putpad(exit_standout_mode);
    319   1.8   hubertf 		moveto(r-1, c-1); putstr("          ");
    320   1.8   hubertf 		moveto(r,   c-1); putstr("          ");
    321   1.8   hubertf 		moveto(r+1, c-1); putstr("          ");
    322   1.8   hubertf 		moveto(r+2, c-1); putstr("          ");
    323   1.8   hubertf 
    324   1.8   hubertf 		moveto(r-3, c-2);
    325   1.8   hubertf 		putstr("Next shape:");
    326   1.8   hubertf 
    327   1.8   hubertf 		/* draw */
    328  1.26       roy 		putpad(enter_standout_mode);
    329  1.28  christos 		setcolor(nextshape->color);
    330   1.8   hubertf 		moveto(r, 2*c);
    331   1.8   hubertf 		putstr("  ");
    332   1.8   hubertf 		for(i=0; i<3; i++) {
    333   1.8   hubertf 			t = c + r*B_COLS;
    334   1.8   hubertf 			t += nextshape->off[i];
    335   1.8   hubertf 
    336   1.8   hubertf 			tr = t / B_COLS;
    337   1.8   hubertf 			tc = t % B_COLS;
    338   1.8   hubertf 
    339   1.8   hubertf 			moveto(tr, 2*tc);
    340   1.8   hubertf 			putstr("  ");
    341   1.8   hubertf 		}
    342  1.26       roy 		putpad(exit_standout_mode);
    343   1.8   hubertf 	}
    344   1.8   hubertf 
    345   1.1       cgd 	bp = &board[D_FIRST * B_COLS];
    346   1.1       cgd 	sp = &curscreen[D_FIRST * B_COLS];
    347   1.1       cgd 	for (j = D_FIRST; j < D_LAST; j++) {
    348   1.1       cgd 		ccol = -1;
    349   1.1       cgd 		for (i = 0; i < B_COLS; bp++, sp++, i++) {
    350   1.1       cgd 			if (*sp == (so = *bp))
    351   1.1       cgd 				continue;
    352   1.1       cgd 			*sp = so;
    353   1.1       cgd 			if (i != ccol) {
    354  1.26       roy 				if (cur_so && move_standout_mode) {
    355  1.26       roy 					putpad(exit_standout_mode);
    356   1.1       cgd 					cur_so = 0;
    357   1.1       cgd 				}
    358   1.1       cgd 				moveto(RTOD(j), CTOD(i));
    359   1.1       cgd 			}
    360  1.26       roy 			if (enter_standout_mode) {
    361   1.1       cgd 				if (so != cur_so) {
    362  1.26       roy 					putpad(so ?
    363  1.26       roy 					    enter_standout_mode :
    364  1.26       roy 					    exit_standout_mode);
    365   1.1       cgd 					cur_so = so;
    366   1.1       cgd 				}
    367  1.28  christos 				setcolor(so);
    368  1.28  christos #ifdef DEBUG
    369  1.28  christos 				char buf[3];
    370  1.28  christos 				snprintf(buf, sizeof(buf), "%d%d", so, so);
    371  1.28  christos 				putstr(buf);
    372  1.28  christos #else
    373   1.1       cgd 				putstr("  ");
    374  1.28  christos #endif
    375   1.1       cgd 			} else
    376   1.1       cgd 				putstr(so ? "XX" : "  ");
    377   1.1       cgd 			ccol = i + 1;
    378   1.1       cgd 			/*
    379   1.1       cgd 			 * Look ahead a bit, to avoid extra motion if
    380   1.1       cgd 			 * we will be redrawing the cell after the next.
    381   1.1       cgd 			 * Motion probably takes four or more characters,
    382   1.1       cgd 			 * so we save even if we rewrite two cells
    383   1.1       cgd 			 * `unnecessarily'.  Skip it all, though, if
    384   1.1       cgd 			 * the next cell is a different color.
    385   1.1       cgd 			 */
    386   1.1       cgd #define	STOP (B_COLS - 3)
    387   1.1       cgd 			if (i > STOP || sp[1] != bp[1] || so != bp[1])
    388   1.1       cgd 				continue;
    389   1.1       cgd 			if (sp[2] != bp[2])
    390   1.1       cgd 				sp[1] = -1;
    391   1.1       cgd 			else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
    392   1.1       cgd 				sp[2] = -1;
    393   1.1       cgd 				sp[1] = -1;
    394   1.1       cgd 			}
    395   1.1       cgd 		}
    396   1.1       cgd 	}
    397   1.1       cgd 	if (cur_so)
    398  1.26       roy 		putpad(exit_standout_mode);
    399   1.1       cgd 	(void) fflush(stdout);
    400   1.3   mycroft 	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    401   1.1       cgd }
    402   1.1       cgd 
    403   1.1       cgd /*
    404   1.1       cgd  * Write a message (set!=0), or clear the same message (set==0).
    405   1.1       cgd  * (We need its length in case we have to overwrite with blanks.)
    406   1.1       cgd  */
    407   1.1       cgd void
    408  1.23  dholland scr_msg(char *s, int set)
    409   1.1       cgd {
    410   1.1       cgd 
    411  1.26       roy 	if (set || clr_eol == NULL) {
    412  1.17       wiz 		int l = strlen(s);
    413   1.1       cgd 
    414   1.1       cgd 		moveto(Rows - 2, ((Cols - l) >> 1) - 1);
    415   1.1       cgd 		if (set)
    416   1.1       cgd 			putstr(s);
    417   1.1       cgd 		else
    418   1.1       cgd 			while (--l >= 0)
    419   1.1       cgd 				(void) putchar(' ');
    420   1.1       cgd 	} else {
    421   1.1       cgd 		moveto(Rows - 2, 0);
    422  1.26       roy 		putpad(clr_eol);
    423   1.1       cgd 	}
    424   1.1       cgd }
    425