1 11a5e2cf 2024-01-16 benni /* $NetBSD: screen.c,v 1.34 2021/05/02 12:50:46 rillig Exp $ */
4 11a5e2cf 2024-01-16 benni * Copyright (c) 1992, 1993
5 11a5e2cf 2024-01-16 benni * The Regents of the University of California. All rights reserved.
7 11a5e2cf 2024-01-16 benni * This code is derived from software contributed to Berkeley by
8 11a5e2cf 2024-01-16 benni * Chris Torek and Darren F. Provine.
10 11a5e2cf 2024-01-16 benni * Redistribution and use in source and binary forms, with or without
11 11a5e2cf 2024-01-16 benni * modification, are permitted provided that the following conditions
13 11a5e2cf 2024-01-16 benni * 1. Redistributions of source code must retain the above copyright
14 11a5e2cf 2024-01-16 benni * notice, this list of conditions and the following disclaimer.
15 11a5e2cf 2024-01-16 benni * 2. Redistributions in binary form must reproduce the above copyright
16 11a5e2cf 2024-01-16 benni * notice, this list of conditions and the following disclaimer in the
17 11a5e2cf 2024-01-16 benni * documentation and/or other materials provided with the distribution.
18 11a5e2cf 2024-01-16 benni * 3. Neither the name of the University nor the names of its contributors
19 11a5e2cf 2024-01-16 benni * may be used to endorse or promote products derived from this software
20 11a5e2cf 2024-01-16 benni * without specific prior written permission.
22 11a5e2cf 2024-01-16 benni * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 11a5e2cf 2024-01-16 benni * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 11a5e2cf 2024-01-16 benni * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 11a5e2cf 2024-01-16 benni * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 11a5e2cf 2024-01-16 benni * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 11a5e2cf 2024-01-16 benni * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 11a5e2cf 2024-01-16 benni * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 11a5e2cf 2024-01-16 benni * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 11a5e2cf 2024-01-16 benni * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 11a5e2cf 2024-01-16 benni * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 11a5e2cf 2024-01-16 benni * SUCH DAMAGE.
34 11a5e2cf 2024-01-16 benni * @(#)screen.c 8.1 (Berkeley) 5/31/93
38 11a5e2cf 2024-01-16 benni * Tetris screen control.
41 11a5e2cf 2024-01-16 benni #include <sys/cdefs.h>
42 11a5e2cf 2024-01-16 benni #include <sys/ioctl.h>
44 11a5e2cf 2024-01-16 benni #include <setjmp.h>
45 11a5e2cf 2024-01-16 benni #include <signal.h>
46 11a5e2cf 2024-01-16 benni #include <stdio.h>
47 11a5e2cf 2024-01-16 benni #include <stdlib.h>
48 11a5e2cf 2024-01-16 benni #include <string.h>
49 11a5e2cf 2024-01-16 benni #include <term.h>
50 11a5e2cf 2024-01-16 benni #include <termios.h>
51 11a5e2cf 2024-01-16 benni #include <unistd.h>
53 11a5e2cf 2024-01-16 benni #ifndef sigmask
54 11a5e2cf 2024-01-16 benni #define sigmask(s) (1 << ((s) - 1))
57 11a5e2cf 2024-01-16 benni #include "screen.h"
58 11a5e2cf 2024-01-16 benni #include "tetris.h"
60 11a5e2cf 2024-01-16 benni static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */
61 11a5e2cf 2024-01-16 benni static int curscore;
62 11a5e2cf 2024-01-16 benni static int isset; /* true => terminal is in game mode */
63 11a5e2cf 2024-01-16 benni static struct termios oldtt;
64 11a5e2cf 2024-01-16 benni static void (*tstp)(int);
66 11a5e2cf 2024-01-16 benni static void scr_stop(int);
67 11a5e2cf 2024-01-16 benni static void stopset(int) __dead;
71 11a5e2cf 2024-01-16 benni * Routine used by tputs().
77 11a5e2cf 2024-01-16 benni return (putchar(c));
81 11a5e2cf 2024-01-16 benni * putstr() is for unpadded strings (either as in termcap(5) or
82 11a5e2cf 2024-01-16 benni * simply literal strings); putpad() is for padded strings with
83 11a5e2cf 2024-01-16 benni * count=1. (See screen.h for putpad().)
85 11a5e2cf 2024-01-16 benni #define putstr(s) (void)fputs(s, stdout)
87 11a5e2cf 2024-01-16 benni static void
88 11a5e2cf 2024-01-16 benni moveto(int r, int c)
92 11a5e2cf 2024-01-16 benni buf = tiparm(cursor_address, r, c);
93 11a5e2cf 2024-01-16 benni if (buf != NULL)
94 11a5e2cf 2024-01-16 benni putpad(buf);
97 11a5e2cf 2024-01-16 benni static void
98 11a5e2cf 2024-01-16 benni setcolor(int c)
100 11a5e2cf 2024-01-16 benni char *buf;
101 11a5e2cf 2024-01-16 benni char monochrome[] = "\033[0m";
102 11a5e2cf 2024-01-16 benni if (nocolor == 1)
104 11a5e2cf 2024-01-16 benni if (set_a_foreground == NULL)
107 11a5e2cf 2024-01-16 benni if (c == 0 || c == 7)
108 11a5e2cf 2024-01-16 benni buf = monochrome;
110 11a5e2cf 2024-01-16 benni buf = tiparm(set_a_foreground, c);
111 11a5e2cf 2024-01-16 benni if (buf != NULL)
112 11a5e2cf 2024-01-16 benni putpad(buf);
116 11a5e2cf 2024-01-16 benni * Set up from termcap.
119 11a5e2cf 2024-01-16 benni scr_init(void)
122 11a5e2cf 2024-01-16 benni setupterm(NULL, 0, NULL);
123 11a5e2cf 2024-01-16 benni if (clear_screen == NULL)
124 11a5e2cf 2024-01-16 benni stop("cannot clear screen");
125 11a5e2cf 2024-01-16 benni if (cursor_address == NULL || cursor_up == NULL)
126 11a5e2cf 2024-01-16 benni stop("cannot do random cursor positioning");
129 11a5e2cf 2024-01-16 benni /* this foolery is needed to modify tty state `atomically' */
130 11a5e2cf 2024-01-16 benni static jmp_buf scr_onstop;
132 11a5e2cf 2024-01-16 benni static void
133 11a5e2cf 2024-01-16 benni stopset(int sig)
135 11a5e2cf 2024-01-16 benni sigset_t set;
137 11a5e2cf 2024-01-16 benni (void) signal(sig, SIG_DFL);
138 11a5e2cf 2024-01-16 benni (void) kill(getpid(), sig);
139 11a5e2cf 2024-01-16 benni sigemptyset(&set);
140 11a5e2cf 2024-01-16 benni sigaddset(&set, sig);
141 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
142 11a5e2cf 2024-01-16 benni longjmp(scr_onstop, 1);
145 11a5e2cf 2024-01-16 benni static void
146 11a5e2cf 2024-01-16 benni scr_stop(int sig)
148 11a5e2cf 2024-01-16 benni sigset_t set;
150 11a5e2cf 2024-01-16 benni scr_end();
151 11a5e2cf 2024-01-16 benni (void) kill(getpid(), sig);
152 11a5e2cf 2024-01-16 benni sigemptyset(&set);
153 11a5e2cf 2024-01-16 benni sigaddset(&set, sig);
154 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0);
155 11a5e2cf 2024-01-16 benni scr_set();
156 11a5e2cf 2024-01-16 benni scr_msg(key_msg, 1);
160 11a5e2cf 2024-01-16 benni * Set up screen mode.
163 11a5e2cf 2024-01-16 benni scr_set(void)
165 11a5e2cf 2024-01-16 benni struct winsize ws;
166 11a5e2cf 2024-01-16 benni struct termios newtt;
167 11a5e2cf 2024-01-16 benni sigset_t nsigset, osigset;
168 11a5e2cf 2024-01-16 benni void (*ttou)(int);
170 11a5e2cf 2024-01-16 benni sigemptyset(&nsigset);
171 11a5e2cf 2024-01-16 benni sigaddset(&nsigset, SIGTSTP);
172 11a5e2cf 2024-01-16 benni sigaddset(&nsigset, SIGTTOU);
173 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
174 11a5e2cf 2024-01-16 benni if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
175 11a5e2cf 2024-01-16 benni (void) signal(SIGTSTP, SIG_IGN);
176 11a5e2cf 2024-01-16 benni if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN)
177 11a5e2cf 2024-01-16 benni (void) signal(SIGTTOU, SIG_IGN);
179 11a5e2cf 2024-01-16 benni * At last, we are ready to modify the tty state. If
180 11a5e2cf 2024-01-16 benni * we stop while at it, stopset() above will longjmp back
181 11a5e2cf 2024-01-16 benni * to the setjmp here and we will start over.
183 11a5e2cf 2024-01-16 benni (void) setjmp(scr_onstop);
184 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
185 11a5e2cf 2024-01-16 benni Rows = 0, Cols = 0;
186 11a5e2cf 2024-01-16 benni if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
187 11a5e2cf 2024-01-16 benni Rows = ws.ws_row;
188 11a5e2cf 2024-01-16 benni Cols = ws.ws_col;
190 11a5e2cf 2024-01-16 benni if (Rows == 0)
191 11a5e2cf 2024-01-16 benni Rows = lines;
192 11a5e2cf 2024-01-16 benni if (Cols == 0)
193 11a5e2cf 2024-01-16 benni Cols = columns;
194 11a5e2cf 2024-01-16 benni if (Rows < MINROWS || Cols < MINCOLS) {
195 11a5e2cf 2024-01-16 benni (void) fprintf(stderr,
196 11a5e2cf 2024-01-16 benni "the screen is too small: must be at least %dx%d, ",
197 11a5e2cf 2024-01-16 benni MINCOLS, MINROWS);
198 11a5e2cf 2024-01-16 benni stop(""); /* stop() supplies \n */
200 11a5e2cf 2024-01-16 benni Offset = (Rows - D_LAST + D_FIRST - 2) / 2;
201 11a5e2cf 2024-01-16 benni if (tcgetattr(0, &oldtt) < 0)
202 11a5e2cf 2024-01-16 benni stop("tcgetattr() fails");
203 11a5e2cf 2024-01-16 benni newtt = oldtt;
204 11a5e2cf 2024-01-16 benni newtt.c_lflag &= ~(ICANON|ECHO);
205 11a5e2cf 2024-01-16 benni newtt.c_oflag &= ~OXTABS;
206 11a5e2cf 2024-01-16 benni if (tcsetattr(0, TCSADRAIN, &newtt) < 0)
207 11a5e2cf 2024-01-16 benni stop("tcsetattr() fails");
208 11a5e2cf 2024-01-16 benni ospeed = cfgetospeed(&newtt);
209 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
212 11a5e2cf 2024-01-16 benni * We made it. We are now in screen mode, modulo TIstr
213 11a5e2cf 2024-01-16 benni * (which we will fix immediately).
215 11a5e2cf 2024-01-16 benni const char *tstr;
216 11a5e2cf 2024-01-16 benni if ((tstr = enter_ca_mode) != NULL)
217 11a5e2cf 2024-01-16 benni putstr(tstr);
218 11a5e2cf 2024-01-16 benni if ((tstr = cursor_invisible) != NULL)
219 11a5e2cf 2024-01-16 benni putstr(tstr);
220 11a5e2cf 2024-01-16 benni if (tstp != SIG_IGN)
221 11a5e2cf 2024-01-16 benni (void) signal(SIGTSTP, scr_stop);
222 11a5e2cf 2024-01-16 benni if (ttou != SIG_IGN)
223 11a5e2cf 2024-01-16 benni (void) signal(SIGTTOU, ttou);
225 11a5e2cf 2024-01-16 benni isset = 1;
226 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
227 11a5e2cf 2024-01-16 benni scr_clear();
231 11a5e2cf 2024-01-16 benni * End screen mode.
234 11a5e2cf 2024-01-16 benni scr_end(void)
236 11a5e2cf 2024-01-16 benni sigset_t nsigset, osigset;
238 11a5e2cf 2024-01-16 benni sigemptyset(&nsigset);
239 11a5e2cf 2024-01-16 benni sigaddset(&nsigset, SIGTSTP);
240 11a5e2cf 2024-01-16 benni sigaddset(&nsigset, SIGTTOU);
241 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
242 11a5e2cf 2024-01-16 benni /* move cursor to last line */
243 11a5e2cf 2024-01-16 benni const char *tstr;
244 11a5e2cf 2024-01-16 benni if ((tstr = cursor_to_ll) != NULL)
245 11a5e2cf 2024-01-16 benni putstr(tstr);
247 11a5e2cf 2024-01-16 benni moveto(Rows - 1, 0);
248 11a5e2cf 2024-01-16 benni /* exit screen mode */
249 11a5e2cf 2024-01-16 benni if ((tstr = exit_ca_mode) != NULL)
250 11a5e2cf 2024-01-16 benni putstr(tstr);
251 11a5e2cf 2024-01-16 benni if ((tstr = cursor_normal) != NULL)
252 11a5e2cf 2024-01-16 benni putstr(tstr);
253 11a5e2cf 2024-01-16 benni (void) fflush(stdout);
254 11a5e2cf 2024-01-16 benni (void) tcsetattr(0, TCSADRAIN, &oldtt);
255 11a5e2cf 2024-01-16 benni isset = 0;
256 11a5e2cf 2024-01-16 benni /* restore signals */
257 11a5e2cf 2024-01-16 benni (void) signal(SIGTSTP, tstp);
258 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
262 11a5e2cf 2024-01-16 benni stop(const char *why)
265 11a5e2cf 2024-01-16 benni if (isset)
266 11a5e2cf 2024-01-16 benni scr_end();
267 11a5e2cf 2024-01-16 benni (void) fprintf(stderr, "aborting: %s\n", why);
272 11a5e2cf 2024-01-16 benni * Clear the screen, forgetting the current contents in the process.
275 11a5e2cf 2024-01-16 benni scr_clear(void)
278 11a5e2cf 2024-01-16 benni putpad(clear_screen);
279 11a5e2cf 2024-01-16 benni curscore = -1;
280 11a5e2cf 2024-01-16 benni memset((char *)curscreen, 0, sizeof(curscreen));
283 11a5e2cf 2024-01-16 benni #if vax && !__GNUC__
284 11a5e2cf 2024-01-16 benni typedef int regcell; /* pcc is bad at `register char', etc */
286 11a5e2cf 2024-01-16 benni typedef cell regcell;
290 11a5e2cf 2024-01-16 benni * Update the screen.
293 11a5e2cf 2024-01-16 benni scr_update(void)
295 11a5e2cf 2024-01-16 benni cell *bp, *sp;
296 11a5e2cf 2024-01-16 benni regcell so, cur_so = 0;
297 11a5e2cf 2024-01-16 benni int i, ccol, j;
298 11a5e2cf 2024-01-16 benni sigset_t nsigset, osigset;
299 11a5e2cf 2024-01-16 benni static const struct shape *lastshape;
301 11a5e2cf 2024-01-16 benni sigemptyset(&nsigset);
302 11a5e2cf 2024-01-16 benni sigaddset(&nsigset, SIGTSTP);
303 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);
305 11a5e2cf 2024-01-16 benni /* always leave cursor after last displayed point */
306 11a5e2cf 2024-01-16 benni curscreen[D_LAST * B_COLS - 1] = -1;
308 11a5e2cf 2024-01-16 benni if (score != curscore) {
309 11a5e2cf 2024-01-16 benni if (cursor_home)
310 11a5e2cf 2024-01-16 benni putpad(cursor_home);
312 11a5e2cf 2024-01-16 benni moveto(0, 0);
313 11a5e2cf 2024-01-16 benni setcolor(0);
314 11a5e2cf 2024-01-16 benni (void) printf("Score: %d", score);
315 11a5e2cf 2024-01-16 benni curscore = score;
318 11a5e2cf 2024-01-16 benni /* draw preview of nextpattern */
319 11a5e2cf 2024-01-16 benni if (showpreview && (nextshape != lastshape)) {
320 11a5e2cf 2024-01-16 benni static int r=5, c=2;
321 11a5e2cf 2024-01-16 benni int tr, tc, t;
323 11a5e2cf 2024-01-16 benni lastshape = nextshape;
325 11a5e2cf 2024-01-16 benni /* clean */
326 11a5e2cf 2024-01-16 benni putpad(exit_standout_mode);
327 11a5e2cf 2024-01-16 benni moveto(r-1, c-1); putstr(" ");
328 11a5e2cf 2024-01-16 benni moveto(r, c-1); putstr(" ");
329 11a5e2cf 2024-01-16 benni moveto(r+1, c-1); putstr(" ");
330 11a5e2cf 2024-01-16 benni moveto(r+2, c-1); putstr(" ");
332 11a5e2cf 2024-01-16 benni moveto(r-3, c-2);
333 11a5e2cf 2024-01-16 benni putstr("Next shape:");
335 11a5e2cf 2024-01-16 benni /* draw */
336 11a5e2cf 2024-01-16 benni setcolor(nextshape->color);
337 11a5e2cf 2024-01-16 benni putpad(enter_standout_mode);
338 11a5e2cf 2024-01-16 benni moveto(r, 2*c);
339 11a5e2cf 2024-01-16 benni putstr(" ");
340 11a5e2cf 2024-01-16 benni for(i=0; i<3; i++) {
341 11a5e2cf 2024-01-16 benni t = c + r*B_COLS;
342 11a5e2cf 2024-01-16 benni t += nextshape->off[i];
344 11a5e2cf 2024-01-16 benni tr = t / B_COLS;
345 11a5e2cf 2024-01-16 benni tc = t % B_COLS;
347 11a5e2cf 2024-01-16 benni moveto(tr, 2*tc);
348 11a5e2cf 2024-01-16 benni putstr(" ");
350 11a5e2cf 2024-01-16 benni putpad(exit_standout_mode);
353 11a5e2cf 2024-01-16 benni bp = &board[D_FIRST * B_COLS];
354 11a5e2cf 2024-01-16 benni sp = &curscreen[D_FIRST * B_COLS];
355 11a5e2cf 2024-01-16 benni for (j = D_FIRST; j < D_LAST; j++) {
356 11a5e2cf 2024-01-16 benni ccol = -1;
357 11a5e2cf 2024-01-16 benni for (i = 0; i < B_COLS; bp++, sp++, i++) {
358 11a5e2cf 2024-01-16 benni if (*sp == (so = *bp))
361 11a5e2cf 2024-01-16 benni if (i != ccol) {
362 11a5e2cf 2024-01-16 benni if (cur_so && move_standout_mode) {
363 11a5e2cf 2024-01-16 benni putpad(exit_standout_mode);
364 11a5e2cf 2024-01-16 benni cur_so = 0;
366 11a5e2cf 2024-01-16 benni moveto(RTOD(j + Offset), CTOD(i));
368 11a5e2cf 2024-01-16 benni if (enter_standout_mode) {
369 11a5e2cf 2024-01-16 benni if (so != cur_so) {
370 11a5e2cf 2024-01-16 benni setcolor(so);
371 11a5e2cf 2024-01-16 benni putpad(so ?
372 11a5e2cf 2024-01-16 benni enter_standout_mode :
373 11a5e2cf 2024-01-16 benni exit_standout_mode);
374 11a5e2cf 2024-01-16 benni cur_so = so;
376 11a5e2cf 2024-01-16 benni #ifdef DEBUG
377 11a5e2cf 2024-01-16 benni char buf[3];
378 11a5e2cf 2024-01-16 benni snprintf(buf, sizeof(buf), "%d%d", so, so);
379 11a5e2cf 2024-01-16 benni putstr(buf);
381 11a5e2cf 2024-01-16 benni putstr(" ");
384 11a5e2cf 2024-01-16 benni putstr(so ? "XX" : " ");
385 11a5e2cf 2024-01-16 benni ccol = i + 1;
387 11a5e2cf 2024-01-16 benni * Look ahead a bit, to avoid extra motion if
388 11a5e2cf 2024-01-16 benni * we will be redrawing the cell after the next.
389 11a5e2cf 2024-01-16 benni * Motion probably takes four or more characters,
390 11a5e2cf 2024-01-16 benni * so we save even if we rewrite two cells
391 11a5e2cf 2024-01-16 benni * `unnecessarily'. Skip it all, though, if
392 11a5e2cf 2024-01-16 benni * the next cell is a different color.
394 11a5e2cf 2024-01-16 benni #define STOP (B_COLS - 3)
395 11a5e2cf 2024-01-16 benni if (i > STOP || sp[1] != bp[1] || so != bp[1])
397 11a5e2cf 2024-01-16 benni if (sp[2] != bp[2])
398 11a5e2cf 2024-01-16 benni sp[1] = -1;
399 11a5e2cf 2024-01-16 benni else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
400 11a5e2cf 2024-01-16 benni sp[2] = -1;
401 11a5e2cf 2024-01-16 benni sp[1] = -1;
405 11a5e2cf 2024-01-16 benni if (cur_so)
406 11a5e2cf 2024-01-16 benni putpad(exit_standout_mode);
407 11a5e2cf 2024-01-16 benni (void) fflush(stdout);
408 11a5e2cf 2024-01-16 benni (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
412 11a5e2cf 2024-01-16 benni * Write a message (set!=0), or clear the same message (set==0).
413 11a5e2cf 2024-01-16 benni * (We need its length in case we have to overwrite with blanks.)
416 11a5e2cf 2024-01-16 benni scr_msg(char *s, int set)
419 11a5e2cf 2024-01-16 benni if (set || clr_eol == NULL) {
420 11a5e2cf 2024-01-16 benni int l = strlen(s);
422 11a5e2cf 2024-01-16 benni moveto(Rows - 2, ((Cols - l) >> 1) - 1);
424 11a5e2cf 2024-01-16 benni putstr(s);
426 11a5e2cf 2024-01-16 benni while (--l >= 0)
427 11a5e2cf 2024-01-16 benni (void) putchar(' ');
429 11a5e2cf 2024-01-16 benni moveto(Rows - 2, 0);
430 11a5e2cf 2024-01-16 benni putpad(clr_eol);