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