NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF lib/libtui
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/net.telnet.i
				.INB inc/libtui.i
*--------------------------------------
				.DUMMY
				.OR ZPLIB

ZPScrnPtr		.BS 2
ZPObjPtr		.BS 2

ZPLineBufPtr	.BS 2
ZPLineBufCnt	.BS 2

ZPPtr1			.BS 2
ZPPtr2			.BS 2
TempW			.BS 2
TempC			.BS 2
TempI			.BS 2
bResize			.BS 1

				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START
				.DA 0
				.DA 0
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA LIB.LOAD
				.DA LIB.UNLOAD
				.DA LIB.Init
				.DA LIB.GetEvent
				.DA LIB.Dispatch
				.DA LIB.Close
				.DA OBJ.New
				.DA OBJ.Destroy
				.DA OBJ.Run
				.DA OBJ.Draw
				.DA OBJ.GetProp
				.DA OBJ.SetProp
				.DA OBJ.Activate
				.DA OBJ.Deactivate
*--------------------------------------
J.ObjNew		.DA SCRN.New
				.DA TBOX.New
				.DA LBOX.New
*--------------------------------------
J.ObjDestroy	.DA SCRN.Destroy
				.DA TBOX.Destroy
				.DA LBOX.Destroy
*--------------------------------------
J.ObjRun		.DA SCRN.Run
				.DA TBOX.Run
				.DA LBOX.Run
*--------------------------------------
J.ObjDraw		.DA SCRN.Draw
				.DA TBOX.Draw
				.DA LBOX.Draw
*--------------------------------------
J.ObjActivate	.DA SCRN.Activate
				.DA TBOX.Activate
				.DA LBOX.Activate
*--------------------------------------
J.ObjDeactivate	.DA SCRN.Deactivate
				.DA TBOX.Deactivate
				.DA LBOX.Deactivate
*--------------------------------------
L.SEQ.INIT		.DA SEQ.INIT
L.SEQ.GOTOXY	.DA SEQ.GOTOXY
L.SEQ.COLOR		.DA SEQ.COLOR
L.SEQ.CLOSE		.DA SEQ.CLOSE
L.FMT.ESCCSI.R	.DA FMT.ESCCSI.R
				.DA 0
*--------------------------------------
LIB.LOAD		lda LibCnt
				bne .8

				>LDYAI 512
				>SYSCALL2 GetMem
				bcs .9

				>STYA LineBuf
				stx hLineBuf



.8				inc LibCnt
				clc
.9				rts
*--------------------------------------
LIB.UNLOAD		dec LibCnt
				bne .8

				lda hLineBuf
				>SYSCALL2 FreeMem

.8				clc
				rts
*--------------------------------------
LIB.Init		sta TempW				F

				>LDYAI S.SCRN
				>SYSCALL2 GetMem
				bcs .99

				>STYA ZPScrnPtr
				txa
				>PUSHA					hScrn

				lda #0
				>PUSHA					Init Counter

				ldy #S.SCRN-1

.10				sta (ZPScrnPtr),y
				dey
				bpl .10

				lda TempW
				ldy #S.OBJ.F
				sta (ZPScrnPtr),y

				jsr LIB.GetC.Reset

				>LDYA L.SEQ.INIT
				jsr LIB.YAOut
				bcs .98

.1				>SLEEP

				ldy #1					hScrn
				lda (pStack),y

				jsr LIB.GetC
				bcs .98

				beq .2

				>SYSCALL PutChar
				bcs .98

				ldy #1					hScrn
				lda (pStack),y
				jsr LIB.GetScrn

.2				ldy #S.OBJ.W
				lda (ZPScrnPtr),y
				bne .8

				lda (pStack)
				dec
				sta (pStack)
				bne .1

				lda #80

				ldy #S.OBJ.W
				sta (ZPScrnPtr),y

				lda #24
				iny						S.OBJ.H
				sta (ZPScrnPtr),y

