home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / SmallDesk 0.0.2 / SmallDesk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-09  |  3.7 KB  |  140 lines  |  [TEXT/KAHL]

  1. // SmallDesk © 1994 Reinder Verlinde
  2. //
  3. // Patches DisableItem, tests for call from Finder and right item,
  4. // calls EnableItem instead if match found.
  5. //
  6. // Detailed description:
  7. //
  8. // Init time:
  9. //   Find the addresses for both 'DisableItem' and EnableItem'
  10. //   Patch DisableItem
  11. //
  12. // Patched DisableItem:
  13. //   When called from the Finder, and the menu is the View menu, call
  14. //   EnableItem instead. Remember the handle of the View menu for faster
  15. //   processing. This is potentially a problem since the Finder may be quit
  16. //   and relaunched while using another handle for the view menu. If this
  17. //   happens smalldesk simply is disabled. More seriously another application
  18. //   might be launched which uses the same handle as a menu handle. That
  19. //   application will then be unable to disable items in that particular menu.
  20. //   I don't expect this to happen easily, though ;-)
  21. //
  22. #include <Traps.h>
  23.  
  24. #define FinderName    0x02E0
  25. #define CurApName    0x0910
  26. #define MenuList    0x0A1C
  27.  
  28. void main( void)
  29. {
  30.     asm
  31.     {
  32.         //
  33.         // DetachResource called to make this code stay around in
  34.         // the system heap after the init has run. The resource
  35.         // is in the system heap and locked (its resource flags are
  36.         // set like that)
  37.         //
  38.                         // #pragma parameter __A0 RecoverHandle(__A0)
  39.         move.l  d0,a0
  40.         dc.w    _RecoverHandle
  41.                         // pascal void DetachResource(Handle theResource)
  42.         Move.l    a0,-(SP)
  43.         dc.w    _DetachResource
  44.         //
  45.         // Remember old values of EnableItem and DisableItem
  46.         // We do not really need to be able to call 'EnableItem', but
  47.         // calling EnableItem instead of DisableItem is way easier than
  48.         // having to figure out what to do with the stack ourselves.
  49.         // It also better reflects the way to do this with MacsBug.
  50.         // (atba DisableItem ';sw pc A939;g')
  51.         //
  52.         move.w    #_EnableItem,d0
  53.         dc.w    _GetToolTrapAddress
  54.         lea        @oldEnable,a1
  55.         move.l    a0,(a1)
  56.     
  57.         move.w    #_DisableItem,d0
  58.         dc.w    _GetToolTrapAddress
  59.         lea        @oldDisable,a1
  60.         move.l    a0,(a1)
  61.         //
  62.         // Install our patch
  63.         //
  64.         lea        @newDisable,a0
  65.         move.w    #_DisableItem,d0
  66.         dc.w    _SetToolTrapAddress
  67.         //
  68.         // and quit
  69.         //
  70.         rts
  71.     oldEnable:
  72.         dc.l    0x00000000
  73.         
  74.     oldDisable:
  75.         dc.l    0x00000000
  76.  
  77.     ViewMenuHandle:
  78.         dc.l    0x00000000
  79.         
  80.     newDisable:
  81.         //
  82.         // This is the patch for DisableItem.
  83.         // First we do a fast test. If we were passed a handle to the 'View'
  84.         // menu earlier we remembered it in 'ViewMenuHandle'. A simple compare
  85.         // is then used to test for the View menu handle. If the view menu handle
  86.         // has not yet been passed we have to check whether the current handle
  87.         // is the handle to the view menu.
  88.         //
  89.         lea        @ViewMenuHandle,a0
  90.         move.l    (a0),d0
  91.         cmp.l    #0x00,d0
  92.         beq.s    @menu_not_yet_seen
  93.         cmp.l    0x06(sp),d0
  94.         beq.s    @doEnable
  95.  
  96.     doDisable:
  97.         move.l    @oldDisable,a0
  98.         jmp        (a0)
  99.  
  100.     menu_not_yet_seen:
  101.         //
  102.         // Is this call done from within the Finder?
  103.         //
  104.         move.l    #FinderName,a0
  105.         move.l    #CurApName,a1
  106.         
  107.         move.b    (a0)+,d0            // length of name of Finder
  108.         cmp.b    (a1)+,d0            // length of name of current application
  109.         bne.s    @doDisable            // length bytes differ => it's not the Finder
  110.         
  111.     again:
  112.         move.b    (a0)+,d1
  113.         cmp.b    (a1)+,d1
  114.         bne.s    @doDisable            // characters differ => it's not the Finder
  115.         dbpl    d0,@again
  116.         //
  117.         // We were called from the Finder. Is this the 'View' menu?
  118.         // We simply look into the Menu Handle for the length byte
  119.         // and first three characters of the menu title. If they are
  120.         // 0x04, respectively 'Vie', we assume this is the View menu.
  121.         //
  122.         move.l    0x06(sp),a0
  123.         move.l    (a0),a0
  124.         move.l    0x0E(a0),d0
  125.         cmp.l    #0x04566965,d0
  126.         bne.s    @doDisable
  127.         //
  128.         // We found the view menu. Remember its handle and do an
  129.         // enable instead of a disable
  130.         //
  131.         move.l    0x06(sp),d0
  132.         lea        @ViewMenuHandle,a0
  133.         move.l    d0,(a0)
  134.  
  135.     doEnable:
  136.         move.l    @oldEnable,a0
  137.         jmp        (a0)
  138.     }
  139. }
  140.