home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / beav1402.zip / kbd.c < prev    next >
Text File  |  1993-04-16  |  4KB  |  181 lines

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