home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Apple Guide / Engineering / Context Check Modules / Standard CC Modules / Chooser Context / Chooser.c next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  10.8 KB  |  411 lines  |  [TEXT/MPS ]

  1. //    Copyright:    © 1993 Apple Computer, Inc. All rights reserved.
  2. //    Author:        Scott Searle (original)
  3. //                Victor J. Hnyp (extensions)
  4. //    Date:        24-Mar-93
  5.  
  6. // Revisions
  7. //    4/25/94        SG    3.01    Fixed:    Replace hard coded strings LaserWriter and AppleTalk ImageWriter with 2 char[32]s
  8. //
  9. //    4/22/94        SG    3.00     New:    New Projector database
  10. //
  11. //    9/3/93        JJ     2.06    Added:     IsAnyZones
  12. //
  13. //    03/24/93    VJH    2.05    Fixed:    isPortInUse
  14. //
  15. //    03/15/93    VJH    2.04    Fixed:    IsPortSelected, isPortInUse
  16. //
  17. //    02/08/93    VJH    2.03    Fixed:    AppleTalk related states
  18. //
  19. //    01/18/93    VJH    2.02    Fixed:    AppleTalk related states
  20. //
  21. //    12/14/92    VJH    2.01    Added:    Is a printer connected directly to this Macintosh?
  22. //                                    Is the number of network printers connected to our zone...?
  23. //                                    Is a serial printer connected to this Macintosh through ModemPort/PrinterPort?
  24. //                                    Is a printer connected to this Macintosh through a SCSI port?
  25. //                                    Is the number of printers connected...?
  26. //                                    Is a printer currently selected through the Chooser?
  27. //                                    Is the selected print driver "..."?
  28. //                                    Is AppleTalk on/off?
  29. //                                    Does the network have any zones?
  30. //                                    Is ModemPort/PrinterPort currently selected?
  31. //                                    Is ModemPort/PrinterPort currently in use?
  32. #pragma    load "AllHeaders.dump"
  33.  
  34. #include "Utility.h"
  35. #include "Proto.h"
  36. #include "Context.h"
  37. #include "Chooser.h"
  38.  
  39. #define    kPrinterNameStrID        (-8192)
  40. #define kMaxResponses            8
  41. #define UTableBase                0x11C
  42.  
  43.  
  44. /* ------------------ Forward declaration -------------------- */
  45. pascal Boolean IsPrinterName(PrinterInfoPtr msg);
  46. pascal Boolean DetermineATState(ATStateInfoPtr msg);
  47. pascal Boolean NumZonePrintersConnected(CompareInfoPtr msg);
  48. pascal Boolean NumberOfZonesCompare(CompareInfoPtr msg);
  49. pascal Boolean IsPortSelectedCompare(ATStateInfoPtr msg);
  50. pascal Boolean IsPortInUseCompare(ATStateInfoPtr msg);
  51. pascal Boolean IsAnyZones();
  52. SPortSel GetPrinterConnection(void);
  53.  
  54.  
  55. //const char    printer1[32] = "\pLaserWriter";                /* allocate 2 strings long enough for localization purpose */
  56. //const char    printer2[32] = "\pAppleTalk ImageWriter";
  57.  
  58.  
  59. pascal OSErr main(ContextSelectorPtr msg, Size inSize,
  60.                     void* outMessage, Size* outSize, Handle /*startGlobals*/)
  61. {
  62.     Boolean        ret    =    false;
  63.     OSErr        err    =    errAECorruptData;
  64.     
  65.     if(inSize < sizeof(ContextSelector))                    /* If inSize is < length of selector (a long), */
  66.         return(err);                                        /* return an error. Would be nice to have a specific error # */
  67.     
  68.     switch (msg->selector)
  69.     {
  70.         case isPrinterDirect:                // Is a printer connected directly to this Macintosh?
  71.             ret = false;
  72.             break;
  73.  
  74.         case isPrintersOnZone:                // Is the number of network printers connected to our zone...?
  75.             ret = NumZonePrintersConnected((CompareInfoPtr) msg);
  76.             break;
  77.  
  78.         case isPrinterSerial:                // Is a serial printer connected to this Macintosh through ModemPort/PrinterPort?
  79.             ret = false;
  80.             break;
  81.  
  82.         case isPrinterSCSI:                    // Is a printer connected to this Macintosh through a SCSI port?
  83.             ret = false;
  84.             break;
  85.  
  86.         case isPrinterType:                    // Is the type of the selected printer "..."?
  87.             ret = IsPrinterName((PrinterInfoPtr)msg);
  88.             break;
  89.             
  90.         case isAppleTalk:                    // Is AppleTalk on/off?
  91.             ret = DetermineATState((ATStateInfoPtr)msg);
  92.             break;
  93.  
  94.         case isNumberOfZones:                // Does the network have any zones?
  95.             ret = NumberOfZonesCompare((CompareInfoPtr) msg);
  96.             break;
  97.  
  98.         case isPortSelected:                // Is ModemPort/PrinterPort currently selected?
  99.             ret = IsPortSelectedCompare((ATStateInfoPtr) msg);
  100.             break;
  101.  
  102.         case isPortInUse:                    // Is ModemPort/PrinterPort currently in use?
  103.             ret = IsPortInUseCompare((ATStateInfoPtr) msg);
  104.             break;
  105.  
  106.         case isAnyZones:                    // Are there 2 or 3 boxes in the Chooser?
  107.             ret = IsAnyZones();
  108.             break;
  109.  
  110.         default:                                            /* None of the pre-defined types. Exit with error */
  111.             return(err);                                    /* Would be nice to have a specific error # */
  112.     }
  113.  
  114.     err    = SetContextResult(&ret, sizeof(Boolean), outMessage, outSize);
  115.     return(err);
  116. }
  117.  
  118.  
  119.  
  120. pascal Boolean IsPrinterName(PrinterInfoPtr msg)
  121. {
  122.     Str255            name;
  123.     Boolean            ret    =    false;
  124.     OSErr            err    =    noErr;
  125.  
  126.     if ((err = GetTheString(kPrinterNameStrID, name)) == noErr)
  127.     {
  128.         ret    = CompareStringSpec(name, &msg->printerName);
  129.     }
  130.  
  131.     return(ret);
  132. }
  133.  
  134.  
  135.  
  136. pascal Boolean DetermineATState(ATStateInfoPtr msg)
  137. {
  138.     Boolean            ret    =    false;
  139.     OSErr            err    =    noErr;
  140.     
  141.     err = ATPLoad();                            // Attempt to load AppleTalk
  142.     
  143.     if((msg->wantedATState) &&                    // If checking for AppleTalk ON
  144.     (!err))                                        // and no error opening the driver
  145.         ret = true;
  146.     
  147.     if(!(msg->wantedATState) &&                    // If checking for AppleTalk OFF
  148.     (err))                                        // and error opening the driver
  149.         ret = true;
  150.     
  151.     return(ret);
  152. }
  153.  
  154.  
  155.  
  156. pascal Boolean NumZonePrintersConnected(CompareInfoPtr msg)
  157. {
  158.     Boolean                ret    =    false;
  159.     OSErr                err    = noErr;
  160.     short                totalCount;
  161.     char                addressBook[kMaxResponses*sizeof(EntityName)];    /* ATP Max buffer */
  162.     EntityName            lookupEntity;                                /* entity currently being looked up */
  163.     NBPparms            NBPPB;                                        /* used for general NBP stuff */
  164.     const char            printer1[32] = "\pLaserWriter";                /* allocate 2 strings long enough for localization purpose */
  165.     const char            printer2[32] = "\pAppleTalk ImageWriter";
  166.     
  167.     if( ATPLoad() )                                                    // AppleTalk is not active!
  168.         return( ret );
  169.         
  170.     NBPSetEntity( (Ptr)&lookupEntity, "\p=", printer1, "\p*");    /* Only within this zone */
  171.  
  172.     totalCount = 0;                                                    /* We got no replies yet */
  173.     
  174.     /* Set up the NBP lookup */
  175.     NBPPB.interval = 4;                                                /* Try for 4 seconds */
  176.     NBPPB.count = 4;                                                /* Try up to 4 times */
  177.     NBPPB.NBPPtrs.entityPtr = (Ptr)&lookupEntity;                    /* This is what we're looking for */
  178.     NBPPB.parm.Lookup.retBuffPtr = addressBook;                        /* Put responses here */
  179.     NBPPB.parm.Lookup.retBuffSize = kMaxResponses*sizeof(EntityName);        /* Buffer size */
  180.     NBPPB.parm.Lookup.maxToGet = kMaxResponses;                        /* How many will we accept? */
  181.     NBPPB.parm.Lookup.numGotten = 0;                                /* Didn't get any yet */
  182.  
  183.     /* Perform the lookup, SYNCHRONOUSLY */
  184.     err = PLookupName((MPPPBPtr)&NBPPB,false);
  185.     
  186.     if( !(err) )
  187.     {
  188.         totalCount = NBPPB.parm.Lookup.numGotten;                    /* Save off # of laserWriters */
  189.         
  190.         NBPSetEntity( (Ptr)&lookupEntity, "\p=", printer2, "\p*");        /* Only within this zone */
  191.  
  192.         /* Set up the NBP lookup */
  193.         NBPPB.interval = 4;                                            /* Try for 4 seconds */
  194.         NBPPB.count = 4;                                            /* Try up to 4 times */
  195.         NBPPB.NBPPtrs.entityPtr = (Ptr)&lookupEntity;                /* This is what we're looking for */
  196.         NBPPB.parm.Lookup.retBuffPtr = addressBook;                    /* Put responses here */
  197.         NBPPB.parm.Lookup.retBuffSize = kMaxResponses*sizeof(EntityName);        /* Buffer size */
  198.         NBPPB.parm.Lookup.maxToGet = kMaxResponses;                    /* How many will we accept? */
  199.         NBPPB.parm.Lookup.numGotten = 0;                            /* Didn't get any yet */
  200.     
  201.         /* Perform the lookup, SYNCHRONOUSLY */
  202.         err = PLookupName((MPPPBPtr)&NBPPB,false);
  203.     }
  204.  
  205.     if( err )                                                        /* For lookups, not unregistering! */
  206.         return( ret );
  207.         
  208.     totalCount = totalCount + NBPPB.parm.Lookup.numGotten;
  209.     
  210.     switch (msg->compareSelector)
  211.     {
  212.         case equals:
  213.             ret = totalCount == msg->compareValue;
  214.             break;
  215.             
  216.         case notEqualTo:
  217.             ret = totalCount != msg->compareValue;
  218.             break;
  219.             
  220.         case greaterThan:
  221.             ret = totalCount > msg->compareValue;
  222.             break;
  223.             
  224.         case lessThan:
  225.             ret = totalCount < msg->compareValue;
  226.             break;
  227.             
  228.         case greaterThanOrEqualTo:
  229.             ret = totalCount >= msg->compareValue;
  230.             break;
  231.             
  232.         case lessThanOrEqualTo:
  233.             ret = totalCount <= msg->compareValue;
  234.             break;
  235.     }
  236.             
  237.     return( ret );
  238.     
  239. } // NumZonePrintersConnected
  240.  
  241.  
  242.  
  243. pascal Boolean NumberOfZonesCompare(CompareInfoPtr msg)
  244. {
  245.     Boolean                ret    = false;
  246.     OSErr                err    = noErr;
  247.     XCallParam            xpb;
  248.     char                theBufferPtr[578];
  249.  
  250.     if( ATPLoad() )                                        // AppleTalk is not active!
  251.         return( ret );
  252.         
  253.     xpb.zipInfoField[0] = 0;
  254.     xpb.zipInfoField[1] = 0;
  255.  
  256.     xpb.ioRefNum = xppRefNum;                              // driver refNum -41
  257.     xpb.csCode = xCall;
  258.     xpb.xppSubCode = zipGetZoneList;
  259.     xpb.xppTimeout = 4;
  260.     xpb.xppRetry = 4;
  261.     xpb.zipBuffPtr = (Ptr)theBufferPtr;
  262.  
  263.     err = PBControl((ParmBlkPtr)&xpb, false);            // Asynchronous call
  264.     if( err )
  265.         return( ret );
  266.  
  267.     // At this point, xpb.zipNumZones returns the number of zones we found.
  268.     
  269.     switch (msg->compareSelector)
  270.     {
  271.         case equals:
  272.             ret = xpb.zipNumZones == msg->compareValue;
  273.             break;
  274.             
  275.         case notEqualTo:
  276.             ret = xpb.zipNumZones != msg->compareValue;
  277.             break;
  278.             
  279.         case greaterThan:
  280.             ret = xpb.zipNumZones > msg->compareValue;
  281.             break;
  282.             
  283.         case lessThan:
  284.             ret = xpb.zipNumZones < msg->compareValue;
  285.             break;
  286.             
  287.         case greaterThanOrEqualTo:
  288.             ret = xpb.zipNumZones >= msg->compareValue;
  289.             break;
  290.             
  291.         case lessThanOrEqualTo:
  292.             ret = xpb.zipNumZones <= msg->compareValue;
  293.             break;
  294.     }
  295.             
  296.     return( ret );
  297.     
  298. } // NumberOfZonesCompare
  299.  
  300.  
  301.  
  302. pascal Boolean IsPortSelectedCompare(ATStateInfoPtr msg)
  303. {
  304.     Boolean            ret    =    false;
  305.     OSErr            err    =    noErr;
  306.     SPortSel        whichPort;
  307.     
  308.     whichPort = GetPrinterConnection();
  309.     
  310.     switch (msg->wantedATState)
  311.     {
  312.         case ModemPort:
  313.             ret = whichPort == sPortA;
  314.             break;
  315.             
  316.         case PrinterPort:
  317.             ret = whichPort == sPortB;
  318.             break;
  319.     }
  320.  
  321.     return(ret);
  322. } // IsPortSelectedCompare
  323.  
  324.  
  325.  
  326. pascal Boolean IsPortInUseCompare(ATStateInfoPtr msg)
  327. {
  328.     Boolean            ret    =    false;
  329.     OSErr            err    =    noErr;
  330.     AuxDCE**         dceh;
  331.     
  332.     switch (msg->wantedATState)
  333.     {
  334.         case ModemPort:
  335.             dceh = (*(AuxDCE****)UTableBase)[5];            // .AIn
  336.             if((*dceh)->dCtlFlags & 0x20)
  337.                 ret = true;
  338.  
  339.             dceh = (*(AuxDCE****)UTableBase)[6];            // .AOut
  340.             if((*dceh)->dCtlFlags & 0x20)
  341.                 ret = true;
  342.             break;
  343.  
  344.         case PrinterPort:
  345.             err = ATPLoad();                                // Attempt to load AppleTalk
  346.             if(!err)
  347.                 ret = true;                                    // AppleTalk is on
  348.  
  349.             dceh = (*(AuxDCE****)UTableBase)[7];            // .BIn
  350.             if((*dceh)->dCtlFlags & 0x20)
  351.                 ret = true;
  352.  
  353.             dceh = (*(AuxDCE****)UTableBase)[8];            // .BOut
  354.             if((*dceh)->dCtlFlags & 0x20)
  355.                 ret = true;
  356.             break;
  357.     } // switch
  358.  
  359.     return(ret);
  360. } // IsPortInUseCompare
  361.  
  362.  
  363.  
  364. SPortSel GetPrinterConnection()
  365. /*
  366.     This function returns sPortA or sPortB, whichever
  367.     the printer is connected to, as determined from
  368.     parameter RAM.  Note:  the result is only valid in
  369.     the case where you have a printer that is connected
  370.     directly to the Macintosh.
  371. */
  372. {
  373.     SysPPtr    pRAMptr;     /* See IM-II pg. 370 */ 
  374.  
  375.     pRAMptr = GetSysPPtr();  /* See IM-II pg. 381 */  
  376.  
  377.             /* bit zero indicates which port */
  378.             /* bit zero = 0 = printer port */
  379.             /* bit zero = 1 = modem port */
  380.     if(pRAMptr->kbdPrint & 0x0001)
  381.         return sPortA;        /* Modem Port */
  382.     else
  383.         return sPortB;        /* Printer Port */
  384. }
  385.  
  386.  
  387. pascal Boolean IsAnyZones()
  388. {
  389.     Boolean            ret    =    false;
  390.     OSErr            err    =    noErr;
  391.     XPPParamBlock    myXPPPB;
  392.     char            ignoredName[33];    //    we could care less about the zone name
  393.     
  394.     if( ATPLoad() )                        // AppleTalk is not active!
  395.         return( ret );
  396.  
  397.     myXPPPB.XCALL.xppTimeout = 2;
  398.     myXPPPB.XCALL.xppRetry = 2;
  399.     myXPPPB.XCALL.zipBuffPtr = ignoredName;
  400.     myXPPPB.XCALL.zipInfoField[0] = 0;
  401.     myXPPPB.XCALL.zipInfoField[1] = 0;
  402.     
  403.     err = GetMyZone( &myXPPPB, false );    //    returns error if no router available
  404.     
  405.     ret = (Boolean)(err == noErr);         // if noErr than we are on a zone, 
  406.     
  407.     return(ret);
  408. }
  409.  
  410.  
  411.