#include <exec/types.h>
#include <ctype.h>

/* WARNING ... this table has been has been fiddled with so that the
   cursor movement keys generate the control code required for the
   vc spreadsheet program
   Cursor Keys generate the following control codes consistent with
   the vc program:
   up is ^P
   down is ^N
   left is ^B
   right is ^F
   This can be overridden dynamically by using the setudlr function

   and the help key generates ?
*/
char keys[96] = {
'`' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
'8' , '9' , '0' , '-' , '=' , '\\',  0  , '0' ,
'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' ,
'o' , 'p' , '[' , ']' ,  0  , '1' , '2' , '3' ,
'a' , 's' , 'd' , 'f' , 'g' , 'h' , 'j' , 'k' ,
'l' , ';' , '\'','\n' ,  0  , '4' , '5' , '6' ,
 0  , 'z' , 'x' , 'c' , 'v' , 'b' , 'n' , 'm' ,
 44 , '.' , '/' ,  0  , '.' , '7' , '8' , '9' ,
' ' ,  8  ,'\t' ,  13 ,  13 , 27  , 127 ,  0  ,
 0  ,  0  , '-' ,  0  ,  16 , 14  ,  6  ,  2  ,
 0  ,  0  ,  0  ,  0  ,  0  ,  0  ,  0  ,  0  ,  /* Fn keys mapped by pgm */
 0  ,  0  ,  0  ,  0  ,  0  ,  0  ,  0  , '?'
 } ;
/*************************************************
*  function to take raw key data and convert it 
*  into ascii chars
**************************************************/
short ctrl = FALSE;
short shift = FALSE;
short capsl = FALSE;
char toasc(code)
USHORT code;
{
short c;

   switch ( code ) {
   case 0x62:
      capsl = TRUE;
      c = 0;
      break;
   case 0x62+0x80:
      capsl = FALSE;
      c = 0;
      break;
   case 0x63:
      ctrl = TRUE;
      c = 0;
      break;
   case 0x63+0x80:
      ctrl = FALSE;
      c = 0;
      break;
   case 0x60:
   case 0x61:
       shift = TRUE;
      c = 0;
      break;
   case 0x60+0x80:
   case 0x61+0x80:
      shift = FALSE;
      c = 0;
      break;
   default:
      if((code > 0x4F) && (code < 0x5A)) c = 0200 + code;
      else if (code < 0x60) {
              c = keys[code];
           }
           else {
              c = 0;
           }
   }
         
/* add modifiers to the keys */

   if ((c != 0) && (c < 0200)) {

      if (ctrl && (c <= 'z') && (c >= 'a'))
         c -= 96;
      else if (shift) {
         if ((c <= 'z') && (c >= 'a'))
            c -= 32;
         else
            switch( c ) {
            case '[':
               c = '{';
               break;
            case ']':
               c = '}';
               break;
            case '\\':
               c = '|';
               break;
            case '\'':
               c = '"';
               break;
            case ';':
               c = ':';
               break;
            case '/':
               c = '?';
               break;
            case '.':
               c = '>';
               break;
            case ',':
               c = '<';
               break;
            case '`':
               c = '~';
               break;
            case '=':
               c = '+';
               break;
            case '-':
               c = '_';
               break;
            case '1':
               c = '!';
               break;
            case '2':
               c = '@';
               break;
            case '3':
               c = '#';
               break;
            case '4':
               c = '$';
               break;
            case '5':
               c = '%';
               break;
            case '6':
               c = '^';
               break;
            case '7':
               c = '&';
               break;
            case '8':
               c = '*';
               break;
            case '9':
               c = '(';
               break;
            case '0':
               c = ')';
               break;
            default:;
            } /* end switch */
      } /* end shift */
      else if (capsl && (c <= 'z') && (c >= 'a'))
           c -= 32;
   } /* end modifiers */
   return(c);
} /* end of routine */

/* setudlr allocates characters to the cursor movement keys.
   All four keys must be set (to zero if you wish)

*/
setudlr(a)
char *a;
{
   keys[0x4c] = *a++;
   keys[0x4d] = *a++;
   keys[0x4f] = *a++;
   keys[0x4e] = *a++;
}
