#include <stdio.h>

#include "DeviceToolKits/Console.h"
#ifndef  TESTPROTO
#define  NO_PRAGMAS  1
#endif
#include "DeviceToolKits/proto/Console.h"
#include "DeviceToolKits/ConsoleBase.h"

ULONG                   DosBase = 0;
ULONG                   DiskFontBase = 0;
struct   IntuitionBase  *IntuitionBase = 0;
ULONG                   GfxBase = 0;

struct   NewWindow      nw = {
   10, 10,                       /* starting position (left,top) */
   620,90,                       /* width, height */
   -1,-1,                        /* detailpen, blockpen */
   0,                            /* flags for idcmp */
   WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|SIMPLE_REFRESH|ACTIVATE|GIMMEZEROZERO,
                                /* window gadget flags */
   0,                           /* pointer to 1st user gadget */
   NULL,                        /* pointer to user check */
   (UBYTE *)"Console Test",     /* title */
   NULL,                        /* pointer to window screen */
   NULL,                        /* pointer to super bitmap */
   100,45,                      /* min width, height */
   640,200,                     /* max width, height */
   WBENCHSCREEN
};

struct   Window         *w;
struct   RastPort       *rp;

DTConsole_t             Console1;
DTConsole_t             Console1Copy;
struct   KeyMap         keymap;
struct   KeyMap         dkeymap;

main(argc,argv)
int   argc;
char  *argv[];
{
   long  status;

/*  Open the necessary libraries  */

   if ((DosBase = (ULONG) OpenLibrary("dos.library",0L)) == (ULONG) NULL) {
      fprintf(stderr,"Could not open dos.library.\n");
      fflush(stderr);
      goto cleanup;
   }
   if ((DiskFontBase = (ULONG) OpenLibrary("diskfont.library",0L)) == (ULONG) NULL) {
      fprintf(stderr,"Could not open diskfont.library.\n");
      fflush(stderr);
      goto cleanup;
   }
   if ((IntuitionBase = (struct IntuitionBase *) OpenLibrary(
    "intuition.library",0L)) == NULL) {
      fprintf(stderr,"Could not open intuition.library.\n");
      fflush(stderr);
      goto cleanup;
   }
   if ((GfxBase = (ULONG) OpenLibrary("graphics.library",0L)) == (ULONG) NULL) {
      fprintf(stderr,"Could not open graphics.library.\n");
      fflush(stderr);
      goto cleanup;
   }

#ifdef   TESTSHARED
/*  Open the Console library  */

   DTConsoleOpen(DTCONSOLEREV);
#endif

/*  Create the window for console testing  */

   w = OpenWindow(&nw);
   if (w == NULL) {
      fprintf(stderr,"Could not open test window.\n");
      fflush(stderr);
      goto cleanup;
   }

/*  Open the Console Device  */

   if ((Console1 = DTConsoleCreate(w,&status)) == NULL) {
      fprintf(stderr,"Error initializing console device = %ld.\n",status);
      fflush(stderr);
      goto cleanup;
   }

/*  Make a copy of the control structure  */

   if ((Console1Copy = DTConsoleCreateLike(Console1,&status)) == NULL) {
      fprintf(stderr,"Error copying console device = %ld.\n",status);
      fflush(stderr);
      goto cleanup;
   }

/*  Begin tests  */

   DTConsoleWriteStr(Console1,"Getting Default KeyMap\r\n");
   if ((status = DTConsoleGetDefaultKeyMap(Console1,&dkeymap)) != 0) {
      fprintf(stderr,"Error getting default keymap = %ld,%ld.\n",
               status,Console1->co_error);
      fflush(stderr);
      goto cleanup;
   }

   DTConsoleWriteStr(Console1Copy,"Getting Current KeyMap\r\n");
   if ((status = DTConsoleGetKeyMap(Console1Copy,&keymap)) != 0) {
      fprintf(stderr,"Error getting current keymap = %ld,%ld.\n",
               status,Console1Copy->co_error);
      fflush(stderr);
      goto cleanup;
   }

   DTConsoleWriteStr(Console1Copy,"Setting Default KeyMap\r\n");
   if ((status = DTConsoleSetDefaultKeyMap(Console1Copy,&keymap)) != 0) {
      fprintf(stderr,"Error setting default keymap = %ld,%ld.\n",
               status,Console1Copy->co_error);
      fflush(stderr);
      goto cleanup;
   }

   DTConsoleWriteStr(Console1,"Setting Current KeyMap\r\n");
   if ((status = DTConsoleSetKeyMap(Console1,&dkeymap)) != 0) {
      fprintf(stderr,"Error setting current keymap = %ld,%ld.\n",
               status,Console1->co_error);
      fflush(stderr);
      goto cleanup;
   }

cleanup:
   DTConsoleFree(Console1);
   DTConsoleFree(Console1Copy);
   if (w != NULL) {
      CloseWindow(w);
   }
#ifdef   TESTSHARED
   DTConsoleClose();
#endif
   if (GfxBase != (ULONG) NULL) {
      CloseLibrary((struct Library *) GfxBase);
   }
   if (IntuitionBase != NULL) {
      CloseLibrary((struct Library *) IntuitionBase);
   }
   if (DiskFontBase != (ULONG) NULL) {
      CloseLibrary((struct Library *) DiskFontBase);
   }
   if (DosBase != (ULONG) NULL) {
      CloseLibrary((struct Library *) DosBase);
   }
}