.8				>LDYA ZPScrnPtr
				jsr LIB.SetObjX2Y2

				ldy #S.OBJ.S
				lda #S.OBJ.S.bACTIVE
				sta (ZPScrnPtr),y

				inc pStack				Discard counter
				>PULLA					hScrn
				clc
.99				rts

.98				pha
				inc pStack				Discard counter
				>PULLA					hScrn
				>SYSCALL2 FreeMem
				pla
				sec

				rts
*--------------------------------------
LIB.GetEvent	stz bResize

				>PUSHA					hScrn

				jsr LIB.GetC
				bcs .9

				bit bResize
				bpl .8
				
				lda (pStack)
				jsr LIB.GetScrn

				ldy #S.OBJ.fRESIZE
				lda (ZPScrnPtr),y
				sta ZPPtr1

				iny
				lda (ZPScrnPtr),y
				sta ZPPtr1+1

				jsr .10

				stz bResize

				lda #0
				clc
.8
.9				inc pStack

				rts

.10				jmp (ZPPtr1)
*--------------------------------------
LIB.Dispatch	jsr OBJ.Run
				bcc .8
				
				lda (pStack)
				cmp #3					Ctrl-C
				beq .9

				clc

.8				

.9				inc pStack				Destroy Event
				inc pStack				on Stack

				rts
*--------------------------------------
LIB.Close		jsr OBJ.Destroy

				>LDYA L.SEQ.CLOSE
				jsr LIB.YAOut

				clc
				rts
*--------------------------------------
LIB.FindActive	ldx ZPObjPtr
				phx
				ldx ZPObjPtr+1
				phx

				jsr LIB.GetObj

*				ldy #S.OBJ.P
*				lda (ZPObjPtr),y
*				sec
*				beq .80

				ldy #S.OBJ.Childs
				
.1				phy
				lda (ZPObjPtr),y
				beq .7
				
				pha

				>SYSCALL2 GetMemPtr
				>STYA ZPPtr1
				ldy #S.OBJ.S
				lda (ZPPtr1),y
				and #S.OBJ.S.bACTIVE
				bne .8
				
				pla

.7				ply
				iny
				cpy #S.OBJ
				bne .1
				
*				sec
				bra .80
				
.8				pla
				ply
				clc
				
.80				plx
				stx ZPObjPtr+1
				plx
				stx ZPObjPtr
			
				rts
*--------------------------------------
LIB.GotoXY		>PUSHW ZPLineBufPtr
				>PUSHW L.SEQ.GOTOXY

				tya
				inc
				>PUSHA

				txa
				inc
				>PUSHA

				>PUSHBI 2
				jmp LIB.LineBufPrintF
*--------------------------------------
LIB.SetColor	tax
				>PUSHW ZPLineBufPtr
				>PUSHW L.SEQ.COLOR

				>PUSHB OBJ.COLORS,x
				>PUSHB OBJ.COLORS+1,x

				>PUSHBI 2
				jmp LIB.LineBufPrintF
*--------------------------------------
LIB.GetScrn		>SYSCALL2 GetMemPtr
				>STYA ZPScrnPtr
				rts
*--------------------------------------
LIB.GetObj		>SYSCALL2 GetMemPtr
				>STYA ZPObjPtr
				rts
*--------------------------------------
LIB.SetObjX2Y2	>STYA ZPPtr1

				ldy #S.OBJ.X1
				jsr .7

				ldy #S.OBJ.Y1
				jsr .7

				ldy #S.OBJ.F
				lda (ZPPtr1),y
				and #S.OBJ.F.bHBorder
				beq .1

				ldy #S.OBJ.InnerX
				lda (ZPPtr1),y
				inc
				sta (ZPPtr1),y

				iny
				iny

				lda (ZPPtr1),y			S.OBJ.InnerW
				dec
				dec
				sta (ZPPtr1),y

.1				ldx #0

				ldy #S.OBJ.F
				lda (ZPPtr1),y
				bit #S.OBJ.F.bVBorder+S.OBJ.F.bTITLE
				beq .2

				dex

.2				bit #S.OBJ.F.bVBorder+S.OBJ.F.bSTATUS
				beq .3

				dex

