// PupControlInit.bcpl
// Copyright Xerox Corporation 1979
// Last modified December 26, 1978  11:31 PM by Boggs

get "Pup0.decl"
get "Pup1.decl"
get "Streams.d"
get "PupTest.decl"

external
[
//outgoing procedures
InitPupControl; InitAltoSLA

//incoming procedures from OS
InitializeZone; AddToZone; Allocate
MyFrame; Zero; CallSwat
OpenFile; FileLength; ReadBlock; Closes; Resets
CreateDisplayStream; ShowDisplayStream; GetFont
InitPupLevel1; Enqueue; InitializeContext

//incoming procedures from PupControl
MouseWatcher; Command; Host; MiscServ
Wss; Ows

//outgoing statics
zone; statFile; hT; dis; ctxQ

//incoming statics
sysZone; lvSysZone; fpSysFont; sysFont; dsp
]

static [ zone; hT; statFile; dis; ctxQ ]

manifest
[
stkLim = #335
display = #420
]

//----------------------------------------------------------------------------
let InitPupControl() be
//----------------------------------------------------------------------------
[
let freeBegin = @stkLim
let freeEnd = MyFrame()-1000
@stkLim = freeEnd
let freeSpace = freeEnd-freeBegin
test freeSpace ls 0
   ifnot zone = InitializeZone(freeBegin, freeSpace)
   ifso
      [
      zone = InitializeZone(freeEnd-#77777, #77777)
      AddToZone(zone, freeBegin, freeSpace-#77777)
      ]

sysZone, @lvSysZone = zone, zone
let font = OpenFile("SysFont.al", ksTypeReadOnly, 0, 0, fpSysFont)
if font eq 0 then CallSwat("Can't open SysFont.al")
let fontSize = (FileLength(font)+1)/2
Resets(font)
sysFont = Allocate(zone, fontSize)
ReadBlock(font, sysFont, fontSize)
Closes(font)
sysFont = sysFont+2
dsp = CreateDisplayStream(5, Allocate(zone, 1000), 1000, sysFont)
ShowDisplayStream(dsp, DSalone)

Resets(dsp)
Wss(dsp, "*N*N*NPupTest Control of December 26, 1978")

ctxQ = Allocate(zone, 2); ctxQ!0 = 0
hT = Allocate(zone, lenHT); Zero(hT, lenHT)
statFile = Allocate(zone, lenStatFile); Zero(statFile, lenStatFile)
let title = MakeSingleLine()
ShowDisplayStream(title, DSbelow, dsp)
let bar1 = MakeBar(zone, 1)
ShowDisplayStream(bar1, DSbelow, dsp)
Ows(title, colNetHost, "Host", true)
Ows(title, colPartner, "Partner", true)
Ows(title, colState, "State", true)
Ows(title, colChksm, "Chksm", true)
Ows(title, colData, "Data", true)
Ows(title, colOvlp, "Ovlp", true)
Ows(title, colThruput, "Byte/sec", true)
Ows(title, colAThruput, "Ave Byte/sec", true)
let bar0 = MakeBar(zone, 0)
ShowDisplayStream(bar0, DSbelow, title)
let bar3 = MakeBar(zone, 0)
ShowDisplayStream(bar3, DSbelow, title)
let bar2 = MakeBar(zone, 1)
ShowDisplayStream(bar2, DSbelow, title)

InitPupLevel1(zone, ctxQ, 12)

let lastStreamShown = bar3
for i = 1 to maxHosts do
   [
   let line = MakeSingleLine()
   ShowDisplayStream(line, DSbelow, lastStreamShown)
   lastStreamShown = line
   hT>>HT.ds↑i = line
   hT>>HT.yMin↑i = GetYPos(line>>DS.cdcb)
   hT>>HT.yMax↑i = hT>>HT.yMin↑i + @(GetFont(dsp)-2)
   let ctx = InitializeContext(Allocate(zone, 250), 250, Host, 1)
   ctx!3 = i
   Enqueue(ctxQ, ctx)
   ]

let bar4 = MakeBar(zone, 0)
ShowDisplayStream(bar4, DSbelow, lastStreamShown)
let bar5 = MakeBar(zone, 1)
ShowDisplayStream(bar5, DSbelow, bar4)
dis = CreateDisplayStream(6, Allocate(zone, 3200),
 3200, sysFont, 38, 0, zone)
ShowDisplayStream(dis, DSbelow, bar5)
let bar6 = MakeBar(zone, 1)
ShowDisplayStream(bar6, DSbelow, dis)

Enqueue(ctxQ,InitializeContext(Allocate(zone, 100), 100, MouseWatcher))
Enqueue(ctxQ,InitializeContext(Allocate(zone, 1100), 1100, Command))
Enqueue(ctxQ,InitializeContext(Allocate(zone, 1500), 1500, MiscServ))
]

//----------------------------------------------------------------------------
and MakeSingleLine() = valof
//----------------------------------------------------------------------------
//manufacture a single line display stream
[
let sizeLine = @(GetFont(dsp)-2)*38+lDCB+10
let Line = Allocate(zone, sizeLine)
resultis CreateDisplayStream(1, Line, sizeLine, sysFont, 38, DSnone, zone)
]

//----------------------------------------------------------------------------
and MakeBar(zone,background) = valof
//----------------------------------------------------------------------------
//manufacture a horizontal bar
[
structure Bar: [ fdcb word; ldcb word; @DCB ]
manifest lenBar = size Bar/16
let bar = Allocate(zone, lenBar, false, true); Zero(bar, lenBar)
bar>>Bar.fdcb = lv bar>>Bar.next
bar>>Bar.ldcb = lv bar>>Bar.next
bar>>Bar.background = background
bar>>Bar.width = 0
bar>>Bar.height = 1
resultis bar
]

//----------------------------------------------------------------------------
and GetYPos(cb) = valof
//----------------------------------------------------------------------------
//return the Y position of the bottom scan line of cb
[
let dcb = @display
let linecounter = 0
while dcb>>DCB.next ne cb do
   [
   linecounter = linecounter + dcb>>DCB.height lshift 1
   dcb = dcb>>DCB.next
   ]
resultis linecounter + dcb>>DCB.height lshift 1
]

//----------------------------------------------------------------------------
and InitAltoSLA(zone, ctxQ) be [ ]
//----------------------------------------------------------------------------