home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / After Dark / Twist 1.0b1 / StubMod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-05  |  3.6 KB  |  127 lines  |  [TEXT/MPCC]

  1. /*
  2.     StubMod.c
  3.     This file creates the 68k 'ADgm' resource to call your PPC module.
  4.     Its designed to be compiled as a code resource and then included in your PPC shared libary project.
  5.  
  6.     Created by Steve Zellers
  7.     6/2/95    aea (Andrew Armstrong) Modifed for CodeWarrior & proper CPU checking.
  8. */
  9.  
  10. #define GENERATING68K 1
  11. #include <ConditionalMacros.h>
  12. #include <CodeFragments.h>
  13. #include <MixedMode.h>
  14. #include <Types.h>
  15. #include <OSUtils.h>
  16. #include <Resources.h>
  17. #include <Memory.h>
  18. #include <A4Stuff.h>
  19. #include <Quickdraw.h>
  20. #include <LowMem.h>
  21. #include <GestaltEqu.h>
  22.  
  23. // 6/2/95 aea    NewRoutineDescriptor is allready defined in MixedMode.h as 68k code.
  24. // By declaring MyNewRoutineDescriptor, I am able to call the MixedModeManager trap.
  25. extern pascal UniversalProcPtr MyNewRoutineDescriptor(ProcPtr theProc, ProcInfoType theProcInfo, ISAType theISA) = {0x7000, 0xAA59};
  26.  
  27. #include "GraphicsModule_Types.h"
  28.  
  29. #define kFragmentName    "\pmain"
  30. #define kEntryPointName    "\pmain"
  31.  
  32. ConnectionID theConnectionID;
  33. Ptr theEntryPoint;
  34. typedef pascal OSErr (*ModuleProcPtr)(Handle* storage, RgnHandle rgn, short msg, GMParamBlockPtr params);
  35.  
  36. ModuleProcPtr theDescriptor = 0l;
  37.  
  38. enum {
  39.     eModuleDispatchSelector = kPascalStackBased
  40.          | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  41.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( Handle*)))
  42.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( RgnHandle)))
  43.          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(short)))
  44.          | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( GMParamBlock*)))
  45. };
  46.  
  47.  
  48. static OSErr LocateFile(short refNum, FSSpec* location, long* fileLen)
  49. {
  50.     FCBPBRec fcbPB;
  51.     OSErr result;
  52.  
  53.     fcbPB.ioNamePtr = (StringPtr)location->name;
  54.     fcbPB.ioCompletion = nil;
  55.     fcbPB.ioFCBIndx = 0;
  56.     fcbPB.ioRefNum = refNum;
  57.     result = PBGetFCBInfo(&fcbPB, false);
  58.     if (result == noErr) {
  59.         location->vRefNum = fcbPB.ioFCBVRefNum;
  60.         location->parID = fcbPB.ioFCBParID;
  61.         *fileLen = fcbPB.ioFCBPLen;
  62.     }
  63.  
  64.     return result;
  65. }
  66.  
  67. pascal OSErr main(Handle* storage, RgnHandle rgn, short msg, GMParamBlockPtr params)
  68. {
  69.     OSErr err = noErr;
  70.     long    nativeCPUtype;
  71.     long    oldA4;
  72.     
  73.     oldA4 = SetCurrentA4();
  74.     // 6/2/95 aea Were going to assume that Apple will only produce PPC processors for awhile.
  75.     // so instead of chekcing for the code fragment manager (which can exist on 68k machines)
  76.     // we will simply look for anything after gestaltCPU601
  77.     err = Gestalt('cput', &nativeCPUtype);
  78.     if ((err != noErr) || (nativeCPUtype < gestaltCPU601))
  79.     {
  80.         Handle errorHandle;
  81.         
  82.         errorHandle = Get1Resource('STR ', 150);
  83.         
  84.         if (!errorHandle)
  85.             BlockMove("\pSorry, this module only runs on a PowerMacintosh.", params->errorMessage, 255);
  86.         else
  87.         {
  88.             HLock(errorHandle);
  89.             BlockMove(*errorHandle, params->errorMessage, 255);
  90.             ReleaseResource(errorHandle);
  91.         }
  92.  
  93.         ExitCodeResource();
  94.         return -1;    
  95.     }
  96.  
  97.     // open the fragment
  98.     if (msg == Initialize) {
  99.         Ptr exportedEntryPoint;
  100.         SymClass theClass;
  101.         FSSpec thisSpec;
  102.         long thisLen;
  103.  
  104.         LocateFile(LMGetCurMap(), &thisSpec, &thisLen);
  105.         err = GetDiskFragment(&thisSpec, 0, kWholeFork, kFragmentName, kLoadNewCopy, &theConnectionID, (Ptr*) &exportedEntryPoint, params->errorMessage);
  106.  
  107.         if (err == noErr)
  108.             err = FindSymbol(theConnectionID, kEntryPointName, &theEntryPoint, &theClass);
  109.     
  110.         if (err == noErr)
  111.             theDescriptor = (ModuleProcPtr) MyNewRoutineDescriptor((ProcPtr) theEntryPoint, eModuleDispatchSelector, kPowerPCISA);
  112.     }
  113.     
  114.     if (theDescriptor && (err == noErr)) {
  115.         err = (*theDescriptor)(storage, rgn, msg, params);
  116.     }
  117.  
  118.     if (msg == Close ||  err != noErr) {
  119.         DisposeRoutineDescriptor((UniversalProcPtr) theDescriptor);
  120.         CloseConnection(&theConnectionID);
  121.  
  122.     }
  123.  
  124.     ExitCodeResource();
  125.     return err;
  126. }
  127.