home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * PAP.C *
- * *
- * With thanks to Mike Schuster whose article in MacTutor showed how to *
- * access the PAP code in the LaserWriter. Also thanks to the CAP team *
- * at for their PAP code and printer access code. *
- * *
- ************************************************************************/
-
- #include "PAP.H"
-
- static Handle papHndl = NIL; /* Handle to PAP manager resource */
- static Ptr papPtr = NIL; /* Points to locked PAP manager */
- static Handle entHndl = NIL; /* Handle to locked entity name */
-
- Str255 LaserWriterName = "\pLaserWriter";
-
- void SetExtnsFolder(void);
-
-
- /************************************************************************
- ************************************************************************/
- OSErr PAPOpen(integer *prefnum, EntityPtr printer,
- integer quantum, PAPStatusRec *status, integer *ostate)
- {
- asm {
- CLR.W -(SP)
- MOVE.L prefnum, -(SP)
- MOVE.L printer, -(SP)
- MOVE.W quantum, -(SP)
- MOVE.L status, -(SP)
- MOVE.L ostate, -(SP)
- MOVE.L papPtr, A0
- JSR 0(A0)
- MOVE.W (SP)+, D0
- }
- }
-
- /************************************************************************
- ************************************************************************/
- OSErr PAPRead(integer prefnum, Ptr data, integer *size,
- integer *eof, integer *rstate)
- {
- asm {
- CLR.W -(SP)
- MOVE.W prefnum, -(SP)
- MOVE.L data, -(SP)
- MOVE.L size, -(SP)
- MOVE.L eof, -(SP)
- MOVE.L rstate, -(SP)
- MOVE.L papPtr, A0
- JSR 4(A0)
- MOVE.W (SP)+, D0
- }
- }
-
- /************************************************************************
- ************************************************************************/
- OSErr PAPWrite(integer prefnum, Ptr data, integer size,
- integer eof, integer *wstate)
- {
- asm {
- CLR.W -(SP)
- MOVE.W prefnum, -(SP)
- MOVE.L data, -(SP)
- MOVE.W size, -(SP)
- MOVE.W eof, -(SP)
- MOVE.L wstate, -(SP)
- MOVE.L papPtr, A0
- JSR 8(A0)
- MOVE.W (SP)+, D0
- }
- }
-
- /************************************************************************
- * This is the PAPStatus routine using the LaserWriter builtin PAP, but *
- * it doesn't always return a status, so the real PAP status is below *
- ************************************************************************/
- OSErr papStatus(EntityPtr prName, PAPStatusRec *status, AddrBlock *prtAddr);
- OSErr PAPStatus(EntityPtr prName, PAPStatusRec *status, AddrBlock *prtAddr)
- {
- asm {
- CLR.W -(SP)
- MOVE.L prName, -(SP)
- MOVE.L status, -(SP)
- MOVE.L prtAddr, -(SP)
- MOVE.L papPtr, A0
- JSR 12(A0)
- MOVE.W (SP)+, D0
- }
- }
- /************************************************************************/
-
- /************************************************************************
- ************************************************************************/
- OSErr PAPClose(integer prefnum)
- {
- asm {
- SUBQ.L #2, SP
- MOVE.W prefnum, -(SP)
- MOVE.L papPtr, A0
- JSR 16(A0)
- MOVE.W (SP)+, D0
- }
- }
-
- /************************************************************************
- ************************************************************************/
- Handle PAPLoad()
- {
- integer LWref;
- integer wdRef;
- LongInt wdID;
- integer vol;
-
- if (entHndl) return entHndl; /* already loaded */
-
- papHndl = NIL;
- papPtr = NIL;
- entHndl = NIL;
-
- GetWDir(&wdRef, &wdID, &vol);
- SetExtnsFolder();
-
- if ( (LWref = OpenResFile(LaserWriterName)) == -1 )
- {
- log_printf("Cannot open laserwriter... \n");
- SetWDir(wdRef, wdID, vol);
- return NIL;
- }
-
- if ( (papHndl = GetResource('PDEF', 10) ) == NIL || ResError() )
- {
- log_printf("Cannot find PDEF... \n");
- CloseResFile(LWref);
- SetWDir(wdRef, wdID, vol);
- return NIL;
- }
- DetachResource(papHndl);
- HLock(papHndl);
- papPtr = *papHndl;
-
- if ( ( entHndl = GetResource('PAPA', -8192) ) == NIL || ResError() )
- {
- HUnlock(papHndl);
- DisposHandle(papHndl);
- CloseResFile(LWref);
- log_printf("Cannot find name...\n");
- SetWDir(wdRef, wdID, vol);
- return NIL;
- }
-
- DetachResource(entHndl);
- HLock(entHndl);
-
- CloseResFile(LWref);
-
- SetWDir(wdRef, wdID, vol);
- return entHndl;
- }
-
-
- /************************************************************************
- ************************************************************************/
- OSErr PAPUnload()
- {
- integer res;
-
- if (papPtr)
- asm {
- MOVE.L papPtr, A0
- CLR.W -(SP)
- JSR 20(A0)
- MOVE.W (SP)+, res
- }
-
- if (entHndl)
- {
- HUnlock(entHndl);
- DisposHandle(entHndl);
- entHndl = NIL;
- }
-
- if (papHndl)
- {
- HUnlock(papHndl);
- DisposHandle(papHndl);
- papHndl = NIL;
- papPtr = NIL;
- }
- return res;
- }
-
- /************************************************************************
- * *
- * The following routine was derived from the same routine in the PAP *
- * code in the CAP package. *
- * *
- ************************************************************************/
-
- OSErr FindPrinter(EntityPtr printer, AddrBlock *addr);
-
- OSErr papStatus(EntityPtr printer,
- PAPStatusRec *status, AddrBlock *prtAddr)
- {
- ATPParamBlock abr;
- ATPParamBlock *atpr;
- BDSElement bds[1];
- PAPUserBytes *pub;
- OSErr err;
- AddrBlock addr;
-
- EntityName bf;
- Byte *prtname = (Byte*)&bf;
-
- BlockMove(printer, &bf, sizeof(EntityName));
-
- if (prtAddr->aNet == 0 || prtAddr->aNode == 0 || prtAddr->aSocket == 0)
- {
- if (err = FindPrinter(&bf, &addr) )
- {
- psprintf(status->statusStr, "%%Can't find <%p:%p@%p>",
- prtname,
- prtname+(prtname[0]+1),
- prtname+(prtname[0]+1)+(prtname[(prtname[0]+1)]+1));
- return err;
- }
- *prtAddr = addr;
- }
- else
- addr = *prtAddr;
-
- atpr = &abr;
- atpr->ATPuserData = 0;
- pub = (PAPUserBytes*) &atpr->ATPuserData;
- pub->PAPtype = papSendStatus;
-
- atpr->ATPaddrBlock = addr;
- atpr->ATPatpSocket = 0;
- atpr->ATPreqLength = 0;
- atpr->ATPreqPointer = NIL;
- atpr->ATPbdsPointer = (Ptr)bds;
- bds[0].buffPtr = (Ptr)status;
- bds[0].buffSize = sizeof(PAPStatusRec);
- atpr->ATPnumOfBuffs = 1; /* actually numOfBuffs */
- atpr->ATPatpFlags = 0;
-
- atpr->ATPtimeOutVal = 1;
- atpr->ATPretryCount = 3;
-
- if ((err = PSendRequest(&abr, FALSE)) == reqFailed)
- {
- psprintf(status->statusStr, "%%<%p:%p@%p> not responding",
- prtname,
- prtname+(prtname[0]+1),
- prtname+(prtname[0]+1)+(prtname[(prtname[0]+1)]+1));
- return reqFailed;
- }
-
- if (atpr->ATPnumOfResps < 1 && bds[0].dataSize < min_PAPpkt_size)
- return -1;
- pub = (PAPUserBytes*)&bds[0].userBytes;
- if (pub->PAPtype != papStatusReply)
- return -1;
-
- return noErr;
- }
-
- /************************************************************************
- ************************************************************************/
- OSErr FindPrinter(EntityPtr printer, AddrBlock *addr)
- {
- char buffer[100];
- MPPParamBlock mpp;
- MPPParamBlock *nbp = &mpp;
- OSErr err;
-
- nbp->NBPinterval = 8;
- nbp->NBPcount = 3;
- nbp->NBPentityPtr = (Ptr)printer;
- nbp->NBPretBuffPtr = buffer;
- nbp->NBPretBuffSize = 100;
- nbp->NBPmaxToGet = 1;
-
- if ( (err = PLookupName(&mpp, FALSE)) == noErr )
- err = NBPExtract(buffer, nbp->NBPnumGotten, 1, printer, addr);
-
- if (err != noErr)
- log_printf("NBP Name lookup error %d\n", err);
-
- return err;
- }
-