home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / memacs400_src.lzh / MEMACS400 / SRC / keyboard.c < prev    next >
Text File  |  1996-04-25  |  15KB  |  534 lines

  1. /*===========================================================================
  2.  
  3.         KEYBOARD.C for MSDOS and OS/2
  4.  
  5.         extcode()
  6.  
  7.         This routine is common to the MSDOS and OS/2 implementations.
  8.         It is used to resolve extended character codes from the keyboard
  9.         into EMACS printable character specifications.
  10.  
  11.         This implementation can handle the extended AT-style keyboard
  12.         if one is fitted.
  13.  
  14.         I don't know what happens on an XT but I suspect that it should
  15.         work as if there were no extended keys.
  16.  
  17.                                 Jon Saxton
  18.                                 24 Jan 1990
  19.  
  20. ===========================================================================*/  
  21.  
  22. #include    <stdio.h>
  23. #include    "estruct.h"
  24. #include    "eproto.h"
  25. #include    "edef.h"
  26. #include    "elang.h"
  27.  
  28. #if MSDOS | OS2
  29. #if HP150 == 0
  30.  
  31. int extcode(c)
  32.  
  33. unsigned c;    /* byte following a zero extended char byte */
  34.         /* High order normally contains 0x00 but may
  35.            contain 0xE0 if an AT-style keyboard is attached */
  36. {
  37.     /* function keys 1 through 9.  High-order will be 0x00 */
  38.     if (c >= 0x3B && c < 0x44)
  39.         return(SPEC | c - 0x3A + '0');
  40.  
  41.     /* function key 10 */
  42.     if (c == 0x44)
  43.         return(SPEC | '0');
  44.  
  45.     /* shifted function keys */
  46.     if (c >= 0x54 && c < 0x5D)
  47.         return(SPEC | SHFT | c - 0x53 + '0');
  48.     if (c == 0x5D)
  49.         return(SPEC | SHFT | '0');
  50.  
  51.     /* control function keys */
  52.     if (c >= 0x5E && c < 0x67)
  53.         return(SPEC | CTRL | c - 0x5D + '0');
  54.     if (c == 0x67)
  55.         return(SPEC | CTRL | '0');
  56.  
  57.     /* ALTed function keys */
  58.     if (c >= 0x68 && c < 0x71)
  59.         return(SPEC | ALTD | c - 0x67 + '0');
  60.     if (c == 0x71)
  61.         return(SPEC | ALTD | '0');
  62.  
  63.     /* ALTed number keys */
  64.     /* This doesn't work for DOS or OS/2.  Using ALT in conjunction with
  65.        the number keys lets you enter any (decimal) character value
  66.        you want.  It is therefore commented out.
  67.  
  68.        Wrongo joker... alting the top row of numbers works fine DML */
  69.     if (c >= 0x78 && c < 0x81)
  70.         return(ALTD | c - 0x77 + '0');
  71.     if (c == 0x81)
  72.         return(ALTD | '0');
  73.  
  74.     /* some others as well */
  75.     switch (c) {
  76.  
  77. case 3:        return(0);            /* null */
  78. case 0x0F:    return(SHFT | CTRL | 'I');    /* backtab */
  79.  
  80. case 0x10:    return(ALTD | 'Q');
  81. case 0x11:    return(ALTD | 'W');
  82. case 0x12:    return(ALTD | 'E');
  83. case 0x13:    return(ALTD | 'R');
  84. case 0x14:    return(ALTD | 'T');
  85. case 0x15:    return(ALTD | 'Y');
  86. case 0x16:    return(ALTD | 'U');
  87. case 0x17:    return(ALTD | 'I');
  88. case 0x18:    return(ALTD | 'O');
  89. case 0x19:    return(ALTD | 'P');
  90.  
  91. case 0x1E:    return(ALTD | 'A');
  92. case 0x1F:    return(ALTD | 'S');
  93. case 0x20:    return(ALTD | 'D');
  94. case 0x21:    return(ALTD | 'F');
  95. case 0x22:    return(ALTD | 'G');
  96. case 0x23:    return(ALTD | 'H');
  97. case 0x24:    return(ALTD | 'J');
  98. case 0x25:    return(ALTD | 'K');
  99. case 0x26:    return(ALTD | 'L');
  100.  
  101. case 0x2C:    return(ALTD | 'Z');
  102. case 0x2D:    return(ALTD | 'X');
  103. case 0x2E:    return(ALTD | 'C');
  104. case 0x2F:    return(ALTD | 'V');
  105. case 0x30:    return(ALTD | 'B');
  106. case 0x31:    return(ALTD | 'N');
  107. case 0x32:    return(ALTD | 'M');
  108.  
  109. case 0x47:    return(SPEC | '<');    /* home */
  110. case 0x48:    return(SPEC | 'P');    /* cursor up */
  111. case 0x49:    return(SPEC | 'Z');    /* page up */
  112. case 0x4B:    return(SPEC | 'B');    /* cursor left */
  113. case 0x4C:    return(SPEC | 'L');    /* NP 5 */
  114. case 0x4D:    return(SPEC | 'F');    /* cursor right */
  115. case 0x4F:    return(SPEC | '>');    /* end */
  116. case 0x50:    return(SPEC | 'N');    /* cursor down */
  117. case 0x51:    return(SPEC | 'V');    /* page down */
  118. case 0x52:    return(SPEC | 'C');    /* insert */
  119. case 0x53:    return(SPEC | 'D');    /* delete */
  120.  
  121. case 0x73:    return(SPEC | CTRL | 'B');    /* control left */
  122. case 0x74:    return(SPEC | CTRL | 'F');    /* control right */
  123. case 0x75:    return(SPEC | CTRL | '>');    /* control end */
  124. case 0x76:    return(SPEC | CTRL | 'V');    /* control page down */
  125. case 0x77:    return(SPEC | CTRL | '<');    /* control home */
  126. case 0x84:    return(SPEC | CTRL | 'Z');    /* control page up */
  127. case 0x8D:    return(SPEC | CTRL | 'P');    /* control up */
  128. case 0x8F:    return(SPEC | CTRL | 'L');    /* control NP5 */
  129. case 0x91:    return(SPEC | CTRL | 'N');    /* control down */
  130. case 0x92:    return(SPEC | CTRL | 'C');    /* control grey insert */
  131. case 0x93:    return(SPEC | CTRL | 'D');    /* control grey delete */
  132.  
  133. case 0x82:    return(ALTD | '-');    /* alt - */
  134. case 0x83:    return(ALTD | '=');    /* alt = */
  135. case 0x27:    return(ALTD | ';');    /* alt ; */
  136. case 0x28:    return(ALTD | '\'');    /* alt ' */
  137. case 0x2B:    return(ALTD | '\\');    /* alt \ */
  138. case 0x1A:    return(ALTD | '[');    /* alt [ */
  139. case 0x1B:    return(ALTD | ']');    /* alt ] */
  140.  
  141. #if ATKBD | OS2
  142.  
  143. /* F11 and F12 */
  144.  
  145. case 0x85:    return(SPEC | '-');         /* F11 */
  146. case 0x86:    return(SPEC | '=');        /* F12 */
  147. case 0x87:    return(SPEC | SHFT | '-');    /* shift F11 */
  148. case 0x88:    return(SPEC | SHFT | '=');    /* shift F12 */
  149. case 0x89:    return(SPEC | CTRL | '-');    /* control F11 */
  150. case 0x8A:    return(SPEC | CTRL | '=');    /* control F12 */
  151. case 0x8B:    return(SPEC | ALTD | '-');    /* alt F11 */
  152. case 0x8C:    return(SPEC | ALTD | '=');    /* alt F12 */
  153.  
  154. /*
  155.    This implementation distinguishes between the cursor controls on the
  156.    number pad and those on the grey keypad if an AT-style keyboard is
  157.    fitted.
  158. */ 
  159.  
  160. case 0xE047:    return(SPEC | 'a');        /* grey home */
  161. case 0xE048:    return(SPEC | 'b');        /* grey cursor up */
  162. case 0xE049:    return(SPEC | 'c');        /* grey page up */
  163. case 0xE04B:    return(SPEC | 'd');        /* grey cursor left */
  164. case 0xE04C:    return(SPEC | 'e');        /* grey center key */
  165. case 0xE04D:    return(SPEC | 'f');        /* grey cursor right */
  166. case 0xE04F:    return(SPEC | 'g');        /* grey end */
  167. case 0xE050:    return(SPEC | 'h');        /* grey cursor down */
  168. case 0xE051:    return(SPEC | 'i');        /* grey page down */
  169. case 0xE052:    return(SPEC | 'j');        /* grey insert */
  170. case 0xE053:    return(SPEC | 'k');        /* grey delete */
  171.  
  172. case 0xE077:    return(SPEC | CTRL | 'a');    /* control grey home */
  173. case 0xE08D:    return(SPEC | CTRL | 'b');    /* control grey up */
  174. case 0xE084:    return(SPEC | CTRL | 'c');    /* control grey page up */
  175. case 0xE073:    return(SPEC | CTRL | 'd');    /* control grey left */
  176. case 0xE074:    return(SPEC | CTRL | 'f');    /* control grey right */
  177. case 0xE075:    return(SPEC | CTRL | 'g');    /* control grey end */
  178. case 0xE091:    return(SPEC | CTRL | 'h');    /* control grey down */
  179. case 0xE076:    return(SPEC | CTRL | 'i');    /* control grey page down */
  180. case 0xE092:    return(SPEC | CTRL | 'j');    /* control grey insert */
  181. case 0xE093:    return(SPEC | CTRL | 'k');    /* control grey delete */
  182.  
  183. case 0xE097:    return(SPEC | ALTD | 'a');    /* alt grey home */
  184. case 0xE098:    return(SPEC | ALTD | 'b');    /* alt grey cursor up */
  185. case 0xE099:    return(SPEC | ALTD | 'c');    /* alt grey page up */
  186. case 0xE09B:    return(SPEC | ALTD | 'd');    /* alt grey cursor left */
  187. case 0xE09D:    return(SPEC | ALTD | 'f');    /* alt grey cursor right */
  188. case 0xE09F:    return(SPEC | ALTD | 'g');    /* alt grey end */
  189. case 0xE0A0:    return(SPEC | ALTD | 'h');    /* alt grey cursor down */
  190. case 0xE0A1:    return(SPEC | ALTD | 'i');    /* alt grey page down */
  191. case 0xE0A2:    return(SPEC | ALTD | 'j');    /* alt grey insert */
  192. case 0xE0A3:    return(SPEC | ALTD | 'k');    /* alt grey delete */
  193.  
  194. case 0x97:    return(SPEC | ALTD | 'a');    /* alt grey home */
  195. case 0x98:    return(SPEC | ALTD | 'b');    /* alt grey cursor up */
  196. case 0x99:    return(SPEC | ALTD | 'c');    /* alt grey page up */
  197. case 0x9B:    return(SPEC | ALTD | 'd');    /* alt grey cursor left */
  198. case 0x9D:    return(SPEC | ALTD | 'f');    /* alt grey cursor right */
  199. case 0x9F:    return(SPEC | ALTD | 'g');    /* alt grey end */
  200. case 0xA0:    return(SPEC | ALTD | 'h');    /* alt grey cursor down */
  201. case 0xA1:    return(SPEC | ALTD | 'i');    /* alt grey page down */
  202. case 0xA2:    return(SPEC | ALTD | 'j');    /* alt grey insert */
  203. case 0xA3:    return(SPEC | ALTD | 'k');    /* alt grey delete */
  204.  
  205. case 0xA6:    return(SPEC | ALTD | 'l');    /* alt grey enter */
  206. case 0xA4:    return(SPEC | ALTD | '/');    /* alt grey / */
  207. case 0x37:    return(SPEC | ALTD | '*');    /* alt grey * */
  208. case 0x4A:    return(SPEC | ALTD | '-');    /* alt grey - */
  209. case 0x4E:    return(SPEC | ALTD | '+');    /* alt grey + */
  210.  
  211. case 0x95:    return(SPEC | CTRL | '/');    /* ctrl grey / */
  212. case 0x96:    return(SPEC | CTRL | '*');    /* ctrl grey * */
  213. case 0x8E:    return(SPEC | CTRL | '-');    /* ctrl grey - */
  214. case 0x90:    return(SPEC | CTRL | '+');    /* ctrl grey + */
  215.  
  216. #endif
  217.  
  218.     }
  219. /* printf("[ALT %d] ", c); */
  220.  
  221.     return(ALTD | c);
  222. }
  223.  
  224. #endif
  225. #endif
  226.  
  227. #if BSD || FREEBSD || USG || AIX || AUX || SMOS || HPUX8 || HPUX9 || SUN || XENIX || (AVVION || TERMIOS) || (VMS && SMG) || MPE
  228.  
  229. #define NKEYSEQ        300        /* Number of keymap entries    */
  230.  
  231. typedef struct keyent {            /* Key mapping entry        */
  232.     struct keyent *samlvl;        /* Character on same level    */
  233.     struct keyent *nxtlvl;        /* Character on next level    */
  234.     unsigned char ch;        /* Character            */
  235.     int code;            /* Resulting keycode        */
  236. } KEYENT;
  237.  
  238. /* Needed Prototype */
  239. #if    PROTO
  240. extern int PASCAL NEAR rec_seq(char *buf, char *bufstart, KEYENT *node);
  241. #else
  242. extern int PASCAL NEAR rec_seq();
  243. #endif
  244.  
  245. /* some globals needed here */
  246. static unsigned char keyseq[256];    /* Prefix escape sequence table    */
  247. static KEYENT keymap[NKEYSEQ];        /* Key map            */
  248. static KEYENT *nxtkey = keymap;        /* Next free key entry        */
  249. static BUFFER *seqbuf;            /* For the pop-up buffer    */
  250.  
  251. /*
  252.  * add-keymap "escape sequence" keyname
  253.  */
  254. #if PROTO
  255. int PASCAL NEAR addkeymap(int f, int n)
  256. #else
  257. int PASCAL NEAR addkeymap( f, n)
  258. int f;
  259. int n;
  260. #endif
  261. {
  262.     int c, ec;
  263.     int idx, col;
  264.     char esc_seq[NSTRING];    /* escape sequence to cook */
  265.     char codeseq[NSTRING];    /* fn key name */
  266.  
  267.     memset(esc_seq, '\0', NSTRING);
  268.  
  269.     if (clexec == TRUE) {
  270.         if (mlreply(NULL, esc_seq, NSTRING) != TRUE) {
  271.             TTbeep();
  272.             return FALSE;
  273.         }
  274.     }
  275.     else {
  276.         /* get the key sequence */
  277.         mlwrite(": add-keymap ");
  278.         col = strlen(": add-keymap ");
  279.         idx = 0;
  280.         for (;;) {
  281.             c = tgetc();
  282.             if ((ec = ctoec(c)) == abortc) {
  283.                 TTbeep();
  284.                 return FALSE;
  285.             } else if (c == '\r') {
  286.                 break;
  287.             } else if (ec == quotec) {
  288.                 c = tgetc();
  289.             }
  290.             esc_seq[idx++] = c;
  291.             movecursor(term.t_nrow, col);    /* Position the cursor    */
  292.             col += echochar(c);
  293.         }
  294.  
  295.         ostring(" ");
  296.     }
  297.  
  298.     if (mlreply(NULL, codeseq, NSTRING) != TRUE) {    /* find the key name (e.g., S-FN#) */
  299.         TTbeep();
  300.         return FALSE;
  301.     }
  302.  
  303.     ec = stock(codeseq);
  304.  
  305.     ostring(codeseq);
  306.     return (addkey(esc_seq, ec));    /* Add to tree */
  307. }
  308.  
  309. /*
  310.  * list-keymappings
  311.  */
  312. #if PROTO
  313. int PASCAL NEAR listkeymaps(int f, int n)
  314. #else
  315. int PASCAL NEAR listkeymaps( f, n)
  316. int f;
  317. int n;
  318. #endif
  319. {
  320.     char outseq[NSTRING];    /* output buffer for key sequence */
  321.  
  322.     /*
  323.      * Get a buffer for it.
  324.      */
  325.     seqbuf = bfind("Key sequence list", TRUE, BFINVS);
  326. /*           "Key sequence list" */
  327.  
  328.     if (seqbuf == NULL || bclear(seqbuf) == FALSE) {
  329.         mlwrite("Cannot display key sequences list");
  330. /*            "Cannot display key sequences list" */
  331.         return(FALSE);
  332.     }
  333.  
  334.     /*
  335.      * Build the list, pop it if all went well.
  336.      */
  337.     *outseq = '"';
  338.     if (rec_seq(outseq + 1, outseq, keymap) == TRUE) {
  339.         wpopup(seqbuf);
  340.         mlerase();
  341.         return(TRUE);
  342.     }
  343.     return FALSE;
  344. }
  345.  
  346. /*
  347.  * recursively track through the tree, finding the escape sequences
  348.  * and their function name equivalents.
  349.  */
  350. #if PROTO
  351. int PASCAL NEAR rec_seq(char *buf, char *bufstart, KEYENT *node)
  352. #else
  353. int PASCAL NEAR rec_seq( buf, bufstart, node)
  354. char *buf;
  355. char *bufstart;
  356. KEYENT *node;
  357. #endif
  358. {
  359.     if (node == NULL)
  360.         return TRUE;
  361.  
  362.     *buf = node->ch;
  363.  
  364.     if (node->nxtlvl == NULL) {
  365.         *(buf + 1) = '"';
  366.         *(buf + 2) = '\0';
  367.         pad(bufstart, 20);
  368.         cmdstr(node->code, bufstart + 20);
  369.         if (addline(seqbuf, bufstart) != TRUE)
  370.             return FALSE;
  371.     }
  372.     else if (rec_seq(buf + 1, bufstart, node->nxtlvl) != TRUE)
  373.         return FALSE;
  374.  
  375.     return (rec_seq(buf, bufstart, node->samlvl));
  376. }
  377.  
  378. /*
  379.  *  addkey  -  Add key to key map
  380.  *
  381.  *  Adds a new escape sequence to the sequence table.
  382.  *  I am not going to try to explain this table to you in detail.
  383.  *  However, in short, it creates a tree which can easily be transversed
  384.  *  to see if input is in a sequence which can be translated to a
  385.  *  function key (arrows and find/select/do etc. are treated like
  386.  *  function keys).  If the sequence is ambiguous or duplicated,
  387.  *  it is silently ignored.
  388.  *
  389.  *  Replaces code in SMG.C, MPE.C, POSIX.C, and UNIX.C
  390.  *  Nothing returned
  391.  *
  392.  *  seq - character sequence
  393.  *  fn  - Resulting keycode
  394.  */
  395. #if PROTO
  396. int PASCAL NEAR addkey(unsigned char * seq, int fn)
  397. #else
  398. int PASCAL NEAR addkey( seq, fn)
  399. unsigned char * seq;
  400. int fn;
  401. #endif
  402. {
  403.     int first;
  404.     KEYENT *cur, *nxtcur;
  405.  
  406.     /* Skip on null sequences or single character sequences. */
  407.     if (seq == NULL || strlen(seq) < 2)
  408.         return FALSE;
  409.  
  410.  
  411.     /* If no keys defined, go directly to insert mode */
  412.     first = 1;
  413.     if (nxtkey != keymap) {
  414.  
  415.         /* Start at top of key map */
  416.         cur = keymap;
  417.  
  418.         /* Loop until matches are exhausted */
  419.         while (*seq) {
  420.  
  421.             /* Do we match current character */
  422.             if (*seq == cur->ch) {
  423.  
  424.                 /* Advance to next level */
  425.                 seq++;
  426.                 cur = cur->nxtlvl;
  427.                 first = 0;
  428.             } else {
  429.  
  430.                 /* Try next character on same level */
  431.                 nxtcur = cur->samlvl;
  432.  
  433.                 /* Stop if no more */
  434.                 if (nxtcur)
  435.                     cur = nxtcur;
  436.                 else
  437.                     break;
  438.             }
  439.         }
  440.     }
  441.  
  442.     /* Check for room in keymap */
  443.     if (strlen(seq) > NKEYSEQ - (nxtkey - keymap)) {
  444.         mlwrite("No more room for key entries.");
  445.         return FALSE;
  446.     }
  447.  
  448.     /* If first character in sequence is inserted, add to prefix table */
  449.     if (first)
  450.         keyseq[*seq] = 1;
  451.  
  452.     /* If characters are left over, insert them into list */
  453.     for (first = 1; *seq; first = 0) {
  454.  
  455.         /* Make new entry */
  456.         nxtkey->ch = *seq++;
  457.         nxtkey->code = fn;
  458.  
  459.         /* If root, nothing to do */
  460.         if (nxtkey != keymap) {
  461.  
  462.             /* Set first to samlvl, others to nxtlvl */
  463.             if (first)
  464.                 cur->samlvl = nxtkey;
  465.             else
  466.                 cur->nxtlvl = nxtkey;
  467.         }
  468.  
  469.         /* Advance to next key */
  470.         cur = nxtkey++;
  471.     }
  472.     return TRUE;
  473. }
  474.  
  475. /*
  476.  * Cook input characters, using the key sequences stored by addkey().
  477.  *
  478.  * To use, we need a grabwait(), grabnowait(), qin() and qrep() function.
  479.  */
  480. #define TIMEOUT    255
  481. VOID cook()
  482. {
  483.     register unsigned char ch;
  484.     KEYENT *cur;
  485.  
  486.     qin(ch = grabwait());    /* Get first character untimed */
  487.  
  488.     /*
  489.      * Skip if the key isn't a special leading escape sequence.
  490.      */
  491.     if (keyseq[ch] == 0) {
  492.         /*
  493.          * But if it is a '\0', make it a (0/1/32).
  494.          */
  495.         if (ch == 0) {
  496.             qin(CTRL >> 8);    /* control */
  497.             qin(32);    /* space */
  498.         }
  499.         return;
  500.     }
  501.  
  502.     /* Start at root of keymap */
  503.     cur = keymap;
  504.  
  505.     /* Loop until keymap exhausts */
  506.     while (cur) {
  507.  
  508.         /* Did we find a matching character */
  509.         if (cur->ch == ch) {
  510.  
  511.             /* Is this the end */
  512.             if (cur->nxtlvl == NULL) {
  513.                 /* Replace all characters with a new sequence */
  514.                 qrep(cur->code);
  515.                 return;
  516.             } else {
  517.                 /* Advance to next level */
  518.                 cur = cur->nxtlvl;
  519.  
  520.                 /* Get next character, timed */
  521.                 ch = grabnowait();
  522.                 if (ch == TIMEOUT)
  523.                     return;
  524.  
  525.                 /* Queue character */
  526.                 qin(ch);
  527.             }
  528.         } else
  529.             /* Try next character on same level */
  530.             cur = cur->samlvl;
  531.     }
  532. }
  533. #endif
  534.