home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / xi-25 / appkeymap.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  7KB  |  174 lines

  1. ;/* appkeymap.c link module - Execute me to compile me with SAS/C 6.56
  2. sc data=far nominc strmer streq nostkchk saveds ign=73 appkeymap.c
  3. quit
  4. */
  5.  
  6. /* appkeymap.c - subroutines to create/delete an application keymap
  7.  *               by modifying a copy of a keymap
  8.  *
  9.  * The modifications made to the keymap are controlled by the "mykeys" array.
  10.  * To create more complex keymappings, see the Rom Kernel Manual Libraries
  11.  * keymap library chapter (p 821 et al).
  12.  *
  13.  * NOTE: disabling all numeric pad keys creates a good keymap for use
  14.  * with either keymap.library MapANSI() or commodities InvertString().
  15.  * If you used a default keymap with the above functions, numeric keypad
  16.  * raw key values would be returned for keys which are available with
  17.  * fewer keypresses on numeric pad than on the normal keyboard.
  18.  * It is generally preferable to have the normal keyboard raw values
  19.  * since many applications attach special meanings to numeric pad keys.
  20.  */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <devices/console.h>
  25. #include <devices/keymap.h>
  26.  
  27. #include <clib/exec_protos.h>
  28. #include <clib/keymap_protos.h>
  29. #include <clib/alib_protos.h>
  30.  
  31. #include "appkeymap.h"
  32.  
  33. /* local functions */
  34. static void CopyKeyMap (struct KeyMap *source, struct KeyMap *dest);
  35.  
  36.  
  37. struct KeyMap *
  38. CreateAppKeyMap (struct KeyMap *defkeymap)
  39. {
  40.   struct KeyMap *appkeymap = NULL;
  41.   struct KeyMapArrays *karrays;
  42.   ULONG keymapsize;
  43.  
  44.                                 /* Because of the way this code allocates memory,  */
  45.                                 /* it must make sure the KeyMap structure will take*/
  46.                                 /* up an even amount of bytes (so the tables that  */
  47.                                 /* follow it will be word aligned).  This adds one */
  48.                                 /* to the size of the KeyMap structure's size if   */
  49.                                 /* necessary.  This is only for future compatibil- */
  50.                                 /* ity as a KeyMap structure is currently even.    */
  51.   keymapsize = (sizeof(struct KeyMap)+1)&0x0000FFFE;
  52.  
  53.                                 /* Allocate the space for the clone of the KeyMap. */
  54.                                 /* This includes space for the KeyMap structure    */
  55.                                 /* which is immediately followed by space for the  */
  56.                                 /* keymapping tables.                              */
  57.   if (appkeymap = (struct KeyMap *)
  58.           AllocVec (sizeof (struct KeyMap) + sizeof (struct KeyMapArrays),
  59.                     MEMF_CLEAR|MEMF_PUBLIC) )
  60.   {
  61.                   /* Initialize the pointer to the KeyMapArrays structure. These   */
  62.                   /* arrays are organized so that the "Lo" tables are immediately  */
  63.                   /* followed by the "Hi" tables, so you don't have to treat the   */
  64.                   /* Lo and Hi maps separately.  This allows AlterKeyMap() to find */
  65.                   /* Hi map raw key entries by use the raw key value as an index   */
  66.                   /* into the Lo map tables.                                       */
  67.     karrays = (struct KeyMapArrays *) ((ULONG)appkeymap + keymapsize);
  68.  
  69.                   /* Initialize the appkeymap fields to point to the KeyMapArrays. */
  70.                   /* Each LH array contains the Lo table followed by the Hi table. */
  71.     appkeymap->km_LoKeyMap      = &karrays->LHKeyMap[0];
  72.     appkeymap->km_HiKeyMap      = &karrays->LHKeyMap[LO_MAP_SIZE];
  73.     appkeymap->km_LoKeyMapTypes = &karrays->LHKeyMapTypes[0];
  74.     appkeymap->km_HiKeyMapTypes = &karrays->LHKeyMapTypes[LO_TYPE_SIZE];
  75.     appkeymap->km_LoCapsable    = &karrays->LHCapsable[0];
  76.     appkeymap->km_HiCapsable    = &karrays->LHCapsable[LO_CAPS_SIZE];
  77.     appkeymap->km_LoRepeatable  = &karrays->LHRepeatable[0];
  78.     appkeymap->km_HiRepeatable  = &karrays->LHRepeatable[LO_REPS_SIZE];
  79.  
  80.  
  81.     CopyKeyMap (defkeymap, appkeymap);     /* Copy the keymap arrays to appkeymap. */
  82.   }
  83.  
  84.   return (appkeymap);
  85. }
  86.  
  87.  
  88. void
  89. DeleteAppKeyMap (struct KeyMap *appkeymap)
  90. {
  91.   if (appkeymap)
  92.     FreeVec (appkeymap);
  93. }
  94.  
  95.  
  96. void
  97. AlterAppKeyMap (struct KeyMap *appkeymap, struct MyKey *mykeys, UWORD mykeycount)
  98. {
  99.   ULONG *keymappings;
  100.   UBYTE *keymaptypes, *capsables, *repeatables;
  101.   UBYTE rawkeynum;
  102.   int i, bytenum, bitnum;
  103.  
  104.  
  105. /* AlterAppKeyMap() assumes that the tables of the keymap passed to it are */
  106. /* contiguous from Lo through Hi. This allows AlterAppKeyMap() to directly */
  107. /* reference any key using its raw key code as an index.  This also limits */
  108. /* an application to using AlterAppKeyMap() to modify only keymap created  */
  109. /* by CreateAppKeyMap().                                                   */
  110.  
  111.  
  112.   keymappings = appkeymap->km_LoKeyMap;
  113.   keymaptypes = appkeymap->km_LoKeyMapTypes;
  114.   capsables   = appkeymap->km_LoCapsable;
  115.   repeatables = appkeymap->km_LoRepeatable;
  116.  
  117.   for (i = 0; i < mykeycount; i++)
  118.   {
  119.  
  120.                                     /* Because we allocated each of our Lo and Hi    */
  121.     rawkeynum = mykeys[i].RawCode;  /* array pairs as sequential memory, we can use  */
  122.                                     /* the RAWKEY values directly  to index into our */
  123.                                     /* sequential Lo/Hi array.                       */
  124.  
  125.     keymaptypes[rawkeynum] = mykeys[i].MapType;
  126.     keymappings[rawkeynum] = mykeys[i].Map;
  127.  
  128.     bytenum = rawkeynum >> 3; /* These are for the Capsable + Repeatable bit tables. */
  129.     bitnum = rawkeynum % 8;   /*    Careful--There's only a 1 bit entry per raw key! */
  130.  
  131.     if (mykeys[i].Capsable)
  132.       capsables[bytenum] |= (1 << bitnum);          /* If capsable, set bit, else... */
  133.     else
  134.       capsables[bytenum] &= (~(1 << bitnum));       /*             ...clear the bit. */
  135.  
  136.     if (mykeys[i].Repeatable)
  137.       repeatables[bytenum] |= (1 << bitnum);      /* If repeatable, set bit, else... */
  138.     else
  139.       repeatables[bytenum] &= (~(1 << bitnum));   /*               ...clear the bit. */
  140.   }
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. static void
  148. CopyKeyMap (struct KeyMap *source, struct KeyMap *dest)
  149. {
  150.   UBYTE *bb;
  151.   ULONG *ll;
  152.   int i;
  153.  
  154.   for (i = 0, ll = source->km_LoKeyMap; i < LO_MAP_SIZE; i++)
  155.     dest->km_LoKeyMap[i] = *ll++;
  156.   for (i = 0, ll = source->km_HiKeyMap; i < HI_MAP_SIZE; i++)
  157.     dest->km_HiKeyMap[i] = *ll++;
  158.  
  159.   for (i = 0, bb = source->km_LoKeyMapTypes; i < LO_TYPE_SIZE; i++)
  160.     dest->km_LoKeyMapTypes[i] = *bb++;
  161.   for (i = 0, bb = source->km_HiKeyMapTypes; i < HI_TYPE_SIZE; i++)
  162.     dest->km_HiKeyMapTypes[i] = *bb++;
  163.  
  164.   for (i = 0, bb = source->km_LoCapsable; i < LO_CAPS_SIZE; i++)
  165.     dest->km_LoCapsable[i] = *bb++;
  166.   for (i = 0, bb = source->km_HiCapsable; i < HI_CAPS_SIZE; i++)
  167.     dest->km_HiCapsable[i] = *bb++;
  168.  
  169.   for (i = 0, bb = source->km_LoRepeatable; i < LO_REPS_SIZE; i++)
  170.     dest->km_LoRepeatable[i] = *bb++;
  171.   for (i = 0, bb = source->km_HiRepeatable; i < HI_REPS_SIZE; i++)
  172.     dest->km_HiRepeatable[i] = *bb++;
  173. }
  174.