; OldIcons v1.2: removes borders from icons.
; by Kyzer/CSG
; based on NoIconBorder by Kamel Biskri / David Jones
;
	incdir	include:
	include	exec/execbase.i
	include	lvo/exec_lib.i
	include	lvo/graphics_lib.i

EXECBASE equr A6
GFXBASE equr A5

START	move.l	4.w,EXECBASE
	lea	gfxname(pc),a1
	moveq	#36,d0
	jsr	_LVOOpenLibrary(a6)	; open graphics.library
	tst.l	d0
	beq.s	.quit
	move.l	d0,GFXBASE

; Our second code hunk contains the patch code.

; START-4 contains a BPTR to the next hunk in our code.
; if we clear this pointer, DOS can't know we have a second code hunk,
; and won't free it. Therefore, the patch code will stay in memory.
;
; We'll also save relocs by addressing the second hunk relatively.

	lea	START-4(pc),a0
	move.l	(a0),a4
	clr.l	(a0)	; remove pointer to second hunk
	adda.l	a4,a4
	adda.l	a4,a4
	addq.l	#4,a4	; a4 is now a pointer to PATCHES

; set up the initial values for the Workbench task check in the patches
	jsr	update-PATCHES(a4)

; now we install the 3 patches.

	jsr	_LVOForbid(a6)	; We Forbid() until we have set everything up

patch	macro ; name, function, base
	lea	\1patch-PATCHES(a4),a1	; get pointer to patch function
	move.l	a1,d0
	move.w	#_LVO\2,a0		; set function offset
	move.l	\3,a1			; and library base
	jsr	_LVOSetFunction(a6)	; install the patch
	move.l	d0,\1jmp+2-PATCHES(a4)	; put old function ptr into patch code
	endm

	patch	task, AddTask, EXECBASE
	patch	rect, RectFill, GFXBASE
	patch	draw, Draw, GFXBASE

	jsr	_LVOPermit(a6)	; We also *must* Permit(). Thanks, Dave!

.quit	moveq	#0,d0
	rts

gfxname	dc.b	'graphics.library',0

;------------------------------------------------------------------------------
	section	"patches",code
PATCHES

; a patch on Draw() and RectFill()
; these graphics functions are normally carried out, except in the following
; cases: 
; - ThisTask == Workbench (other tasks are not affected)
; - 8(sp) == 8  (??? means Workbench is drawing icon borders, not windows?)

drawpatch
WBTEST	cmp.l	#1,$ABCDEF	; this instruction set by call to update()
	bne.s	drawjmp		; call original function if not Workbench
	cmp.l	#8,8(sp)	; call original function if 8(sp) != 8
	bne.s	drawjmp
	rts			; if Workbench/icon, don't draw lines
drawjmp	jmp	$12345

	cnop	0,8
rectpatch

WBTEST2	cmp.l	#1,$ABCDEF	; this instruction set by call to update()
	bne.s	rectjmp		; call original function if not Workbench
	cmp.l	#8,8(sp)	; call original function if 8(sp) != 8
	bne.s	rectjmp
	addq.l	#3,d0		; if Workbench/icon, 'alter' the rectangle
	addq.l	#2,d1		; filled to skip the borders.
	subq.l	#3,d2
	subq.l	#2,d3
rectjmp	jmp	$12345

; a patch on AddTask() - after a new task is added, we always update the
; Workbench task pointer, in case a new copy of Workbench is loaded.
taskpatch
taskjmp	jsr	$12345
	movem.l	d0-d1/a0-a2,-(sp)
	bsr.s	update
	movem.l	(sp)+,d0-d1/a0-a2
	rts

; update: this code updates the addresses of Workbench and ThisTask in
; the graphics patches. Also clears caches.
; It's called once at installation time, and after every AddTask()
update	lea	.wbname(pc),a1
	jsr	_LVOFindTask(a6)
	lea	ThisTask(a6),a1

	lea	WBTEST+2(pc),a2
	move.l	d0,(a2)+	; store current Workbench task or NULL
	move.l	a1,(a2)+	; store address of execbase/ThisTask

	lea	WBTEST2+2(pc),a2
	move.l	d0,(a2)+	; store current Workbench task or NULL
	move.l	a1,(a2)+	; store address of execbase/ThisTask

	jmp	_LVOCacheClearU(a6)

.wbname	dc.b	'Workbench',0
