home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Printing / IsImageWriter / IsImageWriter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  4.6 KB  |  174 lines  |  [TEXT/MPS ]

  1. #include <Printing.h>
  2. #include <Resources.h>
  3. #include <Memory.h>
  4. #include <Dialogs.h>
  5. #include <OSUtils.h>
  6. #include <OSEvents.h>
  7. #include <Fonts.h>
  8. #include <Menus.h>
  9. #include <Serial.h>
  10. #include <stdio.h>
  11. #include <OSUtils.h>
  12.  
  13.  
  14. /* Copyright 1992 Apple Computer Developer Technical Support */
  15. /* 3-9-92 - completed - Brigham Stevens */
  16.  
  17. /* This sample program demonstrates how to determine:
  18.     1. If an ImageWriter is connected
  19.     2. What type of ImageWriter
  20.     3. Which port it is connected to (invalid if AppleTalk IW)
  21. */
  22.  
  23. /* these are the wDev IDs for the ImageWriter and IW LQ printers */
  24. /* see the Q & A Stack for a discussion of the wDev field. */
  25. #define IMAGE_WRITER_WDEVID 1
  26. #define IMAGE_WRITER_LQ_WDEVID 5
  27.  
  28.  
  29. /*  
  30.     This is returned from the IsImageWriter function to
  31.     indicate which kind of ImageWriter is returned.
  32.     a result of zero, or NotImageWriter means that the
  33.     printer is - not an ImageWriter.
  34.     
  35. */
  36. typedef enum { 
  37.     NotImageWriter, 
  38.     SerialImageWriter, 
  39.     AppleTalkImageWriter, 
  40.     SerialImageWriterLQ, 
  41.     AppleTalkImageWriterLQ 
  42. } ImWrtKindFlag;
  43.  
  44. #ifdef powerc
  45.    QDGlobals    qd;
  46. #endif
  47.  
  48. ImWrtKindFlag IsImageWriter()
  49. /*
  50.     This function determines if we are talking to an ImageWriter and
  51.     then if so, what kind.  This function is reliant upon the signature
  52.     resource being 'IWRX' for an AppleTalk ImageWriter, or 'IWRT' for a 
  53.     serial connected ImageWriter, 
  54.     or respectivly 'BWRT', or 'BWRX' for an LQ ImageWriter
  55. */
  56. {
  57.     THPrint    tpr;
  58.     short    prtNum;
  59.     short    result = NotImageWriter;
  60.     
  61.     tpr = (THPrint)NewHandle(sizeof(TPrint));
  62.     if(!tpr) {
  63.         result = MemError();
  64.         goto bail;
  65.     }
  66.     
  67.     PrOpen();        /* open the print driver */
  68.     if(result = PrError())    goto bail;
  69.     
  70.     PrintDefault(tpr);    /* init the print record */
  71.     if(result = PrError())    goto bail;
  72.  
  73.     /* get the printer ID number from the high byte */
  74.     /* of the wDev field.  This is a quasi-unique */
  75.     /* number for each printer type. See the Q & A Stack */
  76.     /* for more information on the wDev field, and the */
  77.     /* problems with relying on it. For the purposed of checking */
  78.     /* for a color ribbon, you should be ok.  You may have to */
  79.     /* revise this technique at some point, when the new print architecture */
  80.     /* is available. */
  81.     prtNum = (**tpr).prStl.wDev >> 8;
  82.  
  83.     if ( prtNum == IMAGE_WRITER_WDEVID )  {
  84.         /* we are talking to an ImageWriter, now see
  85.             if it is serial or appleTalk. */
  86.         if(Count1Resources('IWRT') > 0)
  87.             result = SerialImageWriter;
  88.         else if(Count1Resources('IWRX') > 0)
  89.             result = AppleTalkImageWriter;
  90.         else /* This must be a printer acting like an ImageWriter */
  91.             result = NotImageWriter;
  92.     }
  93.     else if ( prtNum == IMAGE_WRITER_LQ_WDEVID ) {
  94.         if(Count1Resources('BWRT') > 0)
  95.             result = SerialImageWriterLQ;
  96.         else if(Count1Resources('BWRX') > 0)
  97.             result = AppleTalkImageWriterLQ;
  98.         else /* This must be a printer acting like an ImageWriterLQ */
  99.             result = NotImageWriter;
  100.     }
  101. bail:
  102.     if(tpr)
  103.         DisposHandle((Handle)tpr);    /* get rid of our print handle */
  104.     PrClose();        /* close the print driver */
  105.     return result;
  106. }
  107.  
  108. SPortSel GetPrinterConnection()
  109. /*
  110.     This function returns sPortA or sPortB, whichever
  111.     the printer is connected to, as determined from
  112.     parameter RAM.  Note:  the result is only valid in
  113.     the case where you have a printer that is connected
  114.     directly to the Macintosh.
  115. */
  116. {
  117.     SysPPtr    pRAMptr;     /* See IM-II pg. 370 */ 
  118.  
  119.     pRAMptr = GetSysPPtr();  /* See IM-II pg. 381 */  
  120.  
  121.             /* bit zero indicates which port */
  122.             /* bit zero = 0 = printer port */
  123.             /* bit zero = 1 = modem port */
  124.     if(pRAMptr->kbdPrint & 0x0001)
  125.         return sPortA;        /* Modem Port */
  126.     else
  127.         return sPortB;        /* Printer Port */
  128. }
  129.  
  130.  
  131. main ()
  132. {
  133.  
  134.     ImWrtKindFlag        iw;
  135.     SPortSel            whichPort;
  136.     
  137.     InitGraf(&qd.thePort);
  138.     InitFonts();
  139.     InitWindows();
  140.     InitMenus();
  141.     InitCursor();
  142.     TEInit();
  143.     FlushEvents(everyEvent, 0);
  144.     InitDialogs(nil);
  145.  
  146.     printf("\n");
  147.     switch(iw = IsImageWriter()) {
  148.         case NotImageWriter:
  149.                 printf("\nWe are NOT connected to an ImageWriter.");
  150.                 break;
  151.         case SerialImageWriter:
  152.                 printf("\nWe are connected to a Serial ImageWriter.");
  153.                 break;
  154.         case AppleTalkImageWriter:
  155.                 printf("\nWe are connected to an AppleTalk ImageWriter.");
  156.                 break;
  157.         case SerialImageWriterLQ:
  158.                 printf("\nWe are connected to a Serial ImageWriterLQ.");
  159.                 break;
  160.         case AppleTalkImageWriterLQ:
  161.                 printf("\nWe are connected to a AppleTalk ImageWriterLQ.");
  162.                 break;
  163.         default:
  164.                 printf("\nIsImageWriter error code: %i",iw);
  165.     }
  166.     whichPort = GetPrinterConnection();
  167.     if(whichPort == sPortA)
  168.         printf("\nThe printer is connected to the modem port.");
  169.     else if(whichPort == sPortB)
  170.         printf("\nThe printer is connected to the printer port.");
  171.     else
  172.         printf("\nThe printer is connected to an unknown port.");
  173.     printf("\n");
  174. }