home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / PROCWRKB.ZIP / BENCH1.ZIP / WIN / DOS / INCHAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-16  |  6.6 KB  |  364 lines

  1. /* ==( dos/inchar.c )== */
  2. /* ----------------------------------------------- */
  3. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  4. /* Modification to this source is not supported    */
  5. /* by Vestronix Inc.                               */
  6. /*            All Rights Reserved                  */
  7. /* ----------------------------------------------- */
  8. /* Written   Geo  26-Aug-88                        */
  9. /* Modified  Geo  28-Jun-90  See comments below    */
  10. /* ----------------------------------------------- */
  11. /* %W%  (%H% %T%) */
  12.  
  13. /*
  14.  *  Modifications
  15.  *
  16.  *  28-Jun-90  Geo - newer code for exitcheck
  17.  *   1-Mar-90  SBF - tx for CTRL-[A-Z] fixed
  18.  *  26-Jan-90  Geo - tx mixed in for Dos/Unix
  19.  *  25-Jul-89  Geo - Keyboard translate
  20.  *  25-Oct-89  Geo - 1.32 Merge
  21. */
  22.  
  23. /*
  24.  * Input stream handler
  25. */
  26.  
  27. # include <stdio.h>
  28. # include <bench.h>
  29.  
  30.  
  31. # ifdef MSDOS /* And it should be ! */
  32. #  ifndef __TURBOC__   /* Complains about stuff - NIG */
  33. #    ifndef LATTICE
  34. #      include <conio.h> /* Prototye for kbhit() */
  35. #     endif
  36. #  endif
  37. # endif
  38.  
  39. # include "pregs.h"
  40.  
  41. static PROTO (int keyval, (unsigned char, char));
  42. static PROTO (int xlat_val, (int));
  43.  
  44. # ifdef WDEBUG
  45. static PROTO (void exitcheck, (void));
  46. # endif
  47.  
  48. /*
  49.  * Warning do not modify this table
  50.  * without changing corresponding table
  51.  * says Geo 12-Dec-89
  52. */
  53. unsigned char scancodes[] =
  54. {
  55. /* Enter  Tab  Esc    Bs   Btab */
  56.    0x1c, 0x0f, 0x01, 0x0e, 0x00,
  57.  
  58. /* Left  Right  Up   Down */
  59.    0x4b, 0x4d, 0x48, 0x50,
  60.  
  61. /* Ins    Del  Home  End   PgUp  PgDn */
  62.    0x52, 0x53, 0x47, 0x4f, 0x49, 0x51,
  63.  
  64. /* F1    F2    F3    F4    F5 */
  65.    0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  66.  
  67. /* F6    F7    F8    F9    F10 */
  68.    0x40, 0x41, 0x42, 0x43, 0x44,
  69.  
  70. /* F11   F12   F13   F14   F15 */
  71.    0x85, 0x86, 0x00, 0x00, 0x00,
  72.  
  73. /* F16   F17   F18   F19   F20 */
  74.    0x00, 0x00, 0x00, 0x00, 0x00,
  75.  
  76. /* AltF1  AltF2  AltF3  AltF4  AltF5 */
  77.    0x68,  0x69,  0x6a,  0x6b,  0x6c,
  78.  
  79. /* AltF6  AltF7  AltF8  AltF9  AltF10 */
  80.    0x6d,  0x6e,  0x6f,  0x70,  0x71,
  81.  
  82. /* ^F1   ^F2   ^F3   ^F4   ^F5 */
  83.    0x5e, 0x5f, 0x60, 0x61, 0x62,
  84.  
  85. /* ^F6   ^F7   ^F8   ^F9   ^F10 */
  86.    0x63, 0x64, 0x65, 0x66, 0x67,
  87.  
  88. /* AltA  AltB  AltC  AltD  AltE */
  89.    0x1e, 0x30, 0x2e, 0x20, 0x12,
  90.  
  91. /* AltF  AltG  AltH  AltI  AltJ */
  92.    0x21, 0x22, 0x23, 0x17, 0x24,
  93.  
  94. /* AltK  AltL  AltM  AltN  AltO */
  95.    0x25, 0x26, 0x32, 0x31, 0x18,
  96.  
  97. /* AltP  AltQ  AltR  AltS  AltT */
  98.    0x19, 0x10, 0x13, 0x1f, 0x14,
  99.  
  100. /* AltU  AltV  AltW  AltX  AltY */
  101.    0x16, 0x2f, 0x11, 0x2d, 0x15,
  102.  
  103. /* AltZ */
  104.    0x2c,
  105.  
  106. /* Alt1  Alt2  Alt3  Alt4  Alt5 */
  107.    0x78, 0x79, 0x7a, 0x7b, 0x7c,
  108.  
  109. /* Alt6  Alt7  Alt8  Alt9  Alt0 */
  110.    0x7d, 0x7e, 0x7f, 0x80, 0x81,
  111.  
  112. /* Alt-  Alt= */
  113.    0x82, 0x83,
  114.  
  115. /* ^Left  ^Right */
  116.     0x73,  0x74,
  117.  
  118. /* ^PrtSc */
  119.     0x72,
  120.  
  121. /* ^End   ^Home */
  122.     0x4f,  0x47,
  123.  
  124. /* ^PgDn  ^PgUp */
  125.     0x76,  0x84,
  126.  
  127.     0
  128. /*
  129.  * Add extra keys here but update crtio.h first
  130.  * Cannot remap mouse keys
  131.  * Also there are no scancodes for these
  132. */
  133. };
  134.  
  135.  
  136. /* Unget stuff */
  137. struct unget_struct
  138. {
  139.     short key;
  140.     struct unget_struct *next;
  141. };
  142.  
  143. # define UGNULL (struct unget_struct *)0
  144.  
  145. static struct unget_struct *unget_list = UGNULL;
  146.  
  147. void unget_inchar(key)
  148. short key;
  149. {
  150.     struct unget_struct *new;
  151. # ifdef WDEBUGI
  152.     fprintf(stderr, "unget_inchar(%d)\r\n", key);
  153. # endif
  154.  
  155.     new = (struct unget_struct *)alloc(sizeof(struct unget_struct));
  156.     new->key = key;
  157.  
  158.     if (unget_list == UGNULL)
  159.         unget_list = new;
  160.     else
  161.     {
  162.     struct unget_struct *uptr = unget_list;
  163.  
  164.         while (uptr->next != UGNULL)
  165.             uptr = uptr->next;
  166.  
  167.         uptr->next = new;
  168.     }
  169. }
  170.  
  171. int inchar_hit()
  172. {
  173. # ifdef MSDOS
  174.     return(kbhit());
  175. # else
  176.     return(0);
  177. # endif
  178. }
  179.  
  180. inchar_nowait()
  181. {
  182.     if (unget_list != UGNULL || kbhit())
  183.         return(inchar());
  184.     else
  185.         return(0);
  186. }
  187.  
  188. int inchar()
  189. {
  190. unsigned char scode;
  191. char ch;
  192.  
  193.     /*
  194.      * if unget_ichar was called 
  195.     */
  196.     if (unget_list != UGNULL)
  197.     {
  198.         struct unget_struct *uptr;
  199.         short ret;
  200.  
  201.         uptr = unget_list;
  202.         ret = uptr->key;
  203.         unget_list = uptr->next;
  204.         free(uptr);
  205.         return (ret);
  206.     }
  207.  
  208. retry:
  209.    flushscr();
  210.  
  211. # ifdef MOUSE
  212.     /*
  213.     * mouse support section -  RN, Sept., 1989
  214.     */
  215.     {
  216.     short ret;
  217.  
  218.         mouse_cursor(ON);
  219.         if (ret = check_mouse())
  220.             return(ret);    
  221.     }
  222. # endif
  223.  
  224.     {
  225.     RegStorage;
  226.  
  227.         Regs_ah = 0x00;
  228.         Keyboard();
  229.         ch = Regs_al;
  230.         scode = Regs_ah;
  231.     }
  232.  
  233.     /* Ascii char */
  234.     if (ch > 31)
  235.         return(ch);
  236.  
  237.     /*
  238.      * Right - can't get Ctrl-C
  239.      * so redraw the screen - great for UNIX
  240.     */
  241.     if (ch == 3)
  242.     {
  243. # ifdef WDEBUG
  244.         exitcheck();
  245. # else
  246.         redraws();
  247. # endif
  248.         goto retry;
  249.     }
  250.    /* Function key */
  251.    return(keyval(scode, ch));
  252. }
  253.  
  254. /*
  255.  * Re-maps input char
  256.  * according defines in crtio.h
  257. */
  258. static int keyval(scode, ch)
  259. unsigned char  scode;
  260. char ch;
  261. {
  262.     int index  = 0;    
  263.  
  264.       if (scode && !ch)
  265.     {
  266.     unsigned char *result;
  267.  
  268.         /*
  269.          * find the keyval as defined in crtio.h that corresponds to the scancode
  270.         */
  271.         result = memchr(scancodes, scode, sizeof(scancodes));
  272.         if (result != NULL)
  273.         {
  274.             
  275.             /* Watcom C 70 required this cast ! - Geo */
  276.             index = (int)(result - (unsigned char *)scancodes);
  277.             if (index < K_NULL - _K_Base(1))
  278.             {
  279.             extern short ttykeys[]; /* win.c */
  280.             extern int tty_xlat;
  281.  
  282.                 if (tty_xlat)
  283.                     return(ttykeys[index + 1]);
  284.                 else
  285.                     return(_K_Base(index + 1));
  286.             }
  287.         }
  288.     }    
  289.  
  290.    switch(ch)
  291.    {
  292.       /* And the rest ... */
  293.       case '\033' : return(xlat_val(K_ESC));
  294.       case '\n'   :
  295.       case '\r'   : return(xlat_val(K_CR));
  296.       case '\t'   : return(xlat_val(K_TAB));
  297.       case 127    : return(xlat_val(K_DEL));
  298.       case '\b'   : return(xlat_val(K_BS));
  299.    }
  300.  
  301.     /*
  302.      * Attempt to support control chars
  303.      * any clash with standard '\n' etc.
  304.      * will be resolved in above switch
  305.     *  DC1 & DC3 will never appear ... 
  306.     */
  307.     if (iscntrl(ch))
  308.     {
  309.         /* Stronger test */
  310.         if (ch > 0 && ch <= 26)
  311.             return(xlat_val(K_CTRLA + (ch - 1)));
  312.     }
  313.    return(ch);
  314. }
  315.  
  316. static int xlat_val(ch)
  317. int ch;
  318. {
  319.     extern short ttykeys[]; /* win.c */
  320.     extern int tty_xlat;
  321.  
  322.     if (tty_xlat)
  323.         return(ttykeys[ch - _K_Base(0)]);
  324.     else
  325.         return(ch);
  326. }
  327.  
  328.  
  329. # ifdef WDEBUG
  330. /*
  331.  * treat this routine as an interrupt
  332.  * - go back if you don't know what
  333.  * that means
  334. */
  335. static void exitcheck()
  336. {
  337. static int ctrlc = FALSE;
  338.     
  339.     /* Routine not re-entrant */
  340.     if (ctrlc == TRUE)
  341.         return;
  342.  
  343.     ctrlc = TRUE;
  344.  
  345.     create_w(20, 1, 4, 10);
  346.     set_w(REVVIDBLINK, ' ');
  347.     border_w(0, REVVIDBLINK);
  348.  
  349.     xdisp_w(2, 2, BOLD, "  Quit  ");
  350.     xdisp_w(3, 2, BOLD, "Continue");
  351.  
  352.     if (inchar() == 'q')
  353.     {
  354.         resetscr();
  355.         exit(1);
  356.     }
  357.  
  358.     delete_w();
  359.  
  360.     ctrlc = FALSE;
  361. }
  362. # endif
  363.  
  364.