home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol1 / printer / printer.c < prev    next >
C/C++ Source or Header  |  1990-01-26  |  6KB  |  240 lines

  1. (c)  Copyright 1989 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice, and 
  3. is provided "as is" without warranty of any kind, either expressed or implied.  
  4. The entire risk as to the use of this information is assumed by the user.
  5.  
  6.  
  7.  
  8.                    Printer.c - A Printer Text 
  9.                      and Graphics Module
  10.              
  11.                       by Carolyn Scheppner 
  12.  
  13.  
  14.      One strong feature of the Amiga system software is the built-in support 
  15. for a wide variety of printers.  By supporting printers at the system level,
  16. print functions can be device-independent.  This allows the application 
  17. developer to concentrate on the code instead of worrying about the many 
  18. printer prototcol details which differ from one printer to another.  The 
  19. printer.c program listed below is a C source module which lets you easily 
  20. add printer text and graphic capability to your C programs.  
  21.  
  22. The program provides you with 6 functions:  openPrinter(), pString(), pText(), 
  23. dumpScreen(), dumpRpVp() and closePrinter().  When using this module or any 
  24. other printer code, always remember to close the printer when you are not 
  25. actively using it.  This will allow other tasks which may be running to 
  26. access the printer.
  27.  
  28. Here is a summary of the printer.c functions:
  29.  
  30.  
  31.    o  openPrinter() -  Allocates and initializes iodrp, and opens the 
  32.                        printer.device.  
  33.  
  34.                        Usage: iodrp = (struct IODRPReq *)openPrinter();
  35.  
  36.    o  closePrinter() - Closes the printer.device and deallocates the 
  37.                        iodrp.
  38.  
  39.                        Usage: closePrinter(iodrp);
  40.  
  41.  
  42. The following functions all require an open iodrp.  Note that the text 
  43. functions reference the larger iodrp as a simple IOStdRequest.
  44.    
  45.  
  46.    o  pString()      - Prints a null terminated text string.
  47.  
  48.                        Usage: error = pString(ioreq,string);
  49.  
  50.  
  51.    o  pText()        - Prints an arbitrary string of a specified 
  52.                        length.
  53.  
  54.                        Usage: error = pText(ioreq,string,length);
  55.  
  56.  
  57.    o  dumpScreen()   - Does a raster dump of the entire screen to the
  58.                        printer.
  59.  
  60.                        Usage: error = dumpScreen(iodrp,screen);
  61.  
  62.  
  63.    o  dumpRpVp()     - Does a raster dump of the specified portion of
  64.                        any rastport to the printer.
  65.  
  66.                        Usage: error = dumpRpVp(iodrp,rp,vp,x,y,w,h);
  67.  
  68. The printer.c code takes care of setting up the appropriate IO blocks and 
  69. reply port and opens and closes the device for you.  Simply call 
  70. openPrinter().  If a valid (non-zero) iodrp is returned, you may use it
  71. in calls to any of the printer.c output routines.  When you are finished,
  72. pass the iodrp to the closePrinter() routine to free up the printer for other
  73. tasks.
  74.  
  75. /*
  76.  * printer.c - routines to print text or dump rastport
  77.  *             C. Scheppner
  78.  */
  79.  
  80.  
  81. #include "exec/types.h"
  82. #include "intuition/intuition.h"
  83. #include "devices/printer.h"
  84.  
  85. extern struct IODRPReq  *openPrinter();
  86. extern struct IODRPReq  *CreateExtIO();
  87. extern struct MsgPort   *CreatePort();
  88.  
  89.  
  90. /* pString() - Prints null terminated string
  91.  *             You supply ioreq from openPrinter, and ptr to string
  92.  *             Returns 0 or error
  93.  */
  94. pString(ioreq,s)
  95. struct IOStdReq *ioreq;
  96. UBYTE *s;
  97.    {
  98.    return(pText(ioreq,s,strlen(s)));
  99.    }
  100.  
  101.  
  102.    
  103. /* pText() - Prints non-null-terminated text
  104.  *           You supply ioreq from openPrinter, ptr to text, text length
  105.  *           Returns 0 or error
  106.  */
  107. pText(ioreq,s,l)
  108. struct IOStdReq *ioreq;
  109. UBYTE *s;
  110. ULONG l;
  111.    {
  112.    int error = 1;
  113.    if(ioreq)
  114.       {
  115.       ioreq->io_Data = (APTR)s;
  116.       ioreq->io_Length = l;
  117.       ioreq->io_Command = CMD_WRITE;
  118.       error = DoIO(ioreq);
  119.       }
  120.    return(error);
  121.    }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. /* dumpScreen() - Dumps whole screen
  136.  *                You supply open iodrp and pointer to Screen
  137.  *                Returns 0 or error
  138.  */
  139. dumpScreen(iodrp,sc)
  140. struct IODRPReq *iodrp;
  141. struct Screen *sc;
  142.    {
  143.    return(dumpRpVp(iodrp,&sc->RastPort,&sc->ViewPort,0,0,sc->Width,sc->Height));
  144.    }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /* dumpRpVp() - Dumps specified portion of supplied rastport
  151.  *              You supply open iodrp, ptrs to RastPort and ViewPort,
  152.  *              upper left source x,y, and pixel w,h of source
  153.  *              Returns 0 or error
  154.  */
  155. dumpRpVp(iodrp,rp,vp,x,y,w,h)
  156. struct IODRPReq *iodrp;
  157. struct RastPort *rp;
  158. struct ViewPort *vp;
  159. ULONG  x, y, h, w;
  160.    {
  161.    int error = -1;
  162.  
  163.    if(iodrp)
  164.       {
  165.       iodrp->io_Command = PRD_DUMPRPORT;
  166.       iodrp->io_RastPort = rp;
  167.       iodrp->io_ColorMap = vp->ColorMap;
  168.       iodrp->io_Modes = (ULONG)vp->Modes;
  169.       iodrp->io_SrcX = x;
  170.       iodrp->io_SrcY = y;
  171.       iodrp->io_SrcWidth = w;
  172.       iodrp->io_SrcHeight = h;
  173.       /* iodrp->io_DestCols = 0; MEMF_CLEAR zeroed this  */
  174.       /* iodrp->io_DestRows = 0; MEMF_CLEAR zeroed this  */
  175.       iodrp->io_Special = SPECIAL_FULLCOLS|SPECIAL_ASPECT;
  176.  
  177.       error = DoIO(iodrp);
  178.       }
  179.    return(error);
  180.    }
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. /* openPrinter() - Returns largest printer IORequest (iodrp) or 0=failure
  195.  *                 Cast return as IOStdReq for plain text printing
  196.  */
  197. struct IODRPReq *
  198. openPrinter()
  199.    {
  200.    struct IODRPReq *iodrp;    /* Largest printer request */
  201.    struct MsgPort  *printerPort;
  202.    int error = 1;
  203.  
  204.    if(printerPort = CreatePort("CBM_prtport",0))
  205.       {
  206.       if(iodrp=CreateExtIO(printerPort,sizeof(struct IODRPReq)))
  207.          {
  208.          if(!(error=OpenDevice("printer.device",0,iodrp,0)))
  209.             {
  210.             return(iodrp);
  211.             }
  212.          DeleteExtIO(iodrp);
  213.          }
  214.       DeletePort(printerPort);
  215.       }
  216.    return((struct IODRPReq *)NULL);  /* failure */
  217.    }
  218.  
  219.  
  220.  
  221.  
  222. closePrinter(iodrp)
  223. struct IODRPReq *iodrp;
  224.    {
  225.    struct MsgPort *msgport;
  226.  
  227.    if(iodrp)
  228.       {
  229.       CloseDevice(iodrp);
  230.       msgport = iodrp->io_Message.mn_ReplyPort;
  231.       DeleteExtIO(iodrp);
  232.       DeletePort(msgport);
  233.       }
  234.    }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.