home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / GETCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-06  |  2.7 KB  |  95 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3.  
  4. static int    _cfg_ch = _CIOch;    /* getch()/putch() configuration */
  5. unsigned long    _getch = 0L;        /* raw getch() value from OS */
  6.  
  7. cfg_ch(cfg)
  8. int cfg;
  9. /*
  10.  *    Configure getch()/putch() operation.  The following are legal
  11.  *    values for <cfg> and may be combined with the | operator:
  12.  *        _CIOb        Use BIOS level i/o calls
  13.  *        _CIOch        8-bit character codes only (cf:getch)
  14.  *        _CIOvt        Enable VT-52 escape sequence processing
  15.  *    Return the previous value.  If <cfg> == -1, just return current value.
  16.  */
  17. {
  18.     register int oldcfg;
  19.  
  20.     oldcfg = _cfg_ch;
  21.     if(cfg != -1)
  22.         _cfg_ch = cfg;
  23.     return(oldcfg);
  24. }
  25.  
  26. int getch()
  27. /*
  28.  *    Machine dependent console input function.  This function normally
  29.  *    gets a character from the keyboard by calling the GEMDOS "Cconin"
  30.  *    function.  If cfg_ch() is given the _CIOb option, input is gotten
  31.  *    from the BIOS "Bconin" function instead.  The BIOS level functions
  32.  *    don't process ^C, ^S or ^Q, while the GEMDOS functions do.  The
  33.  *    most common use for getch() is when keyboard scan codes are needed
  34.  *    to process special function keys.  The return value from getch()
  35.  *    consists of the scan code in the high-order byte, and the ascii
  36.  *    character code in the low-order byte.  If cfg_ch() is given the
  37.  *    _CIOch option, the return value is always an 8-bit quantity,
  38.  *    either the scan code with the 8th bit set, or the ascii code with
  39.  *    the 8th bit clear.  This is somewhat less informative, since the
  40.  *    scan code form is returned only if the ascii value is 0.  In any
  41.  *    case, the global unsigned long variable "_getch" will contain the
  42.  *    full value returned by the OS.
  43.  */
  44. {
  45.     register unsigned long cc;
  46.     register unsigned int c;
  47.  
  48.     _getch = cc = (_cfg_ch & _CIOb) ? Bconin(2) : Crawcin();
  49.     if(_cfg_ch & _CIOch) {
  50.         if((c = cc) == 0)        /* null character code */
  51.             c = (cc >> 16) | 0x80;    /* get scan code instead */
  52.         c &= 0xFF;            /* make it 8-bit only */
  53.     }
  54.     else
  55.         c = (0x00FF & cc) | (0xFF00 & (cc >> 8));
  56.     return(c);
  57. }
  58.  
  59. char putch(c)
  60. register char c;
  61. /*
  62.  *    Machine dependent (typically quite fast) console output function.
  63.  */
  64. {
  65.     if(_cfg_ch & _CIOb)
  66.         if((c < ' ') || (_cfg_ch & _CIOvt))
  67.             Bconout(2, c);
  68.         else
  69.             Bconout(5, c);
  70.     else
  71.         Cconout(c);
  72.     return(c);
  73. }
  74.  
  75. int getche()
  76. /*
  77.  *    Same as getch() but calls putch() to echo the character.
  78.  */
  79. {
  80.     register char c;
  81.  
  82.     c = getch();            /* do normal getch() */
  83.     putch((char) _getch);        /* echo from raw OS code */
  84.     return(c);
  85. }
  86.  
  87. int kbhit()
  88. /*
  89.  *    Machine dependent function to detect if input is waiting for the
  90.  *    getch() function.  Returns non-zero if the console has data ready.
  91.  */
  92. {
  93.     return((_cfg_ch & _CIOb) ? Bconstat(2) : Cconis());
  94. }
  95.