[e9a3c52] | 1 | /* $OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray Exp $ */
|
---|
| 2 | /* $NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft Exp $ */
|
---|
| 3 |
|
---|
| 4 | /*-
|
---|
| 5 | * Copyright (c) 1992, 1993
|
---|
| 6 | * The Regents of the University of California. All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * This code is derived from software contributed to Berkeley by
|
---|
| 9 | * Chris Torek and Darren F. Provine.
|
---|
| 10 | *
|
---|
| 11 | * Redistribution and use in source and binary forms, with or without
|
---|
| 12 | * modification, are permitted provided that the following conditions
|
---|
| 13 | * are met:
|
---|
| 14 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer.
|
---|
| 16 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 17 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 18 | * documentation and/or other materials provided with the distribution.
|
---|
| 19 | * 3. Neither the name of the University nor the names of its contributors
|
---|
| 20 | * may be used to endorse or promote products derived from this software
|
---|
| 21 | * without specific prior written permission.
|
---|
| 22 | *
|
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 33 | * SUCH DAMAGE.
|
---|
| 34 | *
|
---|
| 35 | * @(#)screen.c 8.1 (Berkeley) 5/31/93
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | /*
|
---|
| 39 | * Tetris screen control.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #include <err.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 | #include <string.h>
|
---|
| 46 | #include <unistd.h>
|
---|
[b917098] | 47 | #include <io/stream.h>
|
---|
[e9a3c52] | 48 |
|
---|
[f1b4e74] | 49 |
|
---|
| 50 | #include <async.h>
|
---|
[e9a3c52] | 51 | #include "screen.h"
|
---|
| 52 | #include "tetris.h"
|
---|
[f1b4e74] | 53 | #include "../console/console.h"
|
---|
[e9a3c52] | 54 |
|
---|
| 55 | static cell curscreen[B_SIZE]; /* 1 => standout (or otherwise marked) */
|
---|
| 56 | static int curscore;
|
---|
| 57 | static int isset; /* true => terminal is in game mode */
|
---|
| 58 | static struct termios oldtt;
|
---|
| 59 | static void (*tstp)(int);
|
---|
| 60 |
|
---|
| 61 | static void scr_stop(int);
|
---|
| 62 | static void stopset(int);
|
---|
| 63 |
|
---|
[f1b4e74] | 64 | static char
|
---|
| 65 | *CEstr; /* clear to end of line */
|
---|
| 66 |
|
---|
| 67 |
|
---|
[e9a3c52] | 68 | /*
|
---|
[f1b4e74] | 69 | * putstr() is for unpadded strings (either as in termcap(5) or
|
---|
| 70 | * simply literal strings);
|
---|
[e9a3c52] | 71 | */
|
---|
[f1b4e74] | 72 | #define putstr(s) puts(s)
|
---|
[e9a3c52] | 73 |
|
---|
[f1b4e74] | 74 | static int con_phone;
|
---|
[e9a3c52] | 75 |
|
---|
| 76 |
|
---|
| 77 |
|
---|
[f1b4e74] | 78 | static void set_style(int fgcolor, int bgcolor)
|
---|
| 79 | {
|
---|
| 80 | send_call_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor);
|
---|
| 81 | }
|
---|
[e9a3c52] | 82 |
|
---|
[f1b4e74] | 83 | static void start_standout(void)
|
---|
[e9a3c52] | 84 | {
|
---|
[f1b4e74] | 85 | set_style(0, 0xe0e0e0);
|
---|
| 86 | }
|
---|
[e9a3c52] | 87 |
|
---|
[f1b4e74] | 88 | static void resume_normal(void)
|
---|
| 89 | {
|
---|
| 90 | set_style(0xe0e0e0, 0);
|
---|
[e9a3c52] | 91 | }
|
---|
| 92 |
|
---|
| 93 | /*
|
---|
[f1b4e74] | 94 | * Clear the screen, forgetting the current contents in the process.
|
---|
[e9a3c52] | 95 | */
|
---|
[f1b4e74] | 96 | void
|
---|
| 97 | scr_clear(void)
|
---|
| 98 | {
|
---|
[e9a3c52] | 99 |
|
---|
[d6cc453] | 100 | resume_normal();
|
---|
[f1b4e74] | 101 | send_call(con_phone, CONSOLE_CLEAR, 0);
|
---|
| 102 | curscore = -1;
|
---|
| 103 | memset((char *)curscreen, 0, sizeof(curscreen));
|
---|
| 104 | }
|
---|
[b917098] | 105 |
|
---|
[e9a3c52] | 106 | /*
|
---|
[f1b4e74] | 107 | * Set up screen
|
---|
[e9a3c52] | 108 | */
|
---|
| 109 | void
|
---|
| 110 | scr_init(void)
|
---|
| 111 | {
|
---|
[b917098] | 112 | con_phone = get_fd_phone(1);
|
---|
[f1b4e74] | 113 | resume_normal();
|
---|
| 114 | scr_clear();
|
---|
[e9a3c52] | 115 | }
|
---|
| 116 |
|
---|
[f1b4e74] | 117 | static void moveto(int r, int c)
|
---|
| 118 | {
|
---|
| 119 | send_call_2(con_phone, CONSOLE_GOTO, r, c);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | static void fflush(void)
|
---|
| 123 | {
|
---|
| 124 | send_call(con_phone, CONSOLE_FLUSH, 0);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | struct winsize {
|
---|
| 128 | ipcarg_t ws_row;
|
---|
| 129 | ipcarg_t ws_col;
|
---|
| 130 | };
|
---|
| 131 |
|
---|
| 132 | static int get_display_size(struct winsize *ws)
|
---|
| 133 | {
|
---|
| 134 | return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
|
---|
| 135 | }
|
---|
[e9a3c52] | 136 |
|
---|
| 137 | static void
|
---|
| 138 | scr_stop(int sig)
|
---|
| 139 | {
|
---|
| 140 |
|
---|
| 141 | scr_end();
|
---|
| 142 | scr_set();
|
---|
| 143 | scr_msg(key_msg, 1);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * Set up screen mode.
|
---|
| 148 | */
|
---|
| 149 | void
|
---|
| 150 | scr_set(void)
|
---|
| 151 | {
|
---|
| 152 | struct winsize ws;
|
---|
| 153 |
|
---|
| 154 | Rows = 0, Cols = 0;
|
---|
[f1b4e74] | 155 | if (get_display_size(&ws) == 0) {
|
---|
[e9a3c52] | 156 | Rows = ws.ws_row;
|
---|
| 157 | Cols = ws.ws_col;
|
---|
| 158 | }
|
---|
| 159 | if (Rows < MINROWS || Cols < MINCOLS) {
|
---|
| 160 | char smallscr[55];
|
---|
| 161 |
|
---|
| 162 | (void)snprintf(smallscr, sizeof(smallscr),
|
---|
| 163 | "the screen is too small (must be at least %dx%d)",
|
---|
| 164 | MINROWS, MINCOLS);
|
---|
| 165 | stop(smallscr);
|
---|
| 166 | }
|
---|
| 167 | isset = 1;
|
---|
[b917098] | 168 |
|
---|
[e9a3c52] | 169 | scr_clear();
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /*
|
---|
| 173 | * End screen mode.
|
---|
| 174 | */
|
---|
| 175 | void
|
---|
| 176 | scr_end(void)
|
---|
| 177 | {
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | void
|
---|
| 181 | stop(char *why)
|
---|
| 182 | {
|
---|
| 183 |
|
---|
| 184 | if (isset)
|
---|
| 185 | scr_end();
|
---|
| 186 | errx(1, "aborting: %s", why);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | /*
|
---|
| 191 | * Update the screen.
|
---|
| 192 | */
|
---|
| 193 | void
|
---|
| 194 | scr_update(void)
|
---|
| 195 | {
|
---|
| 196 | cell *bp, *sp;
|
---|
[d6cc453] | 197 | cell so, cur_so = 0;
|
---|
[e9a3c52] | 198 | int i, ccol, j;
|
---|
| 199 | static const struct shape *lastshape;
|
---|
| 200 |
|
---|
| 201 | /* always leave cursor after last displayed point */
|
---|
| 202 | curscreen[D_LAST * B_COLS - 1] = -1;
|
---|
| 203 |
|
---|
| 204 | if (score != curscore) {
|
---|
[f1b4e74] | 205 | moveto(0, 0);
|
---|
[e9a3c52] | 206 | (void) printf("Score: %d", score);
|
---|
| 207 | curscore = score;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /* draw preview of next pattern */
|
---|
| 211 | if (showpreview && (nextshape != lastshape)) {
|
---|
| 212 | int i;
|
---|
| 213 | static int r=5, c=2;
|
---|
| 214 | int tr, tc, t;
|
---|
| 215 |
|
---|
| 216 | lastshape = nextshape;
|
---|
| 217 |
|
---|
| 218 | /* clean */
|
---|
[f1b4e74] | 219 | resume_normal();
|
---|
[e9a3c52] | 220 | moveto(r-1, c-1); putstr(" ");
|
---|
| 221 | moveto(r, c-1); putstr(" ");
|
---|
| 222 | moveto(r+1, c-1); putstr(" ");
|
---|
| 223 | moveto(r+2, c-1); putstr(" ");
|
---|
| 224 |
|
---|
| 225 | moveto(r-3, c-2);
|
---|
| 226 | putstr("Next shape:");
|
---|
| 227 |
|
---|
| 228 | /* draw */
|
---|
[f1b4e74] | 229 | start_standout();
|
---|
[e9a3c52] | 230 | moveto(r, 2 * c);
|
---|
[f1b4e74] | 231 | putstr(" ");
|
---|
[e9a3c52] | 232 | for (i = 0; i < 3; i++) {
|
---|
| 233 | t = c + r * B_COLS;
|
---|
| 234 | t += nextshape->off[i];
|
---|
| 235 |
|
---|
| 236 | tr = t / B_COLS;
|
---|
| 237 | tc = t % B_COLS;
|
---|
| 238 |
|
---|
| 239 | moveto(tr, 2*tc);
|
---|
[f1b4e74] | 240 | putstr(" ");
|
---|
[e9a3c52] | 241 | }
|
---|
[f1b4e74] | 242 | resume_normal();
|
---|
[e9a3c52] | 243 | }
|
---|
| 244 |
|
---|
| 245 | bp = &board[D_FIRST * B_COLS];
|
---|
| 246 | sp = &curscreen[D_FIRST * B_COLS];
|
---|
| 247 | for (j = D_FIRST; j < D_LAST; j++) {
|
---|
| 248 | ccol = -1;
|
---|
| 249 | for (i = 0; i < B_COLS; bp++, sp++, i++) {
|
---|
| 250 | if (*sp == (so = *bp))
|
---|
| 251 | continue;
|
---|
| 252 | *sp = so;
|
---|
| 253 | if (i != ccol) {
|
---|
| 254 | moveto(RTOD(j), CTOD(i));
|
---|
| 255 | }
|
---|
[f1b4e74] | 256 | if (so != cur_so) {
|
---|
| 257 | if (so)
|
---|
| 258 | start_standout();
|
---|
| 259 | else
|
---|
| 260 | resume_normal();
|
---|
| 261 | cur_so = so;
|
---|
| 262 | }
|
---|
| 263 | putstr(" ");
|
---|
[e9a3c52] | 264 | ccol = i + 1;
|
---|
| 265 | /*
|
---|
| 266 | * Look ahead a bit, to avoid extra motion if
|
---|
| 267 | * we will be redrawing the cell after the next.
|
---|
| 268 | * Motion probably takes four or more characters,
|
---|
| 269 | * so we save even if we rewrite two cells
|
---|
| 270 | * `unnecessarily'. Skip it all, though, if
|
---|
| 271 | * the next cell is a different color.
|
---|
| 272 | */
|
---|
| 273 | #define STOP (B_COLS - 3)
|
---|
| 274 | if (i > STOP || sp[1] != bp[1] || so != bp[1])
|
---|
| 275 | continue;
|
---|
| 276 | if (sp[2] != bp[2])
|
---|
| 277 | sp[1] = -1;
|
---|
| 278 | else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
|
---|
| 279 | sp[2] = -1;
|
---|
| 280 | sp[1] = -1;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
[f1b4e74] | 284 | resume_normal();
|
---|
| 285 |
|
---|
| 286 | fflush();
|
---|
[e9a3c52] | 287 | }
|
---|
| 288 |
|
---|
| 289 | /*
|
---|
| 290 | * Write a message (set!=0), or clear the same message (set==0).
|
---|
| 291 | * (We need its length in case we have to overwrite with blanks.)
|
---|
| 292 | */
|
---|
| 293 | void
|
---|
| 294 | scr_msg(char *s, int set)
|
---|
| 295 | {
|
---|
| 296 |
|
---|
[d6cc453] | 297 | int l = strlen(s);
|
---|
| 298 |
|
---|
| 299 | moveto(Rows - 2, ((Cols - l) >> 1) - 1);
|
---|
| 300 | if (set)
|
---|
| 301 | putstr(s);
|
---|
| 302 | else
|
---|
| 303 | while (--l >= 0)
|
---|
| 304 | (void) putchar(' ');
|
---|
[e9a3c52] | 305 | }
|
---|