home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2872 / kbd.c < prev    next >
C/C++ Source or Header  |  1991-02-27  |  4KB  |  182 lines

  1. /*                      KBD.C
  2. *       Terminal independent keyboard handling.
  3. */
  4. #define LINT_ARGS   1           /* enable lint type checking */
  5. #include    "def.h"
  6.  
  7. char    *keystrings ();
  8.  
  9. extern    char    MSG_tab[];
  10. extern    char    MSG_ret[];
  11. extern    char    MSG_bksp[];
  12. extern    char    MSG_space[];
  13. extern    char    MSG_rubout[];
  14.  
  15. #include    "lintfunc.dec"
  16. /*
  17. * Read in a key, doing the terminal
  18. * independent prefix handling. The terminal specific
  19. * "getkbd" routine gets the first swing, and may return
  20. * one of the special codes used by the special keys
  21. * on the keyboard. The "getkbd" routine returns the
  22. * C0 controls as received; this routine moves them to
  23. * the right spot in 11 bit code.
  24. */
  25. int     getkey ()
  26. {
  27.  
  28.     register int    c;
  29.     c = getkbd ();
  30.     if (c == METACH)            /* M-           */
  31.         c = KMETA | getctl ();
  32.     else if (c == CTRLCH)        /* C-           */
  33.         c = KCTRL | getctl ();
  34.     else if (c == CTMECH)    /* C-M-         */
  35.         c = KCTRL | KMETA | getctl ();
  36.     else if (c >= 0x00 && c <= 0x1F)/* Relocate control.    */
  37.         c = KCTRL | (c + '@');
  38.  
  39.     if (c == (KCTRL | 'X'))     /* C-X          */
  40.         c = KCTLX | getctl ();
  41.     return (c);
  42. }
  43.  
  44.  
  45. /*
  46. * Used above.
  47. */
  48. int     getctl ()
  49. {
  50.  
  51.     register int    c;
  52.  
  53. #if 1
  54.     c = getkbd ();
  55.     if (c == METACH)            /* M-           */
  56.         c = KMETA | getctl ();
  57.     else
  58.         if (c == CTRLCH)        /* C-           */
  59.             c = KCTRL | getctl ();
  60.         else
  61.             if (c == CTMECH)    /* C-M-         */
  62.                 c = KCTRL | KMETA | getctl ();
  63.             else
  64.                 if (c >= 0x00 && c <= 0x1F)/* Relocate control.    */
  65.                     c = KCTRL | (c + '@');
  66. #else
  67.     c = getkey ();              /* Note recursion   */
  68.     if (ISLOWER (c & 0xFF))
  69.         c = (c & ~0xFF) | TOUPPER (c & 0xFF);
  70.     if (c >= 0x00 && c <= 0x1F) /* Relocate control.    */
  71.         c = KCTRL | (c + '@');
  72. #endif
  73.     if (ISLOWER (c & 0xFF))
  74.         c = (c & ~0xFF) | TOUPPER (c & 0xFF);
  75.     return (c);
  76. }
  77.  
  78.  
  79. /*
  80. * Transform a key code into a name,
  81. * using a table for the special keys and combination
  82. * of some hard code and some general processing for
  83. * the rest. None of this code is terminal specific any
  84. * more. This makes adding keys easier.
  85. */
  86. void keyname (cp, k)
  87. register char  *cp;
  88. register int    k;
  89. {
  90.     register char  *np;
  91.     char    nbuf[3];
  92.  
  93.     static char hex[] =
  94.     {
  95.         '0', '1', '2', '3',
  96.         '4', '5', '6', '7',
  97.         '8', '9', 'A', 'B',
  98.         'C', 'D', 'E', 'F'
  99.     };
  100.  
  101.     if (k & 0x80)
  102.         {
  103.         if ((np = keystrings (k)) != NULL)
  104.             {
  105.             if ((k & KMETA) != 0)
  106.                 {
  107.                 *cp++ = 'E';
  108.                 *cp++ = 's';
  109.                 *cp++ = 'c';
  110.                 *cp++ = ' ';
  111.                 }
  112.             strcpy (cp, np);
  113.             }
  114.         else
  115.             *cp = 0;    /* null string */
  116.         return;
  117.         }
  118.  
  119.     if ((k & KCTLX) != 0)
  120.         {
  121.         /* Ctl-X prefix.      */
  122.         *cp++ = 'C';
  123.         *cp++ = 't';
  124.         *cp++ = 'l';
  125.         *cp++ = '-';
  126.         *cp++ = 'X';
  127.         *cp++ = ' ';
  128.         k &= ~KCTLX;
  129.         }
  130.  
  131.     if ((k & KMETA) != 0)
  132.         {
  133.         /* Add Esc- mark.     */
  134.         *cp++ = 'E';
  135.         *cp++ = 's';
  136.         *cp++ = 'c';
  137.         *cp++ = ' ';
  138.         k &= ~KMETA;
  139.         }
  140.  
  141.     if (k == (KCTRL | 'I'))/* Some specials.   */
  142.         np = MSG_tab;
  143.     else
  144.         {
  145.         if (k == (KCTRL | 'M'))
  146.             np = MSG_ret;
  147.         else if (k == (KCTRL | 'H'))
  148.             np = MSG_bksp;
  149.         else if (k == ' ')
  150.             np = MSG_space;
  151.         else if (k == 0x7F)
  152.             np = MSG_rubout;
  153.         else
  154.             {
  155.             if ((k & KCTRL) != 0)
  156.                 {
  157.                 /* Add Ctl- mark.     */
  158.                 *cp++ = 'C';
  159.                 *cp++ = 't';
  160.                 *cp++ = 'l';
  161.                 *cp++ = '-';
  162.                 }
  163.             np = &nbuf[0];
  164.             if (((k & KCHAR) >= 0x20 && (k & KCHAR) <= 0x7E)
  165.                  || ((k & KCHAR) >= 0xA0 && (k & KCHAR) <= 0xFE))
  166.                 {
  167.                 nbuf[0] = k & KCHAR;/* Graphic.     */
  168.                 nbuf[1] = 0;
  169.                 }
  170.             else
  171.                 {
  172.                 /* Non graphic.     */
  173.                 nbuf[0] = hex[(k >> 4) & 0x0F];
  174.                 nbuf[1] = hex[k & 0x0F];
  175.                 nbuf[2] = 0;
  176.                 }
  177.             }
  178.         }
  179.     strcpy (cp, np);
  180. }
  181.  
  182.