]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/screen.c
2d58303b2fa09917fffb10fcd67c4fb13d597e16
1 /* $NetBSD: screen.c,v 1.27 2011/10/03 12:32:28 roy Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)screen.c 8.1 (Berkeley) 5/31/93
38 * Tetris screen control.
41 #include <sys/cdefs.h>
42 #include <sys/ioctl.h>
54 #define sigmask(s) (1 << ((s) - 1))
60 static cell curscreen
[B_SIZE
]; /* 1 => standout (or otherwise marked) */
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
;
71 * Routine used by tputs().
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().)
85 #define putstr(s) (void)fputs(s, stdout)
92 buf
= tiparm(cursor_address
, r
, c
);
98 * Set up from termcap.
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");
111 /* this foolery is needed to modify tty state `atomically' */
112 static jmp_buf scr_onstop
;
119 (void) signal(sig
, SIG_DFL
);
120 (void) kill(getpid(), sig
);
122 sigaddset(&set
, sig
);
123 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
124 longjmp(scr_onstop
, 1);
133 (void) kill(getpid(), sig
);
135 sigaddset(&set
, sig
);
136 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
142 * Set up screen mode.
148 struct termios newtt
;
149 sigset_t nsigset
, osigset
;
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
);
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.
165 (void) setjmp(scr_onstop
);
166 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
168 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
176 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
177 (void) fprintf(stderr
,
178 "the screen is too small: must be at least %dx%d, ",
180 stop(""); /* stop() supplies \n */
182 if (tcgetattr(0, &oldtt
) < 0)
183 stop("tcgetattr() fails");
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
);
193 * We made it. We are now in screen mode, modulo TIstr
194 * (which we will fix immediately).
197 putstr(enter_ca_mode
);
198 if (cursor_invisible
)
199 putstr(cursor_invisible
);
201 (void) signal(SIGTSTP
, scr_stop
);
203 (void) signal(SIGTTOU
, ttou
);
206 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
216 sigset_t nsigset
, osigset
;
218 sigemptyset(&nsigset
);
219 sigaddset(&nsigset
, SIGTSTP
);
220 sigaddset(&nsigset
, SIGTTOU
);
221 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
222 /* move cursor to last line */
224 putstr(cursor_to_ll
);
227 /* exit screen mode */
229 putstr(exit_ca_mode
);
231 putstr(cursor_normal
);
232 (void) fflush(stdout
);
233 (void) tcsetattr(0, TCSADRAIN
, &oldtt
);
235 /* restore signals */
236 (void) signal(SIGTSTP
, tstp
);
237 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
241 stop(const char *why
)
246 (void) fprintf(stderr
, "aborting: %s\n", why
);
251 * Clear the screen, forgetting the current contents in the process.
257 putpad(clear_screen
);
259 memset((char *)curscreen
, 0, sizeof(curscreen
));
263 typedef int regcell
; /* pcc is bad at `register char', etc */
265 typedef cell regcell
;
275 regcell so
, cur_so
= 0;
277 sigset_t nsigset
, osigset
;
278 static const struct shape
*lastshape
;
280 sigemptyset(&nsigset
);
281 sigaddset(&nsigset
, SIGTSTP
);
282 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
284 /* always leave cursor after last displayed point */
285 curscreen
[D_LAST
* B_COLS
- 1] = -1;
287 if (score
!= curscore
) {
292 (void) printf("Score: %d", score
);
296 /* draw preview of nextpattern */
297 if (showpreview
&& (nextshape
!= lastshape
)) {
301 lastshape
= nextshape
;
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(" ");
311 putstr("Next shape:");
314 putpad(enter_standout_mode
);
319 t
+= nextshape
->off
[i
];
327 putpad(exit_standout_mode
);
330 bp
= &board
[D_FIRST
* B_COLS
];
331 sp
= &curscreen
[D_FIRST
* B_COLS
];
332 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
334 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
335 if (*sp
== (so
= *bp
))
339 if (cur_so
&& move_standout_mode
) {
340 putpad(exit_standout_mode
);
343 moveto(RTOD(j
), CTOD(i
));
345 if (enter_standout_mode
) {
348 enter_standout_mode
:
354 putstr(so
? "XX" : " ");
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.
364 #define STOP (B_COLS - 3)
365 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
369 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
376 putpad(exit_standout_mode
);
377 (void) fflush(stdout
);
378 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
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.)
386 scr_msg(char *s
, int set
)
389 if (set
|| clr_eol
== NULL
) {
392 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);