
  *======================================================================*
  *             MakeVPort_Patch -- Patch a bug in MakeVPort              *
  *======================================================================*
  *                  Copyright (c) 1990 Martin Taillefer                 *
  *                   Copyright (c) 1990 Robert Salesas                  *
  *                          All Rights Reserved                         *
  *======================================================================*
  *      Version: 1.0    30-Apr-90    Martin Taillefer                   *
  *      Changes: Original                                               *
  *======================================================================*

                include "exec/types.i"
                include "exec/libraries.i"
                include "exec/memory.i"

                xref _LVOOldOpenLibrary
                xref _LVOCloseLibrary
                xref _LVOSetFunction
                xref _LVOMakeVPort
                xref _LVOAllocMem
                xref _LVOCopyMem
                xref _LVOForbid
                xref _LVOPermit


CALL    MACRO   <Function_Name>
        jsr     _LVO\1(A6)
        ENDM


SysBase   EQUR A6
GfxBase   EQUR A5
PatchMem  EQUR A4
PatchSize EQU 40

                        MOVEA.L 4,SysBase

                        LEA     GfxName(PC),A1
                        CALL    OldOpenLibrary
                        MOVE.L  D0,GfxBase
                        BNE.S   OpenedGfx

                        MOVEQ   #20,D0     ; could not open lib, quit
                        RTS

OpenedGfx:              CMP.W   #36,LIB_VERSION(GfxBase)
                        BGE.S   CloseGfx  ; fixed in V36 and after, so don't patch

                        MOVEQ   #PatchSize,D0
                        MOVE.L  #MEMF_PUBLIC,D1
                        CALL    AllocMem
                        MOVE.L  D0,PatchMem
                        BNE.S   GotMem

                        MOVE.L  GfxBase,A1     ; no mem, so quit
                        CALL    CloseLibrary
                        MOVEQ   #10,D0
                        RTS

GotMem:                 LEA     PatchCode(PC),A0
                        MOVE.L  PatchMem,A1
                        MOVEQ   #PatchSize,D0
                        CALL    CopyMem

                        CALL    Forbid

                        MOVE.L  GfxBase,A1
                        MOVE.L  #_LVOMakeVPort,A0
                        MOVE.L  PatchMem,D0
                        ADDQ.L  #4,D0
                        CALL    SetFunction
                        MOVE.L  D0,(PatchMem)

                        CALL    Permit

CloseGfx:               MOVE.L  GfxBase,A1
                        CALL    CloseLibrary
                        MOVEQ   #0,D0
                        RTS


PatchCode:              DC.L    0
                        MOVEM.L A4/A5,-(A7)
                        MOVE.L  A1,A5        ; A5 has vport
                        LEA     PatchCode(PC),A4 ; A4 has patch addr
                        MOVE.L  (A4),A4      ; load real MakeVPort addr
                        JSR     (A4)         ; do real MakeVPort
                        MOVE.W  $20(A5),D0   ; VPort's ViewModes
                        BTST    #$D,D0       ; check VP_HIDE
                        BEQ.S   ExitPatch    ; not set, so forget it
                        CLR.W   $1A(A5)      ; set DHeight to 0, this is the magic!

ExitPatch:              MOVEM.L (A7)+,A4-A5
                        RTS


GfxName DC.B 'graphics.library',0
