/*******  keymap_test.c  **********/

/***************************************************************************
 *
 *             Test program to illustrate bug in keymapping
 *
 **************************************************************************/


#include "keymap_test.h"


extern Enable_Abort;


ULONG  IntuitionBase;
ULONG  GfxBase;
struct Window    *te_window; /* ptr to a window to which the
                              * console is bound
                              */

struct  NewWindow  NewWindow =
   {
   0, 0,                                /* starting position              */
   640, 200,                            /* window width and height        */
   -1, -1,                              /* use same pens as screen        */
   NULL,                                /* no IDCMP flags                 */
   SMART_REFRESH | BORDERLESS | ACTIVATE, /* window flags      */
   NULL, NULL, NULL,                    /* no gadgets, checkmark & text   */
   NULL,                                /* pointer to screen              */
   NULL,                                /* no bit map                     */
   NULL, NULL, NULL, NULL,              /* window sizing parameters       */
   WBENCHSCREEN,                        /* screen type                    */
   };

struct IOStdReq  *con_write_req, *con_read_req;
struct MsgPort   *con_write_port, *con_read_port;

UBYTE conchar;         /* used for QueueRead() */

struct KeyMap     keymap;

UBYTE lo_types[64];
ULONG lo_keymap[64];
UBYTE lo_cap[8];
UBYTE lo_rep[8];

UBYTE hi_types[40];
ULONG hi_keymap[40];
UBYTE hi_cap[8];
UBYTE hi_rep[8];

UBYTE string_alt_shift[] =
         {
         10,            /* length of string with no qualifier         */
         8,             /* dist of string from beg of descriptor      */
         9,             /* length of string with SHIFT                */
         18,            /* dist of string from beg of descriptor      */
         7,             /* length of string with ALT                  */
         27,            /* dist of string from beg of descriptor      */
         15,            /* length of string with ALT + SHIFT          */
         34,            /* dist of string from beg of descriptor      */
         "2 + noqual",
         "2 + shift",
         "2 + alt",
         "2 + alt + shift",
         };

UBYTE string_vanilla[] =
         {
         10,            /* length of string with no qualifier         */
         16,            /* dist of string from beg of descriptor      */
         9,             /* length of string with SHIFT                */
         26,            /* dist of string from beg of descriptor      */
         7,             /* length of string with ALT                  */
         35,            /* dist of string from beg of descriptor      */
         8,             /* length of string with CTRL                 */
         42,            /* dist of string from beg of descriptor      */
         15,            /* length of string with ALT + SHIFT          */
         50,            /* dist of string from beg of descriptor      */
         14,            /* length of string with CTRL + ALT           */
         65,            /* dist of string from beg of descriptor      */
         16,            /* length of string with CTRL + SHIFT         */
         79,            /* dist of string from beg of descriptor      */
         22,            /* length of string with CTRL + ALT + SHIFT   */
         95,            /* dist of string from beg of descriptor      */
         "3 + noqual",
         "3 + shift",
         "3 + alt",
         "3 + ctrl",
         "3 + alt + shift",
         "3 + ctrl + alt",
         "3 + ctrl + shift",
         "3 + ctrl + alt + shift",
         };

init_window()
{
   GfxBase = (ULONG) OpenLibrary("graphics.library", 0);
   IntuitionBase = (ULONG) OpenLibrary("intuition.library", 0);
   te_window = OpenWindow(&NewWindow);
}


init_req()
{
   con_write_port = CreatePort("my.con.write",0);
   con_write_req  = CreateStdIO(con_write_port);

   con_read_port = CreatePort("my.con.read",0);
   con_read_req =  CreateStdIO(con_read_port);
  
   OpenConsole(con_write_req, con_read_req, te_window);

}



main()
{
   UBYTE c;
   int i;


   init_window();
   init_req();

   QueueRead (con_read_req, &conchar);

   /* initialize keymap tables to zeroes
    */
   for (i = 0; i <= 63; i++)
      {
      lo_types[i] = 0x00;
      lo_keymap[i] = 0x00;
      }

   for (i = 0; i <= 39; i++)
      {
      hi_types[i] = 0x00;
      hi_keymap[i] = 0x00;
      }

   for (i = 0; i <= 7; i++)
      {
      lo_cap[i] = 0x00;
      lo_rep[i] = 0x00;
      hi_cap[i] = 0x00;
      hi_rep[i] = 0x00;
      }

   /* setup key types for keys 1, 2, 3, & 4
    */
   lo_types[1] = 0x05;        /* KCF_CONTROL + KCF_SHIFT           */
   lo_types[2] = 0x43;        /* KCF_STRING + KCF_ALT + KCF_SHIFT  */
   lo_types[3] = 0x47;        /* KCF_STRING + KC_VANILLA           */
   lo_types[4] = 0x01;        /* KC_NOQUAL  + KCF_SHIFT            */


   /* setup key codes for keys 1, 2, 3, & 4
    */
   lo_keymap[1]   = (ULONG) 0x64636261;  /* d = key 1 with CTRL + SHIFT */
                                         /* c = key 1 with CTRL         */
                                         /* b = key 1 with SHIFT        */
                                         /* a = key 1 alone             */
   lo_keymap[2]   = (ULONG) 0x00000000;  /*     key 2                   */
   lo_keymap[3]   = (ULONG) 0x00000000;  /*     key 3                   */
   lo_keymap[4]   = (ULONG) 0x00002434;  /*     key 4                   */


   /* plug in addresses of string descriptors into low keymap
    */
   lo_keymap[2] = (ULONG) string_alt_shift;
   lo_keymap[3] = (ULONG) string_vanilla;


   /* setup pointers to members of keymap structure
    */
   keymap.km_LoKeyMapTypes = (APTR) lo_types;
   keymap.km_LoKeyMap      = (APTR) lo_keymap;
   keymap.km_LoCapsable    = (APTR) lo_cap;
   keymap.km_LoRepeatable  = (APTR) lo_rep;
   keymap.km_HiKeyMapTypes = (APTR) hi_types;
   keymap.km_HiKeyMap      = (APTR) hi_keymap;
   keymap.km_HiCapsable    = (APTR) hi_cap;
   keymap.km_HiRepeatable  = (APTR) hi_rep;

   /* Now set the new keymap
    */
   con_write_req-> io_Command = CD_SETKEYMAP;
   con_write_req-> io_Length  = sizeof(keymap);
   con_write_req-> io_Data    = (APTR) &keymap;

   DoIO (con_write_req);

   Enable_Abort = 0;

   /* Keys 1, 2 & 3 have been setup with key types 0x05, 0x43 & 0x47 
    * respectively.
    * Press key 4 + SHIFT to exit
    */

    for (;;)
      {
      /* Note: Chk_Abort() does not work here
       */

      if (Chk_Abort() != 0)
         break;               /* ctrl-c or ctrl-d was pressed */
      
      c = (UBYTE) ConMayGetChar (con_read_req, con_read_port, &conchar);
      if (c != '$')
         {
         ConPutChar (con_write_req, c);
         }
      else
         break;
      }

   AbortIO(con_read_req);
   CloseDevice(con_read_req);

   DeletePort(con_read_port);
   DeletePort(con_write_port);
   DeleteStdIO(con_read_req);
   DeleteStdIO(con_write_req);

   CloseWindow(te_window);
   CloseLibrary(IntuitionBase);
   CloseLibrary(GfxBase);

}
