#include <stdio.h>
#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>

#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <intuition/intuition.h>
#include <devices/console.h>
#include <devices/keymap.h>
#include <libraries/dos.h>
#include <functions.h>

SHORT key_to_g[] = 
   {
      12,13,14,15,16,17,18,19,20,21,22,23,24,25,99,79,31,32,
      33,34,35,36,37,38,39,40,41,42,99,62,63,64,49,50,51,52,
      53,54,55,56,57,58,59,99,99,44,45,46,99,66,67,68,69,70,
      71,72,73,74,75,99,80,27,28,29,83,26,30,88,60,0,11,99,99,99,87,
      99,61,86,78,77,1,2,3,4,5,6,7,8,9,10,99,99,99,99,99,43,
      65,76,48,47,81,85,82,84
   };

extern UBYTE name[90][11];
extern UBYTE qual_name[8][18];

struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
struct NewWindow nw = 
   {
   500,0,1,1, /* position */
   -1,-1,       /* pens     */
   NULL,
   SMART_REFRESH,
   0,           /* gadet pointer */
   NULL,        /* user check    */
   (unsigned char *)" Console Test Program ",
   NULL,   /* screen */
   NULL,   /* superbitmap */
   1,1,640,200, /* minmax size */
   WBENCHSCREEN
   };

struct Window *w;

#include "showsubs.c"

main(argc,argv)
int argc;
char *argv[];
{
   USHORT which,key,qual,totlen;
   int i,j,k,l;
   int line=0;
   char string[180];

   if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L))) exit(100);
   if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L)))  exit(101);
   if(!(w=OpenWindow(&nw))) exit(102);
   if (OpenConsole(w)!=NULL)     exit(103);

   AllocKeyMap();
   
   DefaultKeyMap();

   printf("/********** Current Key Assignments **********/\n");
   printf("/Key       : Qualifier         :  String      /\n");
   printf("-----------------------------------------------\n");
   for(i=0;i<0x60;i++)
   {
     which = key_to_g[i];
     if(which != 99)
     {
        if(LoTypes[i] & 0x40) 
        {
           totlen=0;
           String(i);
           for (qual=0;qual<8;qual++) 
           {
              if((DStr[qual].length) && (DStr[qual].string[0] != 0x9B))
              {
                  printf("%s : ",name[which]);
                  for(l=k=0;DStr[qual].string[k] != '\0';k++)
                  {
                     switch(DStr[qual].string[k]) 
                     {
                        case 0x0D:
                           string[l++] = '<';
                           string[l++] = 'C';
                           string[l++] = 'R';
                           string[l++] = '>';
                           break;
                        case 0x1F: case 0x1E: case 0x1D:
                        case 0x1C: case 0x1B: case 0x1A: case 0x19:
                        case 0x18: case 0x17: case 0x16: case 0x15:
                        case 0x14: case 0x13: case 0x12: case 0x11:
                        case 0x10: case 0x0F: case 0x0E:
                        case 0x0C: case 0x0B: case 0x0A: case 0x09:
                        case 0x08: case 0x07: case 0x06: case 0x05:
                        case 0x04: case 0x03: case 0x02: case 0x01:
                           string[l++] = 0xDB;
                           break;
                        default:
                           string[l++] = DStr[qual].string[k];
                           break;
                     }
                  }
                  string[l] = '\0';
                  printf("%s : %s\n",qual_name[qual],string);
                  if(totlen>127)
                  {
                     line++;
                     printf("[7m.^. The above key is TOO LONG; Please make shorter! .^.[m\n");
                  }
                  totlen+=DStr[qual].length;
                  line++;
                  if(DStr[qual].length>44)line++;/* wrap around */
                  if(line>18)
                  {
                     line=0;
                     printf("Press Return to Continue");
                     fflush(stdout);
                     getchar();
   printf("/********** Current Key Assignments **********/\n");
   printf("/Key       : Qualifier         :  String      /\n");
   printf("-----------------------------------------------\n");
                  }
               }
            }
         }
      }
   }
   ClearStr(120);
   FreeKeyMap(4);

   ConsoleCleanup();
   CloseWindow(w);
   CloseLibrary(GfxBase);
   CloseLibrary(IntuitionBase);
}
