Blob


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