-- KeysetTest.mesa
--  edited by Sweet, 22-Apr-83 12:52:07

DIRECTORY
  Atom,
  DLionInputOutput USING [IOPage, processorCommandOffset],
  Display USING [Black, White],
  FormSW USING [
    AllocateItemDescriptor, ClientItemsProcType, CommandItem, newLine, ProcType],
  Keys USING [DownUp],
  TIP,
  Tool USING [
    AddThisSW, Create, MakeFormSW, MakeSWsProc, RegisterSWType, 
    SimpleAdjustProc, SWType],
  ToolWindow USING [CreateSubwindow],
  UserTerminal USING [BlinkDisplay],
  Window USING [Box, Handle, Place, ValidateTree];
    
KeysetTest: PROGRAM
  IMPORTS 
    Atom, Display, DLionInputOutput, FormSW, TIP, Tool, ToolWindow,
    UserTerminal, Window =
  BEGIN
  
  CreateWindow: PROC =
    BEGIN
    window ← Tool.Create[
      name: "Keyset Test"L, makeSWsProc: MakeSWs];
    END;
  
  window, formSW, paddleSW: Window.Handle;
  
  paddleType: Tool.SWType = Tool.RegisterSWType[];
  
  paddleState: PACKED ARRAY [1..5] OF Keys.DownUp ← ALL[up];
 
  MakeSWs: Tool.MakeSWsProc =
    BEGIN
    formSW ← Tool.MakeFormSW[window: window, formProc: MakeForm];
    paddleSW ← ToolWindow.CreateSubwindow[parent: window, display: DisplayBars];
    Tool.AddThisSW[window: window, sw: paddleSW, swType: paddleType];
    TIP.CreateClient[paddleSW, myTip, TIPMe];
    END;
    
    
  MakeForm: FormSW.ClientItemsProcType =
    BEGIN OPEN FormSW;
    items ← AllocateItemDescriptor[2];
    items[0] ← CommandItem[tag: "Turn on"L, place: newLine, proc: TurnOn];
    items[1] ← CommandItem[tag: "Turn off"L, proc: TurnOff];
    RETURN[items, TRUE]
    END;
    
  ProcessorCommand: TYPE = MACHINE DEPENDENT {
    busy(100000B), turnOnKeyset(100011B), turnOffKeyset(100012B)};
    
  pProcessorCommand: LONG POINTER TO ProcessorCommand = 
    DLionInputOutput.IOPage + DLionInputOutput.processorCommandOffset;
    
  TurnOn: FormSW.ProcType = 
    BEGIN
    WHILE pProcessorCommand↑ >= busy DO NULL ENDLOOP; -- wait for IOP
    pProcessorCommand↑ ← turnOnKeyset;
    END;
   
  TurnOff: FormSW.ProcType = 
    BEGIN
    WHILE pProcessorCommand↑ >= busy DO NULL ENDLOOP; -- wait for IOP
    pProcessorCommand↑ ← turnOffKeyset;
    END;
   
  DisplayBars: PROC [w: Window.Handle ] =
    BEGIN
    -- repaint everything
    Display.Black[paddleSW, [[x: 31, y: 31], [w: 5*32+1, h: 1]]];
    Display.Black[paddleSW, [[x: 31, y: 31+128], [w: 5*32+1, h: 1]]];
    FOR i: CARDINAL IN [1..6] DO
      Display.Black[paddleSW, [[x: 32*i-1, y: 31], [w: 1, h: 128]]];
      ENDLOOP;
    FOR i: CARDINAL IN [1..5] DO PaintPaddle[i, paddleState[i]]; ENDLOOP;
    END;
    
  PaintPaddle: PROC [n: CARDINAL, state: Keys.DownUp] =
    BEGIN
    proc: PROC [Window.Handle, Window.Box] = 
      IF state = down THEN Display.Black
      ELSE Display.White;
    proc [paddleSW, [[x: 32*n, y: 32], [w: 31, h: 126]]];
    END;
    
  TIPMe: TIP.NotifyProc = 
    BEGIN
    which: CARDINAL ← 0;
    FOR input: TIP.Results ← results, input.Rest[] UNTIL input = NIL DO
      WITH z: input.First[] SELECT FROM
	atom => SELECT z.a FROM
	  down => IF which IN [1..5] THEN {
	    PaintPaddle[which, down]; paddleState[which] ← down};
	  up => IF which IN [1..5] THEN {
	    PaintPaddle[which, up]; paddleState[which] ← up};
	  ENDCASE;
	int => which ← CARDINAL[z.i];
	ENDCASE;
      ENDLOOP;
    Window.ValidateTree[paddleSW];
    END;
    
  myTip: TIP.Table ← NIL;
  
  down, up: Atom.ATOM;

  InitTip: PROCEDURE = {
    tipContents: STRING ←
"-- Keyset.TIP; last edit by
-- Sweet	22-Apr-83 11:54:46
      

SELECT TRIGGER FROM

  Keyset1 Down => 1, Down;
  Keyset2 Down => 2, Down;
  Keyset3 Down => 3, Down;
  Keyset4 Down => 4, Down;
  Keyset5 Down => 5, Down;
  
  Keyset1 Up => 1, Up;
  Keyset2 Up => 2, Up;
  Keyset3 Up => 3, Up;
  Keyset4 Up => 4, Up;
  Keyset5 Up => 5, Up;

  ENDCASE...
"L;
    firstTime: BOOLEAN ← TRUE;
    down ← Atom.MakeAtom["Down"L];
    up ← Atom.MakeAtom["Up"L];
    myTip ← TIP.CreateTable[
      file: "Keyset.TIP"L, contents: tipContents !
      TIP.InvalidTable => IF type = badSyntax THEN {
        UserTerminal.BlinkDisplay[];
	IF firstTime THEN {firstTime ← FALSE; RESUME}}];
    TIP.actionToWindow[Keyset1] ← TRUE;
    TIP.actionToWindow[Keyset2] ← TRUE;
    TIP.actionToWindow[Keyset3] ← TRUE;
    TIP.actionToWindow[Keyset4] ← TRUE;
    TIP.actionToWindow[Keyset5] ← TRUE};

-- main line code

  InitTip[];


	  
  CreateWindow[];
  END.