home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / pine / pico / ibmpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  8.9 KB  |  408 lines

  1. /*
  2.  *
  3.  * $Id: ibmpc.c,v 4.4 1993/08/20 00:14:53 mikes Exp $
  4.  *
  5.  * Program:    IBM PC specific routine
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  * Copyright 1991-1993  University of Washington
  19.  *
  20.  *  Permission to use, copy, modify, and distribute this software and its
  21.  * documentation for any purpose and without fee to the University of
  22.  * Washington is hereby granted, provided that the above copyright notice
  23.  * appears in all copies and that both the above copyright notice and this
  24.  * permission notice appear in supporting documentation, and that the name
  25.  * of the University of Washington not be used in advertising or publicity
  26.  * pertaining to distribution of the software without specific, written
  27.  * prior permission.  This software is made available "as is", and
  28.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  29.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  30.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  31.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  32.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  33.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  34.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  35.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  36.  *
  37.  * Pine and Pico are trademarks of the University of Washington.
  38.  * No commercial use of these trademarks may be made without prior
  39.  * written permission of the University of Washington.
  40.  *
  41.  */
  42.  
  43. /*
  44.  * The routines in this file provide support for the IBM-PC and other
  45.  * compatible terminals. It goes directly to the graphics RAM to do
  46.  * screen output. It compiles into nothing if not an IBM-PC driver
  47.  */
  48.  
  49. #include        <stdio.h>
  50. #include    <conio.h>
  51. #include    <time.h>
  52. #include    "osdep.h"
  53. #if     IBMPC
  54. #define    termdef    1            /* don't define "term" external */
  55. #include    "pico.h"
  56. #include    "estruct.h"
  57. #include        "efunc.h"
  58. #include        "edef.h"
  59.  
  60. #ifdef    ANSI
  61.     int  ibmmove(int, int);
  62.     int  ibmeeol(void);
  63.     int  ibmputc(int);
  64.     int  ibmoutc(char);
  65.     int  ibmeeop(void);
  66.     int  ibmrev(int);
  67.     void beep(unsigned int, unsigned int);
  68.     int  cutebeep(void);
  69.     int  ibmbeep(void);
  70.     int  ibmopen(void);
  71.     int  ibmclose(void);
  72. #if    COLOR
  73.     int     ibmfcol(int);
  74.     int     ibmbcol(int);
  75. #endif    /* COLOR */
  76. #else
  77.     int  ibmmove();
  78.     int  ibmeeol();
  79.     int  ibmputc();
  80.     int  ibmoutc();
  81.     int  ibmeeop();
  82.     int  ibmrev();
  83.     void beep();
  84.     int  cutebeep();
  85.     int  ibmbeep();
  86.     int  ibmopen();
  87.     int  ibmclose();
  88. #if    COLOR
  89.     int     ibmfcol();
  90.     int     ibmbcol();
  91. #endif    /* COLOR */
  92. #endif
  93.  
  94.  
  95. #define NROW    25              /* Screen size.                 */
  96. #define NCOL    80              /* Edit if you want to.         */
  97. #define    MARGIN    8        /* size of minimim margin and    */
  98. #define    SCRSIZ    64        /* scroll size for extended lines */
  99. #define    NPAUSE    200        /* # times thru update to pause */
  100. #define BEL     0x07            /* BEL character.               */
  101. #define ESC     0x1B            /* ESC character.               */
  102. #define    SPACE    32        /* space character        */
  103.  
  104.  
  105. int *scptr[NROW];        /* pointer to screen lines    */
  106. int sline[NCOL];        /* screen line image        */
  107.  
  108. unsigned    cattr    = 0x07;    /* gray by default */
  109.  
  110. static unsigned display_mode;    /*  */
  111.  
  112. #if    COLOR
  113. int    cfcolor = -1;        /* current forground color */
  114. int    cbcolor = -1;        /* current background color */
  115. int    ctrans[] =        /* ansi to ibm color translation table */
  116.     {0, 4, 2, 6, 1, 5, 3, 7};
  117. #endif
  118.  
  119. /*
  120.  * Standard terminal interface dispatch table. Most of the fields point into
  121.  * "termio" code.
  122.  */
  123. TERM    term    = {
  124.         NROW-1,
  125.         NCOL,
  126.     MARGIN,
  127.     SCRSIZ,
  128.         ibmopen,
  129.         ibmclose,
  130.         ttgetc,
  131.     ibmputc,
  132.         ttflush,
  133.         ibmmove,
  134.         ibmeeol,
  135.         ibmeeop,
  136.         ibmbeep,
  137.     ibmrev
  138. #if    COLOR
  139.     , ibmfcol,
  140.     ibmbcol
  141. #endif
  142. };
  143.  
  144.  
  145. extern union REGS rg;
  146.  
  147.  
  148. #if    COLOR
  149. ibmfcol(color)        /* set the current output color */
  150. int color;    /* color to set */
  151. {
  152.     cfcolor = ctrans[color];
  153. }
  154.  
  155.  
  156. ibmbcol(color)        /* set the current background color */
  157. int color;    /* color to set */
  158. {
  159.     cbcolor = ctrans[color];
  160. }
  161. #endif    /* COLOR */
  162.  
  163.  
  164. /*
  165.  * ibmmove - Use BIOS video services, function 2h to set cursor postion
  166.  */
  167. ibmmove(row, col)
  168. int row, col;
  169. {
  170.     rg.h.ah = 2;        /* set cursor position function code */
  171.     rg.h.bh = 0;        /* set screen page number */
  172.     rg.h.dl = col;
  173.     rg.h.dh = row;
  174.     int86(BIOS_VIDEO, &rg, &rg);
  175. }
  176.  
  177.  
  178. /*
  179.  * ibmeeol - erase to the end of the line
  180.  */
  181. ibmeeol()
  182. {
  183.     int col, row, page;
  184.  
  185.     /* find the current cursor position */
  186.     rg.h.ah = 3;        /* read cursor position function code */
  187.     int86(BIOS_VIDEO, &rg, &rg);
  188.     page = rg.h.bh;
  189.     col = rg.h.dl;        /* record current column */
  190.     row = rg.h.dh;        /* and row */
  191.  
  192.     rg.h.ah = 0x09;        /* write char to screen with new attrs */
  193.     rg.h.al = ' ';
  194.     rg.h.bl = cattr;
  195.     rg.h.bh = page;
  196.     rg.x.cx = NCOL-col;
  197.     int86(BIOS_VIDEO, &rg, &rg);
  198. }
  199.  
  200.  
  201. /*
  202.  * ibmputc - put a character at the current position in the
  203.  *         current colors
  204.  */
  205. ibmputc(ch)
  206. int ch;
  207. {
  208.     int col, row, page;
  209.  
  210.     rg.h.ah = 0x03;            /* first, get current position */
  211.     int86(BIOS_VIDEO, &rg, &rg);
  212.     page = rg.h.bh;
  213.     row = rg.h.dh;
  214.     col = rg.h.dl;
  215.     
  216.     if(ch == '\b'){
  217.     if(col > 0)        /* advance the cursor */
  218.       ibmmove(row, --col);
  219.     }
  220.     else{
  221.     rg.h.ah = 0x09;        /* write char to screen with new attrs */
  222.     rg.h.al = ch;
  223.     rg.h.bl = cattr;        /* inverting if needed */
  224.     rg.h.bh = page;
  225.     rg.x.cx = 1;        /* only once */
  226.     int86(BIOS_VIDEO, &rg, &rg);
  227.  
  228.     if(col < 80)        /* advance the cursor */
  229.       ibmmove(row, ++col);
  230.     }
  231. }
  232.  
  233.  
  234. /* 
  235.  * ibmoutc - output a single character with the right attributes, but
  236.  *           don't advance the cursor
  237.  */
  238. ibmoutc(c)
  239. char c;
  240. {
  241.     rg.h.ah = 0x09;        /* write char to screen with new attrs */
  242.     rg.h.al = c;
  243.     rg.h.bl = cattr;    /* inverting if needed */
  244.     rg.h.bh = 0;
  245.     rg.x.cx = 1;        /* only once */
  246.     int86(BIOS_VIDEO, &rg, &rg);
  247. }
  248.  
  249.  
  250. /*
  251.  * ibmeeop - clear from cursor to end of page
  252.  */
  253. ibmeeop()
  254. {
  255.     int attr;            /* attribute to fill screen with */
  256.  
  257.     rg.h.ah = 6;        /* scroll page up function code */
  258.     rg.h.al = 0;        /* # lines to scroll (clear it) */
  259.     rg.x.cx = 0;        /* upper left corner of scroll */
  260.     rg.x.dx = (term.t_nrow << 8) | (term.t_ncol - 1);
  261.     attr    = cattr;
  262.     rg.h.bh = attr;
  263.     int86(BIOS_VIDEO, &rg, &rg);
  264.  
  265.     ibmmove(0, 0);
  266. }
  267.  
  268.  
  269. /*
  270.  * ibmrev - change reverse video state
  271.  */
  272. ibmrev(state)
  273. int state;
  274. {
  275.     cattr = (state) ? 0x70 : 0x07;
  276. }
  277.  
  278.  
  279. /*
  280.  * getrevstate - return the current reverse state
  281.  */
  282. getrevstate()
  283. {
  284.     return(cattr == 0x70);
  285. }
  286.  
  287.  
  288. /* 
  289.  * beep - make the speaker sing!
  290.  */
  291. void
  292. beep(freq, dur)
  293. unsigned freq, dur;
  294. {
  295.     unsigned oport;
  296.  
  297.     if(!freq)
  298.     return;
  299.  
  300.     freq = (unsigned)(1193180 / freq);
  301.     /* set up the timer */
  302.     outp(0x43, 0xb6);            /* set timer channel 2 registers */
  303.     outp(0x42, (0xff&freq));        /* low order byte of count */
  304.     outp(0x42, (freq>>8));        /* hi order byte of count */
  305.  
  306.     /* make the sound */
  307.     oport = inp(0x61);
  308.     outp(0x61, oport | 0x03);
  309.     ssleep((clock_t)((dur < 75) ? 75 : dur));
  310.     outp(0x61, oport);
  311. }
  312.  
  313.  
  314. /*
  315.  * cutebeep - make the speeker sing the way we want!
  316.  */
  317. cutebeep()
  318. {
  319.     beep(575, 50);
  320.     ssleep((clock_t)25);
  321.     beep(485, 90);
  322. }
  323.  
  324.  
  325. /*
  326.  * ibmbeep - system beep...
  327.  */
  328. ibmbeep()
  329. {
  330.     cutebeep();
  331. }
  332.  
  333.  
  334. /*
  335.  * enter_text_mode - get current video mode, saving to be restored 
  336.  *                   later, then explicitly set 80 col text mode.
  337.  *
  338.  *     NOTE: this gets kind of weird.  Both pine and pico call this
  339.  *           during initialization.  To make sure it's only invoked once
  340.  *           it only responds if passed NULL which pico only does if not
  341.  *           called from in pine, and pine does all the time.  make sense?
  342.  *           thought not.
  343.  */
  344. void
  345. enter_text_mode(p)
  346. PICO *p;
  347. {
  348.     static int i = 0;
  349.  
  350.     if(!p && !i++){
  351.     rg.h.ah = 0x0f;            /* save old mode */
  352.     int86(BIOS_VIDEO, &rg, &rg);
  353.     display_mode = rg.h.al;
  354.  
  355.     rg.h.ah = 0;            /* then set text mode */
  356.     rg.h.al = 2;
  357.     int86(BIOS_VIDEO, &rg, &rg);        /* video services */
  358.     }
  359. }
  360.  
  361.  
  362. /*
  363.  * exit_text_mode - leave text mode by restoring saved original 
  364.  *                  video mode.
  365.  */
  366. void
  367. exit_text_mode(p)
  368. PICO *p;
  369. {
  370.     static int i = 0;
  371.  
  372.     if(!p && !i++){            /* called many, invoke once! */
  373.     rg.h.ah = 0;            /* just restore old mode */
  374.     rg.h.al = display_mode;
  375.     int86(BIOS_VIDEO, &rg, &rg);
  376.     }
  377. }
  378.  
  379.  
  380. /*
  381.  * ibmopen - setup text mode and setup key labels...
  382.  */
  383. ibmopen()
  384. {
  385.  
  386.     enter_text_mode(Pmaster);
  387.  
  388.     revexist = TRUE;
  389.     inschar = delchar = 0;
  390.     HelpKeyNames = (gmode&MDFKEY) ? funckeynames : NULL;
  391.  
  392.     ttopen();
  393. }
  394.  
  395.  
  396. ibmclose()
  397. {
  398. #if    COLOR
  399.     ibmfcol(7);
  400.     ibmbcol(0);
  401. #endif
  402.  
  403.     exit_text_mode(Pmaster);
  404.  
  405.     ttclose();
  406. }
  407. #endif
  408.