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