home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d131 / mg1b.lha / Mg1b / Source / kbd.c < prev    next >
C/C++ Source or Header  |  1988-03-14  |  5KB  |  216 lines

  1. /*
  2.  *        Terminal independent keyboard handling.
  3.  */
  4. #include    "def.h"
  5.  
  6. static mapin();
  7.  
  8. #ifdef    DPROMPT
  9. #define    PROMPTL    80
  10.   char    prompt[PROMPTL], *promptp;
  11. #endif
  12.  
  13. /*
  14.  * All input from the user (should!) go through
  15.  * getkey. Quotep is true to get raw keys, false to
  16.  * get 11-bit code keys.
  17.  * Getkey is responsible for putting keystrokes away
  18.  * for macros. It also takes keystrokes out of the macro,
  19.  * though other input routines will can also do this.
  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.  * If the KPROMPT bit in the flags is set and DPROMPT is
  28.  * defined, do delayed prompting.  (dprompt routine is 
  29.  * in sys/???/ttyio.c)
  30.  */
  31. KEY
  32. getkey(f) register int f; {
  33.     register KEY    c;
  34.     KEY        keychar();
  35.     int        getkbd(), ttgetc();
  36.  
  37.     if (kbdmop != NULL) return *kbdmop++;
  38. #ifdef    DPROMPT
  39.     if(!(f&KPROMPT)) prompt[0] = '\0';
  40. #endif
  41.     c = (KEY) mapin(getkbd);
  42. #ifdef    DPROMPT
  43.     if(f&KPROMPT) {
  44.         if(promptp > prompt) *(promptp-1) = ' ';
  45.         if(promptp >= &prompt[PROMPTL-8]) f &= ~KPROMPT;
  46.                 /* must have a lot of prefixes.... */
  47.     }
  48. #endif
  49.     if ((f&KQUOTE) == 0) {
  50. #ifdef    DO_METAKEY
  51.         if ((c & ~KCHAR) == KCTRL)    /* Function key        */
  52.             c = ((char) c) & KCHAR;/* remove wrapping    */
  53.         else if ((c >= 0x80) && (c <= 0xFF)) { /* real meta key    */
  54.             c = ((char) c) & 0x7f;    /* Get the key and mode map it */
  55.             if ((mode&MFLOW) != 0)
  56.                 if (c == CCHR('^')) c = CCHR('Q');
  57.                 else if (c == CCHR('\\')) c = CCHR('S');
  58.             if ((mode&MBSMAP) != 0)
  59.                 if (c == CCHR('H')) c = 0x7f;
  60.                 else if (c == 0x7f) c = CCHR('H');
  61.             c = KMETA | keychar(c & ~0x80, TRUE);
  62.         }
  63. #endif
  64. #ifdef    DPROMPT
  65.         if(f&KPROMPT) {
  66.             keyname(promptp, (c<=0x1F && c>=0x00)?KCTRL|(c+'@'):c);
  67.             strcat(promptp, "-");
  68.         }
  69. #endif
  70.         if (c == METACH)        /* M-            */
  71.             c = KMETA | keychar(mapin(ttgetc), TRUE);
  72.         else if (c == CTRLCH)        /* C-            */
  73.             c = KCTRL | keychar(mapin(ttgetc), TRUE);
  74.         else if (c == CTMECH)        /* C-M-            */
  75.             c = KCTRL | KMETA | keychar(mapin(ttgetc), TRUE);
  76.         else if (c<=0x1F && c>=0x00)    /* Relocate control.    */
  77.             c = KCTRL | (c+'@');
  78.         if (c == (KCTRL|'X'))        /* C-X            */
  79.             c = KCTLX | keychar(mapin(ttgetc), TRUE);
  80.     }
  81.  
  82.     if ((f&KNOMAC) == 0 && kbdmip != NULL) {
  83.         if (kbdmip+1 > &kbdm[NKBDM-3]) {    /* macro overflow */
  84.             (VOID) ctrlg(FALSE, 0, KRANDOM);
  85.             ewprintf("Keyboard macro overflow");
  86.             ttflush();
  87.             return (KCTRL|'G');        /* ^G it for us    */
  88.         }
  89.         *kbdmip++ = c;
  90.     }
  91. #ifdef    DPROMPT
  92.     if(f&KPROMPT) {
  93.         keyname(promptp, c);
  94.         promptp += strlen(promptp);
  95.         *promptp++ = '-';
  96.         *promptp = '\0';
  97.     }
  98. #endif
  99.     return (c);
  100. }
  101.  
  102. /*
  103.  * go get a key, and run it through whatever mapping the modes
  104.  * specify.
  105.  */
  106. static mapin(get) int (*get)(); {
  107.     register int    c;
  108.  
  109. #ifdef    DPROMPT
  110.     if(prompt[0]!='\0' && ttwait()) {
  111.         ewprintf("%s", prompt);        /* avoid problems with % */
  112.         update();            /* put the cursor back     */
  113.         epresf = KPROMPT;
  114.     }
  115. #endif
  116.     c = (*get)();
  117.     if ((mode&MFLOW) != 0) {
  118.         while (c == CCHR('S') || c == CCHR('Q'))
  119.             c = (*get)();
  120.         if (c == CCHR('^')) c = CCHR('Q');
  121.         else if (c == CCHR('\\')) c = CCHR('S');
  122.     }
  123.     if ((mode&MBSMAP) != 0)
  124.         if (c == CCHR('H')) c = 0x7f;
  125.         else if (c == 0x7f) c = CCHR('H');
  126.     return c;
  127. }
  128. /*
  129.  * Transform a key code into a name,
  130.  * using a table for the special keys and combination
  131.  * of some hard code and some general processing for
  132.  * the rest. None of this code is terminal specific any
  133.  * more. This makes adding keys easier.
  134.  */
  135. VOID
  136. keyname(cp, k) register char *cp; register int k; {
  137.     register char    *np;
  138.     register int    c;
  139.     char        nbuf[3];
  140.  
  141.     static    char    hex[] = {
  142.         '0',    '1',    '2',    '3',
  143.         '4',    '5',    '6',    '7',
  144.         '8',    '9',    'A',    'B',
  145.         'C',    'D',    'E',    'F'
  146.     };
  147.  
  148.     if ((k&KCTLX) != 0) {            /* C-X prefix.        */
  149.         *cp++ = 'C';
  150.         *cp++ = '-';
  151.         *cp++ = 'x';
  152.         *cp++ = ' ';
  153.     }
  154.     if ((k&KMETA) != 0) {            /* Add M- mark.        */
  155.         *cp++ = 'E';
  156.         *cp++ = 'S';
  157.         *cp++ = 'C';
  158.         *cp++ = ' ';
  159.     }
  160.     if ((k&KCHAR)>=KFIRST && (k&KCHAR)<=KLAST) {
  161.         if ((np=keystrings[(k&KCHAR)-KFIRST]) != NULL) {
  162.             if ((k&KCTRL) != 0) {
  163.                 *cp++ = 'C';
  164.                 *cp++ = '-';
  165.             }
  166.             (VOID) strcpy(cp, np);
  167.             return;
  168.         }
  169.     }
  170.     c = k & ~(KMETA|KCTLX);
  171.     if (c == (KCTRL|'I'))    /* Some specials.    */
  172.         np = "TAB";
  173.     else if (c == (KCTRL|'M'))
  174.         np = "RET";
  175.     else if (c == (KCTRL|'J'))
  176.         np = "LFD";
  177.     else if (c == ' ')
  178.         np = "SPC";
  179.     else if (c == 0x7F)
  180.         np = "DEL";
  181.     else if (c == (KCTRL|'['))
  182.         np = "ESC";
  183.     else {
  184.         if ((k&KCTRL) != 0) {        /* Add C- mark.        */
  185.             *cp++ = 'C';
  186.             *cp++ = '-';
  187.         }
  188.         if ((k&(KCTRL|KMETA|KCTLX)) != 0 && ISUPPER(k&KCHAR) != FALSE)
  189.             k = TOLOWER(k&KCHAR);
  190.         np = &nbuf[0];
  191.         if (((k&KCHAR)>=0x20 && (k&KCHAR)<=0x7E)
  192.         ||  ((k&KCHAR)>=0xA0 && (k&KCHAR)<=0xFE)) {
  193.             nbuf[0] = k&KCHAR;    /* Graphic.        */
  194.             nbuf[1] = 0;
  195.         } else {            /* Non graphic.        */
  196.             nbuf[0] = hex[(k>>4)&0x0F];
  197.             nbuf[1] = hex[k&0x0F];
  198.             nbuf[2] = 0;
  199.         }
  200.     }
  201.     (VOID) strcpy(cp, np);
  202. }
  203.  
  204. /*
  205.  * turn a key into an internal char.
  206.  */
  207. KEY
  208. keychar(c, f) register int c, f; {
  209.  
  210.     if (f == TRUE && ISLOWER(c) != FALSE)
  211.         c = TOUPPER(c);
  212.     else if (c>=0x00 && c<=0x1F)        /* Relocate control.    */
  213.         c = (KCTRL|(c+'@'));
  214.     return (KEY) c;
  215. }
  216.