screen.c revision 1.32
1/*	$NetBSD: screen.c,v 1.32 2016/03/03 21:38:55 nat 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. 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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 *	@(#)screen.c	8.1 (Berkeley) 5/31/93
35 */
36
37/*
38 * Tetris screen control.
39 */
40
41#include <sys/cdefs.h>
42#include <sys/ioctl.h>
43
44#include <setjmp.h>
45#include <signal.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <term.h>
50#include <termios.h>
51#include <unistd.h>
52
53#ifndef sigmask
54#define sigmask(s) (1 << ((s) - 1))
55#endif
56
57#include "screen.h"
58#include "tetris.h"
59
60static cell curscreen[B_SIZE];	/* 1 => standout (or otherwise marked) */
61static int curscore;
62static int isset;		/* true => terminal is in game mode */
63static struct termios oldtt;
64static void (*tstp)(int);
65
66static	void	scr_stop(int);
67static	void	stopset(int) __dead;
68
69
70/*
71 * Routine used by tputs().
72 */
73int
74put(int c)
75{
76
77	return (putchar(c));
78}
79
80/*
81 * putstr() is for unpadded strings (either as in termcap(5) or
82 * simply literal strings); putpad() is for padded strings with
83 * count=1.  (See screen.h for putpad().)
84 */
85#define	putstr(s)	(void)fputs(s, stdout)
86
87static void
88moveto(int r, int c)
89{
90	char *buf;
91
92	buf = tiparm(cursor_address, r, c);
93	if (buf != NULL)
94		putpad(buf);
95}
96
97static void
98setcolor(int c)
99{
100	char *buf;
101	char monochrome[] = "\033[0m";
102	if (nocolor == 1)
103		return;
104	if (set_a_foreground == NULL)
105		return;
106
107	if (c == 0 || c == 7)
108		buf = monochrome;
109	else
110		buf = tiparm(set_a_foreground, c);
111	if (buf != NULL)
112		putpad(buf);
113}
114
115/*
116 * Set up from termcap.
117 */
118void
119scr_init(void)
120{
121
122	setupterm(NULL, 0, NULL);
123	if (clear_screen == NULL)
124		stop("cannot clear screen");
125	if (cursor_address == NULL || cursor_up == NULL)
126		stop("cannot do random cursor positioning");
127}
128
129/* this foolery is needed to modify tty state `atomically' */
130static jmp_buf scr_onstop;
131
132static void
133stopset(int sig)
134{
135	sigset_t set;
136
137	(void) signal(sig, SIG_DFL);
138	(void) kill(getpid(), sig);
139	sigemptyset(&set);
140	sigaddset(&set, sig);
141	(void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
142	longjmp(scr_onstop, 1);
143}
144
145static void
146scr_stop(int sig)
147{
148	sigset_t set;
149
150	scr_end();
151	(void) kill(getpid(), sig);
152	sigemptyset(&set);
153	sigaddset(&set, sig);
154	(void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
155	scr_set();
156	scr_msg(key_msg, 1);
157}
158
159/*
160 * Set up screen mode.
161 */
162void
163scr_set(void)
164{
165	struct winsize ws;
166	struct termios newtt;
167	sigset_t nsigset, osigset;
168	void (*ttou)(int);
169
170	sigemptyset(&nsigset);
171	sigaddset(&nsigset, SIGTSTP);
172	sigaddset(&nsigset, SIGTTOU);
173	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
174	if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
175		(void) signal(SIGTSTP, SIG_IGN);
176	if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN)
177		(void) signal(SIGTTOU, SIG_IGN);
178	/*
179	 * At last, we are ready to modify the tty state.  If
180	 * we stop while at it, stopset() above will longjmp back
181	 * to the setjmp here and we will start over.
182	 */
183	(void) setjmp(scr_onstop);
184	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
185	Rows = 0, Cols = 0;
186	if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
187		Rows = ws.ws_row;
188		Cols = ws.ws_col;
189	}
190	if (Rows == 0)
191		Rows = lines;
192	if (Cols == 0)
193	    Cols = columns;
194	if (Rows < MINROWS || Cols < MINCOLS) {
195		(void) fprintf(stderr,
196		    "the screen is too small: must be at least %dx%d, ",
197		    MINCOLS, MINROWS);
198		stop("");	/* stop() supplies \n */
199	}
200	Offset = (Rows - D_LAST + D_FIRST - 2) / 2;
201	if (tcgetattr(0, &oldtt) < 0)
202		stop("tcgetattr() fails");
203	newtt = oldtt;
204	newtt.c_lflag &= ~(ICANON|ECHO);
205	newtt.c_oflag &= ~OXTABS;
206	if (tcsetattr(0, TCSADRAIN, &newtt) < 0)
207		stop("tcsetattr() fails");
208	ospeed = cfgetospeed(&newtt);
209	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
210
211	/*
212	 * We made it.  We are now in screen mode, modulo TIstr
213	 * (which we will fix immediately).
214	 */
215	if (enter_ca_mode)
216		putstr(enter_ca_mode);
217	if (cursor_invisible)
218		putstr(cursor_invisible);
219	if (tstp != SIG_IGN)
220		(void) signal(SIGTSTP, scr_stop);
221	if (ttou != SIG_IGN)
222		(void) signal(SIGTTOU, ttou);
223
224	isset = 1;
225	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
226	scr_clear();
227}
228
229/*
230 * End screen mode.
231 */
232void
233scr_end(void)
234{
235	sigset_t nsigset, osigset;
236
237	sigemptyset(&nsigset);
238	sigaddset(&nsigset, SIGTSTP);
239	sigaddset(&nsigset, SIGTTOU);
240	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
241	/* move cursor to last line */
242	if (cursor_to_ll)
243		putstr(cursor_to_ll);
244	else
245		moveto(Rows - 1, 0);
246	/* exit screen mode */
247	if (exit_ca_mode)
248		putstr(exit_ca_mode);
249	if (cursor_normal)
250		putstr(cursor_normal);
251	(void) fflush(stdout);
252	(void) tcsetattr(0, TCSADRAIN, &oldtt);
253	isset = 0;
254	/* restore signals */
255	(void) signal(SIGTSTP, tstp);
256	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
257}
258
259void
260stop(const char *why)
261{
262
263	if (isset)
264		scr_end();
265	(void) fprintf(stderr, "aborting: %s\n", why);
266	exit(1);
267}
268
269/*
270 * Clear the screen, forgetting the current contents in the process.
271 */
272void
273scr_clear(void)
274{
275
276	putpad(clear_screen);
277	curscore = -1;
278	memset((char *)curscreen, 0, sizeof(curscreen));
279}
280
281#if vax && !__GNUC__
282typedef int regcell;	/* pcc is bad at `register char', etc */
283#else
284typedef cell regcell;
285#endif
286
287/*
288 * Update the screen.
289 */
290void
291scr_update(void)
292{
293	cell *bp, *sp;
294	regcell so, cur_so = 0;
295	int i, ccol, j;
296	sigset_t nsigset, osigset;
297	static const struct shape *lastshape;
298
299	sigemptyset(&nsigset);
300	sigaddset(&nsigset, SIGTSTP);
301	(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
302
303	/* always leave cursor after last displayed point */
304	curscreen[D_LAST * B_COLS - 1] = -1;
305
306	if (score != curscore) {
307		if (cursor_home)
308			putpad(cursor_home);
309		else
310			moveto(0, 0);
311		setcolor(0);
312		(void) printf("Score: %d", score);
313		curscore = score;
314	}
315
316	/* draw preview of nextpattern */
317	if (showpreview && (nextshape != lastshape)) {
318		static int r=5, c=2;
319		int tr, tc, t;
320
321		lastshape = nextshape;
322
323		/* clean */
324		putpad(exit_standout_mode);
325		moveto(r-1, c-1); putstr("          ");
326		moveto(r,   c-1); putstr("          ");
327		moveto(r+1, c-1); putstr("          ");
328		moveto(r+2, c-1); putstr("          ");
329
330		moveto(r-3, c-2);
331		putstr("Next shape:");
332
333		/* draw */
334		setcolor(nextshape->color);
335		putpad(enter_standout_mode);
336		moveto(r, 2*c);
337		putstr("  ");
338		for(i=0; i<3; i++) {
339			t = c + r*B_COLS;
340			t += nextshape->off[i];
341
342			tr = t / B_COLS;
343			tc = t % B_COLS;
344
345			moveto(tr, 2*tc);
346			putstr("  ");
347		}
348		putpad(exit_standout_mode);
349	}
350
351	bp = &board[D_FIRST * B_COLS];
352	sp = &curscreen[D_FIRST * B_COLS];
353	for (j = D_FIRST; j < D_LAST; j++) {
354		ccol = -1;
355		for (i = 0; i < B_COLS; bp++, sp++, i++) {
356			if (*sp == (so = *bp))
357				continue;
358			*sp = so;
359			if (i != ccol) {
360				if (cur_so && move_standout_mode) {
361					putpad(exit_standout_mode);
362					cur_so = 0;
363				}
364				moveto(RTOD(j + Offset), CTOD(i));
365			}
366			if (enter_standout_mode) {
367				if (so != cur_so) {
368					setcolor(so);
369					putpad(so ?
370					    enter_standout_mode :
371					    exit_standout_mode);
372					cur_so = so;
373				}
374#ifdef DEBUG
375				char buf[3];
376				snprintf(buf, sizeof(buf), "%d%d", so, so);
377				putstr(buf);
378#else
379				putstr("  ");
380#endif
381			} else
382				putstr(so ? "XX" : "  ");
383			ccol = i + 1;
384			/*
385			 * Look ahead a bit, to avoid extra motion if
386			 * we will be redrawing the cell after the next.
387			 * Motion probably takes four or more characters,
388			 * so we save even if we rewrite two cells
389			 * `unnecessarily'.  Skip it all, though, if
390			 * the next cell is a different color.
391			 */
392#define	STOP (B_COLS - 3)
393			if (i > STOP || sp[1] != bp[1] || so != bp[1])
394				continue;
395			if (sp[2] != bp[2])
396				sp[1] = -1;
397			else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
398				sp[2] = -1;
399				sp[1] = -1;
400			}
401		}
402	}
403	if (cur_so)
404		putpad(exit_standout_mode);
405	(void) fflush(stdout);
406	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
407}
408
409/*
410 * Write a message (set!=0), or clear the same message (set==0).
411 * (We need its length in case we have to overwrite with blanks.)
412 */
413void
414scr_msg(char *s, int set)
415{
416
417	if (set || clr_eol == NULL) {
418		int l = strlen(s);
419
420		moveto(Rows - 2, ((Cols - l) >> 1) - 1);
421		if (set)
422			putstr(s);
423		else
424			while (--l >= 0)
425				(void) putchar(' ');
426	} else {
427		moveto(Rows - 2, 0);
428		putpad(clr_eol);
429	}
430}
431