home *** CD-ROM | disk | FTP | other *** search
/ MacFormat UK 86 / MF_UK_86_1.iso / Shareware Plus / Utilities / Reaper 132 / Source Code / Reaper Trap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-21  |  20.3 KB  |  1,085 lines  |  [TEXT/CWIE]

  1. // AKUA Protos OneFile
  2. #include    "yLibCfg.h"
  3.  
  4. #include    "Reaper.h"
  5.  
  6. #include    "yAgtINIT.h"
  7. #include    "yAgtTRAP.h"
  8. #include    "yDraw.h"
  9. #include    "yFixedMath.h"
  10. #include    "yGlobal.h"
  11. #include    "yString.h"
  12.  
  13. #include    <Appearance.h>
  14.  
  15.  
  16.  
  17. //
  18. // For PPC Code resource
  19. #if            ISAPPC
  20. ProcInfoType    __procinfo = bpiObjAgt;
  21. #endif    //    ISAPPC
  22.  
  23.  
  24. // AKUA Statics Beg
  25. oaVal main(oaObj initBlkObj);
  26. static pascal Ptr xaLMGetFCBSPtr(void);
  27. static SysProc tpLaunchX(LaunchPBPtr pb, OSErr * err);
  28. #if            ISAPPC
  29. static pascal OSErr xaLaunchPPC(LaunchPBPtr pb);
  30. #else    //    ISAPPC
  31. static pascal asm OSErr xaLaunch(void);
  32. #endif    //    ISAPPC
  33. static SysProc tpGet1ResourceX(ident kind, short resNum, Handle * retHdl);
  34. #if            ISAPPC
  35. static pascal Handle xaGet1ResourcePPC(ResType kind, short resNum);
  36. #else    //    ISAPPC
  37. static pascal asm Handle xaGet1Resource(ResType kind, short resID);
  38. #endif    //    ISAPPC
  39. static Handle tpNewHandleX(word tw, Size amt);
  40. #if            ISAPPC
  41. static pascal Handle xaNewHandlePPC(word tw, Size amt);
  42. static pascal Handle xaNewHandleSmartPPC(word tw, Size amt);
  43. #else    //    ISAPPC
  44. static pascal asm void xaNewHandle(void);
  45. static pascal asm void xaNewHandleSmart(void);
  46. #endif    //    ISAPPC
  47. static Ptr tpNewPtrX(word tw, Size amt);
  48. #if        ISAPPC
  49. static pascal Ptr xaNewPtrPPC(word tw, Size amt);
  50. #else
  51. static pascal asm void xaNewPtr(void);
  52. #endif    //    ISAPPC
  53. static SysProc tpDisposePtrX(word tw, Ptr p);
  54. #if            ISAPPC
  55. static pascal void xaDisposePtrPPC(word tw, Ptr p);
  56. #else    //    ISAPPC
  57. static pascal asm void xaDisposePtr(void);
  58. #endif    //    ISAPPC
  59. static PicHandle tpOpenPictureX(rect box);
  60. #if            ISAPPC
  61. static pascal PicHandle xaOpenPicturePPC(rect box);
  62. #else    //    ISAPPC
  63. static pascal asm void xaOpenPicture(void);
  64. #endif    //    ISAPPC
  65. static SysProc tpMaxApplZoneX(void);
  66. #if            ISAPPC
  67. static pascal void xaMaxApplZonePPC(void);
  68. #else    //    ISAPPC
  69. static pascal asm void xaMaxApplZone(void);
  70. #endif    //    ISAPPC
  71. static yError tpInit(initBlk init);
  72. Handle mmHdlNewClr(lwrd s);
  73. void mmBlkClr(void * p, long s);
  74. void mmBlkCpyCode(const void * p, void * q, long s);
  75. void mmBlkCpy(const void * p, void * q, long s);
  76. void mmBlkSwap(void * p, void * q, long s);
  77. SysProc osTrapSwap(word trapWord, void * newTrapAdr);
  78. SysProc osTrapSwap(word trapWord, word selector,
  79.                     ProcInfoType expectedProcInfo, void * newTrapAdr);
  80. // AKUA Statics End
  81.  
  82.  
  83.  
  84.  
  85. enum TrapReplacement
  86.  {
  87.     ktrLaunch,
  88.     ktrGet1Resource,
  89.     ktrMaxApplZone,
  90.  
  91.     ktrOpenPicture,        // Only set at MaxApplZone
  92.     ktrNewHandle,
  93. //    ktrSetHandleSize,
  94.     ktrNewPtr,
  95.     ktrDisposePtr,
  96.  
  97.     ktrCnt
  98.  };
  99.  
  100.  
  101. // Our globals
  102. ReaperGlo    grp =
  103.  {
  104.     igsReaper,
  105.     kvrReaperCurrent,
  106.     0,
  107.     NULL,
  108.     NULL
  109.  };
  110.  
  111. MonkeyFlag    theMonkey;
  112. SysProc        gatTraps[ktrCnt];
  113. // End globals
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. oaVal main(oaObj initBlkObj) 
  121.  {
  122.     EnterCodeRsrc();
  123.  
  124.     initBlk        init = (initBlk)initBlkObj;
  125.  
  126.     // bkkEternal - since we stay resident, load and detach 'PREF' resource
  127.     init->flags = bkkEternal;
  128.  
  129.     // Global Initialization
  130.     theMonkey = bEvMonkeyLives | (init->monkey & (bEvMonkeyPowered | bEvMonkeyHasColourQD | bEvMonkeyUseColourQD));
  131.  
  132.     grp.prefs = (BADAPP)init->prefs;
  133.  
  134.     // Set up traps
  135.     yError err = tpInit(init);        // Init Gestalt Entries
  136.  
  137. #if            ISAPPC
  138.     if (UInt32 * badguy = (UInt32 *)init->trapData)
  139.         grp.nextFCBP = fcSet(badguy, xaLMGetFCBSPtr);
  140. #endif    //    ISAPPC
  141.  
  142. deadlyError:
  143.     LeaveCodeRsrc();
  144.  
  145.     return    err;
  146.  }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. static pascal Ptr xaLMGetFCBSPtr(void)
  153.  {
  154.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  155.     tp        Init
  156.  
  157.     In        InitBlk from main
  158.     Out        
  159.  
  160.     Effect    Fill initBlk with our shit!
  161.  
  162.     Const    
  163.     Errors    
  164.     Flags    
  165.     Global    
  166.     Rsrc    
  167.  
  168.     Version    001
  169.     Notes    
  170.     History
  171.     001    GOD    10.04.96 Use MaxApplZone
  172.     000    
  173.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  174.     if (appItem item = rpFindApp(grp.prefs, osCreator(NULL), osCurApName, TRUE, NULL, NULL))
  175.         if (mcObjTst(item, bbaFixFCB))
  176.             goto okToTouch;
  177.  
  178.     if (grp.nextFCBP)
  179.         return    (*grp.nextFCBP)();
  180.  
  181.     SysError(119);
  182.  
  183. okToTouch:
  184.     return    osFCBSPtr;
  185.  }
  186.  
  187.  
  188.  
  189. static SysProc tpLaunchX(LaunchPBPtr pb, OSErr * err)
  190.  {
  191.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  192.     tp        LaunchX
  193.     
  194.     In        
  195.     Out        
  196.  
  197.     Effect    Grab events, check 'em out, and pass 'em on…
  198.             (Keep track of idle for AutoOK function and
  199.              pop-up utility menus when the user has chosen them).
  200.  
  201.     Const    
  202.     Errors    
  203.     Flags    
  204.     Global    
  205.     Rsrc    
  206.  
  207.     Version    001
  208.     Notes    
  209.     History
  210.     001    GOD 12.07.95 wdSelect action moved here
  211.              Action now includes 2 secs or more of
  212.              events not being processed.
  213.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  214.     EnterCodeRsrc();
  215.  
  216.     SysProc    trap = gatTraps[ktrLaunch];
  217.  
  218.     mcObjSet(&grp, brpIsLaunching);
  219.  
  220. #if            ISAPPC
  221.     // DebugStr("\pLaunch");
  222.     *err = CallUniversalProc(trap, bpiLaunch, pb);
  223. #else    //    ISAPPC
  224.     *err = (*(trapLaunchProc)trap)(pb);
  225. #endif    //    ISAPPC
  226.  
  227.     mcObjClr(&grp, brpIsLaunching);
  228.  
  229.     LeaveCodeRsrc();
  230.  
  231.     // return    trap;
  232.     return        NULL;
  233.  }
  234.  
  235.  
  236. #if            ISAPPC
  237.  
  238. static SysProcDef xaLaunch = BUILD_ROUTINE_DESCRIPTOR(bpiLaunch, xaLaunchPPC);
  239.  
  240. static pascal OSErr xaLaunchPPC(LaunchPBPtr pb)
  241.  {
  242.     OSErr        err;
  243.  
  244.     if (SysProc p = tpLaunchX(pb, &err))
  245.         err = CallUniversalProc(p, bpiLaunch, pb);
  246.  
  247.     return    err;
  248.  }
  249.  
  250. #else    //    ISAPPC
  251.  
  252. static pascal asm OSErr xaLaunch(void)
  253.  {
  254.     // Parms are in registers!!! Careful
  255.     clr.l        -(sp)                                // Space for real trap
  256.     movem.l        a0-a1/d0-d2, -(sp)
  257.  
  258.     pea            4 + 20 + 4(sp)                        // Space, Regs, RTS Adress (the value of this address is also in d0)
  259.     move.l        a1, -(sp)                            // Event Record *
  260.  
  261.     jsr            tpLaunchX
  262.     addq.w        #8, sp                                // Kill parms
  263.  
  264.     move.l        a0, 20(sp)                            // New rts
  265.     movem.l        (sp)+, a0-a1/d0-d2
  266.  
  267.     move.w        4(sp), d0                            // Restore d0 from new return value
  268.     tst.l        (sp)
  269.     bne.s        @1
  270.     addq.w        #6, sp                                // Kill NULL Trap & word return
  271. @1:    rts
  272.  }
  273.  
  274. #endif    //    ISAPPC
  275.  
  276.  
  277.  
  278.  
  279. static SysProc tpGet1ResourceX(ident kind, short resNum, Handle * retHdl)
  280.  {
  281.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  282.     tp        Get1ResourceX
  283.     
  284.     In        
  285.     Out        
  286.  
  287.     Effect    Grab events, check 'em out, and pass 'em on…
  288.             (Keep track of idle for AutoOK function and
  289.              pop-up utility menus when the user has chosen them).
  290.  
  291.     Const    
  292.     Errors    
  293.     Flags    
  294.     Global    
  295.     Rsrc    
  296.  
  297.     Version    001
  298.     Notes    
  299.     History
  300.     001    GOD 12.07.95 wdSelect action moved here
  301.              Action now includes 2 secs or more of
  302.              events not being processed.
  303.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  304.     EnterCodeRsrc();
  305.  
  306.     SysProc    trap = gatTraps[ktrGet1Resource];
  307.  
  308.     if (!mcObjTst(&grp, brpIsLaunching))
  309.         goto noLunch;
  310.  
  311. #if            ISAPPC
  312.     if (kind == kCFragResourceType)
  313.      {
  314.         if (appItem item = rpFindApp(grp.prefs, CurResFile(), 0))
  315.          {
  316.             if (mcObjTst(item, bbaRun68K))
  317.              {
  318.                 osResErr = resNotFound;
  319.                 trap = NULL;
  320.                 goto noLunch;
  321.              }
  322.          }
  323.      }
  324. #endif    //    ISAPPC
  325.  
  326.  
  327.     if (kind == ircSizeOfHeap)
  328.      {
  329. #if            ISAPPC
  330.         if (Handle hdl = (Handle)CallUniversalProc(trap, bpiGet1Resource, kind, resNum))
  331. #else    //    ISAPPC
  332.         if (Handle hdl = (Handle)(*(tpGet1ResourceProc)trap)(kind, resNum))
  333. #endif    //    ISAPPC
  334.          {
  335.             *retHdl = hdl;
  336.             trap = NULL;
  337.  
  338.             long    originalSize = *(long *)(*hdl + 2);
  339.  
  340.             if (!(originalSize & 0xFF))
  341.              {
  342.                 mmHdlPurgeOff(hdl);
  343.                 *(long *)(*hdl + 2) = rpFindHeapSize(grp.prefs, CurResFile(), originalSize);
  344.              }
  345.          }
  346.      }
  347.  
  348. noLunch:
  349.     LeaveCodeRsrc();
  350.     return    trap;
  351.  }
  352.  
  353.  
  354. #if            ISAPPC
  355.  
  356. static SysProcDef xaGet1Resource = BUILD_ROUTINE_DESCRIPTOR(bpiGet1Resource, xaGet1ResourcePPC);
  357.  
  358. static pascal Handle xaGet1ResourcePPC(ResType kind, short resNum)
  359.  {
  360.     Handle                    h;
  361.  
  362.     if (SysProc p = tpGet1ResourceX(kind, resNum, &h))
  363.         return    (Handle)CallUniversalProc(p, bpiGet1Resource, kind, resNum);
  364.  
  365.     return    h;
  366.  }
  367.  
  368. #else    //    ISAPPC
  369.  
  370. static pascal asm Handle xaGet1Resource(ResType kind, short resID)
  371.  {
  372. //    180    GOD    20.03.96    Lots o' changes, including saving A1 around GetResource since ViseInstaller presumes this
  373. //                         the dickheads!
  374.  
  375.     enum ParmSizes { parmsSize = sizeof(ResType) + sizeof(short) };
  376.  
  377.     movem.l        d1-d2/a1, -(sp)
  378.     subq.w        #4, sp                            // Space for return
  379.     pea            (sp)                            // Pointer to it
  380.     move.w        4 + 8 + 12(sp), -(sp)            // resID @ RetAdr + RetVal + Regs
  381.     move.l        4 + 8 + 12 + 4(sp), -(sp)        // kind @ RetAdr + RetVal + Regs + resID + resID(parm)
  382.     jsr            tpGet1ResourceX
  383.     addq.w        #4, sp
  384.     addq.w        #6, sp
  385.     move.l        (sp)+, parmsSize + 4 + 12(sp);    // Parms + RetAdr + SavedRegs
  386.     movem.l        (sp)+, d1-d2/a1
  387.  
  388.     // Return in a0 for MWC!
  389.     move.l        a0, d0
  390.     bne.s        @1
  391.     move.l        (sp)+, a0            // Caller's address
  392.     addq.w        #parmsSize, sp        // We be pascal
  393.     move.w        kosResErr, d0        // Set r0
  394. @1:    jmp            (a0)
  395.  }
  396.  
  397. #endif    //    ISAPPC
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404. static Handle tpNewHandleX(word tw, Size amt)
  405.  {
  406.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  407.     tp Trap    NewHandleX
  408.     
  409.     In        
  410.     Out        
  411.     Errors    
  412.     Effect    Replace known names with icon suites
  413.     Const    
  414.     Global    
  415.     Rsrc    
  416.     Notes    EnterCodeRsrc() -- SetCurrentA4() must be done by 68K caller!!!
  417.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  418.     Handle            h;
  419.     OSErr            err;
  420.  
  421.     if(!(h = TempNewHandle(amt, &err)) || err)
  422.      {
  423.         h = NULL;
  424.         osMemErr = err;
  425.         // Go for alternate zones - going for Sys may loop back to us!
  426. //        h = NewHandleSys(amt);
  427.      }
  428.     else if (mcFlagTst(tw, bosTrapIsClr))
  429.         mmBlkClr(*h, amt);
  430.  
  431.     return    h;
  432.  }
  433.  
  434.  
  435. // GLUE Code for tpNewHandle
  436. #if            ISAPPC
  437.  
  438. static RoutineDescriptor xaNewHandle = BUILD_ROUTINE_DESCRIPTOR(bpiNewHandle, xaNewHandlePPC);
  439.  
  440. static pascal Handle xaNewHandlePPC(word tw, Size amt)
  441.  {
  442.     Handle        h;
  443.  
  444.  
  445.     if (!(h = (Handle)CallOSTrapUniversalProc(gatTraps[ktrNewHandle], bpiNewHandle, tw, amt)))
  446.         if (!mcFlagTst(tw, bosTrapIsSys) && (osTheZone == osApplZone))
  447.             h = tpNewHandleX(tw, amt);
  448.  
  449.     return    h;
  450.  }
  451.  
  452.  
  453.  
  454. static RoutineDescriptor xaNewHandleSmart = BUILD_ROUTINE_DESCRIPTOR(bpiNewHandle, xaNewHandleSmartPPC);
  455.  
  456. static pascal Handle xaNewHandleSmartPPC(word tw, Size amt)
  457.  {
  458.     Handle        h        = NULL;
  459.     bool        smart    = !mcFlagTst(tw, bosTrapIsSys) && (osTheZone == osApplZone);
  460.  
  461.  
  462.     // From OpenPicture?
  463.     if (smart)
  464.         if (mcObjTst(&grp, brpInOpenPicture) || (amt > (osTheZone->zcbFree >> 2)))
  465.             if (osTheZone->zcbFree < TempFreeMem()) // grp.tmpZone->zcbFree)
  466.                 h = tpNewHandleX(tw, amt);
  467.  
  468.     if (!h)
  469.         if (!(h = (Handle)CallOSTrapUniversalProc(gatTraps[ktrNewHandle], bpiNewHandle, tw, amt)))
  470.             if (smart)
  471.                 h = tpNewHandleX(tw, amt);
  472.  
  473.     return    h;
  474.  }
  475.  
  476. #else    //    ISAPPC
  477.  
  478. static pascal asm void xaNewHandle(void)    // Parameter in register d0, trapWord in d1
  479.  {
  480.     movem.l        d1-d4/a1-a4, -(sp)
  481.  
  482.     move.l        d0, d3
  483.     move.w        d1, d4
  484.  
  485.     // Access to our globals
  486.     jsr            SetCurrentA4
  487.  
  488. //    _Debugger
  489.  
  490.     // In System Heap - Leave it alone
  491.     btst        #bosTrapIsSysBit, d4
  492.     bne            @7
  493.  
  494.     // Not App Zone - leave it alone
  495.     move.l        kosApplZone, d0
  496.     move.l        kosTheZone, d1
  497.     cmp.l        d1, d0
  498.     beq            @6
  499.  
  500. @7:    // System or other zone - out of here!
  501.     move.l        d3, d0
  502.     move.l        gatTraps + (ktrNewHandle * 4), a0
  503.     movem.l        (sp)+, d1-d4/a1-a4
  504.     jmp            (a0)
  505.  
  506.     // Quick Check if that much is free
  507. /*    move.l        kosTheZone, a0
  508.     addq.w        #8, a0
  509.     addq.w        #4, a0
  510.     move.l        (a0), d0        // zcbFree
  511.     sub.l        #10240, d0        // reserve for emergencies
  512.     cmp.l        d3, d0
  513.     blt            @4
  514. */
  515. @6:    // Call original NewHandle
  516.     move.l        d3, d0
  517.     // Restore d1 (trapWord)
  518.     move.w        d4, d1
  519.     move.l        gatTraps + (ktrNewHandle * 4), a0
  520.     jsr            (a0)
  521.     tst.w        d0
  522.     beq            @2
  523.  
  524. @4:    // _Debugger
  525.  
  526.     move.l        d3, -(sp)
  527.     move.w        d4, -(sp)
  528.     jsr            tpNewHandleX
  529.     addq.w        #6, sp
  530.  
  531. @3:    move.w        kosMemErr, d0
  532.  
  533. @2:    movem.l        (sp)+, d1-d4/a1-a4    // Restores A4
  534. @1:    rts
  535.  }
  536.  
  537.  
  538.  
  539. static pascal asm void xaNewHandleSmart(void)    // Parameter in register d0, trapWord in d1
  540.  {
  541.     movem.l        d1-d4/a1-a4, -(sp)
  542.  
  543.     move.l        d0, d3
  544.     move.w        d1, d4
  545.  
  546.     // Access to our globals
  547.     jsr            SetCurrentA4
  548.  
  549.     // In System Heap - Leave it alone
  550.     btst        #bosTrapIsSysBit, d4
  551.     bne            @7
  552.  
  553.     // Not App Zone - leave it alone
  554.     move.l        kosApplZone, d0
  555.     move.l        kosTheZone, d1
  556.     cmp.l        d1, d0
  557.     beq            @6
  558.  
  559. @7:    // System or other zone - out of here!
  560.     move.l        d3, d0
  561.     move.l        gatTraps + (ktrNewHandle * 4), a0
  562.     movem.l        (sp)+, d1-d4/a1-a4
  563.     jmp            (a0)
  564.  
  565. @6:    // How much do we have avail?
  566.     move.l        kosTheZone, a0
  567.     addq.w        #8, a0
  568.     addq.w        #4, a0
  569.     move.l        (a0), d0        // zcbFree
  570.     move.l        d0, d1            // … to D1
  571.     asr.l        #2, d0            // 1/4 to D0
  572.  
  573.     // Quick Check if that much is free
  574.     cmp.l        d3, d0            // d0 (free >> 2) > d3 (amt) ?
  575.     ble            @5
  576.  
  577.     // From OpenPicture()?
  578.     move.w        grp.flags, d0
  579.     btst        #brpInOpenPictureBit, d0
  580.     beq            @0        // Go for the bigger zone
  581.  
  582. @5:    // Check if more temp is available than our zone
  583. /*
  584. @5:    move.l        grp.tmpZone, a0
  585.     addq.w        #8, a0
  586.     addq.w        #4, a0
  587.     move.l        (a0), d0
  588. */
  589.     move.l        d1, a3
  590.     subq.w        #4, sp
  591.     moveq        #0x18, d0            // _TempFreeMem
  592.     move.w        d0, -(sp)
  593.     _OSDispatch
  594.     move.l        (sp)+, d0
  595.     move.l        a3, d1
  596.     cmp.l        d0, d1            // D1 (app) > D0 (tmp) ?
  597.     bgt            @0                // Skip it, app has more
  598.  
  599.     // Get some temp mem
  600.     move.l        d3, -(sp)
  601.     move.w        d4, -(sp)
  602.     jsr            tpNewHandleX
  603.     addq.w        #6, sp
  604.     tst.w        kosMemErr
  605.     beq            @3
  606.  
  607.     // Call original NewHandle
  608. @0:    move.l        d3, d0
  609.     // Restore d1 (trapWord)
  610.     move.w        d4, d1
  611.     move.l        gatTraps + (ktrNewHandle * 4), a0
  612.     jsr            (a0)
  613.     tst.w        d0
  614.     beq            @2
  615.  
  616. @4:    // _Debugger
  617.  
  618.     move.l        d3, -(sp)
  619.     move.w        d4, -(sp)
  620.     jsr            tpNewHandleX
  621.     addq.w        #6, sp
  622.  
  623. @3:    move.w        kosMemErr, d0
  624.  
  625. @2:    movem.l        (sp)+, d1-d4/a1-a4    // Restores A4
  626. @1:    rts
  627.  }
  628.  
  629. #endif    //    ISAPPC
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636. static Ptr tpNewPtrX(word tw, Size amt)
  637.  {
  638.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  639.     tp Trap    NewPtrX
  640.     
  641.     In        
  642.     Out        
  643.     Errors    
  644.     Effect    Replace known names with icon suites
  645.     Const    
  646.     Global    
  647.     Rsrc    
  648.     Notes    
  649.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  650.     EnterCodeRsrc();
  651.  
  652.     Ptr                p;
  653.  
  654. #if            ISAPPC
  655.     if (!(p = (Ptr)CallOSTrapUniversalProc(gatTraps[ktrNewPtr], bpiNewPtr, tw, amt)))
  656. #else    //    ISAPPC
  657.     if (!(p = (Ptr)(*(trapNewPtrProc)gatTraps[ktrNewPtr])(tw, amt)))
  658. #endif    //    ISAPPC
  659.      {
  660.         // Try system heap if we not already in System heap…
  661.         if (!(tw & bosTrapIsSys))
  662. #if            ISAPPC
  663.             p = (Ptr)CallOSTrapUniversalProc(gatTraps[ktrNewPtr], bpiNewPtr, tw | bosTrapIsSys, amt);
  664. #else    //    ISAPPC
  665.             p = (Ptr)(*(trapNewPtrProc)gatTraps[ktrNewPtr])(tw | bosTrapIsSys, amt);
  666. #endif    //    ISAPPC
  667.  
  668.         if (!p)
  669.          {
  670.             Handle        h;
  671.  
  672.             if (h = tpNewHandleX(tw, amt + 12))
  673.              {
  674.                 ident        * id;
  675.  
  676.                 mmHdlLock(h);
  677.                 id = (ident *)*h;
  678.                 *id++ = igsReaper;    // Mark as a pointer
  679.                 *id++ = 'Ptr ';
  680.                 *id++ = (lwrd)h;
  681.                 p = (Ptr)id;
  682.              }
  683.          }
  684.      }
  685.  
  686.     LeaveCodeRsrc();
  687.  
  688.     return    p;
  689.  }
  690.  
  691.  
  692. // GLUE Code for tpNewPtr
  693. #if        ISAPPC
  694.  
  695. static RoutineDescriptor xaNewPtr = BUILD_ROUTINE_DESCRIPTOR(bpiNewPtr, xaNewPtrPPC);
  696.  
  697. static pascal Ptr xaNewPtrPPC(word tw, Size amt)
  698.  {
  699.     return    tpNewPtrX(tw, amt);
  700.  }
  701.  
  702. #else
  703.  
  704. static pascal asm void xaNewPtr(void)    // Parameter in register d0
  705.  {
  706.     movem.l        d1-d2/a1, -(sp)
  707.  
  708.     move.l        d0, -(sp)
  709.     move.w        d1, -(sp)
  710.  
  711.     jsr            tpNewPtrX
  712.  
  713.     addq.w        #6, sp
  714.     move.w        kosMemErr, d0
  715.     movem.l        (sp)+, d1-d2/a1
  716.     rts
  717.  }
  718.  
  719. #endif    //    ISAPPC
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726. static SysProc tpDisposePtrX(word tw, Ptr p)
  727.  {
  728.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  729.     tp Trap    DisposePtrX
  730.     
  731.     In        
  732.     Out        
  733.     Errors    
  734.     Effect    Replace known names with icon suites
  735.     Const    
  736.     Global    
  737.     Rsrc    
  738.     Notes    
  739.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  740.     EnterCodeRsrc();
  741.  
  742.     SysProc    trap = gatTraps[ktrDisposePtr];
  743.  
  744.     // One of our pseudo pointers?
  745.     if (!p)
  746.      {
  747.         trap = NULL;
  748.         osMemErr = 0;
  749.      }
  750.     else if (  (*(ident *)(p - 8) == 'Ptr ')
  751.             && (*(ident *)(p - 12) == igsReaper))
  752.      {
  753.         trap = NULL;
  754.         mmHdlDel(*(Handle *)(p - 4));
  755.      }
  756.  
  757.     LeaveCodeRsrc();
  758.  
  759.     return    trap;
  760.  }
  761.  
  762.  
  763. // GLUE Code for tpDisposePtr
  764. #if            ISAPPC
  765.  
  766. static RoutineDescriptor xaDisposePtr = BUILD_ROUTINE_DESCRIPTOR(bpiDisposePtr, xaDisposePtrPPC);
  767.  
  768. static pascal void xaDisposePtrPPC(word tw, Ptr p)
  769.  {
  770.     if (SysProc trap = tpDisposePtrX(tw, p))
  771.         CallOSTrapUniversalProc(trap, bpiDisposePtr, tw, p);
  772.  }
  773.  
  774. #else    //    ISAPPC
  775.  
  776. static pascal asm void xaDisposePtr(void)    // Parameter in register a0
  777.  {
  778.     movem.l        d1-d2/a1, -(sp)
  779.  
  780.     move.l        a0, -(sp)
  781.     move.w        d1, -(sp)                // Trapword
  782.  
  783.     jsr            tpDisposePtrX
  784.  
  785.     addq.w        #2, sp                    // Trapword
  786.     move.l        a0, d0
  787.  
  788.     move.l        (sp)+, a0
  789.     movem.l        (sp)+, d1-d2/a1
  790.  
  791.     beq.s        @1
  792.     move.l        d0, -(sp)                // Real trap
  793. @1:    rts
  794.  }
  795.  
  796. #endif    //    ISAPPC
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803. static PicHandle tpOpenPictureX(rect box)
  804.  {
  805.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  806.     tp Trap    OpenPictureX
  807.     
  808.     In        
  809.     Out        
  810.  
  811.     Effect    Replace NewHandle and NewPtr with our versions
  812.              if the app is in our prefs resource and the
  813.              traps have not been already zapped
  814.  
  815.     Const    
  816.     Errors    
  817.     Flags    
  818.  
  819.     Global    
  820.     Rsrc    
  821.  
  822.     Version    000
  823.     Notes    
  824.     History
  825.     000    GOD    10.04.96 Start
  826.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  827.     EnterCodeRsrc();
  828.     mcObjSet(&grp, brpInOpenPicture);
  829.  
  830.     PicHandle pic = CallTrapOpenPicture(gatTraps[ktrOpenPicture], box);
  831.  
  832.     mcObjClr(&grp, brpInOpenPicture);
  833.     LeaveCodeRsrc();
  834.  
  835.     return    pic;
  836.  }
  837.  
  838.  
  839. // GLUE Code for tpNewHandle
  840. #if            ISAPPC
  841.  
  842. static RoutineDescriptor xaOpenPicture = BUILD_ROUTINE_DESCRIPTOR(bpiOpenPicture, xaOpenPicturePPC);
  843.  
  844. static pascal PicHandle xaOpenPicturePPC(rect box)
  845.  {
  846.     return    tpOpenPictureX(box);
  847.  }
  848.  
  849. #else    //    ISAPPC
  850.  
  851. static pascal asm void xaOpenPicture(void)
  852.  {
  853.     move.l        4(sp), a0            // Box parm
  854.     move.l        (sp)+, (sp)            // Kill it
  855.     movem.l        d0-d2/a1, -(sp)
  856.     move.l        a0, -(sp)            // Box parm
  857. //    _Debugger
  858.     jsr            tpOpenPictureX
  859.     addq.w        #4, sp                // Kill box parm
  860.     movem.l        (sp)+, d0-d2/a1
  861.     move.l        a0, 4(sp)
  862.     rts
  863.  }
  864.  
  865. #endif    //    ISAPPC
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872. static SysProc tpMaxApplZoneX(void)
  873.  {
  874.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  875.     tp Trap    MaxApplZoneX
  876.     
  877.     In        
  878.     Out        
  879.  
  880.     Effect    Replace NewHandle and NewPtr with our versions
  881.              if the app is in our prefs resource and the
  882.              traps have not been already zapped
  883.  
  884.     Const    
  885.     Errors    
  886.     Flags    
  887.  
  888.     Global    
  889.     Rsrc    
  890.  
  891.     Version    000
  892.     Notes    
  893.     History
  894.     000    GOD    10.04.96 Start
  895.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  896.     EnterCodeRsrc();
  897.  
  898.     bool    expansion    = FALSE;
  899.     bool    smart        = FALSE;
  900.  
  901. //    mcObjClr(&grp, brpIsLaunching);    // Can't be launching now?!?
  902.  
  903.     if (appItem item = rpFindApp(grp.prefs, kCurrentProcess))
  904.      {
  905.         expansion    = mcObjBool(item, bbaExpand);
  906.         smart        = mcObjBool(item, bbaSmartHeap);
  907.  
  908. /*
  909.         yError    err;
  910.  
  911.         if (1)
  912.             grp.tmpZone = osTwitchZone;
  913.         else if (Handle crap = TempNewHandle(3, &err))
  914.          {
  915.             grp.tmpZone = HandleZone(crap);
  916.             mmHdlDel(crap);
  917.          }
  918. */     }
  919.     else
  920.      {
  921.         // Reget trap info
  922.         gatTraps[ktrOpenPicture]    = osTrapSwap(_OpenPicture, NULL);
  923.         gatTraps[ktrNewHandle]        = osTrapSwap(_NewHandle, NULL);
  924.         gatTraps[ktrNewPtr]            = osTrapSwap(_NewPtr, NULL);
  925.         gatTraps[ktrDisposePtr]        = osTrapSwap(_DisposePtr, NULL);
  926.      }
  927.  
  928.     if ((expansion || smart) && (osTrapSwap(_NewHandle, NULL) != (smart ? &xaNewHandleSmart : &xaNewHandle)))
  929.      {
  930.         // Replace the traps
  931.         if (osTrapSwap(_NewHandle, smart ? &xaNewHandleSmart : &xaNewHandle) != gatTraps[ktrNewHandle])
  932.          {
  933. //            Debugger();
  934. //            osTrapSwap(_NewHandle, original);
  935. //            SysBeep(9);
  936.          }
  937.  
  938.         if (expansion)
  939.          {
  940.             if (osTrapSwap(_NewPtr, &xaNewPtr) != gatTraps[ktrNewPtr])
  941.              {
  942. //                Debugger();
  943. //                osTrapSwap(_NewPtr, original);
  944. //                SysBeep(9);
  945.              }
  946.         
  947.             if (osTrapSwap(_DisposePtr, &xaDisposePtr) != gatTraps[ktrDisposePtr])
  948.              {
  949. //                Debugger();
  950. //                osTrapSwap(_DisposePtr, original);
  951. //                SysBeep(9);
  952.              }
  953.          }
  954.         
  955.         if (smart)
  956.          {
  957.             if (osTrapSwap(_OpenPicture, &xaOpenPicture) != gatTraps[ktrOpenPicture])
  958.              {
  959. //                Debugger();
  960. //                osTrapSwap(_DisposePtr, original);
  961. //                SysBeep(9);
  962.              }
  963.          }
  964.      }
  965.  
  966.     SysProc    trap = gatTraps[ktrMaxApplZone];
  967.  
  968.     LeaveCodeRsrc();
  969.  
  970.     return    trap;
  971.  }
  972.  
  973.  
  974. // GLUE Code for tpNewHandle
  975. #if            ISAPPC
  976.  
  977. static RoutineDescriptor xaMaxApplZone = BUILD_ROUTINE_DESCRIPTOR(bpiMaxApplZone, xaMaxApplZonePPC);
  978.  
  979. static pascal void xaMaxApplZonePPC(void)
  980.  {
  981.     CallOSTrapUniversalProc(tpMaxApplZoneX(), bpiMaxApplZone);
  982.  }
  983.  
  984. #else    //    ISAPPC
  985.  
  986. static pascal asm void xaMaxApplZone(void)
  987.  {
  988.     movem.l        d0-d2/a1, -(sp)
  989.     jsr            tpMaxApplZoneX
  990.     movem.l        (sp)+, d0-d2/a1
  991.     jmp            (a0)
  992.  }
  993.  
  994. #endif    //    ISAPPC
  995.  
  996.  
  997.  
  998.  
  999.  
  1000. static yError tpInit(initBlk init)
  1001.  {
  1002.  /* ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  1003.     tp        Init
  1004.  
  1005.     In        InitBlk from main
  1006.     Out        
  1007.  
  1008.     Effect    Fill initBlk with our shit!
  1009.  
  1010.     Const    
  1011.     Errors    
  1012.     Flags    
  1013.     Global    
  1014.     Rsrc    
  1015.  
  1016.     Version    001
  1017.     Notes    
  1018.     History
  1019.     001    GOD    10.04.96 Use MaxApplZone
  1020.     000    
  1021.  ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– */
  1022.     trapTable                traps;
  1023.  
  1024.  
  1025.     // Our Trap Table
  1026.     init->traps = traps = (trapTable)NewPtrSysClear(sizeof(TrapTable) + (sizeof(TrapTableItem) * (ktrCnt - 1)));
  1027.  
  1028.     if (!traps)
  1029.         return    memFullErr;
  1030.  
  1031.     traps->originalTraps = gatTraps;
  1032.  
  1033.     // The traps we patch
  1034.     int                cnt    = 1;
  1035.     trapTableItem    t    = traps->item;
  1036.  
  1037.     t->trapWord    = _Launch;
  1038.     t->agt        = &xaLaunch;
  1039.  
  1040.     ++ cnt;
  1041.     ++t;
  1042.     t->trapWord    = _Get1Resource;
  1043.     t->agt        = &xaGet1Resource;
  1044.  
  1045.     ++ cnt;
  1046.     ++t;
  1047.     t->trapWord    = _MaxApplZone;
  1048.     t->agt        = &xaMaxApplZone;
  1049.  
  1050.     // Just for inquiry
  1051.     ++ cnt;
  1052.     ++ t;
  1053.     t->trapWord = _OpenPicture;
  1054.  
  1055.     ++ cnt;
  1056.     ++ t;
  1057.     t->trapWord = _NewHandle;
  1058.  
  1059.     ++ cnt;
  1060.     ++ t;
  1061.     t->trapWord = _NewPtr;
  1062.  
  1063.     ++ cnt;
  1064.     ++ t;
  1065.     t->trapWord = _DisposePtr;
  1066.  
  1067.     // Our gestalts
  1068.     if (init->gestVals)
  1069.         init->gestVals->item[0].value = (long)&grp;
  1070.  
  1071.     // Set the number of traps we want
  1072.     traps->cnt = cnt;
  1073.  
  1074.     return    noErr;
  1075.  }
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082. #include    "mmBlkClr.c"
  1083. #include    "osCreator.c"
  1084. #include    "osTrapSwap.c"
  1085.