 
	Section 	ConsoleDevice,code
	opt	o+,d+,w-
	
	include	system

	*-----------------------------------------------------------*

Start:	Lea	IntName,A1
	Moveq	#0,D0
	CALLEXEC	OpenLibrary
	Move.l	D0,_IntuitionBase
	Beq	Error

	*--------------*

	move.l	#0,a1
	CALLEXEC	FindTask

	lea	Reply_Port,a1
	move.l	d0,MP_SIGTASK(a1)
	CALLEXEC	AddPort
		
	lea	IO_Port,a1
	move.l	#-1,d0
	move.l	#0,d1
	lea	console_dev,a0
	CALLEXEC	OpenDevice
	cmp.l	#0,d0
	bne	Quit2
	
	lea	IO_Port,a0
	move.l	IO_DEVICE(a0),d0
	move.l	d0,ConsoleDevice

	*---------------*

	Lea	MyWindow,a0
	CALLINT	OpenWindow
	Move.l	D0,_MyWinBase
	Beq	Quit
	
	Move.l	D0,A0
	Move.l	wd_RPort(A0),_MyWinRPort
	Move.l	wd_UserPort(a0),_MyWinUserPort

	*-----------------------------------------------*
	
.mainloop	move.l	#0,Ascii
	
	Move.l	_MyWinUserPort,A0
	CALLEXEC	WaitPort
	Move.l	_MyWinUserPort,A0
	CALLEXEC	GetMsg
	
	Move.l	D0,A1
	Move.l	im_Class(A1),D2	;D2=IDCMP Flags Directly
	Move.w	im_Code(A1),D3	;D3=Codes ie key class=Rawkey
	Move.w	im_Qualifier(A1),D4	;D4=things like CTRL SHIFT
	Move.w	im_MouseX(a1),D5	;D5=MouseX Coordinate
	Move.w	im_MouseY(a1),D6	;D6=MouseY Coordinate
	Move.l	im_IAddress(A1),D7	;D7=Addres of Intuition Obj ie Gadget
	
	move.w	d4,OptionKeys

	Cmp.l	#RAWKEY,D2
	bne	.next

	*---------------------*

	Movem.l	D0-D7/A0-A6,-(A7)	
	
	lea	Event,a0
	move.b	#IECLASS_RAWKEY,ie_Class(a0)
	move.w	im_Code(a1),ie_Code(a0)
	move.w	im_Qualifier(a1),ie_Qualifier(a0)
	move.l	im_IAddress,ie_EventAddress(a0)

	lea	ConBuff,a1
 
	move.l	#15,d1
	move.l	#0,a2
	move.l	ConsoleDevice,a6
	jsr	-48(a6)
	cmp.l	#0,d0
	beq	.nokey

	lea	Ascii,a0
	lea	ConBuff,a1
.copy	move.b	(a1),(a0)+
	move.b	#0,(a1)+
	cmp.b	#0,(a1)
	bne	.copy
	move.b	#0,(a0)+
	move.b	#0,(a0)+
	move.b	#0,(a0)+
	move.b	#0,(a0)+

    ;at this point `Ascii' contains ascii code(s) from console device
    ;and `OptionKeys' is stuff like alt, control, shift etc.
		
	move.w	Ascii,$dff180

.nokey	Movem.l	(a7)+,D0-D7/A0-A6
.next	Movem.l	D0-D7/A0-A6,-(A7)	
	CALLEXEC	ReplyMsg
	Movem.l	(a7)+,D0-D7/A0-A6
	
	Cmp.l	#CLOSEWINDOW,D2
	bne	.mainloop


	Move.l	_MyWinBase,A0
	CALLINT	CloseWindow

	*-----------------------------*

Quit	lea	IO_Port,a1
	CALLEXEC	CloseDevice

Quit2	lea	Reply_Port,a1
	CALLEXEC	RemPort
	
	*-----------------------------*
	
CloseInt	Move.l	_IntuitionBase,A1
	CALLEXEC	CloseLibrary

	*-----------------------------*

Error	Moveq	#0,D0
	Rts

*----------------------------------------------------------*

	section	testdata,data

_MyWinBase		Dc.l	0
_MyWinRPort		Dc.l	0
_MyWinUserPort	Dc.l	0
_MyWinVPort		Dc.l	0

_IntuitionBase	Dc.l	0

ConBuff:		ds.b	16
Event:		ds.b	16
Ascii:		dc.l	0,0,0,0
OptionKeys:		dc.l	0

IO_Port:		dcb.l	20,0
Reply_Port:		dcb.l	8,0
ConsoleDevice:	dc.l	0

IntName		INTNAME
console_dev		dc.b	"console.device",0
		even
			
**************************************************

MyWindow:	dc.w	0,0	
	dc.w	640,200
	dc.b	0,1	
	dc.l	CLOSEWINDOW+RAWKEY
	dc.l	NOCAREREFRESH+WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH
	dc.l	0	
	dc.l	0	
	dc.l	MyName
	dc.l	0	
	dc.l	0	
	dc.w	5,5	
	dc.w	-1,-1	
	dc.w	WBENCHSCREEN	

MyName:	dc.b	"Console Device is cool as fuck,",0
	even
	
**************************************************