.3				txa
				beq .4

				ldy #S.OBJ.InnerY
				lda (ZPPtr1),y
				inc
				sta (ZPPtr1),y

 				txa

				iny
				iny

				clc
				adc (ZPPtr1),y			S.OBJ.InnerH
				sta (ZPPtr1),y

.4				ldy #S.OBJ.F
				lda (ZPPtr1),y
				bit #S.OBJ.F.bMENU
				beq .5

				ldy #S.OBJ.InnerY
				lda (ZPPtr1),y
				inc
				sta (ZPPtr1),y

				iny
				iny

				lda (ZPPtr1),y			S.OBJ.InnerH
				dec
				sta (ZPPtr1),y

.5				rts
*--------------------------------------
.7				lda (ZPPtr1),y			S.OBJ.X1/Y1
				tax

				iny
				iny						S.OBJ.W/H
				phy

				clc
				adc (ZPPtr1),y
				dec
				iny
				iny						S.OBJ.X2/Y2
				sta (ZPPtr1),y
				iny
				iny

				txa
				sta (ZPPtr1),y			S.OBJ.InnerX/Y

				ply						S.OBJ.W/H
				lda (ZPPtr1),y
				iny
				iny
				iny
				iny
				iny
				iny
				sta (ZPPtr1),y			S.OBJ.InnerW/H

				rts
*--------------------------------------
LIB.GetC		>PUSHA					hScrn

				ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL2 FEOF
				bcs .9

				tay
				beq .10
				
				inc pStack
				bra .18

.10				>SYSCALL2 GetChar
				bcc .11

.9				inc pStack
				rts

.11				pha

				>PULLA
				jsr LIB.GetScrn

				plx

				ldy #S.SCRN.bEscMode
				lda (ZPScrnPtr),y
				beq .20

				bmi .40					IAC mode

				iny 					S.SCRN.InBufPtr
				lda (ZPScrnPtr),y

				cpx #'['				CSI ?
				bne .12

				cmp #S.SCRN.InBufPtr
				bne .49					buffer not empty...bad SEQ

.12				jsr LIB.GetC.ToBuf
				bcs .49					Buffer Full

				cpx #'['
				beq .18

				cpx #64
				bcs LIB.GetC.ESCSEQ		if CC, not a letter...SEQ is incomplete

.18				lda #0
				clc
				rts
*--------------------------------------
.20				cpx #C.ESC
				bne .30

				inc						A = $01

				bra .31
*--------------------------------------
.30				cpx #IAC
				bne .38

				dec						A = $FF

.31				jsr LIB.GetC.SetMode

				lda #0
				clc
				rts
*--------------------------------------
.38				txa
				clc
.39				rts
*--------------------------------------
.40				jsr LIB.GetC.ToBuf
				bcs .49					Buffer Full

				cpy #S.SCRN.InBuf		First char...
				beq .48

				ldy #S.SCRN.InBuf
				lda (ZPScrnPtr),y
				cmp #SB					First char is SB ?
				bne LIB.GetC.IACSEQ		no ...

				cpx #SE					is SE after SB ..... ?
				bne .48

				bra LIB.GetC.IACSEQ		yes, we have IAC SB .....SE

.49				jsr LIB.GetC.Reset

.48				lda #0
				clc
				rts
*--------------------------------------
LIB.GetC.ESCSEQ	cmp #S.SCRN.InBuf+2
				bcs .2

				ldy #ESC.Out-ESC.In-1
				txa

.1				cmp ESC.In,y
				beq .7

				dey
				bpl .1

				bra .6
