home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 294.lha / Bag_v1.0 / bag.asm < prev    next >
Assembly Source File  |  1989-10-08  |  7KB  |  243 lines

  1. * --------------------------------------------------------------------- *
  2. * bag.a -- SetFunction ActivateGadget for more reliable performance.
  3. * Public Domain Freeware source by Justin V. McCormick. 89-09-26
  4. * --------------------------------------------------------------------- *
  5.      IFD    CAPE
  6.     EXEOBJ
  7.     OPTIMON
  8.     SMALLOBJ
  9.      ENDC
  10.  
  11.     include "exec/types.i"
  12.     include "exec/memory.i"
  13.     include    "libraries/dos.i"
  14.     include "libraries/dosextens.i"
  15.  
  16. * --------------------------------------------------------------------- *
  17. * Equates
  18. * --------------------------------------------------------------------- *
  19. BWDELAY    EQU    100000    ; The busy-wait delay constant,
  20.             ; can be tuned for your CPU speed.
  21.  
  22. * --------------------------------------------------------------------- *
  23. * Macros
  24. * --------------------------------------------------------------------- *
  25. SYS    MACRO        ; Call a Library function, load A6 conditionally
  26.      IFGT    NARG-2
  27.     FAIL    !!!
  28.      ENDC
  29.      IFEQ    NARG-2
  30.     MOVE.L    \2,a6
  31.      ENDC
  32.     JSR    _LVO\1(a6)
  33.     ENDM
  34.  
  35. XLVO    MACRO        ; Declare a LVO offset from amiga.lib
  36.     XREF    _LVO\1
  37.     ENDM
  38.  
  39. * --------------------------------------------------------------------- *
  40. * Library offsets
  41. * --------------------------------------------------------------------- *
  42.      IFD    NOTEXECOBJ
  43.     XLVO    ActivateGadget
  44.     XLVO    AllocMem
  45.     XLVO    CloseLibrary
  46.     XLVO    CopyMem
  47.     XLVO    FindTask
  48.     XLVO    Forbid
  49.     XLVO    FreeMem
  50.     XLVO    GetMsg
  51.     XLVO    OpenLibrary
  52.     XLVO    Output
  53.     XLVO    ReplyMsg
  54.     XLVO    SetFunction
  55.     XLVO    WaitPort
  56.     XLVO    Write
  57.      ELSE
  58. _LVOActivateGadget    EQU    -$01ce
  59. _LVOAllocMem        EQU    -$00c6
  60. _LVOCloseLibrary    EQU    -$019e
  61. _LVOCopyMem        EQU    -$0270
  62. _LVOFindTask        EQU    -$0126
  63. _LVOForbid        EQU    -$0084
  64. _LVOFreeMem        EQU    -$00d2
  65. _LVOGetMsg        EQU    -$0174
  66. _LVOOpenLibrary        EQU    -$0198
  67. _LVOOutput        EQU    -$003c
  68. _LVOReplyMsg        EQU    -$017a
  69. _LVOSetFunction        EQU    -$01a4
  70. _LVOWaitPort        EQU    -$0180
  71. _LVOWrite        EQU    -$0030
  72.      ENDC
  73.  
  74.     SECTION    CODE
  75. * --------------------------------------------------------------------- *
  76. * EXECUTION STARTS HERE
  77. * --------------------------------------------------------------------- *
  78. launch:
  79.     lea    _DT(pc),a4
  80.     movea.l    4,a6
  81.     move.l    a6,_SysBase-_DT(a4)
  82.     suba.l    a1,a1            ;Find our own task pointer
  83.     SYS    FindTask        ;d0 = FindTask (0L)
  84.     movea.l    d0,a2            ;Save it for later
  85.  
  86. * Check whether launched from CLI or WBench, handle appropriately
  87.     moveq    #0,d7            ;Clear out CLI flag/WBench msg pointer
  88.     tst.l    pr_CLI(a2)        ;Is there a CLI out there?
  89.      bne.s    cliargs            ;Yep, no message to wait for...
  90.  
  91. * Must be from WBench, wait for startup message, keep msg as key for removal
  92.     lea    pr_MsgPort(a2),a0
  93.     SYS    WaitPort        ;Wait for startup message
  94.     lea    pr_MsgPort(a2),a0
  95.     SYS    GetMsg            ;Grab Message, pointer in D0
  96.     move.l    d0,d7            ;Save message pointer for ReplyMsg later
  97.  
  98. cliargs:
  99.     lea    _DOSName(pc),a1        ;dos.library string pointer
  100.     moveq    #0,d0            ;Take any version of DOS
  101.     SYS    OpenLibrary        ;OpenLibrary("dos.library", 0L)
  102.     move.l    d0,_DOSBase-_DT(a4)    ;Save DOSBase
  103.  
  104.     lea    _INTUIName(pc),a1    ;intuition.library string pointer
  105.     moveq    #33,d0            ;Take any version of Intuition after 1.2
  106.     SYS    OpenLibrary        ;OpenLibrary("intuition.library", 33L)
  107.     move.l    d0,_IntuitionBase-_DT(a4) ;Save IntuitionBase
  108.      beq    cleanup2        ;Oops, 1.0 or 1.1 user out there still?
  109.  
  110. * Print credits
  111.     lea    _creditmsg(pc),a0
  112.     move.l    a0,d2
  113.     move.l    #_creditmsgend-_creditmsg,d3
  114.     jsr    writemsg
  115.  
  116. * See if our function is already installed
  117.     movea.l    _IntuitionBase-_DT(a4),a0    ;Grab IntuitionBase
  118.     movea.l    _LVOActivateGadget+2(a0),a0    ;Grab vector to current ActivateGadget()
  119.     lea    MyActivateGadget(pc),a1        ;Grab address of our function
  120.     moveq    #OldFunc-MyActivateGadget,d1    ;Length of our function to compare
  121. 1$    cmpm.b    (a0)+,(a1)+            ;Same code?
  122.     dbne    d1,1$                ;Yep, compare next byte
  123.  
  124. * Install or Remove the vector?
  125.     bne.s    installbag        ;Not our function, install ours
  126.  
  127. removebag:
  128.     movea.l    _IntuitionBase-_DT(a4),a1    ;Grab IBase
  129.     move.w    #_LVOActivateGadget,a0        ;The Vector
  130.     movea.l    _LVOActivateGadget+2(a1),a2    ;Grab vector to current ActivateGadget()
  131.     move.l    OldFunc-MyActivateGadget(a2),d0    ;The original vector to restore
  132.     SYS    SetFunction,_SysBase-_DT(a4)    ;SetFunction(IntuitionBase, LVOActivateGadget, oldfunc)
  133.     movea.l    d0,a1                ;Move old function pointer to A1
  134.     moveq    #MyActivateGadgetEnd-MyActivateGadget,d0 ;Size of function
  135.     SYS    FreeMem                ;FreeMem(oldfunc, sizeof(oldfunc))
  136.  
  137.     lea    _removedmsg(pc),a0
  138.     move.l    a0,d2                ;d2 = removed message
  139.     move.l    #_removedmsgend-_removedmsg,d3    ;d3 = strlen(msg)
  140.     jsr    writemsg
  141.     bra.s    cleanup
  142.  
  143. installbag:
  144.     moveq    #0,d1
  145.     moveq    #MyActivateGadgetEnd-MyActivateGadget,d0
  146.     SYS    AllocMem,_SysBase-_DT(a4)    ;AllocMem(sizeof(newfunc), 0L)
  147.     tst.l    d0
  148.      beq.s    cleanup                ;Oops, no memory?
  149.  
  150. * Copy the replacement function to the newly allocated memory
  151.     movea.l    d0,a2                ;Copy mem pointer
  152.     lea    MyActivateGadget(pc),a0
  153.     movea.l    a2,a1
  154.     move.l    #MyActivateGadgetEnd-MyActivateGadget,d0
  155.     SYS    CopyMem
  156.  
  157. * Revector the library, save old function address in replacement block
  158.     movea.l    _IntuitionBase-_DT(a4),a1
  159.     move.w    #_LVOActivateGadget,a0
  160.     move.l    a2,d0
  161.     SYS    SetFunction            ;SetFunction(IntuitionBase, LVOActivateGadget, newfunc)
  162.     move.l    d0,OldFunc-MyActivateGadget(a2) ;Save old ActivateGadget vector
  163.  
  164.     lea    _installedmsg(pc),a0
  165.     move.l    a0,d2                    ;d2 = installed message
  166.     move.l    #_installedmsgend-_installedmsg,d3    ;d3 = strlen(msg)
  167.     jsr    writemsg
  168.  
  169. * Cleanup and exit parent segment
  170. cleanup:
  171.     movea.l    _IntuitionBase-_DT(a4),a1
  172.     SYS    CloseLibrary,_SysBase-_DT(a4) ;CloseLibrary(IntuitionBase)
  173.  
  174. cleanup2:
  175.     movea.l    _DOSBase-_DT(a4),a1
  176.     SYS    CloseLibrary        ;CloseLibrary(DOSBase)
  177.  
  178.     tst.l    d7            ;Were we called from WBench?
  179.      beq.s    exit            ;from CLI - don't signal WBench
  180.     SYS    Forbid            ;Prevent WBench from UnloadSeg'ing
  181.     movea.l    d7,a1            ;Grab saved message pointer
  182.     SYS    ReplyMsg        ;Let WBench task know we're done
  183.  
  184. exit:
  185.     rts
  186.  
  187. * --------------------------------------------------------------------- *
  188. writemsg:
  189.     SYS    Output,_DOSBase-_DT(a4)    ;Get stdout handle
  190.     move.l    d0,d1            ;Is there a console to write to?
  191.      beq.s    exit            ;Nope, blow it off
  192.     jmp    _LVOWrite(a6)        ;Write(stdout, msg, strlen(msg))
  193.  
  194. * --------------------------------------------------------------------- *
  195. * Calls ActivateGadget() up to five times before giving up.
  196. * Adjust BWDELAY for time delay between attempts.
  197. * --------------------------------------------------------------------- *
  198.     XDEF    MyActivateGadget
  199. MyActivateGadget:
  200.     movem.l    d2-d3/a2-a3,-(sp)    ;Save affected regs
  201.  
  202.     movea.l    OldFunc(pc),a3        ;Grab original vector
  203.     movem.l    a0-a2,-(sp)        ;Push parameters onto stack
  204.  
  205.     moveq    #4,d2            ;Try 5 times
  206. 1$    movem.l    (sp),a0-a2
  207.     jsr    (a3)            ;Call ActivateGadget(gad, win, req)
  208.                     ;             a0   a1   a2
  209.     tst.w    d0            ;Success?
  210.      bne.s    3$            ;Yep, done here!
  211.     move.l    #BWDELAY,d3        ;Kill some time
  212. 2$    subq.l    #1,d3
  213.      bgt    2$
  214.     dbf    d2,1$            ;Try again
  215.  
  216. 3$    lea    12(sp),sp        ;Pop stack parms a0,a1,a2
  217.  
  218.     movem.l    (sp)+,d2-d3/a2-a3    ;Restore affected regs
  219.     rts
  220. OldFunc:
  221.     dc.l    0            ;Pointer to original function
  222. MyActivateGadgetEnd:
  223.  
  224. * --------------------------------------------------------------------- *
  225. * PC Relative data
  226. * --------------------------------------------------------------------- *
  227. _DT:
  228. _SysBase:    dc.l    0
  229. _DOSBase:    dc.l    0
  230. _IntuitionBase:    dc.l    0
  231. _DOSName:    DOSNAME
  232. _INTUIName:    dc.b    'intuition.library',$00
  233.  
  234. _creditmsg:    dc.b    'BAG 1.0 by Justin McCormick - '
  235.         dc.b    'BetterActivateGadget '
  236. _creditmsgend:
  237. _installedmsg:    dc.b    'Installed',$0a
  238. _installedmsgend:
  239. _removedmsg:    dc.b    'Removed',$0a
  240. _removedmsgend:
  241. * ------------------------------------------------------------------------- *
  242.     END
  243.