****************************************************
* Mensch.s (C) May 14 1994 by CHS
*
* Add an input handler, which waits till they
* press the RMB on the Screen Cycle gadget,
* Then slam up a menu of available screens,
* Let them choose one, then bring it to the
* front.
*
****************************************************

	incdir	include:
	include	exec/exec.i
	include	devices/input.i
	include	devices/inputevent.i
	include	dh0:text/proto/lvo.offs
	include	intuition/intuitionbase.i
	include	intuition/screens.i

main	move.l	4.w,a6
	lea	intname,a1
	jsr	LVO_OldOpenLibrary(a6)	Open Intuition...
	move.l	d0,intbase

	move.l	4.w,a6
	lea	dosname,a1
	jsr	LVO_OldOpenLibrary(a6)	Open dos...
	move.l	d0,dosbase

	move.l	d0,a6
	jsr	LVO_Output(a6)	Get output for usage
	move.l	d0,stdout

	move.l	4.w,a6
	lea	name,a1
	jsr	LVO_FindPort(a6)
	tst.l	d0
	beq.s	.go

	move.l	dosbase,a6
	move.l	stdout,d1
	move.l	#alread,d2
	move.l	#alreadlen,d3
	jsr	LVO_Write(a6)
	bra	.mainexit1

.go	lea	taskport,a4	make a message port
	bsr	makeport
	bne	.mainexit1
	move.l	a4,a1
	move.l	4.w,a6
	jsr	LVO_AddPort(a6)	Add the Port

	lea	myioreq,a0	Init my IoReq
	move.l	#taskport,MN_REPLYPORT(a0)	Make IoStdReq

	move.l	4.w,a6
	lea	inname,a0
	move.l	#0,d0	Unit 0
	lea	myioreq,a1
	moveq	#0,d1
	jsr	LVO_OpenDevice(a6)	Open the device
	tst.l	d0
	bne	.exit1

	bsr	getsignal	get a signal for talking

	lea	myint,a0
	move.l	#myhandler,IS_CODE(a0)
	move.l	#myspace,IS_DATA(A0)
	move.b	#60,LN_PRI(a0)	Priority > Intuitions

	lea	myioreq,a1
	move	#IND_ADDHANDLER,IO_COMMAND(a1)
	move.l	#myint,IO_DATA(a1)
	move.l	4.w,a6
	jsr	LVO_DoIO(a6)	Add the Handler ?
	tst.l	d0
	bne	.exit1	or quit

	sf	dunner
	sf	lamiga
	sf	dont

.mor	move.l	4.w,a6
	move.l	signalset,d0
	jsr	LVO_Wait(a6)

	;Do the list of windows here...

	st	dont
	bsr	windowlister
	sf	dont

	bra	.mor	go wait more

.killer	move.l	intbase,a6
	sub.l	a0,a0
	jsr	LVO_DisplayBeep(a6)	Deader!

	lea	myioreq,a1
	move	#IND_REMHANDLER,IO_COMMAND(a1)
	move.l	#myint,IO_DATA(a1)
	move.l	4.w,a6
	jsr	LVO_DoIO(a6)	Remove the Handler

.exit1	move.l	4.w,a6
	lea	taskport,a1	remove message port
	jsr	LVO_RemPort(a6)

.mainexit1	move.l	4.w,a6
	move.l	dosbase,a1
	jsr	LVO_CloseLibrary(a6)	Close dos

	move.l	intbase,a1
	jsr	LVO_CloseLibrary(a6)	Close int

	moveq	#0,d0
	rts

; Init a message port

makeport	move.l	a4,a1
	moveq	#7,d7
.gg	clr.l	(a1)+
	dbra	d7,.gg
	move.l	4.w,a6
	sub.l	a1,a1
	jsr	LVO_FindTask(a6)
	move.l	d0,taskptr
	move.l	d0,MP_SIGTASK(a4)
	moveq	#-1,d0
	jsr	LVO_AllocSignal(a6)
	tst.l	d0
	bmi.s	.cant
	move.b	d0,MP_SIGBIT(a4)
	move.l	#name,LN_NAME(a4)
	move.b	#5,LN_PRI(a4)
	move.b	#NT_MSGPORT,LN_TYPE(a4)
	moveq	#0,d0
	rts
.cant	moveq	#1,d0
	rts

getsignal	moveq	#-1,d0
	jsr	LVO_AllocSignal(a6)
	moveq	#1,d1
	lsl.l	d0,d1
	move.l	d1,signalset
	rts

*******************************************************
* The Handler

