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