Subversion Repositories HelenOS

Rev

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