home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / a2 / Source / curses.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  6.0 KB  |  281 lines

  1. /*
  2.  *  a2, an Apple II emulator in C
  3.  *  (c) Copyright 1990 by Rich Skrenta
  4.  *
  5.  *  Command line interface written by Tom Markson
  6.  *
  7.  *  Distribution agreement:
  8.  *
  9.  *    You may freely copy or redistribute this software, so long
  10.  *    as there is no profit made from its use, sale, trade or
  11.  *    reproduction.  You may not change this copyright notice,
  12.  *    and it must be included prominently in any copy made.
  13.  *
  14.  *  Send emulator related mail to:  skrenta@blekko.commodore.com
  15.  *                    skrenta@blekko.uucp
  16.  */
  17.  
  18. /*
  19. **  This a screen management library borrowed with permission from the
  20. **  Elm mail system (a great mailer--I highly recommend it!).
  21. **
  22. **  I've hacked this library to only provide what I need.
  23. **
  24. **  Original copyright follows:
  25. */
  26. /*******************************************************************************
  27.  *  The Elm Mail System  -  $Revision: 2.1 $   $State: Exp $
  28.  *
  29.  *             Copyright (c) 1986 Dave Taylor
  30.  ******************************************************************************/
  31.  
  32. #import <stdio.h>
  33. #import <libc.h>
  34. #import <c.h>
  35. #import <strings.h>
  36. #import <ctype.h>
  37.  
  38. #define DEFAULT_term_lines    24
  39. #define DEFAULT_COLUMNS    80
  40. #define TTYIN    0
  41.  
  42.  
  43. #define        VERY_LONG_STRING    2500
  44.  
  45. int term_lines = DEFAULT_term_lines - 1;
  46. int term_cols  = DEFAULT_COLUMNS;
  47. extern char escape_char;
  48.  
  49. #ifdef SHORTNAMES
  50. # define _cleartoeoln    _clrtoeoln
  51. # define _cleartoeos    _clr2eos
  52. #endif
  53.  
  54. #define TCGETA    TIOCGETP
  55. #define TCSETAW    TIOCSETP
  56.  
  57. struct sgttyb raw_tty,
  58.           orig_tty;
  59.  
  60. static int inraw = 0;                  /* are we IN rawmode?    */
  61.  
  62.  
  63. #ifdef STUFFY_TURTLE
  64. static int _memory_locked = 0;        /* are we IN memlock??   */
  65.  
  66. static int _intransmit;            /* are we transmitting keys? */
  67. #endif
  68.  
  69. static
  70. char *_clearscreen, *_moveto, *_cleartoeoln, *_cleartoeos,
  71.     *_setinverse, *_clearinverse;
  72.  
  73.  
  74. static
  75. int _lines,_columns;
  76.  
  77. static char _terminal[1024];              /* Storage for terminal entry */
  78. static char _capabilities[1024];           /* String for cursor motion */
  79.  
  80. static char *ptr = _capabilities;    /* for buffering         */
  81.  
  82. void tputs(register char *cp, int affcnt, int (*outc)(char));
  83. char  *tgetstr(),                    /* Get termcap capability */
  84.       *tgoto();                /* and the goto stuff    */
  85. int tgetnum();                    /* Get termcap capability */
  86. int  tgetent();                /* get termcap entry */
  87.  
  88. int myoutchar(char c)
  89. {
  90.   /** output the given character.  From tputs... **/
  91.   /** Note: this CANNOT be a macro!              **/
  92.  
  93.   return putc(c, stdout);
  94. }
  95.  
  96. int InitScreen(void)
  97. {
  98. int     err;
  99. char termname[40];
  100.     
  101.     if (getenv("TERM") == NULL) {
  102.         fprintf(stderr,
  103.           "TERM variable not set; Screen capabilities required\n");
  104.         return(FALSE);
  105.     }
  106.     if (strcpy(termname, getenv("TERM")) == NULL) {
  107.         fprintf(stderr,"Can't get TERM variable\n");
  108.         return(FALSE);
  109.     }
  110.     if ((err = tgetent(_terminal, termname)) != 1) {
  111.         fprintf(stderr,"Can't get entry for TERM\n");
  112.         return(FALSE);
  113.     }
  114.  
  115.     /* load in all those pesky values */
  116.     _clearscreen       = tgetstr("cl", &ptr);
  117.     _moveto            = tgetstr("cm", &ptr);
  118.     _cleartoeoln       = tgetstr("ce", &ptr);
  119.     _cleartoeos        = tgetstr("cd", &ptr);
  120.     _setinverse        = tgetstr("so", &ptr);
  121.     _clearinverse      = tgetstr("se", &ptr);
  122.     _lines                 = tgetnum("li");
  123.     _columns       = tgetnum("co");
  124.  
  125.     if (!_clearscreen) {
  126.         fprintf(stderr,
  127.             "Terminal must have clearscreen (cl) capability\n");
  128.         return(FALSE);
  129.     }
  130.     if (!_moveto) {
  131.         fprintf(stderr,
  132.             "Terminal must have cursor motion (cm)\n");
  133.         return(FALSE);
  134.     }
  135.     if (!_cleartoeoln) {
  136.         fprintf(stderr,
  137.             "Terminal must have clear to end-of-line (ce)\n");
  138.         return(FALSE);
  139.     }
  140.     if (!_cleartoeos) {
  141.         fprintf(stderr,
  142.             "Terminal must have clear to end-of-screen (cd)\n");
  143.         return(FALSE);
  144.     }
  145.     if (_lines == -1)
  146.         _lines = DEFAULT_term_lines;
  147.     if (_columns == -1)
  148.         _columns = DEFAULT_COLUMNS;
  149.     return(TRUE);
  150. }
  151.  
  152. void ScreenSize(int *lines, int *columns)
  153. {
  154.     /** returns the number of lines and columns on the display. **/
  155.  
  156.     if (_lines == 0) _lines = DEFAULT_term_lines;
  157.     if (_columns == 0) _columns = DEFAULT_COLUMNS;
  158.  
  159.     *lines = _lines - 1;        /* assume index from zero*/
  160.     *columns = _columns;        /* assume index from one */
  161. }
  162.  
  163. void ClearScreen(void)
  164. {
  165.     /* clear the screen: returns -1 if not capable */
  166.  
  167.     tputs(_clearscreen, 1, myoutchar);
  168.     fflush(stdout);      /* clear the output buffer */
  169. }
  170.  
  171. void MoveCursor(int row, int col)
  172. {
  173.     /** move cursor to the specified row column on the screen.
  174.             0,0 is the top left! **/
  175.  
  176.     char *stuff, *tgoto();
  177.  
  178.     stuff = tgoto(_moveto, col, row);
  179.     tputs(stuff, 1, myoutchar);
  180. /*    fflush(stdout);    */
  181. }
  182.  
  183. void CleartoEOLN(void)
  184. {
  185.     /** clear to end of line **/
  186.  
  187.     tputs(_cleartoeoln, 1, myoutchar);
  188.     fflush(stdout);  /* clear the output buffer */
  189. }
  190.  
  191. void CleartoEOS(void)
  192. {
  193.     /** clear to end of screen **/
  194.  
  195.     tputs(_cleartoeos, 1, myoutchar);
  196.     fflush(stdout);  /* clear the output buffer */
  197. }
  198.  
  199. void Raw(int state)
  200. {
  201.     /** state is either TRUE or FALSE, as indicated by call **/
  202.  
  203.     if (state == FALSE && inraw) {
  204.       (void) ioctl(TTYIN, TCSETAW, &orig_tty);
  205.       inraw = 0;
  206.     }
  207.     else if (state == TRUE && ! inraw) {
  208.  
  209.       (void) ioctl(TTYIN, TCGETA, &orig_tty);    /** current setting **/
  210.  
  211.       (void) ioctl(TTYIN, TCGETA, &raw_tty);    /** again! **/
  212. #ifdef STUFFY_TURTLE
  213.       raw_tty.sg_flags &= ~(ECHO | CRMOD);    /* echo off */
  214. #endif
  215.       raw_tty.sg_flags &= ~ECHO;        /* echo off */
  216.       raw_tty.sg_flags |= CBREAK;    /* raw on    */
  217.  
  218. #ifndef NeXT
  219.       raw_tty.c_cc[VINTR]= escape_char;
  220. #endif
  221.       (void) ioctl(TTYIN, TCSETAW, &raw_tty);
  222.       inraw = 1;
  223.     }
  224. }
  225.  
  226. int
  227. ReadCh(void)
  228. {
  229.     /** read a character with Raw mode set! **/
  230.  
  231.     register int result;
  232.     char ch;
  233.     result = read(0, &ch, 1);
  234.         return((result <= 0 ) ? EOF : ch);
  235. }
  236.  
  237.  
  238. #if 0
  239. static int inverse = FALSE;
  240.  
  241. SetInverse() {
  242.  
  243.     if (!inverse) {
  244.         StartInverse();
  245.         inverse = TRUE;
  246.     }
  247. }
  248.  
  249. SetNormal() {
  250.  
  251.     if (inverse) {
  252.         EndInverse();
  253.         inverse = FALSE;
  254.     }
  255. }
  256. #endif
  257.  
  258.  
  259. void StartInverse(void)
  260. {
  261.     /** set inverse video mode **/
  262.  
  263.     if (!_setinverse)
  264.         return;
  265.  
  266.     tputs(_setinverse, 1, myoutchar);
  267. /*    fflush(stdout);    */
  268. }
  269.  
  270.  
  271. void EndInverse(void)
  272. {
  273.     /** compliment of startinverse **/
  274.  
  275.     if (!_clearinverse)
  276.         return;
  277.  
  278.     tputs(_clearinverse, 1, myoutchar);
  279. /*    fflush(stdout);    */
  280. }
  281.