]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/screen.c
1 /* $NetBSD: screen.c,v 1.33 2017/03/20 22:05:27 christos 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
);
101 char monochrome
[] = "\033[0m";
104 if (set_a_foreground
== NULL
)
107 if (c
== 0 || c
== 7)
110 buf
= tiparm(set_a_foreground
, c
);
116 * Set up from termcap.
122 setupterm(NULL
, 0, NULL
);
123 if (clear_screen
== NULL
)
124 stop("cannot clear screen");
125 if (cursor_address
== NULL
|| cursor_up
== NULL
)
126 stop("cannot do random cursor positioning");
129 /* this foolery is needed to modify tty state `atomically' */
130 static jmp_buf scr_onstop
;
137 (void) signal(sig
, SIG_DFL
);
138 (void) kill(getpid(), sig
);
140 sigaddset(&set
, sig
);
141 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
142 longjmp(scr_onstop
, 1);
151 (void) kill(getpid(), sig
);
153 sigaddset(&set
, sig
);
154 (void) sigprocmask(SIG_UNBLOCK
, &set
, (sigset_t
*)0);
160 * Set up screen mode.
166 struct termios newtt
;
167 sigset_t nsigset
, osigset
;
170 sigemptyset(&nsigset
);
171 sigaddset(&nsigset
, SIGTSTP
);
172 sigaddset(&nsigset
, SIGTTOU
);
173 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
174 if ((tstp
= signal(SIGTSTP
, stopset
)) == SIG_IGN
)
175 (void) signal(SIGTSTP
, SIG_IGN
);
176 if ((ttou
= signal(SIGTTOU
, stopset
)) == SIG_IGN
)
177 (void) signal(SIGTTOU
, SIG_IGN
);
179 * At last, we are ready to modify the tty state. If
180 * we stop while at it, stopset() above will longjmp back
181 * to the setjmp here and we will start over.
183 (void) setjmp(scr_onstop
);
184 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
186 if (ioctl(0, TIOCGWINSZ
, &ws
) == 0) {
194 if (Rows
< MINROWS
|| Cols
< MINCOLS
) {
195 (void) fprintf(stderr
,
196 "the screen is too small: must be at least %dx%d, ",
198 stop(""); /* stop() supplies \n */
200 Offset
= (Rows
- D_LAST
+ D_FIRST
- 2) / 2;
201 if (tcgetattr(0, &oldtt
) < 0)
202 stop("tcgetattr() fails");
204 newtt
.c_lflag
&= ~(ICANON
|ECHO
);
205 newtt
.c_oflag
&= ~OXTABS
;
206 if (tcsetattr(0, TCSADRAIN
, &newtt
) < 0)
207 stop("tcsetattr() fails");
208 ospeed
= cfgetospeed(&newtt
);
209 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
212 * We made it. We are now in screen mode, modulo TIstr
213 * (which we will fix immediately).
216 if ((tstr
= enter_ca_mode
) != NULL
)
218 if ((tstr
= cursor_invisible
) != NULL
)
221 (void) signal(SIGTSTP
, scr_stop
);
223 (void) signal(SIGTTOU
, ttou
);
226 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
236 sigset_t nsigset
, osigset
;
238 sigemptyset(&nsigset
);
239 sigaddset(&nsigset
, SIGTSTP
);
240 sigaddset(&nsigset
, SIGTTOU
);
241 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
242 /* move cursor to last line */
244 if ((tstr
= cursor_to_ll
) != NULL
)
248 /* exit screen mode */
249 if ((tstr
= exit_ca_mode
) != NULL
)
251 if ((tstr
= cursor_normal
) != NULL
)
253 (void) fflush(stdout
);
254 (void) tcsetattr(0, TCSADRAIN
, &oldtt
);
256 /* restore signals */
257 (void) signal(SIGTSTP
, tstp
);
258 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
262 stop(const char *why
)
267 (void) fprintf(stderr
, "aborting: %s\n", why
);
272 * Clear the screen, forgetting the current contents in the process.
278 putpad(clear_screen
);
280 memset((char *)curscreen
, 0, sizeof(curscreen
));
284 typedef int regcell
; /* pcc is bad at `register char', etc */
286 typedef cell regcell
;
296 regcell so
, cur_so
= 0;
298 sigset_t nsigset
, osigset
;
299 static const struct shape
*lastshape
;
301 sigemptyset(&nsigset
);
302 sigaddset(&nsigset
, SIGTSTP
);
303 (void) sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
305 /* always leave cursor after last displayed point */
306 curscreen
[D_LAST
* B_COLS
- 1] = -1;
308 if (score
!= curscore
) {
314 (void) printf("Score: %d", score
);
318 /* draw preview of nextpattern */
319 if (showpreview
&& (nextshape
!= lastshape
)) {
323 lastshape
= nextshape
;
326 putpad(exit_standout_mode
);
327 moveto(r
-1, c
-1); putstr(" ");
328 moveto(r
, c
-1); putstr(" ");
329 moveto(r
+1, c
-1); putstr(" ");
330 moveto(r
+2, c
-1); putstr(" ");
333 putstr("Next shape:");
336 setcolor(nextshape
->color
);
337 putpad(enter_standout_mode
);
342 t
+= nextshape
->off
[i
];
350 putpad(exit_standout_mode
);
353 bp
= &board
[D_FIRST
* B_COLS
];
354 sp
= &curscreen
[D_FIRST
* B_COLS
];
355 for (j
= D_FIRST
; j
< D_LAST
; j
++) {
357 for (i
= 0; i
< B_COLS
; bp
++, sp
++, i
++) {
358 if (*sp
== (so
= *bp
))
362 if (cur_so
&& move_standout_mode
) {
363 putpad(exit_standout_mode
);
366 moveto(RTOD(j
+ Offset
), CTOD(i
));
368 if (enter_standout_mode
) {
372 enter_standout_mode
:
378 snprintf(buf
, sizeof(buf
), "%d%d", so
, so
);
384 putstr(so
? "XX" : " ");
387 * Look ahead a bit, to avoid extra motion if
388 * we will be redrawing the cell after the next.
389 * Motion probably takes four or more characters,
390 * so we save even if we rewrite two cells
391 * `unnecessarily'. Skip it all, though, if
392 * the next cell is a different color.
394 #define STOP (B_COLS - 3)
395 if (i
> STOP
|| sp
[1] != bp
[1] || so
!= bp
[1])
399 else if (i
< STOP
&& so
== bp
[2] && sp
[3] != bp
[3]) {
406 putpad(exit_standout_mode
);
407 (void) fflush(stdout
);
408 (void) sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
412 * Write a message (set!=0), or clear the same message (set==0).
413 * (We need its length in case we have to overwrite with blanks.)
416 scr_msg(char *s
, int set
)
419 if (set
|| clr_eol
== NULL
) {
422 moveto(Rows
- 2, ((Cols
- l
) >> 1) - 1);