0010  // Console demo
0020
0030 USE SYSTEM
0040 USE WINDOWS
0050 USE MESSAGES
0060 USE DEVICES
0070
0080 POINTER consolewindow@ TO window@
0090 POINTER conwrtport@ TO msgport@, conreadport@ TO msgport@
0100 POINTER conwrtreq@ TO iostdreq@, conreadreq@ TO iostdreq@
0110 POINTER char%
0120
0130 init
0140
0150 REPEAT
0160   readconsole(t$)
0170   writeconsole(t$)
0180   IF t$=chr$(13) THEN writeconsole(chr$(10))
0190 UNTIL ret'signal# BITAND close'mask#
0200
0210 clean'up
0220
0230 PROC open'window(REF windowptr@,REF name$) CLOSED
0240   POINTER mynewwindow@ TO newwindow@
0250   allocate(mynewwindow@)
0260   mynewwindow@:=newwindow@
0270   mynewwindow@.leftedge%:=300
0280   mynewwindow@.topedge%:=100
0290   mynewwindow@.type%:=customscreen%
0300   mynewwindow@.screen#:=comalstruct@.exec_io@.screen#
0310   mynewwindow@.maxwidth%:=600
0320   mynewwindow@.minwidth%:=50
0330   mynewwindow@.minheight%:=20
0340   mynewwindow@.maxheight%:=200
0350   mynewwindow@.flags#:=windowdrag#+windowsizing#+windowdepth#+activate#
0360   mynewwindow@.flags#:+windowclose#
0370   mynewwindow@.title#:=^name$+4
0380   mynewwindow@.idcmpflags#:=$200 // windowclose
0390   ^windowptr@:=openwindow(^mynewwindow@)
0400   deallocate(mynewwindow@)
0410 ENDPROC open'window
0420
0430 PROC init
0440   dev'open:=false
0450   WAIT 0.5
0460   name$:="ConsoleIO"
0470   open'window(consolewindow@,name$)
0480   IF ^consolewindow@=0 THEN clean'up
0490   ^conwrtport@:=createport("ConWrtPort",0)
0500   IF ^conwrtport@=0 THEN clean'up
0510   ^conreadport@:=createport("ConReadPort",0)
0520   IF ^conreadport@=0 THEN clean'up
0550   readsignal!:=conreadport@.mp_sigbit!
0560   read'mask#:=2^readsignal!
0570   closesignal!:=consolewindow@.userport@.mp_sigbit!
0580   close'mask#:=2^closesignal!
0590   sig'mask#:=read'mask# BITOR close'mask#
0600   ^conwrtreq@:=createstdio(^conwrtport@)
0610   IF ^conwrtreq@=0 THEN clean'up
0620   ^conreadreq@:=createstdio(^conreadport@)
0630   IF ^conreadreq@=0 THEN clean'up
0640   conwrtreq@.io_data#:=^consolewindow@
0650   conwrtreq@.io_length#:=varsize(consolewindow@)
0660   conname$:="console.device"
0670   IF pckopendevice(^conname$+4,0,^conwrtreq@,0)<>0 THEN clean'up
0680   dev'open:=true
0690   conreadreq@.io_device#:=conwrtreq@.io_device#
0700   conreadreq@.io_unit#:=conwrtreq@.io_unit#
0710   allocate(char%,1)
0720   queueread
0730 ENDPROC init
0740
0750 PROC clean'up
0760   IF dev'open THEN pckabortio(^conreadreq@)
0770   IF dev'open THEN pckclosedevice(^conwrtreq@)
0780   IF ^conreadreq@<>0 THEN deletestdio(^conreadreq@)
0790   IF ^conwrtreq@<>0 THEN deletestdio(^conwrtreq@)
0800   IF ^conreadport@<>0 THEN deleteport(^conreadport@)
0810   IF ^conwrtport@<>0 THEN deleteport(^conwrtport@)
0820   IF ^consolewindow@<>0 THEN closewindow(^consolewindow@)
0830   freeallsignals
0840 ENDPROC clean'up
0850
0860 PROC writeconsole(string$)
0870   conwrtreq@.io_data#:=^string$+4
0880   conwrtreq@.io_length#:=-1
0890   conwrtreq@.io_command%:=cmd_write%
0900   dummy:=pckdoio(^conwrtreq@)
0910 ENDPROC writeconsole
0920
0930 PROC queueread
0940   conreadreq@.io_data#:=^char%+1
0950   conreadreq@.io_length#:=1
0960   conreadreq@.io_command%:=cmd_read%
0970   pcksendio(^conreadreq@)
0980 ENDPROC queueread
0990
1000 PROC readconsole(REF c$)
1010   REPEAT
1020     ret'signal#:=comalwait(sig'mask#)
1030   UNTIL ret'signal# BITAND sig'mask#
1040   dummy:=pckgetmsg(^conreadport@)
1050   IF conreadreq@.io_actual#=1 THEN
1060     c$:=chr$(char%)
1070   ELSE
1080     c$:=""
1090   ENDIF
1100   queueread
1110 ENDPROC readconsole
