   /*                        -=-=-=-=-=-=-=-=-=-=-=-                      */
  /*                         -=-=  Screen  Dump =-=-                     */
 /*                          -=-=-=-=-=-=-=-=-=-=-=-                    */
/*                                                                        */
/*                                                                        */
/*             ========================================                   */
/*             : Copyright (c) 1986 by James R. White :                   */
/*             :         All Rights Reserved          :                   */
/*             ========================================                   */
/*                                                                        */
/*   Last Modified 1/6/88  - Native Star Version & Modular               */
/*
*                                                                           *
*     Date       Revision Details                                           *
*   ========     ========================================================   *
*   1/12/87      Changed the program to write to RAM:AP.File instead of     *
*                PAR: to speed up the printing process.  The user will      *
*                now need to do a copy RAM:AP.File to PAR: to get a         *
*                hardcopy.                                                  *
*                                                                           *
*   ========     ========================================================   *
*   3/19/87      Recompiled with Lattice 3.10 and changed inner loops to    *
*                putc instead of fprintf calls                              *
*                                                                           *
*                                                                           *
*   ========     ========================================================   *
*   1/6 /88      Recompiled with Lattice 4.00 and changed header files      *
*                to use prototypes.                                         *
*                                                                           *
*   ========     ========================================================   *
*   1/6 /88      Changed type of unsigned char variables to int             *
*                                                                           *
*   ========     ========================================================   *
*   1/6 /88      Changed limits in screendump loop to prevent printing last *
*                two blank lines, change innermost loop logic.              *
*                                                                           *
*                                                                           *
*                                                                           *
*                                                                          */
#include "exec/types.h"
#include "exec/nodes.h"
#include "exec/lists.h"
#include "exec/devices.h"
#include "devices/keymap.h"
#include "intuition/intuition.h"
#include "graphics/text.h"
#include "graphics/regions.h"
#include "graphics/copper.h"
#include "graphics/gels.h"
#include <proto/potgo.h>                    /* New Version 4.0 includes */
#include <proto/timer.h>                    /* New Version 4.0 includes */
#include <proto/clist.h>                    /* New Version 4.0 includes */
#include <proto/console.h>                  /* New Version 4.0 includes */
#include <proto/exec.h>                     /* New Version 4.0 includes */
#include <proto/mathffp.h>                  /* New Version 4.0 includes */
#include <proto/dos.h>                      /* New Version 4.0 includes */
#include <proto/mathtrans.h>                /* New Version 4.0 includes */
#include <proto/mathieeedoubbas.h>          /* New Version 4.0 includes */
#include <proto/diskfont.h>                 /* New Version 4.0 includes */
/* #include <proto/cstrings.h> */           /* New Version 4.0 includes */
#include <proto/layers.h>                   /* New Version 4.0 includes */
#include <proto/mathieeesingbas.h>          /* New Version 4.0 includes */
#include <proto/expansion.h>                /* New Version 4.0 includes */
#include <proto/icon.h>                     /* New Version 4.0 includes */
#include <proto/intuition.h>                /* New Version 4.0 includes */
#include <proto/translator.h>               /* New Version 4.0 includes */
#include <proto/graphics.h>                 /* New Version 4.0 includes */
#include <stdlib.h>                         /* New Version 4.0 includes */
#include <string.h>                         /* New Version 4.0 includes */
#include "stdio.h"

USHORT wakeup;         /* Wake me up for event */
USHORT class;          /* Intu event class */
USHORT code;           /* Intu event code */
USHORT qual;           /* Intu event qualifier */
USHORT mode;           /* Intu event mode */
APTR address;          /* address of gadget that user hit */

int ScreenDump()

