home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / printer / demo_dump.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  15KB  |  314 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  ***************************************************************************
  27.  *
  28.  * Demo_Dump.c
  29.  *
  30.  * Simple example of dumping a rastport to the printer, changing
  31.  * printer preferences programmatically and handling error codes.
  32.  *
  33.  * Compile with SAS C 5.10a. lc -cfist -v -L Demo_Dump
  34.  *
  35.  * Run from CLI only
  36.  */
  37.  
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <exec/ports.h>
  41. #include <devices/printer.h>
  42. #include <devices/prtbase.h>
  43. #include <dos/dos.h>
  44. #include <intuition/intuition.h>
  45. #include <intuition/screens.h>
  46. #include <graphics/displayinfo.h>
  47.  
  48. #include <clib/exec_protos.h>
  49. #include <clib/alib_protos.h>
  50. #include <clib/alib_stdio_protos.h>
  51. #include <clib/graphics_protos.h>
  52. #include <clib/intuition_protos.h>
  53.  
  54. struct IntuitionBase *IntuitionBase;
  55. struct GfxBase *GfxBase;
  56.  
  57. union printerIO
  58. {
  59.     struct IOStdReq    ios;
  60.     struct IODRPReq    iodrp;
  61.     struct IOPrtCmdReq iopc;
  62. };
  63.  
  64. struct EasyStruct reqES =
  65. {
  66.     sizeof(struct EasyStruct), 0, "DemoDump",
  67.     "%s",
  68.     NULL,
  69. };
  70.  
  71. /* Possible printer.device and I/O errors */
  72. static UBYTE *ErrorText[] =
  73. {
  74.     "PDERR_NOERR",
  75.     "PDERR_CANCEL",
  76.     "PDERR_NOTGRAPHICS",
  77.     "INVERTHAM",        /* OBSOLETE */
  78.     "BADDIMENSION",
  79.     "DIMENSIONOVFLOW",    /* OBSOLETE */
  80.     "INTERNALMEMORY",
  81.     "BUFFERMEMORY",
  82.     /* IO_ERRs */
  83.     "IOERR_OPENFAIL",
  84.     "IOERR_ABORTED",
  85.     "IOERR_NOCMD",
  86.     "IOERR_BADLENGTH"
  87. };
  88.  
  89. /* Requester Action text */
  90. static UBYTE *ActionText[] =
  91. {
  92.     "OK|CANCEL",
  93.     "Continue",
  94.     "Abort",
  95. };
  96.  
  97. #define OKCANCELTEXT 0
  98. #define CONTINUETEXT 1
  99. #define ABORTTEXT    2
  100.  
  101. VOID main(VOID);
  102.  
  103. VOID main(VOID)
  104. {
  105. struct MsgPort  *PrinterMP;
  106. union printerIO *PIO;
  107. struct PrinterData *PD;
  108. struct PrinterExtendedData *PED;
  109. struct Screen *pubscreen;
  110. struct ViewPort *vp;
  111. STRPTR textbuffer;
  112. LONG modeID, i,j;
  113. ULONG dcol[5], drow[5];
  114. ULONG signal;
  115.  
  116. /* Fails silently if not V37 or greater. Nice thing to do would be to put up
  117.  * a V33 requester of course.
  118.  */
  119.  
  120. /* Set up once */
  121. reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  122.  
  123. if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37))
  124.     {
  125.     /* Using graphics.library to get the displaymodeID of the public screen,
  126.      * which we'll pass to the printer.device.
  127.      */
  128.     if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37))
  129.         {
  130.         if (textbuffer = (STRPTR)AllocMem(256, MEMF_CLEAR))
  131.             {
  132.             /* Create non-public messageport. Since we depend on V37 already, we'll
  133.              * use the new Exec function.
  134.              */
  135.             if (PrinterMP = CreateMsgPort())
  136.                 {
  137.                 /* Allocate printerIO union */
  138.                 if (PIO = (union printerIO *)CreateExtIO(PrinterMP, sizeof(union printerIO)))
  139.                     {
  140.                     /* Open the printer.device */
  141.                     if (!(OpenDevice("printer.device",0,(struct IORequest *)PIO,0)))
  142.                         {
  143.                         /* Yahoo, we've got it.
  144.                          * We'll use the PrinterData structure to get to the the printer
  145.                          * preferences later on. The PrinterExtendedData structure will
  146.                          * reflect the changes we'll make to the preferences.
  147.                          */
  148.  
  149.                         PD = (struct PrinterData *)PIO->iodrp.io_Device;
  150.                         PED = (struct PrinterExtendedData *)&PD->pd_SegmentData->ps_PED;
  151.  
  152.                         /* We're all set. We'll grab the default public screen (normally
  153.                          * Workbench) and see what happens when we dump it with different
  154.                          * densities.
  155.                          * Next we'll put up a nice requester for the user and ask if
  156.                          * (s)he wants to actually do the dump.
  157.                          */
  158.  
  159.                         if (pubscreen = LockPubScreen(NULL))
  160.                             {
  161.                             vp = &(pubscreen->ViewPort);
  162.                             /* Use graphics.library/GetVPModeID() to get the ModeID of the screen. */
  163.                             if ((modeID = GetVPModeID(vp)) != INVALID_ID)
  164.                                 {
  165.                                 /* Seems we got a valid ModeID for the default public screen (surprise).
  166.                                  * Do some fake screen dumps with densities 1, 3, 5 and 7. Depending on
  167.                                  * the driver, one or more may be the same.
  168.                                  */
  169.  
  170.                                 /* Fill in those parts of the IODRPRequest which won't change */
  171.                                 PIO->iodrp.io_Command = PRD_DUMPRPORT;
  172.                                 PIO->iodrp.io_RastPort = &(pubscreen->RastPort);
  173.                                 PIO->iodrp.io_ColorMap = vp->ColorMap;
  174.                                 PIO->iodrp.io_Modes = modeID;
  175.                                 PIO->iodrp.io_SrcX = pubscreen->LeftEdge;
  176.                                 PIO->iodrp.io_SrcY = pubscreen->TopEdge;
  177.                                 PIO->iodrp.io_SrcWidth = pubscreen->Width;
  178.                                 PIO->iodrp.io_SrcHeight = pubscreen->Height;
  179.  
  180.                                 for (i = 1,j=0; i < 8; i+=2,j++)
  181.                                     {
  182.                                     /* On return these will contain the actual dump dimension */
  183.                                     PIO->iodrp.io_DestCols = 0;
  184.                                     PIO->iodrp.io_DestRows = 0;
  185.                                     /* We'll simply change our local copy of the
  186.                                      * Preferences structure. Likewise we could change
  187.                                      * all printer-related preferences.
  188.                                      */
  189.                                     PD->pd_Preferences.PrintDensity = i;
  190.                                     PIO->iodrp.io_Special = SPECIAL_NOPRINT|SPECIAL_ASPECT;
  191.  
  192.                                     /* No need to do asynchronous I/O here */
  193.                                     DoIO((struct IORequest *)PIO);
  194.  
  195.                                     if (PIO->iodrp.io_Error == 0)
  196.                                         {
  197.                                         dcol[j] = PIO->iodrp.io_DestCols;
  198.                                         drow[j] = PIO->iodrp.io_DestRows;
  199.                                         }
  200.                                     else
  201.                                         {
  202.                                         j = PIO->iodrp.io_Error;
  203.                                         if (j < 0)
  204.                                             j = j * -1 + 7;
  205.  
  206.                                         sprintf(textbuffer, "Error: %s\n", ErrorText[j]);
  207.                                         reqES.es_GadgetFormat = ActionText[CONTINUETEXT];
  208.                                         EasyRequest(NULL, &reqES, NULL, textbuffer);
  209.