NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
IO.GetC			jsr IO.GetChar
				bcs .9

				tax

				ldy #S.CTX.bEscMode
				lda (ZPpCtx),y
				beq .20

				bmi .40					IAC mode

				iny 					S.CTX.InBufPtr
				lda (ZPpCtx),y

				cpx #'['				CSI ?
				bne .12

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

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

				cpx #'['
				beq .18

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

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

				inc						A = $01

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

				dec						A = $FF

.31				jsr IO.GetC.SetMode

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

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

				ldy #S.CTX.InBuf
				lda (ZPpCtx),y
				cmp #SB					First char is SB ?
				bne .41
*				bne IO.GetC.IACSEQ		no ...

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

.41				bra IO.GetC.IACSEQ		yes, we have IAC SB .....SE

.49				jsr IO.GetC.Reset

.48				lda #0
				clc
				rts
*--------------------------------------
IO.GetC.ESCSEQ	cmp #S.CTX.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.CTX.InBuf+1		skip ESC [
				jsr IO.GetC.GetCtxEA

				>SS
				>PUSHYA
				>PUSHW L.FMT.ESCCSI.R
				lda #S.CTX.H
				jsr IO.GetC.GetCtxEA
				>PUSHYA
				lda #S.CTX.W
				jsr IO.GetC.GetCtxEA
				>PUSHYA
				>PUSHBI 4				2 pointers
				>LIBC SScanF
				>SR
				bcs .9

.6				ldx #0
				bra .8

.7				ldx ESC.Out,y

.8				jsr IO.GetC.Reset

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

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

				iny
				lda (ZPpCtx),y
				bne .8

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

				lda #80

.1				tax

				iny
				lda (ZPpCtx),y
				bne .8

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

				lda #24

.2				ldy #S.CTX.H

				sta (ZPpCtx),y

				dey						S.CTX.W

				txa
				sta (ZPpCtx),y

				jsr IO.GetC.Reset

				lda #EV.RESIZE
				clc
				rts

.8				jsr IO.GetC.Reset

				lda #0
				clc
.9				rts
*--------------------------------------
IO.GetC.Reset	lda #0

IO.GetC.SetMode
				ldy #S.CTX.bEscMode
				sta (ZPpCtx),y

				iny						Y = S.CTX.InBufPtr
				tya
				sta (ZPpCtx),y

				rts
*--------------------------------------
IO.GetC.GetCtxEA
				clc
				adc ZPpCtx
				tay
				lda ZPpCtx+1
				adc #0
.9				rts
*--------------------------------------
IO.GetC.ToBuf	ldy #S.CTX.InBufPtr
				lda (ZPpCtx),y

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

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

*				clc

.9				rts
*--------------------------------------
IO.GetChar		ldx ZPpCtx
				phx
				ldx ZPpCtx+1
				phx
				ldx ZPpObj
				phx
				ldx ZPpObj+1
				phx

				>LIBC GetChar
				bcs .9

				cmp #C.CR
				clc
				bne .9

				ldy #S.PS.pStdIn		Check for any extra LF
				jsr A2osX.GetPSy
				pha
				iny
				jsr A2osX.GetPSy
				ply
				>LIBC FEOF
				bcs .9

				tya
				bne .8

				>LIBC GetChar
				bcs .9

.8				lda #C.CR

.9				plx
				stx ZPpObj+1
				plx
				stx ZPpObj
				plx
				stx ZPpCtx+1
				plx
				stx ZPpCtx

				rts
*--------------------------------------
IO.fputs		ldx ZPpCtx
				phx
				ldx ZPpCtx+1
				phx
				ldx ZPpObj
				phx
				ldx ZPpObj+1
				phx

				>SS
				>PUSHYA
				ldy #S.PS.pStdOut+1
				jsr A2osX.GetPSy
				>PUSHA
				dey
				jsr A2osX.GetPSy
				>PUSHA
				>LIBC FPutS
				>SR

				plx
				stx ZPpObj+1
				plx
				stx ZPpObj
				plx
				stx ZPpCtx+1
				plx
				stx ZPpCtx

				rts
*--------------------------------------
IO.LBWrite		ldx ZPpCtx
				phx
				ldx ZPpCtx+1
				phx
				ldx ZPpObj
				phx
				ldx ZPpObj+1
				phx

				>SS
				ldy #S.PS.pStdOut+1
				jsr A2osX.GetPSy
				>PUSHA
				dey
				jsr A2osX.GetPSy
				>PUSHA
				>PUSHW pLineBuf
				>PUSHW ZPLineBufCnt
				>LIBC FWrite
				>SR

				plx
				stx ZPpObj+1
				plx
				stx ZPpObj
				plx
				stx ZPpCtx+1
				plx
				stx ZPpCtx

				rts
*--------------------------------------
MAN
SAVE usr/src/lib/libtui.s.io
LOAD usr/src/lib/libtui.s
ASM
