home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 235_02 / ovtty.c < prev    next >
Text File  |  1987-06-17  |  14KB  |  377 lines

  1. /*  045  26-Feb-87  ovtty.c
  2.  
  3.         Copyright (c) 1986 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include <dos.h>
  7. #include "ov.h"
  8.  
  9. #ifndef NULL
  10. #define NULL (0)
  11. #endif
  12.  
  13. static int vmode, vwidth, vpage;          /* initial video mode, width, page */
  14. static int crow, ccol, cstscan, cendscan; /* initial cursor stuff */
  15. static int save_row, save_col, save_stscan, save_endscan; /* save cursor */
  16.  
  17. char far *screen = NULL;               /* address of screen image */
  18. char far *cursor = NULL;               /* cursor location on screen */
  19. static char far *save_cursor;               /* loc to save cursor value */
  20. static int imageidx = 0;                    /* index of next image[] to use */
  21. static char far *image[2] = { NULL, NULL }; /* address of saved screen image */
  22. static char far *savcursor[2];              /* saved cursor location */
  23. char far *malloc_f();
  24.  
  25. unsigned char vid_attrib;              /* current video attribute to use */
  26. unsigned char vid_mode;                /* video mode in use */
  27. unsigned char attribs[7];              /* video attrib's in use */
  28.  
  29. /****************************************************************************
  30.  Following is a patch area used, updated by ovdef.c.  Do not change anything
  31.  in this area without also checking ovdef.c
  32.  ****************************************************************************/
  33.  
  34. char patchstr[] = "VIDEO HERE";        /* identify vid_snow in .EXE file */
  35. unsigned char vid_snow = 1;            /* NZ if must chk for video snow */
  36.  
  37. /* DIS_NORM, DIS_HIGH, DIS_BOX, DIS_HIBOX, DIS_HEAD, DIS_TEXT, DIS_TAGD */
  38.  
  39. unsigned char monattr[] = { 0x07, 0x70, 0x0F, 0x70, 0x70, 0x07, 0x0F };/*mono*/
  40. unsigned char cgaattr[] = { 0x0B, 0x30, 0x0E, 0x30, 0x1B, 0x0A, 0x0E };/* cga*/
  41.  
  42. /*****************************************************************************
  43.                             End of patch area
  44.  *****************************************************************************/
  45.  
  46.  
  47. /******************************************************************************
  48.  **                 I N I T  /  R E S E T _ T T Y                            **
  49.  *****************************************************************************/
  50.  
  51. init_tty() {           /* initialize the display (terminal?) */
  52.  
  53.    vmode = getvideomode(&vwidth,&vpage);   /* save current video state */
  54.  
  55.    /* initialize the display adapter, currently only modes 3 and 7
  56.       are supported.  Set the mode (which also clears the screen),
  57.       and if a CGA, make sure display page 0 is active */
  58.  
  59.    if (vmode < 7) {                            /* initialize a CGA */
  60.       setvideomode(vid_mode = 3);              /* color, 80x25 text */
  61.       setdisplaypage(0);                       /* display page 0 */
  62.       strncpy(attribs,cgaattr,sizeof(cgaattr));/* set up cga display attr's */
  63.  
  64.       FP_SEG(screen) = 0xB800;                 /* set screen base address */
  65.  
  66.    } else {                                    /* initialize a MONO adapter */
  67.       setvideomode(vid_mode = 7);              /* clears the screen */
  68.       strncpy(attribs,monattr,sizeof(monattr));/* set up mono display attr's */
  69.       vid_snow = 0;                            /* no snow on mono */
  70.       FP_SEG(screen) = 0xB000;                 /* set screen base address */
  71.    }
  72.  
  73.    setvattrib(DIS_NORM);          /* set default video attribute */
  74.    cursor = screen;               /* cursor is at top of screen */
  75.  
  76.    /* save data about the cursor position and size, we're going
  77.       to turn off the cursor and we want to be able to restore
  78.       it later */
  79.  
  80.    readcursor(vpage,&crow,&ccol,&cstscan,&cendscan);  /* save info */
  81.  
  82.    setcursorsize(32,0);           /* turn off hardware cursor */
  83. }
  84.  
  85. reinit_tty() {         /* reinitialize tty from unknown state */
  86.  
  87.    int mode, dummy;
  88.  
  89.    mode = getvideomode(&dummy,&dummy); /* get current video mode */
  90.    if (mode != vid_mode)               /* reset video mode if need be */
  91.       setvideomode(vid_mode);
  92.    if (vid_mode == 3)                  /* set display page 0 if cga */
  93.       setdisplaypage(0);
  94.    hidecursor();                       /* make sure cursor is off */
  95. }
  96.  
  97.  
  98. reset_tty() {         /* restore the display to pre-overview status */
  99.  
  100.    setvideomode(vmode);                   /* restore the video mode */
  101.  
  102.    setdisplaypage(vpage);                 /* restore display page */
  103.  
  104.    setcursorsize(cstscan,cendscan);       /* turn the curosr back on */
  105. }
  106.  
  107.  
  108. /******************************************************************************
  109.  **                      S E T V A T T R I B                                 **
  110.  *****************************************************************************/
  111.  
  112. int ALTCALL
  113. setvattrib(i)          /* set the video attribute */
  114. int i;
  115. {
  116.    vid_attrib = attribs[i];
  117. }
  118.  
  119.  
  120. /******************************************************************************
  121.  **                       D I S P _ S T R _ A T                              **
  122.  *****************************************************************************/
  123.  
  124. int ALTCALL
  125. disp_str_at(s,r,c)     /* display a string at specified row, column */
  126. char *s;
  127. int r,c;
  128. {
  129.    gotorc(r,c);        /* goto display location */
  130.    disp_str(s);        /* use our own routine */
  131. }
  132.  
  133.  
  134. /******************************************************************************
  135.  **                    D I S P _ C H A R _ A T                               **
  136.  *****************************************************************************/
  137.  
  138. int ALTCALL
  139. disp_char_at(ch,r,c)   /* display a char at specified row, column */
  140. int ch, r, c;
  141. {
  142.    gotorc(r,c);        /* goto display location */
  143.    disp_char(ch);      /* use our other routine to display it */
  144. }
  145.  
  146.  
  147. /******************************************************************************
  148.                          D I S P _ R E P _ A T
  149.  *****************************************************************************/
  150.  
  151. int ALTCALL
  152. disp_rep_at(ch,cnt,r,c)        /* display cnt ch's at specified row, column */
  153. int ch, cnt, r, c;
  154. {
  155.    gotorc(r,c);        /* goto display location */
  156.    disp_rep(ch,cnt);   /* use our other routine to display it */
  157. }
  158.  
  159.  
  160. /******************************************************************************
  161.  **                     P U T C H   /  P U T S T R                           **
  162.  *****************************************************************************/
  163.  
  164. putchr(ch) {                   /* these routines only exist because they */
  165.    putch(ch);                  /* update the hardware cursor position */
  166. }                              /* while doing output, a function that is */
  167.                                /* useful when doing direct screen i/o, but */
  168. putstr(s)                      /* some functions require the user to see */
  169. register char *s;              /* a cursor.  The simulated cursor could */
  170. {                              /* be made to handle this, but this is so */
  171.    while (*s)                  /* much easier and the usage of these */
  172.       putch(*s++);             /* functions is quite low */
  173. }
  174.  
  175. putstr_nomove(s)               /* here's a strange one, it displays a string */
  176. char *s;                       /*   without moving the hardware or software */
  177. {                              /*   cursor */
  178.    int row, col, x;
  179.    char far *savec;
  180.  
  181.    savec = cursor;                     /* save software cursor location */
  182.    readcursor(0,&row,&col,&x,&x);      /* get hardware cursor location */
  183.  
  184.    /* move software cursor to hardware location and display string */
  185.  
  186.    cursor = screen + (row * 160) + (col << 1);
  187.    disp_str(s);                               /* doesn't move hardware cursor */
  188.  
  189.    cursor = savec;             /* restore software cursor */
  190. }
  191.  
  192.  
  193. /******************************************************************************
  194.  **                          G E T C H R                                     **
  195.  *****************************************************************************/
  196.  
  197. getchr() {             /* get a character from the terminal */
  198.  
  199.    register int ch;
  200.  
  201.    static unsigned char kbdmap[] = {
  202.       /* 3B */     HELP, TAG, GOPAR, GOSUB, NEXTT, PREVT,
  203.       /* 41 */     OPENW, CLOSEW, NEXTW, PREVW, 0, 0,
  204.       /* 47 */     HOME, UP, PGUP, 0, LEFT, 0, RIGHT, 0, END,
  205.       /* 50 */     DOWN, PGDN, INS, DEL };
  206.  
  207.    /* get a char from the user with no echo.  If its a normal (not
  208.       extended char, return it as is */
  209.  
  210. readch:
  211.  
  212.    if (ch = getraw())          /* get a char from user with no echo */
  213.       return(ch);              /* return it if it's a normal character */
  214.  
  215.    /*  must be an extended char, map it to an internal code if used */
  216.  
  217.    ch = getraw();              /* get scan code of char */
  218.  
  219.    if (ch >= 0x3b && ch <= 0x53) { /* one of our mapped codes? */
  220.       ch = kbdmap[ch-0x3b];        /* yes, translate it */
  221.       if (ch == HELP) {            /* HACK is this a cry for help? HACK */
  222.          help();                   /* do the help subsystem */
  223.          goto readch;              /* then read another char from user */
  224.       }
  225.    }
  226.  
  227.    return(ch);                 /* tell caller what was read */
  228. }
  229.  
  230.  
  231. /******************************************************************************
  232.  **                          O U T _ X                                       **
  233.  *****************************************************************************/
  234.  
  235. int ALTCALL
  236. out_int(num,fsize,fillch)      /* display an int in a fixed length field */
  237. int num, fsize, fillch;
  238. {
  239.    int flen;
  240.    char numstr[11];
  241.  
  242.    itoa(num,numstr,10);
  243.    if ((flen = fsize - strlen(numstr)) > 0)
  244.       disp_rep(fillch,flen);
  245.    disp_str(numstr);
  246. }
  247.  
  248. int ALTCALL
  249. out_long(num,fsize,fillch)     /* display a long in a fixed length field */
  250. long num;
  251. int fsize, fillch;
  252. {
  253.    int flen;
  254.    char numstr[11];
  255.  
  256.    ultoa(num,numstr,10);
  257.    if ((flen = fsize - strlen(numstr)) > 0)
  258.       disp_rep(fillch,flen);
  259.    disp_str(numstr);
  260. }
  261.  
  262. int ALTCALL
  263. out_str(s,fsize,fillch)        /* disp a string (left justified) in a field */
  264. register char *s;
  265. int fsize, fillch;
  266. {
  267.    register char *ep;
  268.    int len, flen, savch;
  269.  
  270.    if ((len = strlen(s)) > fsize) {    /* string too big for field? */
  271.       savch = *(ep = s + fsize);       /* yes, fake it out by temp */
  272.       *ep = '\0';                      /*   setting it to length of fld */
  273.       disp_str(s);                     /*   display it like normal */
  274.       *ep = savch;                     /*   restore it */
  275.  
  276.    } else {                    /* strlen <= field size */
  277.  
  278.       disp_str(s);
  279.       if ((flen = fsize - len) > 0)
  280.          disp_rep(fillch,flen);
  281.    }
  282. }
  283.  
  284.  
  285. /******************************************************************************
  286.  **            L O W   L E V E L   V I D E O   C O N T R O L                 **
  287.  *****************************************************************************/
  288.  
  289. savescreen() {         /* save the current screen image */
  290.  
  291.    /* allocate a far buffer for the screen image */
  292.  
  293.    if ((image[imageidx] = malloc_f(SCREEN_ROWS*SCREEN_COLS*2)) == NULL)
  294.       show_error(0,13,1,"No memory to save screen image");
  295.  
  296.    scrcpy(image[imageidx],screen);             /* copy the screen image */
  297.    savcursor[imageidx] = cursor;               /* save the current cursor loc */
  298.    imageidx++;                                 /* "stack" the screen image */
  299. }
  300.  
  301. restorescreen() {      /* restore the saved screen image */
  302.  
  303.    imageidx--;                                 /* "pop off" a screen image */
  304.    scrcpy(screen,image[imageidx]);             /* restore the screen image */
  305.    cursor = savcursor[imageidx];               /* restore the cursor position */
  306.    free_f(image[imageidx]);                    /* release screen memory */
  307. }
  308.  
  309.  
  310. savecursor() {         /* save the current cursor state */
  311.  
  312.    save_cursor = cursor;               /* save the software cursor */
  313.  
  314.    /* save the hardware cursor status */
  315.    readcursor(0,&save_row,&save_col,&save_stscan,&save_endscan);
  316. }
  317.  
  318. restorecursor() {      /* restore cursor to saved status */
  319.  
  320.    union REGS r;
  321.  
  322.    cursor = save_cursor;               /* restore software cursor */
  323.  
  324.    /* restore hardware cursor */
  325.  
  326.    r.h.ah = 2;                 /* position cursor */
  327.    r.h.dh = save_row;
  328.    r.h.dl = save_col;
  329.    r.h.bh = 0;
  330.    int86(16,&r,&r);
  331.  
  332.    setcursorsize(save_stscan,save_endscan);    /* restore cursor size */
  333. }
  334.  
  335.  
  336. hidecursor() {         /* hide the terminal cursor */
  337.    setcursorsize(32,0);           /* turn off hardware cursor */
  338. }
  339.  
  340.  
  341. showcursor() {         /* make the terminal cursor visible */
  342.  
  343.   union REGS rg;
  344.  
  345.   rg.h.ah = 2;                       /* if direct screen i/o is being done, */
  346.   rg.h.dh = FP_OFF(cursor) / 160;    /* the hardware cursor position is not */
  347.   rg.h.dl = (FP_OFF(cursor)%160)>>1; /* updated along with the faked cursor */
  348.   rg.h.bh = 0;                       /* position, sync the hardware to the  */
  349.   int86(16, &rg, &rg);               /* simulated cursor before displaying it */
  350.  
  351.   setcursorsize(cstscan,cendscan);       /* turn the curosr back on */
  352. }
  353.  
  354.  
  355. clr_scr() {            /* clear the screen */
  356.  
  357.    gotorc(0,0);
  358.    disp_rep(' ',SCREEN_ROWS*SCREEN_COLS);
  359.    gotorc(0,0);
  360. }
  361.  
  362. int ALTCALL
  363. clr_eol() {            /* clear to the end of the current line */
  364.  
  365.    char far *cp;
  366.  
  367.    cp = cursor;                        /* start clearing at cursor loc */
  368.  
  369.    disp_rep(' ',(160 - (FP_OFF(cursor) % 160)) >> 1);
  370.  
  371.    cursor = cp;                        /* resotre cursor location */
  372. }
  373.  
  374. beep() {               /* ring the bell on the teriminal */
  375.    putchr(7);
  376. }
  377.