home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / WackyWindows / Patches / HideWindowPatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-19  |  4.4 KB  |  178 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.  
  15. //#include "debug.h"
  16.  
  17.  
  18. // Semi- Automagic macros for generic Fat patches
  19. #define PatchTrap                _HideWindow
  20.  
  21. #define PatchUPPProcInfo        ThePatchuppProcInfo
  22. #define PatchUPPProcPtr            ThePatchProcPtr
  23. #define PatchFun                ThePatch
  24.  
  25. #ifdef powerc
  26.     enum {
  27.         PatchUPPProcInfo = kPascalStackBased
  28.              | RESULT_SIZE(SIZE_CODE(kNoByteCode))
  29.              | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(WindowPtr)))
  30.     };
  31.     typedef UniversalProcPtr PatchUPP;
  32.  
  33.     #define NEW_PROC(proc) (PatchUPP) \
  34.             NewRoutineDescriptor((ProcPtr)(proc), PatchUPPProcInfo, GetCurrentArchitecture())
  35.  
  36.     #define NEW_68KPROC(proc) (PatchUPP) \
  37.             NewRoutineDescriptor((ProcPtr)(proc), PatchUPPProcInfo, kM68kISA);
  38.  
  39.     #define NEW_FATPROC(proc68K, procPPC) (PatchUPP) \
  40.             NewFatRoutineDescriptor((ProcPtr)(proc68K), (ProcPtr)(procPPC), PatchUPPProcInfo)
  41.  
  42.     #define CALL_PROC(proc, p1) \
  43.             CallUniversalProc((UniversalProcPtr) (proc) , PatchUPPProcInfo, p1)
  44. #else
  45.     enum {
  46.         PatchUPPProcInfo = 0
  47.     };
  48.     typedef pascal void (*PatchUPPProcPtr)(WindowPtr);
  49.     typedef PatchUPPProcPtr PatchUPP;
  50.     #define NEW_PROC(proc) (PatchUPP)(proc)
  51.     #define CALL_PROC(proc,p1) (* (PatchUPPProcPtr) (proc))(p1)
  52. #endif
  53.  
  54. // Function Prototypes
  55. pascal ProcPtr main(PlaySoundUPP soundPlayer68K, PlaySoundUPP soundPlayerPPC, short rID, short install);
  56. pascal void PatchFun(WindowPtr  theWindow);
  57.  
  58. // Globals
  59. PlaySoundUPP gSoundPlayer = nil;
  60. PatchUPP gOriginal = nil;
  61.  
  62. #ifdef powerc
  63.     // for Metrowerks' linker, this defines the interface for main().
  64.     ProcInfoType __procinfo = PatchInstallerUPPProcInfo;
  65. #endif
  66.  
  67.  
  68. // * ******************************************************************************
  69. // *     main
  70. // *             Entry point for the HideWindow Patch Installer
  71. // * ******************************************************************************
  72. pascal ProcPtr main(PlaySoundUPP soundPlayer68K, PlaySoundUPP soundPlayerPPC, short rID, short install)
  73. {
  74. #ifndef powerc
  75.     #pragma unused(soundPlayerPPC, rID)
  76. #else
  77.     Handle theHandle;
  78.     ProcPtr the68KProc;
  79.     PatchInstallerUPP patchInstaller68K;
  80. #endif
  81.  
  82.     PatchUPP Patch;
  83.     THz theZone;
  84.     ProcPtr theProc;
  85.  
  86.     EnterCodeResource();
  87.  
  88.     theZone = GetZone();
  89.     SetZone(SystemZone());
  90.  
  91. //    DEBUG("Enter HideWindowPatch Installer");
  92.  
  93.     Patch = nil;
  94.     theProc = (ProcPtr)PatchFun;
  95.     gSoundPlayer = soundPlayer68K;
  96.     gOriginal = (PatchUPP) 
  97.             NGetTrapAddress(PatchTrap, (PatchTrap & 0x0800) ? ToolTrap : OSTrap);
  98.  
  99. #ifdef powerc
  100.     if (soundPlayerPPC)
  101.         gSoundPlayer = soundPlayerPPC;
  102.     else
  103.         if (gSoundPlayer->goMixedModeTrap != _MixedModeMagic)
  104.             gSoundPlayer = NEW_68KPLAYSOUNDPROC(gSoundPlayer);
  105.  
  106.     if (gOriginal->goMixedModeTrap != _MixedModeMagic)
  107.         gOriginal = NEW_68KPROC(gOriginal);
  108. #endif
  109.  
  110.     if (!install) {
  111.         // DEBUG("Exit HideWindowPatch Installer (no Install)");
  112.  
  113.         SetZone(theZone);
  114.         ExitCodeResource();
  115.         return theProc;
  116.     }
  117.  
  118. #ifdef powerc
  119.     theHandle = GetResource(kPatchType68K, rID);
  120.     if (theHandle)  {
  121.  
  122.         LoadResource(theHandle);
  123.         HLockHi(theHandle);
  124.         DetachResource(theHandle);
  125.  
  126.         the68KProc = 0;
  127.         patchInstaller68K = NEW_68K_PATCH_INSTALLER_PROC(*theHandle);
  128.         the68KProc = (ProcPtr) CALL_PATCH_INSTALLER_PROC(patchInstaller68K, soundPlayer68K, soundPlayerPPC, rID, false);
  129.         DisposeRoutineDescriptor(patchInstaller68K);
  130.  
  131.         if (the68KProc)
  132.             Patch = NEW_FATPROC(the68KProc, theProc);
  133.     }
  134. #endif
  135.  
  136.     if (!Patch)
  137.         Patch = NEW_PROC(theProc);
  138.  
  139.     NSetTrapAddress((UniversalProcPtr) Patch, PatchTrap, (PatchTrap & 0x0800) ? ToolTrap : OSTrap);
  140.  
  141.     SetZone(theZone);
  142.  
  143. //     DEBUG("Exit HideWindowPatch Installer");
  144.  
  145.     ExitCodeResource();
  146.     return theProc;
  147. }
  148.  
  149. // * ******************************************************************************
  150. // *     ThePatch
  151. // *             HideWindow Patch Entry Point
  152. // * ******************************************************************************
  153. pascal void PatchFun(WindowPtr theWindow)
  154. {
  155.     Point savePt;
  156.     long saveRefCon;
  157.  
  158.     EnterCodeResource();
  159.  
  160.     saveRefCon = ((WindowPeek)theWindow)->refCon;
  161.     if (((WindowPeek)theWindow)->refCon != kPatchSigniture) {
  162.         
  163.         ((WindowPeek)theWindow)->refCon = kPatchSigniture;            
  164.         if (gSoundPlayer) {
  165.             CALL_SOUNDPROC(gSoundPlayer);
  166.         }
  167.         if (((WindowPeek)theWindow)->visible) {
  168.             AnimateWindow(theWindow, &savePt);
  169.         }
  170.     }
  171.  
  172.     CALL_PROC(gOriginal, theWindow);
  173.  
  174.     ((WindowPeek)theWindow)->refCon = saveRefCon;
  175.     ExitCodeResource();
  176. }
  177.  
  178.