netris version 0.4
[netris.git] / curses.c
index 2d9390064446c9b8244e2627cc7e4a2d413d7a11..047efa4bc496de98ca707516db7cc60d53fb8c07 100644 (file)
--- a/curses.c
+++ b/curses.c
@@ -1,6 +1,6 @@
 /*
- * Netris -- A free networked version of Tetris
- * Copyright (C) 1994,1995  Mark Weaver <Mark_Weaver@brown.edu>
+ * Netris -- A free networked version of T*tris
+ * Copyright (C) 1994,1995,1996  Mark H. Weaver <mhw@netris.org>
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -16,7 +16,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- * $Id: curses.c,v 1.29 1995/07/11 08:53:21 mhw Exp $
+ * $Id: curses.c,v 1.32 1996/02/09 08:47:25 mhw Exp $
  */
 
 #include "netris.h"
@@ -35,22 +35,34 @@ static EventGenRec keyGen =
 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
 static int statusYPos, statusXPos;
 
+static char *term_vi;  /* String to make cursor invisible */
+static char *term_ve;  /* String to make cursor visible */
+
 ExtFunc void InitScreens(void)
 {
        MySigSet oldMask;
 
+       GetTermcapInfo();
+
+       /*
+        * Do this atomically.  Otherwise a badly timed Ctrl-C during
+        * initialization will leave your terminal in a bad state.
+        */
        BlockSignals(&oldMask, SIGINT, 0);
        initscr();
        AtExit(CleanupScreens);
        RestoreSignals(NULL, &oldMask);
+
        cbreak();
        noecho();
+       OutputTermStr(term_vi, 0);
        AddEventGen(&keyGen);
+
        move(0, 0);
        addstr("Netris ");
        addstr(version_string);
-       addstr(" (C) 1994,1995  Mark Weaver         "
-               "\"netris -h\" for more info");
+       addstr(" (C) 1994,1995,1996  Mark H. Weaver     "
+                       "\"netris -h\" for more info");
        statusYPos = 22;
        statusXPos = 0;
 }
@@ -59,6 +71,72 @@ ExtFunc void CleanupScreens(void)
 {
        RemoveEventGen(&keyGen);
        endwin();
+       OutputTermStr(term_ve, 1);
+}
+
+ExtFunc void GetTermcapInfo(void)
+{
+       char *term, *buf, *data;
+       int bufSize = 10240;
+
+       if (!(term = getenv("TERM")))
+               return;
+       if (tgetent(scratch, term) == 1) {
+               /*
+                * Make the buffer HUGE, since tgetstr is unsafe.
+                * Allocate it on the heap too.
+                */
+               data = buf = malloc(bufSize);
+
+               /*
+                * There is no standard include file for tgetstr, no prototype
+                * definitions.  I like casting better than using my own prototypes
+                * because if I guess the prototype, I might be wrong, especially
+                * with regards to "const".
+                */
+               term_vi = (char *)tgetstr("vi", &data);
+               term_ve = (char *)tgetstr("ve", &data);
+
+               /* Okay, so I'm paranoid; I just don't like unsafe routines */
+               if (data > buf + bufSize)
+                       fatal("tgetstr overflow, you must have a very sick termcap");
+
+               /* Trim off the unused portion of buffer */
+               buf = realloc(buf, data - buf);
+       }
+
+       /*
+        * If that fails, use hardcoded vt220 codes.
+        * They don't seem to do anything bad on vt100's, so
+        * we'll try them just in case they work.
+        */
+       if (!term_vi || !term_ve) {
+               static char *vts[] = {
+                               "vt100", "vt101", "vt102",
+                               "vt200", "vt220", "vt300",
+                               "vt320", "vt400", "vt420",
+                               "screen", "xterm", NULL };
+               int i;
+
+               for (i = 0; vts[i]; i++)
+                       if (!strcmp(term, vts[i]))
+                       {
+                               term_vi = "\033[?25l";
+                               term_ve = "\033[?25h";
+                               break;
+                       }
+       }
+       if (!term_vi || !term_ve)
+               term_vi = term_ve = NULL;
+}
+
+ExtFunc void OutputTermStr(char *str, int flush)
+{
+       if (str) {
+               fputs(str, stdout);
+               if (flush)
+                       fflush(stdout);
+       }
 }
 
 ExtFunc void InitScreen(int scr)
@@ -133,10 +211,10 @@ ExtFunc void PlotUnderline(int scr, int x, int flag)
 ExtFunc void ShowDisplayInfo(void)
 {
        move(statusYPos - 9, statusXPos);
-       printw("Seed = %d", initSeed);
+       printw("Seed: %d", initSeed);
        clrtoeol();
        move(statusYPos - 8, statusXPos);
-       printw("Speed = %dms", speed / 1000);
+       printw("Speed: %dms", speed / 1000);
        clrtoeol();
        if (robotEnable) {
                move(statusYPos - 6, statusXPos);
@@ -207,6 +285,11 @@ ExtFunc void RefreshScreen(void)
        refresh();
 }
 
+ExtFunc void ScheduleFullRedraw(void)
+{
+       touchwin(stdscr);
+}
+
 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
 {
        if (MyRead(gen->fd, &event->u.key, 1))
@@ -215,3 +298,7 @@ static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
                return E_none;
 }
 
+/*
+ * vi: ts=4 ai
+ * vim: noai si
+ */