	opt	AMIGA

; ExamPub 1.0 COPYRIGHT ©1995 F.Delacroix - All rights reserved
;
; Purpose: Examines the specified, frontmost or default public screen, sets
;	   some local or global variables, optionnally prints the name on the
;	   output console and brings it to the front. Most useful with the
;	   `backticks` in a toolmanager configurations. For example:
;	   Multiview DH0:Text/Autodocs PUBSCREEN `ExamPub STF` FONTNAME $ScreenFont FONTSIZE $ScreenFontSize
;
; Revision History:
;  Version 1.0 - Dimanche 23 Juillet 1995
;   --- Initial Release: FREEWARE ---

	include	dos/var.i
	include	dos/rdargs.i

	STRUCTURE	Variables,0
	 APTR	v_ExecBase
	 APTR	v_IntuitionBase
	 APTR	v_GraphicsBase
	 APTR	v_DOSBase
	 APTR	v_RDArgs
	 APTR	v_RDArgs2
	 APTR	v_ScreenName
	 ULONG	v_LocalSwitch
	 ULONG	v_ScreenToFrontSwitch
	 ULONG	v_QuietSwitch
	 APTR	v_LockedScreen
	 UWORD	v_Width
	 UWORD	v_Height
	 UWORD	v_FontSize
	 UBYTE	v_Depth
	 STRUCT	v_ResultingName,MAXPUBSCREENNAME+1
	 STRUCT	v_FontName,108
	 STRUCT	v_NumericBuffer,12
	LABEL	variables_SIZEOF

	move.l	4.w,a6
	moveq	#100,d7		; Return code
	move.l	#variables_SIZEOF,d0
	move.l	#MEMF_CLEAR,d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,a5
	move.l	a5,d0
	beq	exit
	move.l	a6,v_ExecBase(a5)
	lea	Intuition.Name(pc),a1
	moveq	#37,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,v_IntuitionBase(a5)
	beq	FreeVars
	lea	Graphics.Name(pc),a1
	moveq	#37,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,v_GraphicsBase(a5)
	beq	CloseIntuition
	lea	DOS.Name(pc),a1
	moveq	#37,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,v_DOSBase(a5)
	beq	CloseGraphics
	move.l	d0,a6
	move.l	#DOS_RDARGS,d1
	moveq	#0,d2
	jsr	_LVOAllocDosObject(a6)
	move.l	d0,v_RDArgs(a5)
	beq.s	CloseDOS
	move.l	d0,a0
	move.l	#Help.String,RDA_ExtHelp(a0)
	move.l	a0,d3
	move.l	#Args.Template,d1
	lea	v_ScreenName(a5),a0
	move.l	a0,d2
	jsr	_LVOReadArgs(a6)
	move.l	d0,v_RDArgs2(a5)
	beq.s	FreeDosObj

	moveq	#10,d7
	move.l	v_IntuitionBase(a5),a6
	move.l	v_ScreenName(a5),d0
	beq	LockFront
	move.l	d0,a0
	lea	v_ResultingName(a5),a1
	bsr.s	StrCpy
	jsr	_LVOLockPubScreen(a6)
	move.l	d0,v_LockedScreen(a5)
	bne	ExamScreen
GetDefaultPub
	lea	v_ResultingName(a5),a0
	jsr	_LVOGetDefaultPubScreen(a6)
	suba.l	a0,a0
	jsr	_LVOLockPubScreen(a6)
	move.l	d0,v_LockedScreen(a5)
	bne	ExamScreen

FreeArgs
	move.l	v_RDArgs2(a5),d1
	move.l	v_DOSBase(a5),a6
	jsr	_LVOFreeArgs(a6)
FreeDosObj
	move.l	#DOS_RDARGS,d1
	move.l	v_RDArgs(a5),d2
	jsr	_LVOFreeDosObject(a6)
