	IFND LIBRARIES_PATCH_I
LIBRARIES_PATCH_I SET 1
**
**	Filename:	libraries/patch.i
**	Release:	6.0
**	Date:		13.12.97
**
**	(C) Copyright 1993-97 Stefan Fuchs
**	All rights reserved
**
**      definition of patch.library public structures, returncodes and macros


	IFND EXEC_TYPES_I
	include "exec/types.i"
	ENDC

	IFND EXEC_LISTS_I
	include "exec/lists.i"
	ENDC

	IFND EXEC_LIBRARIES_I
	include "exec/libraries.i"
	ENDC

	IFND EXEC_SEMAPHORES_I
	include "exec/semaphores.i"
	ENDC

;-------------------------------------------------------------------------
;ErrorCodes:
PATERR_Ok		equ	0	;Everything Ok
PATERR_PatchInUse	equ	1	;Patch Usecount <> 0
PATERR_InvalidHandle	equ	2	;Pointer to patch is not (no longer) valid
PATERR_OutOfMem		equ	3	;Out of memory
PATERR_OpenLib		equ	4	;Failed to open requested library
PATERR_FuncNotStd	equ	5	;Function to patch is not in the standard format
PATERR_PatchInstalled	equ	6	;Can't remove patch because another program has
					;installed a non-patch.library patch later
PATERR_InvalidTags	equ	7	;There was an error in the combination of the specified Tags
PATERR_OpenDev		equ	8	;Failed to open requested device
PATERR_NoTaskList	equ	9	;PATT_AddTask... or PATT_RemTask... was specified for a patch,
					;which had no PATT_CreateTaskList set
PATERR_TaskListExists	equ    10	;PATT_CreateTaskList was specified, but there already exists a TaskList
PATERR_InvalidTaskList	equ    11	;PATT_CreateTaskList was specified with a wrong parameter
PATERR_PatchUnnamed	equ    12	;PATT_PatchName with GetPatch was specified, for a patch without an IDString
PATERR_NoSemaphore	equ    13	;Could not allocate semaphore

PATERR_KickTooOld	equ    14	;Kickstart version is too old for this function
PATERR_InvalidPattern	equ    15	;Pattern passed via PATT_AddTaskPattern is not valid
PATERR_Restricted	equ    16	;Certain tags/features may not be used when patching certain functions

;-------------------------------------------------------------------------
 STRUCTURE PatchNotifyMessage,0
    STRUCT pnm_ExecMessage,MN_SIZE
    ULONG  pnm_Class
    UWORD  pnm_Code
    APTR   pnm_Object
    LABEL  PatchNotifyMessage_SIZEOF	;Don't depend on this

;-------------------------------------------------------------------------
PATCHNOTIFY_CLASS	equ	$50000000
;-------------------------------------------------------------------------
;Notification codes (pnm_Code) (ignore messages with other codes):
PATCOD_PatchInstalled	equ 	1	;new patch has been added
PATCOD_PatchRemoved	equ	2	;a patch has been removed
PATCOD_PatchChanged	equ	3	;attributes of a patch have been changed

;-------------------------------------------------------------------------
 STRUCTURE PatchXResult,0	;Use PatchAlloc(PS_TYPE_XRESULT) to allocate this
    STRUCT pxr_Node,LN_SIZE	;Reserved
    ULONG  pxr_RegPattern	;Which of the following register fields are used (see below)
    ULONG  pxr_RegD0
    ULONG  pxr_RegD1
    ULONG  pxr_RegD2
    ULONG  pxr_RegD3
    ULONG  pxr_RegD4
    ULONG  pxr_RegD5
    ULONG  pxr_RegD6
    ULONG  pxr_RegD7
    ULONG  pxr_RegA0
    ULONG  pxr_RegA1
    ULONG  pxr_RegA2
    ULONG  pxr_RegA3
    ULONG  pxr_RegA4
    ULONG  pxr_RegA5
    ULONG  pxr_RegA6
    UWORD  pxr_FlowControl	;What patches follow this one (see below)
    LABEL  PatchXResult_SIZEOF	;Don't depend on this
;-------------------------------------------------------------------------
;Valid values for per_FlowControl:
PATFC_Normal		equ	0	;No change in the sequnce of patchcodes
PATFC_FallBack		equ	1	;Use original function (only valid, if PATT_Priority is NULL)
;-------------------------------------------------------------------------
; Register definitions for obsolete PATT_SaveRegisters and pxr_RegPattern:
PATREG_D0		equ $8000
PATREG_D1		equ $4000
PATREG_D2		equ $2000
PATREG_D3		equ $1000
PATREG_D4		equ $0800
PATREG_D5		equ $0400
PATREG_D6		equ $0200
PATREG_D7		equ $0100
PATREG_A0		equ $0080
PATREG_A1		equ $0040
PATREG_A2		equ $0020
PATREG_A3		equ $0010
PATREG_A4		equ $0008
PATREG_A5		equ $0004
PATREG_A6		equ $0002
;---------------------------------------------------------------
PATCHNAME	MACRO
	dc.b "patch.library",0
	ENDM