*--------------------------------------
* \e[xxx;yyyR
*--------------------------------------
.2				cpx #'R'				Response to cursor position query?
				bne .6

				lda #S.SCRN.InBuf+1		skip ESC [
				jsr LIB.GetC.GetScrnOfs
				>PUSHYA

				>PUSHW L.FMT.ESCCSI.R

				lda #S.OBJ.H
				jsr LIB.GetC.GetScrnOfs
				>PUSHYA

				lda #S.OBJ.W
				jsr LIB.GetC.GetScrnOfs
				>PUSHYA

				>PUSHBI 4				2 pointers
				>SYSCALL2 sscanf

.6				ldx #0
				bra .8

.7				ldx ESC.Out,y

.8				jsr LIB.GetC.Reset

				txa
				clc
				rts
*--------------------------------------
* IAC SB TN.O.NAWS ww ww hh hh IAC SE
*--------------------------------------
LIB.GetC.IACSEQ	ldy #S.SCRN.InBuf
				lda (ZPScrnPtr),y
				cmp #SB
				bne .8

				iny
				lda (ZPScrnPtr),y
				cmp #TN.O.NAWS
				bne .8

				iny
				lda (ZPScrnPtr),y
				bne .8

				iny
				lda (ZPScrnPtr),y
				cmp #80
				bcs .1

				lda #80

.1				tax

				iny
				lda (ZPScrnPtr),y
				bne .8

				iny
				lda (ZPScrnPtr),y
				cmp #24
				bcs .2

				lda #24

.2				ldy #S.OBJ.H

				sta (ZPScrnPtr),y

				dey						S.OBJ.W

				txa
				sta (ZPScrnPtr),y

				>LDYA ZPScrnPtr
				jsr LIB.SetObjX2Y2

				sec
				ror bResize

.8				jsr LIB.GetC.Reset

				lda #0
				clc
				rts
*--------------------------------------
LIB.GetC.Reset	lda #0

LIB.GetC.SetMode
				ldy #S.SCRN.bEscMode
				sta (ZPScrnPtr),y

				iny						Y = S.SCRN.InBufPtr
				tya
				sta (ZPScrnPtr),y

				rts
*--------------------------------------
LIB.GetC.GetScrnOfs
				clc
				adc ZPScrnPtr
				tay
				lda ZPScrnPtr+1
				adc #0
.9				rts
*--------------------------------------
LIB.GetC.ToBuf	ldy #S.SCRN.InBufPtr
				lda (ZPScrnPtr),y

				cmp #S.SCRN.EndBuf		buffer full
				bcs .9

				phy
				inc
				tay
				txa
				sta (ZPScrnPtr),y
				tya
				ply
				sta (ZPScrnPtr),y

*				clc

.9				rts
*--------------------------------------
LIB.LineBufInit	>LDYA LineBuf
				>STYA ZPLineBufPtr
				stz ZPLineBufCnt
				stz ZPLineBufCnt+1
				rts
*--------------------------------------
LIB.LineBufSeqOut
.1				lda SEQS,x
				beq .8

				jsr LIB.LineBufCOut
				inx
				bra .1

.8				rts
*--------------------------------------
LIB.LineBufYAOut
				ldx #0
*--------------------------------------
LIB.LineBufYAOutX
				>STYA ZPPtr1			X = MaxLen

				ldy #$ff

.1				iny
				lda (ZPPtr1),y
				beq .8

				jsr LIB.LineBufCOut
				dex
				bne .1

.8				rts
*--------------------------------------
LIB.LineBufPrintF
				>SYSCALL2 SPrintF
				tya

				clc
				adc ZPLineBufPtr
				sta ZPLineBufPtr
				bcc .1

				inc ZPLineBufPtr+1
				clc

.1				tya
				adc ZPLineBufCnt
				sta ZPLineBufCnt
				bcc .8
				
				inc ZPLineBufCnt+1
				clc

.8				rts
*--------------------------------------
LIB.LineBufHOut	pha
				lsr
				lsr
				lsr
				lsr
				jsr .1

				pla
				and #$0F

.1				ora #$30
				cmp #'9'+1
				bcc LIB.LineBufCOut

				adc #6
*--------------------------------------
LIB.LineBufCOut	sta (ZPLineBufPtr)
				inc ZPLineBufPtr
				bne .1

				inc ZPLineBufPtr+1

.1				inc ZPLineBufCnt
				bne .8
	
				inc ZPLineBufCnt+1
				
.8				rts
*--------------------------------------
LIB.LineBufOut	ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW LineBuf
				>PUSHW ZPLineBufCnt
				>SYSCALL fwrite
				rts
*--------------------------------------
LIB.YAOut		dec pStack
				>PUSHYA
				ldy #S.PS.hStdOut
				lda (pPS),y
				ldy #2
				sta (pStack),y
				>SYSCALL fputs
				rts
*--------------------------------------
				.INB usr/src/lib/libtui.s.obj
				.INB usr/src/lib/libtui.s.lbox
				.INB usr/src/lib/libtui.s.scrn
				.INB usr/src/lib/libtui.s.tbox
*--------------------------------------
CS.END
*--------------------------------------
LibCnt			.BS 1
hLineBuf		.BS 1
LineBuf			.BS 2
*--------------------------------------
ESC.In			.AS "DBAC"
ESC.Out			.DA #C.BS,#C.LF,#C.VT,#C.FS
*--------------------------------------
FMT.ESCCSI.R	.AZ "%d;%d"
*--------------------------------------
OBJ.SIZES		.DA S.SCRN,S.TBOX,S.LBOX
*--------------------------------------
OBJ.COLORS		.DA #93,#44				SCRN
				.DA #93,#104			TBOX
				.DA #93,#104			LBOX
*--------------------------------------
SEQ.INIT		.DA #C.ESC
				.AS "c"					FF
				.DA #C.ESC
				.AS "[999;999H"			Set Cursor Pos to 999,999
				.DA #C.ESC
				.AS "[6n"				then query Cursor Pos
				.DA #C.ESC
				.AS "(B"				font select
				.DA #C.ESC
				.AS ")0"				font select
				.DA #C.ESC
				.AS "[?7l"				Disable Line Wrap
				.DA #C.ESC
				.AS "[?25l"				Hide Cursor
				.DA #0
*--------------------------------------
SEQ.GOTOXY		.AZ "\e[%d;%dH"
*--------------------------------------
SEQ.COLOR		.AZ "\e[%d;%dm"
*--------------------------------------
SEQS			.EQ *
*--------------------------------------
SEQ.CURON		.DA #C.ESC
				.AS "[?25h"				Show Cursor
				.DA #0
SEQ.CUROFF		.DA #C.ESC
				.AS "[?25l"				Hide Cursor
				.DA #0
*--------------------------------------
SEQ.BAR			.DA #C.ESC
				.AS "[1m"				BOLD
				.DA #C.ESC
				.AS "[7m"				INVERSE
				.DA #C.ESC
				.AS "[37;40m"
				.DA #0
SEQ.BAREND		.DA #C.ESC
				.AS "[0m"
				.DA #0
*--------------------------------------
SEQ.BORDERTL	.DA #C.SO,#'l'
				.DA #0
SEQ.BORDERTR	.DA #C.SO,#'k',#C.SI
				.DA #0
SEQ.BORDERBL	.DA #C.SO,#'m'
				.DA #0
SEQ.BORDERBR	.DA #C.SO,#'j',#C.SI
				.DA #0
*SEQ.BORDERH		.DA #C.SO,#'q',#C.SI
*					.DA #0
SEQ.BORDERV		.DA #C.SO,#'x',#C.SI
				.DA #0
SEQ.INVERSE		.DA #C.ESC
				.AS "[7m"
				.DA #0
SEQ.NORMAL		.DA #C.ESC
				.AS "[0m"
				.DA #0
*--------------------------------------
*SEQ.SCROLLCURUP	.AS "\e[?7l\e[%d;%dr"
*SEQ.SCROLLUP	.AZ "\eD"
*SEQ.SCROLLCURDN	.AS "\e[?7l\e[%d;%dr"
*SEQ.SCROLLDN	.AZ "\eM"
*--------------------------------------
SEQ.CLOSE		.DA #C.ESC
				.AS "c"
				.DA #C.ESC
				.AZ "[?7h"				Enable Line Wrap
*--------------------------------------
MAN
SAVE usr/src/lib/libtui.s
ASM
