| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011 Martin Decky | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** Attributations | 
|---|
| 30 | * | 
|---|
| 31 | * screen.c 8.1 (Berkeley) 5/31/93 | 
|---|
| 32 | * NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft | 
|---|
| 33 | * OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray | 
|---|
| 34 | * | 
|---|
| 35 | * Based upon BSD Tetris | 
|---|
| 36 | * | 
|---|
| 37 | * Copyright (c) 1992, 1993 | 
|---|
| 38 | *      The Regents of the University of California. | 
|---|
| 39 | *      Distributed under BSD license. | 
|---|
| 40 | * | 
|---|
| 41 | * This code is derived from software contributed to Berkeley by | 
|---|
| 42 | * Chris Torek and Darren F. Provine. | 
|---|
| 43 | * | 
|---|
| 44 | */ | 
|---|
| 45 |  | 
|---|
| 46 | /** @addtogroup tetris | 
|---|
| 47 | * @{ | 
|---|
| 48 | */ | 
|---|
| 49 | /** @file | 
|---|
| 50 | */ | 
|---|
| 51 |  | 
|---|
| 52 | /* | 
|---|
| 53 | * Tetris screen control. | 
|---|
| 54 | */ | 
|---|
| 55 |  | 
|---|
| 56 | #include <err.h> | 
|---|
| 57 | #include <stdio.h> | 
|---|
| 58 | #include <stdlib.h> | 
|---|
| 59 | #include <str.h> | 
|---|
| 60 | #include <unistd.h> | 
|---|
| 61 | #include <vfs/vfs.h> | 
|---|
| 62 | #include <async.h> | 
|---|
| 63 | #include <bool.h> | 
|---|
| 64 | #include <io/console.h> | 
|---|
| 65 | #include <io/style.h> | 
|---|
| 66 | #include "screen.h" | 
|---|
| 67 | #include "tetris.h" | 
|---|
| 68 |  | 
|---|
| 69 | #define STOP  (B_COLS - 3) | 
|---|
| 70 |  | 
|---|
| 71 | static cell curscreen[B_SIZE];  /* non-zero => standout (or otherwise marked) */ | 
|---|
| 72 | static int curscore; | 
|---|
| 73 | static int isset;               /* true => terminal is in game mode */ | 
|---|
| 74 |  | 
|---|
| 75 | static bool use_color;          /* true => use colors */ | 
|---|
| 76 |  | 
|---|
| 77 | static const struct shape *lastshape; | 
|---|
| 78 |  | 
|---|
| 79 | static suseconds_t timeleft = 0; | 
|---|
| 80 |  | 
|---|
| 81 | console_ctrl_t *console; | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | /* | 
|---|
| 85 | * putstr() is for unpadded strings (either as in termcap(5) or | 
|---|
| 86 | * simply literal strings); | 
|---|
| 87 | */ | 
|---|
| 88 | static inline void putstr(const char *s) | 
|---|
| 89 | { | 
|---|
| 90 | while (*s) | 
|---|
| 91 | putchar(*(s++)); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | static void start_standout(uint32_t color) | 
|---|
| 95 | { | 
|---|
| 96 | console_flush(console); | 
|---|
| 97 | console_set_rgb_color(console, use_color ? color : 0x000000, | 
|---|
| 98 | 0xffffff); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | static void resume_normal(void) | 
|---|
| 102 | { | 
|---|
| 103 | console_flush(console); | 
|---|
| 104 | console_set_style(console, STYLE_NORMAL); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void clear_screen(void) | 
|---|
| 108 | { | 
|---|
| 109 | console_clear(console); | 
|---|
| 110 | moveto(0, 0); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | /* | 
|---|
| 114 | * Clear the screen, forgetting the current contents in the process. | 
|---|
| 115 | */ | 
|---|
| 116 | void scr_clear(void) | 
|---|
| 117 | { | 
|---|
| 118 | resume_normal(); | 
|---|
| 119 | console_clear(console); | 
|---|
| 120 | curscore = -1; | 
|---|
| 121 | memset(curscreen, 0, sizeof(curscreen)); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /* | 
|---|
| 125 | * Set up screen | 
|---|
| 126 | */ | 
|---|
| 127 | void scr_init(void) | 
|---|
| 128 | { | 
|---|
| 129 | console_cursor_visibility(console, 0); | 
|---|
| 130 | resume_normal(); | 
|---|
| 131 | scr_clear(); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | void moveto(sysarg_t r, sysarg_t c) | 
|---|
| 135 | { | 
|---|
| 136 | console_flush(console); | 
|---|
| 137 | console_set_pos(console, c, r); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | winsize_t winsize; | 
|---|
| 141 |  | 
|---|
| 142 | static int get_display_size(winsize_t *ws) | 
|---|
| 143 | { | 
|---|
| 144 | return console_get_size(console, &ws->ws_col, &ws->ws_row); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | static bool get_display_color_sup(void) | 
|---|
| 148 | { | 
|---|
| 149 | sysarg_t ccap; | 
|---|
| 150 | int rc = console_get_color_cap(console, &ccap); | 
|---|
| 151 |  | 
|---|
| 152 | if (rc != 0) | 
|---|
| 153 | return false; | 
|---|
| 154 |  | 
|---|
| 155 | return ((ccap & CONSOLE_CAP_RGB) == CONSOLE_CAP_RGB); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | /* | 
|---|
| 159 | * Set up screen mode. | 
|---|
| 160 | */ | 
|---|
| 161 | void scr_set(void) | 
|---|
| 162 | { | 
|---|
| 163 | winsize_t ws; | 
|---|
| 164 |  | 
|---|
| 165 | Rows = 0; | 
|---|
| 166 | Cols = 0; | 
|---|
| 167 |  | 
|---|
| 168 | if (get_display_size(&ws) == 0) { | 
|---|
| 169 | Rows = ws.ws_row; | 
|---|
| 170 | Cols = ws.ws_col; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | use_color = get_display_color_sup(); | 
|---|
| 174 |  | 
|---|
| 175 | if ((Rows < MINROWS) || (Cols < MINCOLS)) { | 
|---|
| 176 | char smallscr[55]; | 
|---|
| 177 |  | 
|---|
| 178 | snprintf(smallscr, sizeof(smallscr), | 
|---|
| 179 | "the screen is too small (must be at least %dx%d)", | 
|---|
| 180 | MINROWS, MINCOLS); | 
|---|
| 181 | stop(smallscr); | 
|---|
| 182 | } | 
|---|
| 183 | isset = 1; | 
|---|
| 184 |  | 
|---|
| 185 | scr_clear(); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /* | 
|---|
| 189 | * End screen mode. | 
|---|
| 190 | */ | 
|---|
| 191 | void scr_end(void) | 
|---|
| 192 | { | 
|---|
| 193 | console_cursor_visibility(console, 1); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | void stop(const char *why) | 
|---|
| 197 | { | 
|---|
| 198 | if (isset) | 
|---|
| 199 | scr_end(); | 
|---|
| 200 |  | 
|---|
| 201 | errx(1, "aborting: %s", why); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | /* | 
|---|
| 205 | * Update the screen. | 
|---|
| 206 | */ | 
|---|
| 207 | void scr_update(void) | 
|---|
| 208 | { | 
|---|
| 209 | cell *bp; | 
|---|
| 210 | cell *sp; | 
|---|
| 211 | cell so; | 
|---|
| 212 | cell cur_so = 0; | 
|---|
| 213 | int i; | 
|---|
| 214 | int j; | 
|---|
| 215 | int ccol; | 
|---|
| 216 |  | 
|---|
| 217 | /* Always leave cursor after last displayed point */ | 
|---|
| 218 | curscreen[D_LAST * B_COLS - 1] = -1; | 
|---|
| 219 |  | 
|---|
| 220 | if (score != curscore) { | 
|---|
| 221 | moveto(0, 0); | 
|---|
| 222 | printf("Score: %d", score); | 
|---|
| 223 | curscore = score; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | /* Draw preview of next pattern */ | 
|---|
| 227 | if ((showpreview) && (nextshape != lastshape)) { | 
|---|
| 228 | int i; | 
|---|
| 229 | static int r = 5, c = 2; | 
|---|
| 230 | int tr, tc, t; | 
|---|
| 231 |  | 
|---|
| 232 | lastshape = nextshape; | 
|---|
| 233 |  | 
|---|
| 234 | /* Clean */ | 
|---|
| 235 | resume_normal(); | 
|---|
| 236 | moveto(r - 1, c - 1); | 
|---|
| 237 | putstr("          "); | 
|---|
| 238 | moveto(r, c - 1); | 
|---|
| 239 | putstr("          "); | 
|---|
| 240 | moveto(r + 1, c - 1); | 
|---|
| 241 | putstr("          "); | 
|---|
| 242 | moveto(r + 2, c - 1); | 
|---|
| 243 | putstr("          "); | 
|---|
| 244 |  | 
|---|
| 245 | moveto(r - 3, c - 2); | 
|---|
| 246 | putstr("Next shape:"); | 
|---|
| 247 |  | 
|---|
| 248 | /* Draw */ | 
|---|
| 249 | start_standout(nextshape->color); | 
|---|
| 250 | moveto(r, 2 * c); | 
|---|
| 251 | putstr("  "); | 
|---|
| 252 | for (i = 0; i < 3; i++) { | 
|---|
| 253 | t = c + r * B_COLS; | 
|---|
| 254 | t += nextshape->off[i]; | 
|---|
| 255 |  | 
|---|
| 256 | tr = t / B_COLS; | 
|---|
| 257 | tc = t % B_COLS; | 
|---|
| 258 |  | 
|---|
| 259 | moveto(tr, 2*tc); | 
|---|
| 260 | putstr("  "); | 
|---|
| 261 | } | 
|---|
| 262 | resume_normal(); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | bp = &board[D_FIRST * B_COLS]; | 
|---|
| 266 | sp = &curscreen[D_FIRST * B_COLS]; | 
|---|
| 267 | for (j = D_FIRST; j < D_LAST; j++) { | 
|---|
| 268 | ccol = -1; | 
|---|
| 269 | for (i = 0; i < B_COLS; bp++, sp++, i++) { | 
|---|
| 270 | if (*sp == (so = *bp)) | 
|---|
| 271 | continue; | 
|---|
| 272 |  | 
|---|
| 273 | *sp = so; | 
|---|
| 274 | if (i != ccol) { | 
|---|
| 275 | if (cur_so) { | 
|---|
| 276 | resume_normal(); | 
|---|
| 277 | cur_so = 0; | 
|---|
| 278 | } | 
|---|
| 279 | moveto(RTOD(j), CTOD(i)); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | if (so != cur_so) { | 
|---|
| 283 | if (so) | 
|---|
| 284 | start_standout(so); | 
|---|
| 285 | else | 
|---|
| 286 | resume_normal(); | 
|---|
| 287 | cur_so = so; | 
|---|
| 288 | } | 
|---|
| 289 | putstr("  "); | 
|---|
| 290 |  | 
|---|
| 291 | ccol = i + 1; | 
|---|
| 292 | /* | 
|---|
| 293 | * Look ahead a bit, to avoid extra motion if | 
|---|
| 294 | * we will be redrawing the cell after the next. | 
|---|
| 295 | * Motion probably takes four or more characters, | 
|---|
| 296 | * so we save even if we rewrite two cells | 
|---|
| 297 | * `unnecessarily'.  Skip it all, though, if | 
|---|
| 298 | * the next cell is a different color. | 
|---|
| 299 | */ | 
|---|
| 300 |  | 
|---|
| 301 | if ((i > STOP) || (sp[1] != bp[1]) || (so != bp[1])) | 
|---|
| 302 | continue; | 
|---|
| 303 |  | 
|---|
| 304 | if (sp[2] != bp[2]) | 
|---|
| 305 | sp[1] = -1; | 
|---|
| 306 | else if ((i < STOP) && (so == bp[2]) && (sp[3] != bp[3])) { | 
|---|
| 307 | sp[2] = -1; | 
|---|
| 308 | sp[1] = -1; | 
|---|
| 309 | } | 
|---|
| 310 | } | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | if (cur_so) | 
|---|
| 314 | resume_normal(); | 
|---|
| 315 |  | 
|---|
| 316 | console_flush(console); | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | /* | 
|---|
| 320 | * Write a message (set != 0), or clear the same message (set == 0). | 
|---|
| 321 | * (We need its length in case we have to overwrite with blanks.) | 
|---|
| 322 | */ | 
|---|
| 323 | void scr_msg(char *s, bool set) | 
|---|
| 324 | { | 
|---|
| 325 | int l = str_size(s); | 
|---|
| 326 |  | 
|---|
| 327 | moveto(Rows - 2, ((Cols - l) >> 1) - 1); | 
|---|
| 328 |  | 
|---|
| 329 | if (set) | 
|---|
| 330 | putstr(s); | 
|---|
| 331 | else | 
|---|
| 332 | while (--l >= 0) | 
|---|
| 333 | (void) putchar(' '); | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | /** Sleep for the current turn time | 
|---|
| 337 | * | 
|---|
| 338 | * Eat any input that might be available. | 
|---|
| 339 | * | 
|---|
| 340 | */ | 
|---|
| 341 | void tsleep(void) | 
|---|
| 342 | { | 
|---|
| 343 | suseconds_t timeout = fallrate; | 
|---|
| 344 |  | 
|---|
| 345 | while (timeout > 0) { | 
|---|
| 346 | kbd_event_t event; | 
|---|
| 347 |  | 
|---|
| 348 | if (!console_get_kbd_event_timeout(console, &event, &timeout)) | 
|---|
| 349 | break; | 
|---|
| 350 | } | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 | /** Get char with timeout | 
|---|
| 354 | * | 
|---|
| 355 | */ | 
|---|
| 356 | int tgetchar(void) | 
|---|
| 357 | { | 
|---|
| 358 | /* | 
|---|
| 359 | * Reset timeleft to fallrate whenever it is not positive | 
|---|
| 360 | * and increase speed. | 
|---|
| 361 | */ | 
|---|
| 362 |  | 
|---|
| 363 | if (timeleft <= 0) { | 
|---|
| 364 | faster(); | 
|---|
| 365 | timeleft = fallrate; | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | /* | 
|---|
| 369 | * Wait to see if there is any input. If so, take it and | 
|---|
| 370 | * update timeleft so that the next call to tgetchar() | 
|---|
| 371 | * will not wait as long. If there is no input, | 
|---|
| 372 | * make timeleft zero and return -1. | 
|---|
| 373 | */ | 
|---|
| 374 |  | 
|---|
| 375 | wchar_t c = 0; | 
|---|
| 376 |  | 
|---|
| 377 | while (c == 0) { | 
|---|
| 378 | kbd_event_t event; | 
|---|
| 379 |  | 
|---|
| 380 | if (!console_get_kbd_event_timeout(console, &event, &timeleft)) { | 
|---|
| 381 | timeleft = 0; | 
|---|
| 382 | return -1; | 
|---|
| 383 | } | 
|---|
| 384 |  | 
|---|
| 385 | if (event.type == KEY_PRESS) | 
|---|
| 386 | c = event.c; | 
|---|
| 387 | } | 
|---|
| 388 |  | 
|---|
| 389 | return (int) c; | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | /** Get char without timeout | 
|---|
| 393 | * | 
|---|
| 394 | */ | 
|---|
| 395 | int twait(void) | 
|---|
| 396 | { | 
|---|
| 397 | wchar_t c = 0; | 
|---|
| 398 |  | 
|---|
| 399 | while (c == 0) { | 
|---|
| 400 | kbd_event_t event; | 
|---|
| 401 |  | 
|---|
| 402 | if (!console_get_kbd_event(console, &event)) | 
|---|
| 403 | return -1; | 
|---|
| 404 |  | 
|---|
| 405 | if (event.type == KEY_PRESS) | 
|---|
| 406 | c = event.c; | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 | return (int) c; | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | /** @} | 
|---|
| 413 | */ | 
|---|