Subversion Repositories HelenOS

Rev

Rev 1964 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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. /** @addtogroup tetris
  39.  * @{
  40.  */
  41. /** @file
  42.  */
  43.  
  44. /*
  45.  * Tetris screen control.
  46.  */
  47.  
  48. #include <err.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. #include <io/stream.h>
  54.  
  55.  
  56. #include <async.h>
  57. #include "screen.h"
  58. #include "tetris.h"
  59. #include "../console/console.h"
  60.  
  61. static cell curscreen[B_SIZE];  /* 1 => standout (or otherwise marked) */
  62. static int curscore;
  63. static int isset;       /* true => terminal is in game mode */
  64. static void (*tstp)(int);
  65.  
  66. static void scr_stop(int);
  67. static void stopset(int);
  68.  
  69. static char
  70.         *CEstr;         /* clear to end of line */
  71.  
  72.  
  73. /*
  74.  * putstr() is for unpadded strings (either as in termcap(5) or
  75.  * simply literal strings);
  76.  */
  77. static inline void putstr(char *s)
  78. {
  79.     while (*s)
  80.         putchar(*(s++));
  81. }
  82.  
  83. static int con_phone;
  84.  
  85.  
  86.  
  87. static void set_style(int fgcolor, int bgcolor)
  88. {
  89.     async_msg_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor);
  90. }
  91.  
  92. static void start_standout(void)
  93. {
  94.     set_style(0xf0f0f0, 0);
  95. }
  96.  
  97. static void resume_normal(void)
  98. {
  99.     set_style(0, 0xf0f0f0);
  100. }
  101.  
  102.  
  103. void clear_screen(void)
  104. {
  105.     async_msg(con_phone, CONSOLE_CLEAR, 0);
  106.     moveto(0,0);
  107. }
  108.  
  109. /*
  110.  * Clear the screen, forgetting the current contents in the process.
  111.  */
  112. void
  113. scr_clear(void)
  114. {
  115.  
  116.     resume_normal();
  117.     async_msg(con_phone, CONSOLE_CLEAR, 0);
  118.     curscore = -1;
  119.     memset((char *)curscreen, 0, sizeof(curscreen));
  120. }
  121.  
  122. /*
  123.  * Set up screen
  124.  */
  125. void
  126. scr_init(void)
  127. {
  128.     con_phone = get_fd_phone(1);
  129.     async_msg(con_phone, CONSOLE_CURSOR_VISIBILITY, 0);
  130.     resume_normal();
  131.     scr_clear();
  132. }
  133.  
  134. void moveto(int r, int c)
  135. {
  136.     async_msg_2(con_phone, CONSOLE_GOTO, r, c);
  137. }
  138.  
  139. static void fflush(void)
  140. {
  141.     async_msg(con_phone, CONSOLE_FLUSH, 0);
  142. }
  143.  
  144. winsize_t winsize;
  145.  
  146. static int get_display_size(winsize_t *ws)
  147. {
  148.     return async_req_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
  149. }
  150.  
  151. static void
  152. scr_stop(int sig)
  153. {
  154.  
  155.     scr_end();
  156.     scr_set();
  157.     scr_msg(key_msg, 1);
  158. }
  159.  
  160. /*
  161.  * Set up screen mode.
  162.  */
  163. void
  164. scr_set(void)
  165. {
  166.     winsize_t ws;
  167.  
  168.     Rows = 0, Cols = 0;
  169.     if (get_display_size(&ws) == 0) {
  170.         Rows = ws.ws_row;
  171.         Cols = ws.ws_col;
  172.     }
  173.     if (Rows < MINROWS || Cols < MINCOLS) {
  174.         char smallscr[55];
  175.  
  176.         snprintf(smallscr, sizeof(smallscr),
  177.             "the screen is too small (must be at least %dx%d)",
  178.             MINROWS, MINCOLS);
  179.         stop(smallscr);
  180.     }
  181.     isset = 1;
  182.  
  183.     scr_clear();
  184. }
  185.  
  186. /*
  187.  * End screen mode.
  188.  */
  189. void
  190. scr_end(void)
  191. {
  192. }
  193.  
  194. void
  195. stop(char *why)
  196. {
  197.  
  198.     if (isset)
  199.         scr_end();
  200.     errx(1, "aborting: %s", why);
  201. }
  202.  
  203.  
  204. /*
  205.  * Update the screen.
  206.  */
  207. void
  208. scr_update(void)
  209. {
  210.     cell *bp, *sp;
  211.     cell so, cur_so = 0;
  212.     int i, ccol, j;
  213.     static const struct shape *lastshape;
  214.  
  215.     /* always leave cursor after last displayed point */
  216.     curscreen[D_LAST * B_COLS - 1] = -1;
  217.  
  218.     if (score != curscore) {
  219.         moveto(0, 0);
  220.         printf("Score: %d", score);
  221.         curscore = score;
  222.     }
  223.  
  224.     /* draw preview of next pattern */
  225.     if (showpreview && (nextshape != lastshape)) {
  226.         int i;
  227.         static int r=5, c=2;
  228.         int tr, tc, t;
  229.  
  230.         lastshape = nextshape;
  231.  
  232.         /* clean */
  233.         resume_normal();
  234.         moveto(r-1, c-1); putstr("          ");
  235.         moveto(r,   c-1); putstr("          ");
  236.         moveto(r+1, c-1); putstr("          ");
  237.         moveto(r+2, c-1); putstr("          ");
  238.  
  239.         moveto(r-3, c-2);
  240.         putstr("Next shape:");
  241.  
  242.         /* draw */
  243.         start_standout();
  244.         moveto(r, 2 * c);
  245.         putstr("  ");
  246.         for (i = 0; i < 3; i++) {
  247.             t = c + r * B_COLS;
  248.             t += nextshape->off[i];
  249.  
  250.             tr = t / B_COLS;
  251.             tc = t % B_COLS;
  252.  
  253.             moveto(tr, 2*tc);
  254.             putstr("  ");
  255.         }
  256.         resume_normal();
  257.     }
  258.  
  259.     bp = &board[D_FIRST * B_COLS];
  260.     sp = &curscreen[D_FIRST * B_COLS];
  261.     for (j = D_FIRST; j < D_LAST; j++) {
  262.         ccol = -1;
  263.         for (i = 0; i < B_COLS; bp++, sp++, i++) {
  264.             if (*sp == (so = *bp))
  265.                 continue;
  266.             *sp = so;
  267.             if (i != ccol) {
  268.                 if (cur_so) {
  269.                     resume_normal();
  270.                     cur_so = 0;
  271.                 }
  272.                 moveto(RTOD(j), CTOD(i));
  273.             }
  274.             if (so != cur_so) {
  275.                 if (so)
  276.                     start_standout();
  277.                 else
  278.                     resume_normal();
  279.                 cur_so = so;
  280.             }
  281.             putstr("  ");
  282.  
  283.             ccol = i + 1;
  284.             /*
  285.              * Look ahead a bit, to avoid extra motion if
  286.              * we will be redrawing the cell after the next.
  287.              * Motion probably takes four or more characters,
  288.              * so we save even if we rewrite two cells
  289.              * `unnecessarily'.  Skip it all, though, if
  290.              * the next cell is a different color.
  291.              */
  292. #define STOP (B_COLS - 3)
  293.             if (i > STOP || sp[1] != bp[1] || so != bp[1])
  294.                 continue;
  295.             if (sp[2] != bp[2])
  296.                 sp[1] = -1;
  297.             else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
  298.                 sp[2] = -1;
  299.                 sp[1] = -1;
  300.             }
  301.         }
  302.     }
  303.     if (cur_so)
  304.         resume_normal();
  305.     fflush();
  306. }
  307.  
  308. /*
  309.  * Write a message (set!=0), or clear the same message (set==0).
  310.  * (We need its length in case we have to overwrite with blanks.)
  311.  */
  312. void
  313. scr_msg(char *s, int set)
  314. {
  315.    
  316.     int l = strlen(s);
  317.    
  318.     moveto(Rows - 2, ((Cols - l) >> 1) - 1);
  319.     if (set)
  320.         putstr(s);
  321.     else
  322.         while (--l >= 0)
  323.             (void) putchar(' ');
  324. }
  325.  
  326. /** @}
  327.  */
  328.  
  329.