home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / Dme / src / keyboard.c < prev    next >
C/C++ Source or Header  |  1989-11-27  |  14KB  |  628 lines

  1.  
  2. /*
  3.  *  KEYBOARD.C
  4.  *
  5.  *    (C)Copyright 1987 by Matthew Dillon
  6.  *
  7.  *  Handle keyboard related stuff such as keyboard mappings.  Every time
  8.  *  a key is pressed, KEYCTL() is called with the code.  KEYCTL() remembers
  9.  *  which qualifier keys are currently held down, and when a non-qualifier
  10.  *  key is pressed finds the hash entry for the key.  If no hash entry
  11.  *  exists (e.g. you type a normal 'a') the default keymap is used.
  12.  */
  13.  
  14. #include "defs.h"
  15. #include <stdio.h>
  16.  
  17. extern ubyte *cqtoa();
  18. void addhash();
  19. void keyboard_init();
  20.  
  21. typedef struct IOStdReq CIO;
  22.  
  23. #define QUAL_SHIFT   0x01
  24. #define QUAL_CTRL    0x02
  25. #define QUAL_AMIGA   0x04
  26. #define QUAL_ALT     0x08
  27. #define QUAL_LMB     0x10
  28. #define QUAL_MMB     0x20
  29. #define QUAL_RMB     0x40
  30.  
  31. #define HASHSIZE  64            /*    power of 2  */
  32. #define HASHMASK  (HASHSIZE-1)
  33.  
  34. typedef struct _HASH {
  35.     struct _HASH *next;     /* next hash   */
  36.     ubyte code;         /* keycode       */
  37.     ubyte mask;         /* qual. mask  */
  38.     ubyte qual;         /* qual. comp  */
  39.     ubyte stat;         /* string static? */
  40.     char *str;            /* command string */
  41. } HASH;
  42.  
  43. HASH *Hash[HASHSIZE];
  44.  
  45. struct Library *ConsoleDevice;
  46.  
  47. ubyte    ctoa[128];
  48. ubyte    cstoa[128];
  49.  
  50. void
  51. keyctl(im, code, qual)
  52. IMESS *im;
  53. register USHORT qual;
  54. {
  55.     ubyte buf[256];
  56.     ubyte c2;
  57.     short blen = 0;
  58.  
  59.     code &= 0xFF;
  60.     if (im) {
  61.     im->Qualifier &= ~IEQUALIFIER_REPEAT;
  62.     blen = DeadKeyConvert(im, buf+1, 254, NULL);
  63.     if (blen < 0)
  64.         return;
  65.     }
  66.     c2 = 0;
  67.     if (qual & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT))
  68.     c2 |= QUAL_SHIFT;
  69.     if (qual & (IEQUALIFIER_CONTROL))
  70.     c2 |= QUAL_CTRL;
  71.     if (qual & (IEQUALIFIER_LCOMMAND|IEQUALIFIER_RCOMMAND))
  72.     c2 |= QUAL_AMIGA;
  73.     if (qual & (IEQUALIFIER_LALT|IEQUALIFIER_RALT))
  74.     c2 |= QUAL_ALT;
  75.     if ((qual & IEQUALIFIER_CAPSLOCK) && blen == 1 && buf[1] >= 'a' && buf[1] <= 'z')
  76.     c2 |= QUAL_SHIFT;
  77.     if (qual & IEQUALIFIER_LEFTBUTTON)
  78.     c2 |= QUAL_LMB;
  79.     if (qual & IEQUALIFIER_MIDBUTTON)
  80.     c2 |= QUAL_MMB;
  81.     if (qual & (IEQUALIFIER_RBUTTON))
  82.     c2 |= QUAL_RMB;
  83.  
  84.     {
  85.     register HASH *hash;
  86.     for (hash = Hash[code&HASHMASK]; hash; hash = hash->next) {
  87.         if (hash->code == code && (c2 & hash->mask) == hash->qual)
  88.         break;
  89.     }
  90.  
  91.     /*
  92.      *  Use hash entry only if not in command line mode, or if the
  93.      *  entry does not correspond to an alpha key.
  94.      */
  95.  
  96.     if (hash) {
  97.         if (c2 || !Comlinemode || blen > 1 || !ctoa[code]) {
  98.         strcpy(buf, hash->str);
  99.         do_command(buf);
  100.         return;
  101.         }
  102.     }
  103.     }
  104.  
  105.     /*
  106.      *    No hash entry
  107.      */
  108.  
  109.     if (blen == 1) {
  110.     buf[0] = '\'';
  111.     buf[2] = 0;
  112.     } else {
  113.     buf[0] = '\`';
  114.     buf[blen+1] = '\'';
  115.     }
  116.     if (blen)
  117.     do_command(buf);
  118. }
  119.  
  120. void
  121. dealloc_hash()
  122. {
  123.     register HASH *hash, *hnext = NULL;
  124.     register short i;
  125.  
  126.     for (i = 0; i < HASHSIZE; ++i) {
  127.     for (hash = Hash[i]; hash; hash = hnext) {
  128.         hnext = hash->next;
  129.         if (!hash->stat)
  130.         FreeMem(hash->str, strlen(hash->str)+1);
  131.         FreeMem(hash, sizeof(HASH));
  132.     }
  133.     Hash[i] = NULL;
  134.     }
  135. }
  136.  
  137. void
  138. resethash()
  139. {
  140.     register short i;
  141.     CIO cio;
  142.     static struct {
  143.     char *from, *to;
  144.     } defmap[] = {
  145.     "esc",      "esc",
  146.     "c-esc",    "recall",
  147.     "return",   "return insline up firstnb down",
  148.     "enter",    "return",
  149.     "up",       "up",
  150.     "down",     "down",
  151.     "right",    "right",
  152.     "left",     "left",
  153.     "bs",       "bs",
  154.     "del",      "del",
  155.     "tab",      "tab",
  156.     "a-up",     "scrollup",
  157.     "a-down",   "scrolldown",
  158.     "a-r",      "nextr",
  159.     "a-u",      "while cl (tlate -32 right)",
  160.     "a-l",      "while cu (tlate +32 right)",
  161.     "s-up",     "top",
  162.     "s-down",   "bottom",
  163.     "s-right",  "last",
  164.     "s-left",   "first",
  165.     "s-tab",    "backtab",
  166.     "s-del",    "deline",
  167.     "s- ",      "( )",              /* shift space to space */
  168.     "c-1",      "goto block",
  169.     "c-c",      "",                 /* break.. map to a nop */
  170.     "c-l",      "wleft",
  171.     "c-r",      "wright",
  172.     "c-i",      "insertmode on",
  173.     "c-o",      "insertmode off",
  174.     "c-j",      "join",
  175.     "c-s",      "split first down",
  176.     "c-del",    "remeol",
  177.     "c-n",      "next",
  178.     "c-p",      "prev",
  179.     "c-/",      "escimm (find )",
  180.     "c-]",      "ref",
  181.     "c-[",      "ctags",
  182.     "c-g",      "escimm (goto )",
  183.     "c-up",     "pageup",
  184.     "c-down",   "pagedown",
  185.     "c-q",      "quit",
  186.     "c-f",      "reformat",
  187.     "c-w",      "wordwrap toggle",
  188.     "f1",       "escimm (insfile )",
  189.     "f2",       "escimm (newfile )",
  190.     "f3",       "escimm (newwindow newfile )",
  191.     "f6",       "saveold iconify",
  192.     "f7",       "escimm (bsave )",
  193.     "f8",       "saveold escimm (newfile )",
  194.     "f9",       "saveold",
  195.     "f10",      "saveold quit",
  196.     "c-b",      "block",
  197.     "c-u",      "unblock",
  198.     "a-d",      "bdelete",
  199.     "a-c",      "bcopy",
  200.     "a-m",      "bmove",
  201.     "a-s",      "bsource",
  202.     "a-S",      "unblock block block bsource",
  203.     "L-lmb",    "tomouse",      /*  left button                 */
  204.     "L-mmo",    "tomouse",      /*  mouse move w/left held down */
  205.     "R-rmb",    "iconify",      /*  right button                */
  206.     NULL, NULL
  207.     };
  208.  
  209.     dealloc_hash();
  210.     OpenDevice("console.device", -1, (struct IORequest *)&cio, 0);
  211.     ConsoleDevice = (struct Library *)cio.io_Device;
  212.     keyboard_init();
  213.     for (i = 0; defmap[i].from; ++i) {
  214.     ubyte code, qual;
  215.     if (get_codequal(defmap[i].from, &code, &qual))
  216.         addhash(code, 1, 0xFF, qual, defmap[i].to);
  217.     }
  218. }
  219.  
  220. returnoveride(n)
  221. {
  222.     HASH *hash;
  223.     static ubyte *str;
  224.     static int stat;
  225.  
  226.     for (hash = Hash[0x44&HASHMASK]; hash; hash = hash->next) {
  227.     if (hash->code == 0x44 && hash->qual == 0) {
  228.         if (n) {
  229.         str = (ubyte *)hash->str;
  230.         stat= hash->stat;
  231.         hash->str = "return";
  232.         hash->stat = 1;
  233.         } else {
  234.         if (str == NULL) {
  235.             remhash(0x44, (ubyte)-1, 0);
  236.         } else {
  237.             hash->str = (char *)str;
  238.             hash->stat= stat;
  239.         }
  240.         }
  241.         return(0);
  242.     }
  243.     }
  244.     if (n) {
  245.     addhash(0x44, 1, 0xFF, 0, "return");
  246.     str = NULL;
  247.     }
  248. }
  249.  
  250. void
  251. addhash(code, stat, mask, qual, str)
  252. ubyte code, stat, mask, qual;
  253. ubyte *str;
  254. {
  255.     register HASH **p, *hash;
  256.  
  257.     hash = *(p = &Hash[code&HASHMASK]);
  258.     while (hash) {
  259.     if (hash->code == code && hash->qual == qual && hash->mask == mask) {
  260.         if (!hash->stat)
  261.         FreeMem(hash->str, strlen(hash->str)+1);
  262.         goto newstr;
  263.     }
  264.     hash = *(p = &hash->next);
  265.     }
  266.     *p = hash = (HASH *)AllocMem(sizeof(HASH), 0);
  267.     hash->next = NULL;
  268. newstr:
  269.     hash->code = code;
  270.     hash->stat = stat;
  271.     hash->mask = mask;
  272.     hash->qual = qual;
  273.     hash->str = (char *)str;
  274.     if (!stat)                  /* if not static */
  275.     hash->str = (char *)strcpy((char *)AllocMem(strlen(str)+1, MEMF_PUBLIC), str);
  276. }
  277.  
  278.  
  279. remhash(code, mask, qual)
  280. ubyte code, mask, qual;
  281. {
  282.     register HASH *hash, **p;
  283.  
  284.     hash = *(p = &Hash[code&HASHMASK]);
  285.     while (hash) {
  286.     if (hash->code == code && hash->qual == qual && hash->mask == mask) {
  287.         if (!hash->stat)
  288.         FreeMem(hash->str, strlen(hash->str)+1);
  289.         *p = hash->next;
  290.         FreeMem(hash, sizeof(HASH));
  291.         return(1);
  292.     }
  293.     hash = *(p = &hash->next);
  294.     }
  295.     return(0);
  296. }
  297.  
  298. char *
  299. keyspectomacro(str)
  300. char *str;
  301. {
  302.     HASH *hash;
  303.     ubyte code, qual;
  304.  
  305.     if (get_codequal(str, &code, &qual)) {
  306.     for (hash = Hash[code&HASHMASK]; hash; hash = hash->next) {
  307.         if (hash->code == code) {
  308.         if (hash->qual == (qual & hash->mask)) {
  309.             return(hash->str);
  310.         }
  311.         }
  312.     }
  313.     }
  314.     return(NULL);
  315. }
  316.  
  317. void
  318. do_map()
  319. {
  320.     ubyte code, qual;
  321.  
  322.     if (get_codequal(av[1], &code, &qual)) {
  323.     addhash(code, 0, 0xFF, qual, av[2]);
  324.     } else {
  325.     title("Unknown Key");
  326.     }
  327. }
  328.  
  329. void
  330. do_unmap()        /* key   */
  331. {
  332.     ubyte code, qual;
  333.  
  334.     if (get_codequal(av[1], &code, &qual)) {
  335.     remhash(code, (ubyte)-1, qual);
  336.     } else {
  337.     title("Unknown Command");
  338.     }
  339. }
  340.  
  341. void
  342. do_clearmap()
  343. {
  344.     resethash();
  345. }
  346.  
  347. /*
  348.  * SAVEMAP  file
  349.  * SAVESMAP file
  350.  */
  351.  
  352. void
  353. do_savemap()
  354. {
  355.     char sysalso;
  356.     char err = 0;
  357.     char buf[256];
  358.     void *xfi;
  359.     register int i;
  360.     register HASH *hash;
  361.     register ubyte *ptr;
  362.  
  363.     xfi = xfopen(av[1], "w", 512);
  364.     if (xfi) {
  365.     sysalso = av[0][4] == 's';
  366.     for (i = 0; i < HASHSIZE; ++i) {
  367.         for (hash = Hash[i]; hash; hash = hash->next) {
  368.         if (hash->stat == 0 || sysalso) {
  369.             char soc = '(';
  370.             char eoc = ')';
  371.             char ksoc = '(';
  372.             char keoc = ')';
  373.             short len;
  374.  
  375.             for (ptr = (ubyte *)hash->str; *ptr; ++ptr) {
  376.             if (*ptr == '(')
  377.                 break;
  378.             if (*ptr == '\`') {
  379.                 soc = '\`';
  380.                 eoc = '\'';
  381.                 break;
  382.             }
  383.             }
  384.             len = strlen(ptr = cqtoa(hash->code, hash->qual)) - 1;
  385.             if (ptr[len] == '(' || ptr[len] == ')') {
  386.             ksoc = '\`';
  387.             keoc = '\'';
  388.             }
  389.             sprintf(buf, "map %c%s%c %c%s%c\n", ksoc, cqtoa(hash->code, hash->qual), keoc, soc, hash->str, eoc);
  390.             xfwrite(xfi, buf, strlen(buf));
  391.         }
  392.         }
  393.     }
  394.     xfclose(xfi);
  395.     if (err)
  396.         title ("Unable to Write");
  397.     else
  398.         title ("OK");
  399.     } else {
  400.     title("Unable to open file");
  401.     }
  402. }
  403.  
  404. /*
  405.  *  Nitty Gritty.
  406.  *
  407.  *  keyboard_init:  initialize for get_codequal() and cqtoa()
  408.  *  get_codequal:   convert a qualifier-string combo to a keycode and qual.
  409.  *  cqtoa:        convert a keycode and qual to a qual & string
  410.  */
  411.  
  412. #define LN(a,b,c,d)  ((a<<24)|(b<<16)|(c<<8)|d)
  413.  
  414. long lname[] = {
  415.     LN('e','s','c', 0  ), LN('f','1', 0 , 0  ), LN('f','2', 0 , 0  ),
  416.     LN('f','3', 0 , 0  ), LN('f','4', 0 , 0  ), LN('f','5', 0 , 0  ),
  417.     LN('f','6', 0 , 0  ), LN('f','7', 0 , 0  ), LN('f','8', 0 , 0  ),
  418.     LN('f','9', 0 , 0  ), LN('f','1','0', 0  ), LN('d','e','l', 0  ),
  419.     LN('b','a','c', 0  ), LN('b','s', 0 , 0  ), LN('t','a','b', 0  ),
  420.     LN('h','e','l', 0  ), LN('r','e','t', 0  ), LN('u','p', 0 , 0  ),
  421.     LN('d','o','w', 0  ), LN('r','i','g', 0  ), LN('l','e','f', 0  ),
  422.     LN('e','n','t', 0  ), LN('n','k','-', 0  ), LN('n','k','.', 0  ),
  423.     LN('n','k','0', 0  ),   /* 24 */
  424.     LN('n','k','1', 0  ), LN('n','k','2', 0  ), LN('n','k','3', 0  ),
  425.     LN('n','k','4', 0  ), LN('n','k','5', 0  ), LN('n','k','6', 0  ),
  426.     LN('n','k','7', 0  ), LN('n','k','8', 0  ), LN('n','k','9', 0  ),
  427.     LN('n','k','(', 0  ), LN('n','k',')', 0  ), LN('n','k','/', 0  ), /*34-36*/
  428.     LN('n','k','*', 0  ), LN('n','k','+', 0  ),
  429.     LN('l','m','b',0xE8), LN('m','m','b',0xEA), LN('r','m','b',0xE9),
  430.     LN('m','m','o',QMOVE),
  431.     0
  432. };
  433.  
  434.  
  435. /*
  436.  *  ESC:    x1B
  437.  *  FUNCKEYS:    x9B 30 7E to x9B 39 7E
  438.  *  DEL:    x7E
  439.  *  BS:     x08
  440.  *  TAB:    x09
  441.  *  RETURN:    x0D
  442.  *  HELP    x9B 3F 7E
  443.  *  UP/D/L/R    x9B 41/42/44/43
  444.  *  NK0-9,-,.,ENTER
  445.  *
  446.  *  Mouse buttons
  447.  */
  448.  
  449. void
  450. keyboard_init()
  451. {
  452.     static struct InputEvent ievent = { NULL, IECLASS_RAWKEY };
  453.     ubyte buf[32];
  454.     register short i, len;
  455.  
  456.     lname[16] |= 0x44;
  457.     lname[21] |= 0x43;
  458.  
  459.     for (i = 0; i < 128; ++i) {
  460.     ievent.ie_Code = i;
  461.     ievent.ie_Qualifier = 0;
  462.     ievent.ie_position.ie_addr = NULL;
  463.     len = RawKeyConvert(&ievent,buf,32,NULL);
  464.     switch(len) {
  465.     case 1:     /*    ESC/DEL/BS/TAB/NKx  */
  466.         if (buf[0] >= 32 && buf[0] < 127)
  467.         ctoa[i] = buf[0];
  468.         switch(buf[0]) {
  469.         case 0x1B:    lname[ 0] |= i; break;
  470.         case 0x7F:    lname[11] |= i; break;
  471.         case 0x09:    lname[14] |= i; break;
  472.         case 0x08:    lname[12] |= i; lname[13] |= i; break;
  473.         case '(': if (i > 0x3A) lname[34] |= i; break;
  474.         case ')': if (i > 0x3A) lname[35] |= i; break;
  475.         case '/': if (i > 0x3A) lname[36] |= i; break;
  476.         case '*': if (i > 0x3A) lname[37] |= i; break;
  477.         case '-': if (i > 0x3A) lname[22] |= i; break;
  478.         case '+': if (i > 0x3A) lname[38] |= i; break;
  479.         case '.': if (i > 0x3A) lname[23] |= i; break;
  480.         default:
  481.         if (i >= 0x0F && buf[0] >= '0' && buf[0] <= '9')
  482.             lname[24+buf[0]-'0'] |= i;
  483.         }
  484.         break;
  485.     case 2:     /*    cursor            */
  486.         if (buf[0] == 0x9B) {
  487.         switch(buf[1]) {
  488.         case 0x41:  lname[17] |= i;  break;
  489.         case 0x42:  lname[18] |= i;  break;
  490.         case 0x43:  lname[19] |= i;  break;
  491.         case 0x44:  lname[20] |= i;  break;
  492.         }
  493.         }
  494.         break;
  495.     case 3:     /*    function/help        */
  496.         if (buf[0] == 0x9B && buf[2] == 0x7E) {
  497.         if (buf[1] == 0x3F)
  498.             lname[15] |= i;
  499.         if (buf[1] >= 0x30 && buf[1] <= 0x39)
  500.             lname[buf[1]-0x30+1] |= i;
  501.         }
  502.         break;
  503.     }
  504.     }
  505.     for (i = 0; i < 128; ++i) {
  506.     ievent.ie_Code = i;
  507.     ievent.ie_Qualifier = IEQUALIFIER_LSHIFT;
  508.     ievent.ie_position.ie_addr = NULL;
  509.     len = RawKeyConvert(&ievent,buf,32,NULL);
  510.     if (len == 1)
  511.         cstoa[i] = buf[0];
  512.     }
  513.     {
  514.     ubyte code, qual;
  515.     get_codequal("c", &code, &qual);
  516.     CtlC = code;
  517.     }
  518. }
  519.  
  520.  
  521. ubyte *
  522. cqtoa(code, qual)
  523. register int qual;
  524. {
  525.     static ubyte buf[32];
  526.     register ubyte *ptr = buf;
  527.     register int i;
  528.  
  529.     if (qual & QUAL_SHIFT)
  530.     *ptr++ = 's';
  531.     if (qual & QUAL_CTRL)
  532.     *ptr++ = 'c';
  533.     if (qual & QUAL_ALT)
  534.     *ptr++ = 'a';
  535.     if (qual & QUAL_AMIGA)
  536.     *ptr++ = 'A';
  537.     if (qual & QUAL_LMB)
  538.     *ptr++ = 'L';
  539.     if (qual & QUAL_MMB)
  540.     *ptr++ = 'M';
  541.     if (qual & QUAL_RMB)
  542.     *ptr++ = 'R';
  543.     if (qual)
  544.     *ptr++ = '-';
  545.     for (i = 0; i < sizeof(lname)/sizeof(lname[0]); ++i) {
  546.     if ((lname[i]&0xFF) == code) {
  547.         *ptr++ = (lname[i]>>24);
  548.         *ptr++ = (lname[i]>>16);
  549.         *ptr++ = (lname[i]>>8);
  550.         break;
  551.     }
  552.     }
  553.     if (i == sizeof(lname)/sizeof(lname[0]))
  554.     *ptr++ = ctoa[code];
  555.     *ptr++ = 0;
  556.     return(buf);
  557. }
  558.  
  559.  
  560. get_codequal(str, pcode, pqual)
  561. ubyte *pcode, *pqual;
  562. register ubyte *str;
  563. {
  564.     register ubyte qual;
  565.     register short i;
  566.  
  567.     qual = 0;
  568.     if (strlen(str) > 1) {
  569.     for (; *str && *str != '-'; ++str) {
  570.         if (*str == 's')
  571.         qual |= QUAL_SHIFT;
  572.         if (*str == 'c')
  573.         qual |= QUAL_CTRL;
  574.         if (*str == 'a')
  575.         qual |= QUAL_ALT;
  576.         if (*str == 'A')
  577.         qual |= QUAL_AMIGA;
  578.         if (*str == 'L')
  579.         qual |= QUAL_LMB;
  580.         if (*str == 'M')
  581.         qual |= QUAL_MMB;
  582.         if (*str == 'R')
  583.         qual |= QUAL_RMB;
  584.         if (!qual)
  585.         goto notqual;
  586.     }
  587.     if (*str)
  588.         ++str;
  589.     }
  590. notqual:
  591.     if (strlen(str) != 1) {           /* long name   */
  592.     register short shift = 24;
  593.     register long mult = 0;
  594.  
  595.     *pqual = qual;
  596.     while (*str && shift >= 8) {
  597.         if (*str >= 'A' && *str <= 'Z')
  598.         *str = *str - 'A' + 'a';
  599.         mult |= *str << shift;
  600.         shift -= 8;
  601.         ++str;
  602.     }
  603.     for (i = 0; lname[i]; ++i) {
  604.         if (mult == (lname[i] & 0xFFFFFF00)) {
  605.         *pcode = lname[i] & 0xFF;
  606.         return(1);
  607.         }
  608.     }
  609.     } else {            /*    single character keycap */
  610.     for (i = 0; i < sizeof(ctoa); ++i) {
  611.         if (*str == ctoa[i]) {
  612.         *pcode = i;
  613.         *pqual = qual;
  614.         return(1);
  615.         }
  616.     }
  617.     for (i = 0; i < sizeof(cstoa); ++i) {
  618.         if (*str == cstoa[i]) {
  619.         *pcode = i;
  620.         *pqual = qual|QUAL_SHIFT;
  621.         return(1);
  622.         }
  623.     }
  624.     }
  625.     return(0);
  626. }
  627.  
  628.