CloseDOS
	move.l	v_DOSBase(a5),a1
	move.l	v_ExecBase(a5),a6
	jsr	_LVOCloseLibrary(a6)
CloseGraphics
	move.l	v_GraphicsBase(a5),a1
	jsr	_LVOCloseLibrary(a6)
CloseIntuition
	move.l	v_IntuitionBase(a5),a1
	jsr	_LVOCloseLibrary(a6)
FreeVars
	move.l	a5,a1
	move.l	#variables_SIZEOF,d0
	jsr	_LVOFreeMem(a6)
exit	move.l	d7,d0
	rts

ScreenToFront	; ScreenToFront()
	movem.l	d0-d1/a0-a1/a6,-(sp)
	tst.l	v_ScreenToFrontSwitch(a5)
	beq.s	.NoSTF
	move.l	v_LockedScreen(a5),a0
	move.l	v_IntuitionBase(a5),a6
	jsr	_LVOScreenToFront(a6)
.NoSTF	movem.l	(sp)+,d0-d1/a0-a1/a6
	rts

StrCpy	; StrCpy(Source,Dest)(A0,A1)
	movem.l	a0-a1,-(sp)
.Loop	move.b	(a0)+,(a1)+
	bne.s	.Loop
	movem.l	(sp)+,a0-a1
	rts

StrNCpy	; StrNCpy(Source,Dest,Max)(A0,A1,D0)
	movem.l	a0-a1/d0,-(sp)
	bra.s	.Beg
.Loop	move.b	(a0)+,(a1)+
.Beg	dbeq	d0,.Loop
	movem.l	(sp)+,a0-a1/d0
	rts

LockFront
	move.l	v_ExecBase(a5),a6
	jsr	_LVOForbid(a6)
	moveq	#0,d0
	move.l	v_IntuitionBase(a5),a6
	jsr	_LVOLockIBase(a6)
	move.l	ib_FirstScreen(a6),a2
	move.l	d0,a0
	jsr	_LVOUnlockIBase(a6)
	jsr	_LVOLockPubScreenList(a6)
	move.l	v_ExecBase(a5),a6
	jsr	_LVOPermit(a6)
	move.l	v_IntuitionBase(a5),a6
	move.l	d0,a0
.NotThisScreen
	move.l	(a0),a0
	tst.l	(a0)
	beq.s	.NoMoreScreen
	cmp.l	psn_Screen(a0),a2
	bne.s	.NotThisScreen
	move.l	LN_NAME(a0),a0
	lea	v_ResultingName(a5),a1
	bsr	StrCpy
	jsr	_LVOLockPubScreen(a6)
	move.l	d0,v_LockedScreen(a5)
.NoMoreScreen
	jsr	_LVOUnlockPubScreenList(a6)
	tst.l	v_LockedScreen(a5)
	beq	GetDefaultPub

ExamScreen	; At this point, a screen (either requested, frontmost or default)
		; is locked, and we examine it for setting environment variables
		; (the name is already in ResultingName)
	bsr	ScreenToFront
	move.l	v_LockedScreen(a5),a4
	move.w	sc_Width(a4),v_Width(a5)
	move.w	sc_Height(a4),v_Height(a5)
	move.l	sc_RastPort+rp_BitMap(a4),a0
	move.l	v_GraphicsBase(a5),a6
	cmp.w	#39,LIB_VERSION(a6)
	blt.s	.OldDepth
	move.l	#BMA_DEPTH,d1
	jsr	_LVOGetBitMapAttr(a6)
	move.b	d0,v_Depth(a5)
	bra.s	.DepthGot
.OldDepth
	move.b	bm_Depth(a0),v_Depth(a5)
