// GateControlInit.bcpl
// Last modified July 2, 1983  8:53 PM by Boggs

get "GateConMenu.decl"

external
[
// outgoing procedures
InitGateControl

// incoming procedures
MyFrame; Zero; Wss
InitializeZone; AddToZone; Allocate; SysErr
InitPupLevel1; Enqueue; Dequeue
InitializeContext; InitContextSched
InitMenuPackage; CreateMenu; CreateBox; CreateBoxStream
DumbBox; StatusCtx; OtherPupCtx
CreateKeyboardStream; BFSInit

// outgoing statics
sysZone; sysDisk; ctxQ; oldStatsQ
dsp; errorDsp; titleDsp; cmdMenu

// incoming statics
gacha10; otherPupQ; mpd; versionText
]

static
[
sysZone; sysDisk; ctxQ; oldStatsQ
dsp; errorDsp; titleDsp; cmdMenu

statHeight = 180
ttyHeight = 300
]

manifest
[
stackLim = 335b
dspWidth = 34
]

//---------------------------------------------------------------------------
let InitGateControl() be
//---------------------------------------------------------------------------
[
CreateKeyboardStream()
// create a free storage zone
let freeEnd = MyFrame() -1500
let freeBegin = @stackLim
@stackLim = freeEnd
test freeEnd - freeBegin ls 0
   ifso
      [
      sysZone = InitializeZone(freeEnd-77777b, 77777b, SysErr, 0)
      freeEnd = freeEnd - 77777b
      if freeEnd - freeBegin gr 100 then
         AddToZone(sysZone, freeBegin, freeEnd-freeBegin)
      ]
   ifnot sysZone = InitializeZone(freeBegin, freeEnd-freeBegin, SysErr, 0)

sysDisk = BFSInit(sysZone, true, 0)
//freeEnd = MyFrame() -200
//AddToZone(sysZone, freeEnd, 1300)

oldStatsQ = Allocate(sysZone, 2); oldStatsQ!0 = 0
ctxQ = Allocate(sysZone, 2); ctxQ!0 = 0
InitContextSched()
InitPupLevel1(sysZone, ctxQ, 10)
InitMenuPackage(ctxQ, gacha10, sysZone, 1500)

CreateDCB(32)

// Title window
titleDsp = CreateBoxStream(CreateBox(CreateMenu(CreateDCB(16, dspWidth)),
 0, 0, 544, 16, DumbBox, menuBoxBorder), false, true)
Wss(titleDsp, versionText)

CreateDCB(16)

// Gateway Status window
let ctx = InitializeContext(Allocate(sysZone, 300), 300, StatusCtx, 1)
ctx!3 = CreateBoxStream(CreateBox(CreateMenu(CreateDCB(statHeight, dspWidth)),
 0, 0, 544, statHeight, DumbBox, menuBoxBorder))
Enqueue(ctxQ, ctx)

CreateDCB(16)

// Menu window
cmdMenu = CreateMenu(CreateDCB(52, dspWidth))
CreateBox(cmdMenu, 0, 0, 544, 52, DumbBox, menuBoxBorder)

CreateDCB(16)

// TTY simulation window
dsp = CreateBoxStream(CreateBox(CreateMenu(CreateDCB(ttyHeight, dspWidth)),
 0, 0, 544, ttyHeight, DumbBox, menuBoxBorder))

CreateDCB(16)

// Error window
errorDsp = CreateBoxStream(CreateBox(CreateMenu(CreateDCB(16, dspWidth)),
 0, 0, 544, 16, DumbBox, menuBoxBorder), false, true)
otherPupQ = Allocate(sysZone, 2); otherPupQ!0 = 0
Enqueue(ctxQ, InitializeContext(Allocate(sysZone, 300), 300, OtherPupCtx))

while mpd>>MPD.menuQ.head ne 0 do Dequeue(lv mpd>>MPD.menuQ)
Enqueue(lv mpd>>MPD.menuQ, cmdMenu)
]

//---------------------------------------------------------------------------
and CreateDCB(height, width; numargs na) = valof
//---------------------------------------------------------------------------
[
let dcb = Allocate(sysZone, lDCB, false, true); Zero(dcb, lDCB)
if na gr 1 & width ne 0 then
   [
   dcb>>DCB.width = width
   dcb>>DCB.bitmap = Allocate(sysZone, height*width, false, true)
   Zero(dcb>>DCB.bitmap, height*width)
   dcb>>DCB.indentation = 2
   ]
dcb>>DCB.height = height/2
let lvDCB = displayListHead
while @lvDCB ne 0 do lvDCB = @lvDCB
@lvDCB = dcb
resultis dcb
]