myhandler	movem.l	d1-a6,-(a7)

	tst.b	dont
	bne.s	.ex	Already up ?

	move.b	ie_Class(a0),d2

	cmp.b	#IECLASS_RAWMOUSE,d2	Was a mouse ?
	bne	.ex

	cmp	#$69,ie_Code(a0)	RMB hit ?
	bne.s	.ex

	move.l	intbase,a6
	move.l	ib_FirstScreen(a6),a1
	move	sc_MouseX(a1),d1
	move	sc_MouseY(a1),d2
	move	sc_Width(a1),d3
	sub	d1,d3
	cmp	#20,d3	!? Size of Gadget ? Where ?
	bgt.s	.ex	Hmm fuckit
	cmp	#11,d2
	bgt.s	.ex

	move.b	#IECLASS_NULL,ie_Class(a0)	Snaff It!

	move.l	a0,-(a7)

	move.l	4.w,a6
	move.l	taskptr,a1
	move.l	signalset,d0
	jsr	LVO_Signal(a6)	Tell it to do it

	move.l	(a7)+,a0

.ex	move.l	a0,d0
	movem.l	(a7)+,d1-a6

	rts

windowlister	bsr	buildnamelist	Build the menu

	move.l	intbase,a6
	move.l	ib_FirstScreen(a6),a1
	move.l	a1,nw_Screen+newwin
	move	sc_Width(a1),d0
	sub	#62,d0
	move	d0,Menu1+4	Put menu on the right

	lea	newwin,a0
	move.l	intbase,a6
	jsr	LVO_OpenWindow(a6)
	move.l	d0,wndw
	beq	exit

	move.l	wndw,a0
	lea	Menu1,a1
	jsr	LVO_SetMenuStrip(a6)


	lea	myioreq,a1
	lea	myinputevent,a2
	move	#IND_WRITEEVENT,IO_COMMAND(a1)
	move.l	a2,IO_DATA(a1)
	move.l	#ie_SIZEOF,IO_LENGTH(a1)

	clr.l	ie_NextEvent(a2)
	move.b	#IECLASS_RAWMOUSE,ie_Class(a2)
	clr.b	ie_SubClass(a2)
	move	#$69,ie_Code(a2)
	move	#IEQUALIFIER_RBUTTON,ie_Qualifier(a2)
	move.l	4.w,a6
	jsr	LVO_DoIO(a6)	Do the IO ?

	move.l	4.w,a6
	move.l	wndw,a0
	move.l	wd_UserPort(a0),a0
	jsr	LVO_WaitPort(a6)

	move.l	4.w,a6
	move.l	wndw,a0
	move.l	wd_UserPort(a0),a0
	jsr	LVO_GetMsg(a6)

	move.l	d0,-(a7)
	move.l	d0,a1
	jsr	LVO_ReplyMsg(a6)
	move.l	(a7)+,a0

	cmp.l	#MENUPICK,im_Class(a0)
	bne.s	.dd

	move	im_Code(a0),d0
	move	d0,-(a7)
	move.l	intbase,a6
	lea	MenuList1,a0
	jsr	LVO_ItemAddress(a6)	points to struct ?
	move	(a7)+,d1
	tst.l	d0
	beq.s	.dd

	move	d1,d0
	move	d0,d1
	move	d0,d2
	lsr	#5,d1
	lsr	#8,d2	! 11
	lsr	#3,d2
	and	#$1f,d0	menu
	and	#$3f,d1	item
	and	#$1f,d2	subitem

	add	d1,d1
	add	d1,d1
	lea	scptrs,a0
	move.l	(a0,d1),a0
	move.l	a0,-(a7)
	jsr	LVO_ScreenToFront(a6)
	move.l	(a7)+,a0
	move.l	sc_FirstWindow(a0),d0
	beq.s	.dd
	move.l	d0,a0
	move.l	d0,-(a7)
	jsr	LVO_ActivateWindow(a6)
	move.l	(a7)+,a0
	jsr	LVO_RefreshWindowFrame(A6)

.dd	move.l	wndw,a0
	move.l	intbase,a6
	jsr	LVO_CloseWindow(a6)

exit	rts

*******************************************
; Build list of all available screens

buildnamelist	move.l	intbase,a0
	move.l	ib_FirstScreen(a0),a0

	lea	nambuff,a2
	lea	MenuItem1,a4
	lea	IText1,a5
	lea	scptrs,a6

	moveq	#0,d6	Y pos

.mor	move.l	a0,(a6)+	Remember which screen

	move.l	a2,a3	Pad this string
	moveq	#19,d7
.ggcl	move.b	#' ',(a3)+
	dbra	d7,.ggcl
	clr.b	(a3)+

	move.l	sc_Title(a0),a1
	cmp.l	#0,a1
	bne.s	.pp

