home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / backgmmn.zip / CIAO.C < prev    next >
Text File  |  1987-07-31  |  30KB  |  994 lines

  1. /*
  2. **   ciao.c
  3. **   sept 10, 1986, by david c. oshel, ames, iowa
  4. **
  5. **
  6. **   Console input and output for the 101% IBM PC clone.  This is the first
  7. **   module in my CIAO.LIB library.
  8. **
  9. **   These are FAST primitives to read from and write to the IBM video RAM.
  10. **   Ignores the ROM-BIOS except to set text mode and/or to read or set the 
  11. **   machine cursor (see just below).
  12. **
  13. **   The module is self-initializing.  Vid_init(n) is only required to set
  14. **   a particular mode, e.g., vid_init(3) to set 80x25 color text.  The
  15. **   requested mode is only set if the hardware supports it, and only
  16. **   monochrome mode 7 and cga modes 2 or 3 are valid.  No graphics modes.
  17. **
  18. **   Global functions which ALTER THE CONTENTS OF THE SCREEN test the 
  19. **   initialized flag.  In general, static, ROM-BIOS and cursor functions 
  20. **   do NOT test the flag.  If the flag is still zero, vid_init executes.
  21. **
  22. **   The machine cursor and the video RAM write location ("soft cursor")
  23. **   are independent, but are synchronized by default.  See setsynch fn.
  24. **
  25. **   Compiler is Microsoft C ver. 4.00, but this particular module should be 
  26. **   fairly portable to another compiler (such as Microsoft C ver. 3.00). 
  27. **
  28. */
  29.  
  30.  
  31. /*=======================================================================*
  32.      There are 16 public Video Attribute Registers:  vid[0] ... vid[15]
  33.  
  34.      The Clairol routine (CLAIROL.OBJ) recognizes four major message
  35.      levels, associated with vid[0], vid[1], vid[2], vid[3].  This is
  36.      the popout window that allows user access to the first four video
  37.      registers (only).
  38.  
  39.      Programmers have access to all 16 registers at all times, using
  40.      the CLAIROL.H header file.
  41.  
  42.      Clairol           VidReg   ^ commands that set the attribute
  43.      -------------------------------------------------------------------
  44.      Normal            vid[0]   wputs("^0"); wputs("^");
  45.      Bold              vid[1]   wputs("^1"); 
  46.      Emphasis          vid[2]   wputs("^2"); 
  47.      Attention!        vid[3]   wputs("^3"); 
  48.  
  49.                        vid[4]   wputs("^4");
  50.                        .
  51.                        .
  52.                        .
  53.                        vid[ 9]  wputs("^9");
  54.                        vid[10]  wputs("^Ω"); keystroke is ALT 234
  55.                        vid[11]  wputs("^δ");      "       ALT 235
  56.                        vid[12]  wputs("^∞");      "       ALT 236
  57.                        vid[13]  wputs("^φ");      "       ALT 237
  58.                        vid[14]  wputs("^ε");      "       ALT 238
  59.                        vid[15]  wputs("^∩");      "       ALT 239
  60.  
  61.  
  62.      The DEFAULT contents of these registers is as follows:
  63.  
  64.      Contents      *Color/Graphics Adapt.   Monochrome Adapter
  65.      -----------------------------------------------------------------
  66.      Normal         brite white on blue     normal
  67.      Bold           brite yellow on black   bright normal
  68.      Emphasis       brite blue on white     reverse
  69.      Attention      blink br. white on red  blinking reverse
  70.  
  71.      vid[ 4]       *red, 4                  underline
  72.      vid[ 5]        magenta, 5              bright underline    
  73.      vid[ 6]        dark yellow, 6          blinking normal
  74.      vid[ 7]        ordinary white, 7       blinking underline
  75.      vid[ 8]        dark grey, 8            blinking bright normal 
  76.      vid[ 9]        brite blue, 9           blinking bright underline
  77.      vid[10]        brite green, 0x0a       normal
  78.      vid[11]        brite cyan, 0x0b        normal
  79.      vid[12]        brite red, 0x0c         normal
  80.      vid[13]        brite magenta, 0x0d     normal
  81.      vid[14]        brite yellow, 0x0e      normal
  82.      vid[15]        brite white, 0x0f       normal
  83.  
  84.      *The default background is black for registers vid[4]..vid[15], and
  85.       blink is off.
  86.  
  87.  *=======================================================================*/
  88.  
  89.  
  90.  
  91. #define LINT_ARGS
  92.  
  93. #include <malloc.h>     /* _fmalloc(), _ffree()
  94. #include <conio.h>      /* direct console: putch(), getch(), etc */
  95.  
  96. #include "ciao.h"
  97.  
  98. /* these defines are for ciao.c alone; they are local, not for ciao.h
  99. */
  100.  
  101.  
  102. #define SCRLIM 4000                         /* 80x25 chars & attrs in screen */
  103. #define TOPX 0                              /* 80x25 screen margin defaults  */
  104. #define TOPY 0
  105. #define BTMX 79
  106. #define BTMY 24
  107. #define GOXY (2*(col+(row*80)))             /* yields absolute screen address */
  108. #define SPC ' '                             /* blank char, for clreol(), etc. */
  109.  
  110. /* monochrome monitor attributes :----------------*/
  111.  
  112. #define INV '\000'   /* invisible                 */
  113. #define UNL '\001'   /* underline                 */
  114. #define NRM '\007'   /* normal                    */
  115. #define BRU '\011'   /* bright underline          */
  116. #define BRN '\017'   /* bright normal             */
  117. #define RVR '\160'   /* reverse                   */
  118. #define BLU '\201'   /* blinking underline        */
  119. #define BLN '\207'   /* blinking normal           */
  120. #define BBU '\211'   /* blinking bright underline */
  121. #define BBN '\217'   /* blinking bright normal    */
  122. #define BLR '\360'   /* blinking reverse          */
  123.  
  124.  
  125. /*
  126. ** globals
  127. */
  128.  
  129.  
  130. int vid[16] =  /* vid_init() changes this table if cga */
  131. {
  132.     NRM, BRN, RVR, BLR, 
  133.     UNL, BRU, BLN, BLU, 
  134.     BBN, BBU, NRM, NRM,
  135.     NRM, NRM, NRM, NRM
  136. };
  137.  
  138. int vid_mode = 7;                  /* monochrome is default */
  139. int rasterh  = 12, rasterl  = 13;  /* monochrome cursor default raster lines */
  140.  
  141.  
  142. /*
  143. ** locals
  144. */
  145.  
  146. static int Initialized = 0;        /* are all the critical pointers set? */ 
  147.  
  148. static union REGS old_vid;
  149.  
  150. static int vid_seg  = 0xB000,      /* monochrome screen RAM base address */
  151.            vid_attr = 7,           /* HEAVILY USED HEREIN */
  152.            vid_page = 0,           /* "active" page is default (unused?) */ 
  153.            vid_wide = 80;          /* unused */
  154.  
  155. static char far *scribble;         /* transfer depot for RAM read/write */
  156. static char far *hidescreen;       /* pointer to invisible screen buffer */
  157.  
  158. static int activescreen = 0xB000, 
  159.            row = 0,
  160.            col = 0; 
  161.  
  162. static union REGS xy;              /* holds machine cursor address for set */
  163.  
  164. static int synchronized = 1;       /* default to hard & soft cursors alike */
  165.  
  166. static int lm = TOPX,
  167.            rm = BTMX,
  168.            tm = TOPY,
  169.            bm = BTMY;              /* default window margins */
  170.  
  171.  
  172.  
  173. /* H_pfill().  Pattern fill routine, hardwired to active screen, scribble.
  174. **
  175. ** Called by rptchar(), scrollup(), scrolldn().  Use with discretion
  176. ** because there is NO error checking!
  177. **
  178. ** Assumes scribble is already set, plus any number of other hardwired
  179. ** characteristics.  Generalizing this for any size of pattern source buffer
  180. ** and any size of destination fill buffer might be useful.
  181. **
  182. ** Movedata is very efficient.  I suspect it just sets up registers and
  183. ** then executes a single 8086 machine instruction to do the block move.  
  184. ** The result is instantaneous, at least to the proverbial naked orb.
  185. */
  186.  
  187. static void H_pfill( base, cnt ) int base, cnt;  
  188. {
  189.      static int width = 2;  /* hardwired pattern (scribble) size */
  190.  
  191.      cnt *= width;    /* translate number of pattern objects 
  192.                       ** to number of destination bytes 
  193.                       */
  194.  
  195.      /* SET PATTERN IN DEST BUFFER:  Write pattern at least once.
  196.      */
  197.      movedata( FP_SEG(scribble), FP_OFF(scribble), /* from */ 
  198.                activescreen, base,                 /* to   */
  199.                width);
  200.  
  201.      cnt -= width;                           /* one object already moved */
  202.      if (cnt > 0)                            /* shall we continue? */
  203.      {
  204.         /* ULTRAFAST PATTERN FILL:  A source byte moved to the destination
  205.         ** on iteration N extends the source pattern for iteration N+1.
  206.         */
  207.         movedata( activescreen, base,           /* srce (!)  */
  208.                   activescreen, base + width,   /* dest (!!) */
  209.                   cnt);
  210.      }
  211. }
  212.  
  213.  
  214. void gotoxy( x, y ) /* 0,0 RELATIVE TO TOPLEFT CORNER OF CURRENT WINDOW! */
  215. int x, y;
  216. {
  217.     void setmchnloc(void); /* forward ref to static ROM-BIOS function, below */
  218.  
  219.     y %= 1 + bm - tm;  /* mod number of lines in window */
  220.     x %= 1 + rm - lm;  /* mod number of columns in line */
  221.  
  222.     row = tm + y;      /* translate window-relative to fullscreen-absolute */
  223.     col = lm + x;
  224.  
  225.     if (synchronized)  /* update machine cursor? */
  226.         setmchnloc();
  227.  
  228.  
  229.  
  230.  
  231. /* Scroll functions honor the current window, and do not alter global
  232. ** col & row settings.  The window scrolls up or down one line at a time.
  233. **
  234. ** NOTICE:   The fresh, blank line which results from scrolling has the
  235. **           Normal (vid[0]) attribute.  If this "fixup" were not attempted,
  236. ** the line would have the attribute of the last character in the text which
  237. ** formerly occupied the lowest (or highest) line.  Either approach is wrong
  238. ** in some circumstances, but the Normal attribute should be correct in most
  239. ** cases.  Caller may either execute clreol() to repair the window line, or
  240. ** modify vid[0] prior to scrolling.
  241. */
  242.  
  243. void scrollup()  /* scroll current window up one line */
  244. {
  245.     register int i, b1;
  246.     auto int count, limit;
  247.  
  248.     if (!Initialized) vid_init(0);
  249.  
  250.     b1 = 2*(lm+(tm*80));           /* find top left corner of window */
  251.     limit = bm - tm;               /* and number of lines in window, less one */
  252.     count = 2*(1 + rm - lm);       /* width of line */
  253.  
  254.     for (i = 0; i < limit; i++)  /* move all contents up one line */
  255.     {
  256.         movedata( activescreen, b1 + 160,   /* source */
  257.                   activescreen, b1,         /* destination */
  258.                   count); 
  259.         b1 += 160;
  260.     }
  261.  
  262.     scribble[0] = SPC;           /* clear bottom line of window */
  263.     scribble[1] = vid[0];
  264.     H_pfill( 2*(lm+(bm*80)), 1 + rm - lm );
  265. }
  266.  
  267. void scrolldn()  /* scroll current window down one line */ 
  268. {
  269.     register int i, b1; 
  270.     auto intcount, limit;
  271.  
  272.     if (!Initialized) vid_init(0);
  273.  
  274.     b1 = 2*(lm+(bm*80));           /* find bottom left corner of window */
  275.     limit = bm - tm;               /* and number of lines in window, less one */
  276.     count = 2*(1 + rm - lm);       /* width of line */
  277.  
  278.     for (i = 0; i < limit; i++)  /* move all contents down one line */
  279.     {
  280.         movedata( activescreen, b1 - 160,   /* source */
  281.                   activescreen, b1,         /* destination */
  282.                   count);
  283.         b1 -= 160;
  284.     }
  285.  
  286.     scribble[0] = SPC;           /* clear top line of window */
  287.     scribble[1] = vid[0];
  288.     H_pfill( 2*(lm+(tm*80)), 1 + rm - lm );
  289. }
  290.  
  291.  
  292.  
  293. void getwindow( tx, ty, bx, by )  /* read current window dimensions */
  294. int *tx, *ty, *bx, *by;
  295. {
  296.     *tx = lm;
  297.     *ty = tm;
  298.     *bx = rm;
  299.     *by = bm;
  300. }
  301.  
  302.  
  303. void setwindow( tx, ty, bx, by )
  304. int tx, ty, bx, by;
  305. {
  306.    if (bx > BTMX) bx = BTMX;
  307.    if (tx < TOPX) tx = TOPX;
  308.    if (by > BTMY) by = BTMY;
  309.    if (ty < TOPY) ty = TOPY;
  310.  
  311.    if (tx >= bx || ty >= by)  /* absurd values? set full screen reduced by 1 */
  312.    { 
  313.        putch(7); /* complain but continue */
  314.        lm = TOPX + 1; rm = BTMX - 1; tm = TOPY + 1; bm = BTMY - 1; 
  315.    } 
  316.    else
  317.    {
  318.        lm = tx;
  319.        rm = bx;
  320.        tm = ty;
  321.        bm = by;
  322.    }
  323. }
  324.  
  325.  
  326.  
  327.  
  328. void fullscreen()
  329. {
  330.    setwindow( TOPX, TOPY, BTMX, BTMY );
  331. }
  332.  
  333.  
  334.  
  335.  
  336. /*===========================*/
  337. /* screen character handlers */
  338. /*===========================*/
  339.  
  340. void setscreen( n ) int n;  /* set activescreen = 1 or 2, screen 1 is visible */
  341. {
  342.      if (!Initialized) vid_init(0);
  343.  
  344.      switch (n)
  345.      {
  346.          case 2:  { activescreen = FP_SEG( hidescreen ); break; }
  347.          default:
  348.          case 1:  { activescreen = vid_seg; break; }
  349.      }
  350. }
  351.  
  352.  
  353. void swapscreen( n ) int n;  /* swap hidden screen n with visible screen 1 */
  354. {                            /* for now, n can only be 2  */   
  355.      char far *save;
  356.      int tricseg, tracseg, saveseg, temp;
  357.  
  358.      if (!Initialized) vid_init(0);
  359.  
  360.      save = _fmalloc( SCRLIM );
  361.      saveseg = FP_SEG( save );
  362.  
  363.      temp = activescreen;
  364.  
  365.      if (n < 2) n = 2;       /* just now, range check on n is trivial */
  366.      if (n > 2) n = 2; 
  367.  
  368.      setscreen( n );              /* hidden screen n */
  369.         tracseg = activescreen;
  370.      setscreen( 1 );              /* visible screen 1 */ 
  371.         tricseg = activescreen;
  372.  
  373.      activescreen = temp;
  374.  
  375.      movedata( tracseg, 0, saveseg, 0, SCRLIM);  /* swap contents of screens */
  376.      movedata( tricseg, 0, tracseg, 0, SCRLIM);
  377.      movedata( saveseg, 0, tricseg, 0, SCRLIM);
  378.  
  379.      _ffree( save );
  380. }
  381.  
  382.  
  383.  
  384. void setsynch( on ) int on;  /* synchronized is ON by default */
  385. {
  386.      if ((synchronized = on? 1: 0) == 1)
  387.          setmchnloc();
  388. }
  389.  
  390.  
  391.  
  392.  
  393. void rptchar( ch, cnt ) char ch; int cnt;  /* does NOT update screen position */
  394. {
  395.      if (!Initialized) vid_init(0);
  396.  
  397.      scribble[0] = ch;
  398.      scribble[1] = vid_attr;
  399.      if (cnt >= 1) 
  400.          H_pfill( GOXY, cnt );  /* use pattern fill for speed */
  401. }
  402.  
  403.  
  404.  
  405. void readscreenchar( ch ) SCW *ch;
  406. {
  407.     if (!Initialized) vid_init(0);
  408.  
  409.     movedata( activescreen, GOXY,
  410.               FP_SEG(scribble), FP_OFF(scribble), 
  411.               2);
  412.     ch->byte = scribble[0];
  413.     ch->attr = scribble[1];
  414. }
  415.  
  416.  
  417.  
  418. /*============================*/
  419. /* screen clearing functions  */
  420. /*============================*/
  421.  
  422.  
  423.  
  424. void clreol()  /* honors current window setting */
  425. {
  426.     if (!Initialized) vid_init(0);
  427.  
  428.     rptchar(SPC, 1 + rm - col);
  429. }
  430.  
  431.  
  432. void clrwindow()   /* home cursor, clear window */
  433. {
  434.     int lin, yrel, xrel, temp;
  435.  
  436.     if (!Initialized) vid_init(0);
  437.  
  438.     temp = synchronized;
  439.     setsynch( 0 );        /* don't bother gotoxy with machine cursor updates */
  440.     lin = tm;
  441.     xrel = yrel = 0;
  442.     while (lin++ <= bm)
  443.     {
  444.         gotoxy(xrel, yrel++);
  445.         clreol();
  446.     }
  447.     gotoxy(0,0);
  448.     setsynch( temp );
  449. }
  450.  
  451.  
  452. void clreos()  /* screen only, ignores window */
  453. {
  454.     if (!Initialized) vid_init(0);
  455.     rptchar( SPC, (SCRLIM - GOXY) / 2 );
  456.  
  457. }
  458.  
  459.  
  460. void clrscrn()  /* resets full screen, homes cursor, clears screen */
  461. {
  462.     if (!Initialized) vid_init(0);
  463.  
  464.     fullscreen();
  465.     gotoxy(0,0);
  466.     rptchar(SPC,SCRLIM /2);
  467. }
  468.  
  469.  
  470. /*======================*/
  471. /* WindowBox            */
  472. /*======================*/
  473.  
  474. /* this version draws even the largest box almost instantaneously
  475. ** dco, 8/29/86
  476. */
  477.  
  478. void windowbox( tx, ty, bx, by ) int tx, ty, bx, by;
  479. {
  480.         register int i,j; 
  481.         auto int temp;
  482.  
  483.         if (!Initialized) vid_init(0);
  484.  
  485.         /* first, clear the working area to startle and amuse...
  486.         */
  487.  
  488.         setwindow( tx, ty, bx, by );
  489.         clrwindow();
  490.  
  491.         /* now, draw a crisp double-lined box (user's vid_attr ignored)
  492.         */
  493.  
  494.         temp = vid_attr;
  495.         vid_attr = (vid_mode == 2)? vid[15]: vid[10];
  496.  
  497.         tx = lm; /* use values that setwindow range checking may have found! */
  498.         ty = tm;
  499.         bx = rm;
  500.         by = bm;
  501.  
  502.         /* then, draw the border AROUND the area!
  503.         */
  504.  
  505.         fullscreen();        /* allows gotoxy to use the argument dimensions */
  506.  
  507.         if (tx > TOPX) { --tx; }
  508.         if (ty > TOPY) { --ty; }
  509.         if (bx < BTMX) { ++bx; }
  510.         if (by < BTMY) { ++by; }
  511.  
  512.         /* top line
  513.         */
  514.         gotoxy( tx, ty );
  515.         rptchar('═',bx - tx);
  516.  
  517.         /* right side
  518.         */
  519.         gotoxy( bx, ty );
  520.         rptchar('╗',1);
  521.  
  522.         j = by - ty;       /* put the for loop condition into a register int */
  523.         for ( i = 1; i < j; i++ )
  524.         {
  525.              gotoxy( bx, ty + i);
  526.              rptchar('║',1);
  527.         }
  528.  
  529.         /* bottom line 
  530.         */
  531.         gotoxy( bx, by );
  532.         rptchar('╝',1);
  533.  
  534.         gotoxy( tx, by );
  535.         rptchar('═',bx - tx);
  536.         rptchar('╚',1);
  537.  
  538.         /* left side
  539.         */
  540.         for ( i = 1; i < j; i++ )
  541.         {
  542.              gotoxy( tx, ty + i);
  543.              rptchar('║',1);
  544.         }
  545.  
  546.         gotoxy(tx,ty);
  547.         rptchar('╔',1);
  548.  
  549.         /* last, restore entry dimensions, with fixups on squashed margins
  550.         */
  551.  
  552.         /* Box is AROUND window    Window is squashed onto box's outline */
  553.         /* ---------------------   ------------------------------------- */
  554.            ++tx;                   /* else tx always was TOPX, so tx++;  */
  555.            ++ty;                   /* else ty always was TOPY, so ty++;  */
  556.            --bx;                   /* else bx always was BTMX, so bx--;  */
  557.            --by;                   /* else by always was BTMY, so by--;  */
  558.  
  559.         setwindow( tx, ty, bx, by );
  560.         gotoxy(0,0);
  561.         vid_attr = temp;  /* restore user's vid_attr */
  562. }
  563.  
  564.  
  565.  
  566.  
  567. void wink( c )  /* window character out, obeys backspace and newline */
  568. char c;
  569. {
  570.  
  571.     if (!Initialized) vid_init(0);
  572.  
  573.     /* newline? */
  574.     if (c == '\n' || c == '\r') col = 80;  /* set range err to force CR */
  575.  
  576.     /* backspace? nondestructive = 8, destructive = 127 */
  577.     else if ( c == '\b' || c == 127)  /* NOTICE:  THIS PATH RETURNS */
  578.     {
  579.         col--;               /* decrement the column */
  580.         if (col < lm)        /* beyond left margin? */
  581.         {
  582.              row--;          /* yes, decrement the line */
  583.              if (row < tm)   /* above top margin? */
  584.              {
  585.                 row = tm;    /* yes, stick at 0,0 -- does not scroll down! */
  586.                 col = lm;
  587.              }
  588.              else col = rm;  /* no, jump to rightmost column */ 
  589.         }
  590.         if (c == 127) rptchar(SPC,1); /* destroy existing char on screen    */
  591.         goto synch;
  592.     } 
  593.  
  594.     else rptchar( c, 1 );    /* uses vid_attr attribute */
  595.  
  596.     /* bump x cursor position, do newline or scroll window up if necessary */
  597.  
  598.     if ( col >= rm )      /* need to newline? */
  599.     {
  600.         col = lm;
  601.         if ( row >= bm )  /* need to scroll? */
  602.         {
  603.             row = bm;
  604.             scrollup();
  605.         }
  606.         else row++;
  607.     }
  608.     else col++;
  609.  
  610.     synch:
  611.     if (synchronized) /* update machine cursor */
  612.         setmchnloc();
  613.  
  614. } /* wink */
  615.  
  616.  
  617.  
  618.  
  619.  
  620. void wputs( p )  /* write string into window, uses wink */
  621. char *p;
  622. {
  623.      register int test;
  624.      auto int temp;
  625.  
  626.      if (!Initialized) vid_init(0);
  627.  
  628.      temp = synchronized;
  629.      setsynch(0);
  630.      while (*p) 
  631.      {
  632.          if (*p == '^')  /* escape character? switch modes */
  633.          {
  634.              p++;              /* read escape command char */
  635.              test = *p & 0xFF; /* kill sign extension */
  636.              switch ( test ) 
  637.              {
  638.                  case  '0': { vid_attr = vid[ 0]; break; }
  639.                  case  '1': { vid_attr = vid[ 1]; break; }
  640.                  case  '2': { vid_attr = vid[ 2]; break; }
  641.                  case  '3': { vid_attr = vid[ 3]; break; }
  642.  
  643.                  case  '4': { vid_attr = vid[ 4]; break; }
  644.                  case  '5': { vid_attr = vid[ 5]; break; }
  645.                  case  '6': { vid_attr = vid[ 6]; break; }
  646.                  case  '7': { vid_attr = vid[ 7]; break; }
  647.                  case  '8': { vid_attr = vid[ 8]; break; }
  648.                  case  '9': { vid_attr = vid[ 9]; break; }
  649.  
  650.                  case  'Ω': { vid_attr = vid[10]; break; } /* ea */
  651.                  case  'δ': { vid_attr = vid[11]; break; } /* eb */
  652.                  case  '∞': { vid_attr = vid[12]; break; } /* ec */
  653.                  case  'φ': { vid_attr = vid[13]; break; } /* ed */
  654.                  case  'ε': { vid_attr = vid[14]; break; } /* ee */
  655.                  case  '∩': { vid_attr = vid[15]; break; } /* ef */
  656.  
  657.                  case  '^': { 
  658.                             wink( *p );  /* verbatim ^ char */
  659.                             break; 
  660.                             }
  661.                  default:   { 
  662.                             --p; 
  663.                             vid_attr = vid[0];  /* just ^, no command arg */
  664.                             break; 
  665.                             }
  666.              }
  667.              p++;  /* next after escape command */
  668.          }
  669.          else 
  670.          {
  671.              wink( *p++ );  /* obeys newline, tab, if present */
  672.          }
  673.      }
  674.      setsynch(temp); /* update machine cursor only at last, if at all */
  675.  
  676. } /* wputs */
  677.  
  678.  
  679.  
  680. /*================================*/
  681. /*  ROM-BIOS dependent functions  */
  682. /*================================*/
  683.  
  684. static void setmchnloc()  /* the ROM-BIOS gotoxy function */
  685. {
  686.     xy.h.ah = 2;
  687.     xy.h.bh = 0;
  688.     xy.h.dh = row;
  689.     xy.h.dl = col;
  690.     int86( 0x10, &xy, &xy );
  691. }
  692.  
  693.  
  694. void setcursize( r1, r2 )
  695. int r1,r2;
  696. {
  697.     union REGS rx;
  698.  
  699.     rx.h.ah = 1;
  700.     rx.h.ch = r1;
  701.     rx.h.cl = r2;
  702.     int86( 0x10, &rx, &rx );
  703. }
  704.  
  705.  
  706. void defcursor()  /* set default cursor for cga or mono */
  707. {
  708.     setsynch(1);
  709.     setcursize( rasterh, rasterl );
  710. }
  711.  
  712.  
  713. /* The cursor save and restore routines recognize the synchronized flag.
  714. ** If synchronized, the machine cursor is saved or restored, and the soft
  715. ** cursor is identical to the machine cursor; if not synchronized, the
  716. ** soft cursor is saved or restored, and the machine cursor (wherever it
  717. ** happens to be) is ignored.
  718. **
  719. ** The value of the current synchronized flag is saved or returned:
  720. **
  721. **   Savecursor() returns the value of the synchronized flag, and saves it.
  722. **   Restcursor() returns the value of the synchronized flag that was active
  723. **                when the cursor was saved, but does not restore it.
  724. */
  725.  
  726. int savecursor( x )
  727. union REGS *x;
  728. {
  729.     if (synchronized)  /* hard and soft are identical, save hard */
  730.     {
  731.        x->h.ah = 3;
  732.        x->h.bh = 0;
  733.        int86( 0x10, x, x );
  734.     }
  735.     else               /* save soft */
  736.     {
  737.        x->h.dh = row;
  738.        x->h.dl = col;
  739.     }
  740.     x->h.ah = synchronized;  /* save the flag for proper kind of restore */
  741.     return( x->h.ah );
  742. }
  743.  
  744.  
  745. int restcursor( x )
  746. union REGS *x;
  747. {
  748.     if (x->h.ah != 0)        /* non-zero, synchronization was ON when saved */
  749.     {
  750.        x->h.ah = 1;          /* so restore machine size */
  751.        x->h.bh = 0;          /* ensure "active" page */
  752.        int86( 0x10, x, x );
  753.        x->h.ah = 2;          /* and machine location */
  754.        int86( 0x10, x, x );
  755.     }
  756.     row = x->h.dh;           /* in any case, set soft location to match */
  757.     col = x->h.dl;
  758.     return( x->h.ah );
  759. }
  760.  
  761.  
  762.  
  763. void hidecursor()
  764. {
  765.     setsynch(0);
  766.     setcursize( 32,32 );
  767. }
  768.  
  769.  
  770. /*
  771. **  ================================================
  772. **  Sample calls to savescreen(), restorescreen()
  773. **  Note:  These only work with text screens, 
  774. **  they do not save CRTpage/CPUpage screen info.
  775. **  They DO save the current window margins!
  776. **  ================================================
  777. **
  778. **   main() {
  779. **
  780. **   union REGS x,z;
  781. **   char far *screen1, *screen2;
  782. **
  783. **   vid_init();                            WARNING: must call vid_init()
  784. **   screen1 = savescreen( &x );
  785. **   .
  786. **   .
  787. **          screen2 = savescreen( &z );     WARNING: these MUST nest properly
  788. **          .                                        or heap allocation will
  789. **          .                                        be annihilated! 
  790. **          .
  791. **          restorescreen( screen2, &z );
  792. **   .
  793. **   .
  794. **   restorescreen( screen1, &x );
  795. **   }
  796. **
  797. */
  798.  
  799. static void sorry( n ) int n;
  800. {
  801.     char *p,*q;
  802.     q = "init";
  803.     switch (n)
  804.     {
  805.     case 2:  q = "restorescreen"; p = "no screen saved, can't restore"; break;
  806.     case 1:  q = "savescreen";
  807.     case 0:  p = "not enough k, can't continue"; break;
  808.     default: q = "nasty"; p = "looks fatal"; break;
  809.     } 
  810.     printf("\n\aCIAO: %s error: %s\n",q,p);
  811.     exit(1);
  812. }
  813.  
  814.  
  815. char far *savescreen( cursor )  /* caller must keep pointer to storage! */
  816. union REGS *cursor;
  817. {
  818.     union REGS ry;
  819.     char far *scrfptr;
  820.      
  821.     if (!Initialized) vid_init(0);
  822.  
  823.     ry.h.ah = 0x03;                   /* read cursor position...       */
  824.     ry.h.bh = 0;
  825.     int86( 0x10, &ry, cursor );       /* store result in caller's var  */
  826.  
  827.     cursor->x.cflag = 1066;           /* a famous date in history...   */
  828.     cursor->h.ah = tm;                /* save caller's window settings */
  829.     cursor->h.al = lm;                /* cannot use bx, cx or dx       */
  830.     cursor->x.di = rm;
  831.     cursor->x.si = bm;
  832.  
  833.     scrfptr = _fmalloc( SCRLIM );
  834.     if ( scrfptr == 0L )
  835.     {
  836.         sorry(1);
  837.     }
  838.     movedata( vid_seg, 0, FP_SEG( scrfptr ), FP_OFF( scrfptr ), SCRLIM );
  839.     return (scrfptr);
  840. }
  841.  
  842.  
  843.  
  844.  
  845. void restorescreen( scrfptr, cursor ) /* caller keeps pointer to storage! */
  846. char far *scrfptr;
  847. union REGS *cursor;
  848. {
  849.     union REGS rx;
  850.  
  851.     if (!Initialized || cursor->x.cflag != 1066)
  852.     {
  853.         sorry(2);
  854.     }
  855.  
  856.     movedata( FP_SEG( scrfptr ), FP_OFF( scrfptr ), vid_seg, 0, SCRLIM );
  857.     _ffree( scrfptr );
  858.  
  859.     tm = cursor->h.ah;        /* restore caller's window settings */
  860.     lm = cursor->h.al;
  861.     rm = cursor->x.di;
  862.     bm = cursor->x.si;
  863.     cursor->x.ax = cursor->x.si = cursor->x.di = cursor->x.cflag = 0;
  864.  
  865.     rx.h.ah = 1;
  866.     rx.x.bx = cursor->x.bx;
  867.     rx.x.cx = cursor->x.cx;     /* set old cursor size     */
  868.     int86( 0x10, &rx, &rx );
  869.     rx.h.ah = 2;
  870.     rx.x.bx = cursor->x.bx;
  871.     rx.x.dx = cursor->x.dx;
  872.     int86( 0x10, &rx, cursor ); /* set old cursor position */
  873. }
  874.  
  875.  
  876.  
  877.  
  878. /*
  879. ** Initialize, Deinitialize
  880. */
  881.  
  882. static void init_criticals(mode) int mode;
  883. {
  884.     int i;
  885.     union REGS rx;
  886.  
  887.     if (((scribble = (char far *) _fmalloc(2)) == 0L) ||
  888.        ((hidescreen  = (char far *) _fmalloc(SCRLIM)) == 0L)) 
  889.            sorry(0);
  890.  
  891.     rasterh  = 12;                 /* assume monochrome cursor */
  892.     rasterl  = 13;
  893.  
  894.     rx.h.ah = 15;                  /* read current video mode */
  895.     int86( 0x10, &rx, &old_vid );
  896.     vid_page = old_vid.h.bh;
  897.     vid_wide = old_vid.h.ah;
  898.     vid_mode = old_vid.h.al;   /* a 7 means we found monochrome hardware */
  899.  
  900.  
  901.     if (mode == 0) mode = vid_mode;  /* request to stay in current mode? */
  902.  
  903.     /* in any case, allow only mono mode 7, or cga modes 2 or 3 */
  904.  
  905.     if (vid_mode != 7)         /* is hardware a color/graphics adapter? */
  906.     {
  907.        vid_seg = 0xB800;       /* yes, screen RAM base address is different! */
  908.        if ( mode == 3 )        /* asking for color?  set it up! */
  909.        {
  910.           for (i = 0; i < 16; i++) 
  911.               vid[i] = i;                      /* set color defaults */
  912.           vid[0]  = 0x17 | 0x08;               /* set normal         */
  913.           vid[1]  = 0x06 | 0x08;               /*  "  bold           */
  914.           vid[2]  = 0x71 | 0x08;               /*  "  emphasis       */
  915.           vid[3]  = 0x47 | 0x08 | 0x80;        /*  "  attention      */
  916.        }
  917.        else 
  918.        {
  919.           mode = 2;                 /* only modes 2 and 3 are allowed, here */
  920.           for (i = 4; i < 16; i++)  /* all else defaults to normal */
  921.               vid[i] = vid[0];
  922.        }
  923.        rasterh = 6;               /* cga default cursor raster lines */
  924.        rasterl = 7;
  925.        if (vid_mode != mode)      /* not already in the requested mode? */
  926.        {
  927.           rx.h.ah = 0;            /* if not, set requested mode */
  928.           rx.h.al = mode;
  929.           int86( 0x10, &rx, &rx );
  930.        }
  931.        rx.h.ah = 15;              /* read current mode again */
  932.        int86( 0x10, &rx, &rx );
  933.        vid_page = rx.h.bh;        /* set globals accordingly */ 
  934.        vid_wide = rx.h.ah;
  935.        vid_mode = rx.h.al;
  936.     }
  937.     Initialized = 1;              /* we are, basically, but...  */
  938.     setscreen(1);                 /* ensure activescreen is set */
  939.     vid_attr = vid[0];            /* and ensure vid_attr is set */
  940. }
  941.  
  942.  
  943.  
  944.  
  945. /* Vid_Init() - This is a "MUST CALL", but if the various routines which
  946. **              depend on finding initialized values and pointers find the
  947. **              Initialized flag set to zero, they will take it on themselves
  948. **              to call vid_init(0).  Any argument to vid_init is valid, but
  949. **              mode 7 is set if the monochrome adapter is in the system, and
  950. **              only modes 2 or 3 are set if the cga card is found.  The 
  951. **              0 argument requests that a valid current mode not be changed;
  952. **              this prevents the firmware from clearing the screen, if it's
  953. **              not absolutely necessary.  Vid_init() will always select the
  954. **              visible screen, and synchronize the soft & machine cursors.
  955. **              Also, selects appropriate default cursor size; cga and mono
  956. **              have different cursors.
  957. */
  958.  
  959. void vid_init( mode ) int mode;
  960. {
  961.     auto union REGS x;
  962.  
  963.     init_criticals(mode);      /* sets initialized flag on exit */
  964.  
  965.     savecursor( &x );          /* We can now make sure that hard and   */
  966.     row = x.h.dh;              /* soft cursors actually are identical, */
  967.     col = x.h.dl;              /* going soft <- hard!  Otherwise, the  */
  968.     synchronized = 1;          /* first savescreen( &cursor ) doesn't! */
  969. }                              
  970.  
  971.  
  972.  
  973.  
  974. void vid_exit()
  975. {
  976.      if (Initialized)
  977.      {
  978.           if (old_vid.h.al != vid_mode)
  979.           {
  980.                old_vid.h.ah = 0;
  981.                int86( 0x10, &old_vid, &old_vid );
  982.           }
  983.           _ffree(hidescreen);
  984.           _ffree(scribble);
  985.           Initialized = 0;  /* we're NOT initialized any more! */
  986.      }
  987.      setcursize( rasterh, rasterl );
  988. }
  989.  
  990.  
  991. /* eof: ciao.c */
  992.  
  993.