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