home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / WackyWindows / Patches / ShowHidePatch.c < prev   
Encoding:
C/C++ Source or Header  |  1998-06-19  |  6.0 KB  |  225 lines  |  [TEXT/CWIE]

  1. // ******************************************************************
  2. //    program written by 
  3. //                         Paul Baxter
  4. //                         MacHack'98
  5. //
  6. //
  7. // ******************************************************************
  8.  
  9. #include <A4Stuff.h>
  10. #include <StdLib.h>
  11.  
  12. #include "patch.h"
  13. #include "animate.h"
  14. // #include "debug.h"
  15.  
  16. // Semi- Automagic macros for generic Fat patches
  17. #define PatchTrap                _ShowHide
  18.  
  19. #define kPatchType68K            'PT68'
  20. #define kPatchTypePPC            'PTPP'
  21.  
  22. #define PatchUPPProcInfo        ThePatchuppProcInfo
  23. #define PatchUPPProcPtr            ThePatchProcPtr
  24. #define PatchFun                ThePatch
  25.  
  26. #ifdef powerc
  27.     enum {
  28.         PatchUPPProcInfo = kPascalStackBased
  29.              | RESULT_SIZE(SIZE_CODE(kNoByteCode))
  30.              | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(WindowPtr)))
  31.              | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Boolean)))
  32.     };
  33.     typedef UniversalProcPtr PatchUPP;
  34.     #define NEW_FATPROC(proc68K, procPPC) (PatchUPP) \
  35.             NewFatRoutineDescriptor((ProcPtr)(proc68K), (ProcPtr)(procPPC), PatchUPPProcInfo)
  36.     #define NEW_PROC(proc) (PatchUPP) \
  37.             NewRoutineDescriptor((ProcPtr)(proc), PatchUPPProcInfo, GetCurrentArchitecture())
  38.     #define NEW_68KPROC(proc) (PatchUPP) \
  39.             NewRoutineDescriptor((ProcPtr)(proc), PatchUPPProcInfo, kM68kISA);
  40.     #define CALL_PROC(proc, p1, p2) \
  41.             CallUniversalProc((UniversalProcPtr) (proc) , PatchUPPProcInfo, p1, p2)
  42. #else
  43.     enum {
  44.         PatchUPPProcInfo = 0
  45.     };
  46.     typedef pascal void (*PatchUPPProcPtr)(WindowPtr, Boolean);
  47.     typedef PatchUPPProcPtr PatchUPP;
  48.     #define NEW_PROC(proc) (PatchUPP)(proc)
  49.     #define CALL_PROC(proc, p1, p2) (* (PatchUPPProcPtr) (proc))(p1, p2)
  50. #endif
  51.  
  52. // Function Prototypes
  53. pascal ProcPtr main(PlaySoundUPP soundPlayer68K, PlaySoundUPP soundPlayerPPC, short rID, short install);
  54. pascal void PatchFun(WindowPtr theWindow, Boolean show);
  55. void memclear(void* memptr, long len);
  56.  
  57. // Globals
  58. PlaySoundUPP gSoundPlayer = nil;
  59. PatchUPP gOriginal = nil;
  60. Boolean gGoAwayCalled = false;
  61.  
  62. #ifdef powerc
  63.     // for Metrowerks' linker, this defines the interface for main().
  64.     ProcInfoType __procinfo = PatchInstallerUPPProcInfo;
  65. #endif
  66.  
  67. // * ******************************************************************************
  68. // *     main
  69. // *             Entry point for the ShowHide Patch Installer
  70. // * ******************************************************************************
  71. pascal ProcPtr main(PlaySoundUPP soundPlayer68K, PlaySoundUPP soundPlayerPPC, short rID, short install)
  72. {
  73. #ifndef powerc
  74.     #pragma unused(soundPlayerPPC, rID)
  75. #else
  76.     Handle theHandle;
  77.     ProcPtr the68KProc;
  78.     PatchInstallerUPP patchInstaller68K;
  79. #endif
  80.  
  81.     PatchUPP Patch;
  82.     THz theZone;
  83.     ProcPtr theProc;
  84.  
  85.     EnterCodeResource();
  86.  
  87.     theZone = GetZone();
  88.     SetZone(SystemZone());
  89.  
  90.     // DEBUG("Enter ShowHide Installer");
  91.  
  92.     Patch = nil;
  93.     theProc = (ProcPtr)PatchFun;
  94.     gSoundPlayer = soundPlayer68K;
  95.     gOriginal = (PatchUPP) 
  96.             NGetTrapAddress(PatchTrap, (PatchTrap & 0x0800) ? ToolTrap : OSTrap);
  97.  
  98. #ifdef powerc
  99.     if (soundPlayerPPC)
  100.         gSoundPlayer = soundPlayerPPC;
  101.     else
  102.         if (gSoundPlayer->goMixedModeTrap != _MixedModeMagic)
  103.             gSoundPlayer = NEW_68KPLAYSOUNDPROC(gSoundPlayer);
  104.  
  105.     if (gOriginal->goMixedModeTrap != _MixedModeMagic)
  106.         gOriginal = NEW_68KPROC(gOriginal);
  107. #endif
  108.  
  109.     if (!install) {
  110.         // DEBUG("Exit ShowHide Installer (no Install)");
  111.  
  112.         SetZone(theZone);
  113.         ExitCodeResource();
  114.         return theProc;
  115.     }
  116.  
  117. #ifdef powerc
  118.     theHandle = GetResource(kPatchType68K, rID);
  119.     if (theHandle)  {
  120.  
  121.         LoadResource(theHandle);
  122.         HLockHi(theHandle);
  123.         DetachResource(theHandle);
  124.  
  125.         the68KProc = 0;
  126.         patchInstaller68K = NEW_68K_PATCH_INSTALLER_PROC(*theHandle);
  127.         the68KProc = (ProcPtr) CALL_PATCH_INSTALLER_PROC(patchInstaller68K, soundPlayer68K, soundPlayerPPC, rID, false);
  128.         DisposeRoutineDescriptor(patchInstaller68K);
  129.  
  130.         if (the68KProc)
  131.             Patch = NEW_FATPROC(the68KProc, theProc);
  132.     }
  133. #endif
  134.  
  135.     if (!Patch)
  136.         Patch = NEW_PROC(theProc);
  137.  
  138.     NSetTrapAddress((UniversalProcPtr) Patch, PatchTrap, (PatchTrap & 0x0800) ? ToolTrap : OSTrap);
  139.  
  140.     SetZone(theZone);
  141.  
  142.     // DEBUG("Exit ShowHide Installer");
  143.  
  144.     ExitCodeResource();
  145.     return theProc;
  146. }
  147.  
  148. // * ******************************************************************************
  149. // *     ThePatch
  150. // *             ShowHide Patch Entry Point
  151. // * ******************************************************************************
  152. pascal void PatchFun(WindowPtr theWindow, Boolean show)
  153. {
  154.  
  155.     ProcessSerialNumber PSN;
  156.     ProcessInfoRec    info;
  157.     Point savePt;
  158.     long saveRefCon;
  159.     OSErr err;
  160.  
  161.     EnterCodeResource();
  162.  
  163.     saveRefCon = ((WindowPeek)theWindow)->refCon;
  164.  
  165.     if (!gGoAwayCalled && !show && saveRefCon == kPatchSigniture)
  166.         gGoAwayCalled = true;
  167.  
  168.     if (!gGoAwayCalled) {
  169.         memclear(&info, sizeof(info));
  170.         info.processInfoLength = sizeof(info);
  171.         err = GetFrontProcess(&PSN);
  172.         if (!err) {
  173.             memclear(&info, sizeof(info));
  174.             info.processInfoLength = sizeof(info);
  175.             err = GetProcessInformation(&PSN,&info);
  176.             if ((!err) && (info.processSignature != 'MACS')) 
  177.                 gGoAwayCalled = true;
  178.         }
  179.     }
  180.  
  181.     if (((WindowPeek)theWindow)->refCon != kPatchSigniture) {
  182.         if ((show == false) && ((WindowPeek)theWindow)->visible) {        
  183.             ((WindowPeek)theWindow)->refCon = kPatchSigniture;            
  184.             if (gSoundPlayer) {
  185.                 CALL_SOUNDPROC(gSoundPlayer);
  186.             }
  187.             AnimateWindow(theWindow, &savePt);
  188.         }
  189.     }
  190.     CALL_PROC(gOriginal, theWindow, show);
  191.     if (gGoAwayCalled && (((WindowPeek)theWindow)->refCon != kPatchSigniture) && (show) && ((WindowPeek)theWindow)->visible) {
  192.         ((WindowPeek)theWindow)->refCon = kPatchSigniture;            
  193.         if (gSoundPlayer) {
  194.             CALL_SOUNDPROC(gSoundPlayer);
  195.             AnimateWindow(theWindow, &savePt);
  196.             CALL_PROC(gOriginal, theWindow, show);
  197.         }
  198.     }
  199.     ((WindowPeek)theWindow)->refCon = saveRefCon;
  200.  
  201.     ExitCodeResource();
  202. }
  203.  
  204. // * ******************************************************************************
  205. // *     ThePatch
  206. // *                Clear a block of memory
  207. // * ******************************************************************************
  208. void memclear(void* memptr, long len)
  209. {
  210.     long count, finishcount;
  211.     double* curPtr;
  212.     Ptr finishPtr;
  213.  
  214.     count = len / sizeof(double);
  215.     finishcount = len % count;
  216.  
  217.     curPtr = (double*)memptr;
  218.     while (count--)
  219.         *curPtr++ = 0.0;
  220.     
  221.     finishPtr = (Ptr)curPtr;
  222.     while (finishcount--)
  223.         *finishPtr++ = 0;
  224. }
  225.