.dd	move.l	sc_DefaultTitle(a0),a1
	cmp.l	#0,a1
	beq.s	.nn

.pp	;Skip leading spaces...


.ff	cmp.b	#' ',(a1)
	bne.s	.go

	addq.w	#1,a1
	bra.s	.ff

.go	tst.b	(a1)
	bne.s	.copy

	bra.s	.copy

.nn	;Hmmm, try to get name from a window?

	move.l	sc_FirstWindow(a0),a3

.koi	cmp.l	#0,a3
	beq.s	.nn1

	move.l	wd_ScreenTitle(a3),a1
	cmp.l	#0,a1
	bne.s	.ff

	move.l	wd_NextWindow(a3),a3
	bra.s	.koi

.nn1	lea	noname,a1

.copy	moveq	#24,d7

	move.l	a2,a3

	moveq	#0,d2	spaces

.mo	move.b	(a1)+,d0
	beq.s	.dun1
	cmp.b	#' ',d0
	bne.s	.nor
	tst	d2
	bne.s	.dun1
	addq	#1,d2
.nor	move.b	d0,(a3)+
	dbra	d7,.mo

.dun1	;Build menu item

	clr.l	(a4)+
	move	#-120,(a4)+
	move	d6,(a4)+	Y
	move	#200,(a4)+
	move	#8,(a4)+
	move	#ITEMTEXT+ITEMENABLED+HIGHCOMP,(a4)+
	clr.l	(a4)+
	move.l	a5,(a4)+
	clr.l	(a4)+
	clr	(a4)+
	clr.l	(a4)+
	move	#MENUNULL,(a4)+

	;Build itext

	move.b	#3,(a5)+
	move.b	#1,(a5)+
	move.b	#RP_COMPLEMENT,(a5)+
	move.b	#0,(a5)+
	move	#0,(a5)+
	move	#0,(a5)+
	move.l	#TOPAZ80,(a5)+
	move.l	a2,(a5)+	TEXT POINTER
	clr.l	(a5)+

	lea	25(a2),a2	Next nambuff

	move.l	sc_NextScreen(a0),d0
	beq.s	.dun

	move.l	a4,-34(a4)

	addq.w	#8,d6

	move.l	d0,a0
	bra	.mor

.dun	rts

*******************************************

	section	bill,data

noname	dc.b	"Unnamed",0
	even

newwin	dc.w	0,0	;window XY origin relative to TopLeft of screen
	dc.w	1,1	;window width and height
	dc.b	0,1	;detail and block pens
	dc.l	IDCMP_INACTIVEWINDOW!$100	;IDCMP flags MENUPICK
	dc.l	SIMPLE_REFRESH+ACTIVATE+NOCAREREFRESH+BORDERLESS	;other window flags
	dc.l	0	;first gadget in gadget list
	dc.l	0	;custom CHECKMARK imagery
	dc.l	0	;window title
	dc.l	0	;custom screen pointer
	dc.l	0	;custom bitmap
	dc.w	5,5	;minimum width and height
	dc.w	-1,-1	;maximum width and height
	dc.w	CUSTOMSCREEN	;destination screen type

MenuList1
Menu1	dc.l	0
	dc.w	0,0
	dc.w	62,0
	dc.w	MENUENABLED
	dc.l	Menu1Name
	dc.l	MenuItem1
	dc.w	0,0,0,0

Menu1Name	dc.b	'Screens',0
	even

scptrs	ds.l	15
MenuItem1	ds.b	34*15	Room for 15 items
IText1	ds.b	20*15
nambuff	ds.b	26*15
	even

TOPAZ80	dc.l	TOPAZname
	dc.w	TOPAZ_EIGHTY
	dc.b	0,0

TOPAZname	dc.b	'topaz.font',0
	even

wndw	ds.l	1


	section	boll,data

inname	dc.b	"input.device",0
dosname	dc.b	"dos.library",0
intname	dc.b	"intuition.library",0
name	dc.b	"MENSCH_PORT",0
alread	dc.b	"Mensch is already running...",10
alreadlen	=	*-alread

	section	cak,bss

taskport	ds.l	8
myioreq	ds.b	IOSTD_SIZE
myint	ds.b	IS_SIZE
myinputevent	ds.b	ie_SIZEOF
myspace	ds.b	10
dosbase	ds.l	1
intbase	ds.l	1
signalset	ds.l	1
taskptr	ds.l	1
dunner	ds.b	1
lamiga	ds.b	1
dont	ds.b	2
argv	ds.l	1	args details
argc	ds.w	1
numargs	ds.w	1
nargs	ds.l	1
args	ds.b	256
argptrs	ds.l	16
stdout	ds.l	1