;---------------------------------------------------------------
;--- FALLBACK - Call this macro instead of a 'rts' instruction
;--- in your patchcode, if you want to return to the original
;--- library code
;--- WARNING: The FALLBACK macro may be replaced by a better solution in V5,
;--- and may eventually no longer work in later versions. Please E-Mail me,
;--- if you use the FALLBACK macro.
;--- Now this is the V5 release of patch.library! So you might wonder, why
;--- the macro is still here. The answer is simple. I got used to this little
;--- macro. So i'll keep it at least until V6 and likely even beyond.
;---------------------------------------------------------------
FALLBACK	MACRO
	move.l (sp),-(sp)
	clr.l 4(sp)
	rts

	ENDM

;---------------------------------------------------------------
;--- GETPATCHBASE - Call this macro, if you need to call patch.library
;--- functions within a patchcode
;--- - DO NOT USE OUTSIDE A PATCHCODE
;--- - DO NOT USE, IF YOU PATCH EXEC.LIBRARY/FINDNAME()
;--- Returns patchbase in a6
;---------------------------------------------------------------
GETPATCHBASE	MACRO
	movem.l d0/d1/a0/a1,-(sp)
	move.l 4.w,a6
	move.l LibList(a6),a0
	lea.l 2$(pc),a1
	jsr -276(a6)		;FindName
	move.l d0,a6
	movem.l (sp)+,d0/d1/a0/a1
	bra 1$
2$	dc.b "patch.library",0
	even
1$
	ENDM


;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
;The following structures are not needed by normal patch.library applications
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
	STRUCTURE PatchBase,LIB_SIZE
	UBYTE PB_Flags				;see below
	UBYTE PB_reserved1
	ULONG PB_private1

	STRUCT	PB_Semaphore,SS_SIZE		;Semaphore to protect MasterPatchList
	STRUCT	PB_MasterPatchHeader,LH_SIZE	;Listheader structure for MasterPatch structures

	;...

;PB_Flags:
	BITDEF	PB,CheckPC,0		;If TRUE: Enable safe removal of patches!
					;Patch.library will use a hack, which
					;is highly dependent on OS versions and installed
					;processors and may therefore not work with
					;certain configurations
					;It's unlikely to crash the machine though
					;New with V5, default: FALSE
	BITDEF	PB,PatchSetFuncActive,1	;If TRUE: A program redirects SetFunction() to patch.library
					;(older versions of PatchSetFunc do not set this bit)
					;New with V4, default: FALSE
	BITDEF  PB,BreakForbid,2	;If TRUE: Break forbid and disable, when
					;trying to get the global semaphore
					;New with V6, default: FALSE
	BITDEF  PB,NoStackAllocmem,3	;If TRUE: Do not call Allocmem for stack extension
					;New with V6, default: FALSE

;-------------------------------------------------------------------------
; Before accessing this structure, make sure MPS_TYPE is PS_TYPE_MASTER

 STRUCTURE MasterPatch,0	;This structure is READONLY
	APTR	MPS_SUCC	;Pointer to next (successor)
	APTR	MPS_PRED	;Pointer to previous (predecessor)
	UBYTE	MPS_TYPE	;MUST be PS_TYPE_MASTER
	BYTE	MPS_PRI		;Priority, for sorting
	APTR	MPS_NAME	;may contain a pointer to the name of the patched library
				;To compensate the loss of a valid librarybase
				;MPS_NAME is now (V4) guaranteed to contain a valid name
				;this may however be a library, device or resource name
	UWORD	MPS_Flags			;no public flags defined
	STRUCT	MPS_PatchHeader,MLH_SIZE	;Listheader Structure for Patch structures
	APTR	MPS_Private01			;This pointer, up to V3 known as MPS_PatchedLibraryBase
						;has been renamed, because with the introduction of the
						;PatchSupervisor it may or may not contain a valid
						;librarybase, it may even contain 0.
	WORD	MPS_PatchedLVO			;Library Vector Offset in patched Library

	;...

;--------------------------------------------------------------------------
; Before accessing this structure, make sure PS_TYPE is either PS_TYPE_USER or PS_TYPE_SYSTEM
; Make sure your program can handle lists, that do not contain any nodes of
; PS_TYPE_USER or PS_TYPE_SYSTEM

 STRUCTURE Patch,0		;This structure is READONLY
	APTR	PS_SUCC		;Pointer to next (successor)
	APTR	PS_PRED		;Pointer to previous (predecessor)
	UBYTE	PS_TYPE		;MUST be PS_TYPE_USER or PS_TYPE_SYSTEM
	BYTE	PS_PRI		;Priority, for sorting
	APTR	PS_NAME		;may contain a pointer to the name of the patch
	UWORD	PS_Flags	;no public flags defined

	;...

;-------------------------------------------------------------------------
;Public types of structures:
PS_TYPE_MASTER	= 1	;node is MasterPatch structure
PS_TYPE_USER	= 2	;node was installed by an application using the library interface
PS_TYPE_SYSTEM	= 3	;node was installed by the support program PatchSetFunc
PS_TYPE_XRESULT	= 30	;this struct is an PatchXResult structure


	ENDC	;LIBRARIES_PATCH_I