{
/* long GfxBase = 0;             Base of graphics library */
/* long IntuitionBase = 0;       Base of Intuition library */
extern struct Window *OpenWindow();
/* extern struct InputEvent *Intuition(); Version 1.40 modification */
extern struct RastPort *CDRastPort();
extern struct Screen *OpenScreen();
extern struct Window *w;
extern struct RastPort *rp;
/* struct RastPort *rp,*cdrp; */
extern struct ViewPort *vp;
extern struct IntuiMessage *message;
extern struct Screen *screen;
/* struct NewScreen *ns; */
/* struct NewWindow *nw; */
int ix,iy,result;
int c,c1,c2,c6,escape;
register int i,newcolor,ii,jj;
register unsigned char chr;
int mask[9];
FILE *filenum;

vp = &screen->ViewPort;
ix = vp->DWidth;
iy = vp->DHeight;
iy = iy - 8;     /* Modification for Version 1.40 */

   /* Lets see if the user has been trying to close the window */

if(w->UserPort->mp_SigBit) /* Did we get an InituiMessage? */
   {
   message = (struct IntuiMessage *)GetMsg(w->UserPort);
   if(message != NULL)
      {
      class = message->Class; /* Yep, store it */
      ReplyMsg(message);   /* and Reply...     */
      printf("Screen Dump Aborted by User. \n");
      return(0);              /* bail out!        */
      }
   }
         /* Lets open the RAM: file as a write-only file */

filenum = fopen("RAM:AP.File","w");
if(filenum == NULL)
   {
   printf("I can't open RAM:AP.File for writing!\nAmigaPlot aborting.\n");
   return (2);
   }

mask[8] =   1;
mask[7] =   2;
mask[6] =   4;
mask[5] =   8;
mask[4] =  16;
mask[3] =  32;
mask[2] =  64;
mask[1] = 128;
mask[0] = 128;  /* this number is not used */

escape = 27;

  /* write escape code sequence to change print pitch to 10        */

c = 18; fprintf(filenum,"%c",c);

  /* write escape code sequence to change left and right margins   */
  /* escape-M-chr$(1) changes the left margin to col 1 in Star Mode */

c = 1; fprintf(filenum,"%cM%c",escape,c);

  /* escape-Q-chr$(136) changes the right margin to col 136 in Star Mode */

c = 136; fprintf(filenum,"%cQ%c",escape,c);

  /* write escape code sequence to change line spacing
     escape-A-8 changes line spacing to 8/72 inch       */

c = 8; fprintf(filenum,"%cA%c",escape,c);

         /* define escape code sequence to put into graphics mode */

c  = 103;    /* ascii 'g' */
c2 = ix/256;
c1 = ix - (c2 * 256);
c6 = 6;

/*  Start scanning across the top of the screen 8 pixels at a time */

for (ii = 0; ii < iy ; ii = ii + 8 ) {

   /* Write escape code sequence to print in graphics mode.  The     */
   /* escape-g-6-n1-n2 sequence tells the printer to print n1+n2*256 */
   /* graphics characters in CRT graphics mode-II (90 dots/in)       */

   fprintf(filenum,"    %c%c%c%c%c",escape,c,c6,c1,c2);

   for (i = 1; i <= ix; ++i) {
      for (jj = 1, chr = 0; jj <= 8; ++jj) {
         newcolor = ReadPixel(rp, i, ii+jj);
         if (newcolor > 0)
            chr = chr + mask[jj];}
      putc(chr,filenum); }
   fprintf(filenum,"\n");   /* send newline */
   if(w->UserPort->mp_SigBit) /* Did we get an InituiMessage? */
      {
         message = (struct IntuiMessage *)GetMsg(w->UserPort);
         if(message != NULL)
         {
            class = message->Class; /* Yep, store it */
            ReplyMsg(message);   /* and Reply... */
            printf("Screen Dump Aborted by User. \n");
            ii = iy;             /* set counter ready to bail out! */
         }
      }
} /* For Loop */

/* Eject a page by sending a form feed to the printer */
c2 = 12;
fprintf(filenum,"%c",c2);  /* note c2 = 12 = FF = Form Feed */

/* Write escape code sequence to change line spacing back to normal
   using the sequence escape-A-12*/

fprintf(filenum,"%cA%c",escape,c2);

printf("Screen Dump All Done. \n");
result = fclose(filenum);     /* close file */
return (result);

/*                   -=-=-=-=-=-=-=-=-=-=-=-=-=-=-                    */
/*                 -=-=-=  End of Screen Dump =-=-=-                  */
/*                   -=-=-=-=-=-=-=-=-=-=-=-=-=-=-                    */

}

int event()   /* Check for an event that we may want */
{
   switch(class)
   {
      case CLOSEWINDOW:     /* Did the user click the close box? */
         return(0);   /* Yes, return FALSE */
         break;
      default:
         return(1);   /* No, loop */
         break;
   }
}