.DepthGot
	move.l	sc_Font(a4),a3	; TextAttr pointer
	move.w	ta_YSize(a3),v_FontSize(a5)
	move.l	ta_Name(a3),a0
	lea	v_FontName(a5),a1
	moveq	#108,d0	; should be sufficient, since fonts are stored as files
	bsr	StrNCpy

	; now we can unlock the screen and set the environment variables
	move.l	a4,a1
	suba.l	a0,a0
	move.l	v_IntuitionBase(a5),a6
	jsr	_LVOUnlockPubScreen(a6)

	lea	ScreenName.Var(pc),a0
	lea	v_ResultingName(a5),a1
	bsr.s	SetVar
	lea	FontName.Var(pc),a0
	lea	v_FontName(a5),a1
	bsr.s	SetVar
	moveq	#0,d0
	lea	FontSize.Var(pc),a0
	move.w	v_FontSize(a5),d0
	bsr.s	SetNumVar
	moveq	#0,d0
	lea	Width.Var(pc),a0
	move.w	v_Width(a5),d0
	bsr.s	SetNumVar
	moveq	#0,d0
	lea	Height.Var(pc),a0
	move.w	v_Height(a5),d0
	bsr.s	SetNumVar
	moveq	#0,d0
	lea	Depth.Var(pc),a0
	move.b	v_Depth(a5),d0
	bsr.s	SetNumVar
	moveq	#0,d7
	tst.l	v_QuietSwitch(a5)
	bne	FreeArgs
	lea	v_ResultingName(a5),a0
	move.l	a0,d1
	move.l	v_DOSBase(a5),a6
	jsr	_LVOPutStr(a6)
	bra	FreeArgs

SetVar	; SetVar(Name,Value)(A0,A1)
	movem.l	a0-a1/a6/d0-d4,-(sp)
	move.l	a0,d1
	move.l	a1,d2
	moveq	#-1,d3
	move.l	#GVF_LOCAL_ONLY,d4
	tst.l	v_LocalSwitch(a5)
	bne.s	.DoIt
	move.l	#GVF_GLOBAL_ONLY,d4
.DoIt	move.l	v_DOSBase(a5),a6
	jsr	_LVOSetVar(a6)
	movem.l	(sp)+,a0-a1/a6/d0-d4
	rts

SetNumVar	; SetNumVar(Name,Value)(A0,D0)
	movem.l	a0-a4/a6/d0-d1,-(sp)
	move.l	a0,a4
	lea	Decimal.Fmt(pc),a0
	move.l	d0,-(sp)
	move.l	sp,a1
	lea	StoreChar.Func(pc),a2
	lea	v_NumericBuffer(a5),a3
	move.l	v_ExecBase(a5),a6
	jsr	_LVORawDoFmt(a6)
	addq.l	#4,sp
	move.l	a4,a0
	lea	v_NumericBuffer(a5),a1
	bsr	SetVar
	movem.l	(sp)+,a0-a4/a6/d0-d1
	rts
StoreChar.Func
	move.b	d0,(a3)+
	rts

Intuition.Name	dc.b	'intuition.library',0
Graphics.Name	dc.b	'graphics.library',0
DOS.Name	dc.b	'dos.library',0
Args.Template	dc.b	'SCREENNAME,LV=LOCALVARS/S,STF=SCREENTOFRONT/S,QUIET/S',0
ScreenName.Var	dc.b	'PubScreen',0
FontName.Var	dc.b	'ScreenFont',0
FontSize.Var	dc.b	'ScreenFontSize',0
Width.Var	dc.b	'ScreenWidth',0
Height.Var	dc.b	'ScreenHeight',0
Depth.Var	dc.b	'ScreenDepth',0
Decimal.Fmt	dc.b	'%lu',0
	dc.b	'$VER:'
Help.String	dc.b	'ExamPub 1.0 (23.07.95) by F.Delacroix, FREEWARE',10
	dc.b	' ExamPub will examine the specified (or frontmost) public screen',10
	dc.b	' and print its name. A few environment variables are set, most',10
	dc.b	' useful when using hotkeys in ToolManager for a prog like Multiview',10,0
