home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 307_01 / video_io.c < prev   
C/C++ Source or Header  |  1990-03-20  |  6KB  |  226 lines

  1. /*============================================================
  2.  
  3.  Video_io.c.
  4.  
  5.  A set of customised video routines to work
  6.  with the disk utilities ADU and DE.
  7.  
  8.  By Alex Cameron.
  9.  
  10.  Note:  REGS template is defined in dos.h.
  11. ==============================================================*/
  12.  
  13. #include <dos.h>
  14. #include <stdio.h>
  15. #define    LINLEN  64
  16.  
  17. /*----------------------Function prototypes--------------------*/
  18.  
  19. void    clrscrn( );
  20. void    put_string(char *,int);
  21. void    clr_window(int,int,int,int,int);
  22. void    scroll_window(int,int,int,int,int);
  23. void    gotoxy(int,int);
  24. void    getxy(int *,int *);
  25. int     putfa(int,int,char *,int,int);
  26. void    get_string (char *);
  27. void    put_char (int, int);
  28.  
  29. /*--------------------------------------------------------------*/
  30.  
  31.  
  32. /*
  33. ** Clear the screen utilising the BIOS call.
  34. */
  35. void clrscrn () {
  36. union REGS inregs,outregs;
  37.  
  38.     inregs.h.ah = 6;       /* funtion */
  39.     inregs.h.al = 0;
  40.     inregs.h.bh = 7;
  41.     inregs.x.cx = 0;
  42.     inregs.h.dl = 79;      /* column */
  43.     inregs.h.dh = 24;      /* row    */
  44.     int86(0X10, &inregs, &outregs);
  45. }
  46. /*
  47. ** Clear the window specified.  The attribute taken, is defined
  48. ** in the IBM Tech Reference Manual.
  49. */
  50. void clr_window (x1,y1,x2,y2,attr) int x1,y1,x2,y2,attr; {
  51. union REGS inregs,outregs;
  52.  
  53.     inregs.h.ah = 6;
  54.     inregs.h.al = 0;
  55.     inregs.h.bh = attr;
  56.     inregs.h.ch = y1;
  57.     inregs.h.cl = x1;
  58.     inregs.h.dh = y2;
  59.     inregs.h.dl = x2;
  60.     int86(0X10, &inregs, &outregs);
  61. }
  62. /*
  63. ** BIOS call to scroll the window specified by
  64. ** x1,y1 and x2,y2 in the direction passed by
  65. ** the parameter dir.
  66. */
  67. void scroll_window (x1,y1,x2,y2,dir) int x1,y1,x2,dir; {
  68. union REGS inregs,outregs;
  69.  
  70.     inregs.h.ah = dir;
  71.     inregs.h.al = 1;
  72.     inregs.h.bh = 7;
  73.     inregs.h.ch = y1;
  74.     inregs.h.cl = x1;
  75.     inregs.h.dh = y2;
  76.     inregs.h.dl = x2;
  77.     int86(0X10, &inregs, &outregs);
  78.  
  79. }
  80. /*
  81. ** BIOS call to goto point specified by x and y.
  82. */
  83. void gotoxy( x,y ) int x,y; {
  84. union REGS inregs,outregs;
  85.  
  86.     inregs.h.ah = 2;
  87.     inregs.h.bh = 0;
  88.     inregs.h.dh = y;           /* column   */
  89.     inregs.h.dl = x;           /* row  */
  90.     int86(0X10, &inregs, &outregs);
  91.  
  92. }
  93.  
  94. /*
  95. ** BIOS call to obtain the current cursor position on
  96. ** the screen.  The actual addresses of where the x and y
  97. ** coordinates are to placed are passed as arguments
  98. ** to getxy.
  99. */
  100. void getxy(x,y) int *x,*y; {
  101. union REGS outregs,inregs;
  102.  
  103.     inregs.h.ah = 3;
  104.     inregs.h.bh = 0;
  105.     int86(0x10,&inregs,&outregs);
  106.     *x = outregs.h.dl;          /* pass the row back */
  107.     *y = outregs.h.dh;          /* pass the column back */
  108. }
  109.  
  110. /*
  111.  Output a formated string with attributes
  112.  according to the printf formats contained
  113.  in *fstring.  Return the count to assist
  114.  in cursor positioning.
  115. */
  116.  
  117. int putfa(x,y,fstring,arg,attr) int x,y,arg,attr; char *fstring; {
  118. int count;
  119. static char    out_buf[128];
  120.  
  121.      gotoxy(x,y);
  122.      count = sprintf(out_buf,fstring,arg);
  123.      put_string(out_buf,attr);
  124.      return count;
  125. }
  126.  
  127. /*
  128.  This is a dumb routine which output a string
  129.  at the cursor postion with the attributes
  130.  provided.  It will not check for wrap around etc.
  131. */
  132.  
  133. void put_string (sptr,attr) char *sptr; int attr; {
  134. union REGS inregs,outregs;
  135. int x,y;
  136.  
  137.     getxy(&x,&y);           /* get the current cursor position */
  138.     while ((inregs.h.al = *sptr++) != NULL) {
  139.         inregs.h.ah = 9;
  140.         inregs.h.bh = 0;
  141.         inregs.h.bl = attr;
  142.         inregs.x.cx = 1;
  143.         int86(0x10,&inregs,&outregs);
  144.         gotoxy(++x,y);      /* move the cursor along one position */
  145.     }
  146. }
  147. /*
  148.  Customised version of gets().  Used primarly to obtain
  149.  the command line, thereby avoiding DOS's echo to the screen
  150.  of any key which will muck up the screen layout.  This
  151.  routine also allows customisation of the command line
  152.  re-issued commands etc.  If you  don't like what this
  153.  routine does, then use gets(sptr).
  154. */
  155.  
  156. void get_string (sptr) char *sptr; {
  157. int c, count;
  158. static int eolc;            /* count of characters to eol */
  159. char *init_sptr;
  160.  
  161.     init_sptr = sptr;
  162.     count = 0;
  163.     while ( (c = getch()) != '\r' && count < LINLEN) {
  164.         switch (c) {
  165.  
  166.             case 0x1b:  continue;       /* trap escape for edit */
  167.  
  168.             case '\b':  if (sptr > init_sptr) {  /* back space */
  169.                             sptr--; count--;
  170.                             putchar('\b');
  171.                             putchar('\ ');
  172.                             putchar('\b');
  173.                         }
  174.                         continue;
  175.  
  176.             case '\0':  c = getch();    /* process extended chars */
  177.                         switch (c) {
  178.  
  179.                             /* F1 => Help */
  180.                             case 59: sptr = init_sptr;
  181.                                      *sptr++ = '?';  /* simulate ? */
  182.                                      *sptr = '\0';
  183.                                      return;
  184.  
  185.                             /* F3 => re-issue command */
  186.                             case 61: put_string(sptr,0x07);
  187.                                      sptr += eolc;  /* position pointer */
  188.                                      count = eolc;  /* to last position */
  189.                                      continue;      /* edit the command */
  190.  
  191.                             /* F4 => repeat the previous command */
  192.                             case 62: put_string(sptr,0x07); 
  193.                                      return; 
  194.  
  195.                         } 
  196.                         continue;   /* disregard all the others */ 
  197.  
  198.             default:    count++;    /* assume normal char */ 
  199.                         putchar(c); 
  200.                         *sptr++ = c; 
  201.         } 
  202.     } 
  203.     eolc = count;                   /* keep track of line lenght */ 
  204.     *sptr++ = '\0'; 
  205.  
  206.  
  207. /* 
  208.  Simple routine to output one character with attributes 
  209.  at the current cursor position. 
  210. */ 
  211.  
  212. void put_char (c,attr) int c,attr; { 
  213. union REGS outregs,inregs; 
  214.  
  215.     inregs.h.al = c; 
  216.     inregs.h.ah = 9; 
  217.     inregs.h.bh = 0;
  218.     inregs.h.bl = attr;
  219.     inregs.x.cx = 1;
  220.     int86(0x10,&inregs,&outregs);
  221.  
  222.  
  223.  
  224.