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