%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  PDSS (PIMOS Development Support System)  Version 2.52		 %
%  (C) Copyright 1988,1989,1990,1992.					 %
%  Institute for New Generation Computer Technology (ICOT), Japan.	 %
%  Read "../COPYRIGHT" for detailed information.			 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%  Micro-PIMOS Window Device Driver.

:- module mpimos_window_device_driver.
:- public create/7, device_create/6.
:- with_macro pdss.

%%  Create a window.
%%  create(LF,RI,RQ,TF,TC,CS,WN).
%%	LF :: Loop stream IN.
%%	RI :: Loop stream OUT.
%%	RQ :: Command request stream from moniter.
%%	TF :: Termination flag from task moniter.
%%	CS :: Control stream to window.
%%	WN :: Window name.
%%	TC :: Task control stream to task moniter.

create(LF,RI,RQ,TF,CS,WN,TC):-
    true |
	CS=reset(AB,AT,_,CSL),
	merge(RQ,RQ2),
	window(LF,RI,RQ2,TF,TC,CSL,_,AB,AT,status(WN,normal)).

device_create(LF,RI,RQ,TF,CS,WN):-
    true |
	window(LF,RI,RQ,TF,_,CS,_,_,_,status(WN,device)).


%%  Window process.
%%  window(LF,RI,RQ,TF,TC,CS,AF,AB,AT,ST).
%%	LF :: Loop stream IN.
%%	RI :: Loop stream OUT.
%%	RQ :: Command request stream from moniter.
%%	TF :: Termination flag from task moniter.
%%	TC :: Task control stream to task moniter.
%%	CS :: Control stream to window.
%%	AF :: Abort flag from user.
%%	AB :: Abort flag.
%%	AT :: Attention flag.
%%	ST :: Status. (vector..status(Name,Type)).

%%  From Manager.
window([switch(Name,F)|LF],RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    vector_element(ST,1,Name) |
	CS=show(F,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window([switch(Name,F)|LF],RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    vector_element(ST,1,NM),NM\=Name |
	RI=[switch(Name,F)|RIL],
	window(LF,RIL,RQ,TF,TC,CS,AF,AB,AT,ST).
window([switch(F)|LF],RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=show(F,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window([switch_next(F)|LF],RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    true |
	RI=[switch(F)|RIL],
	window(LF,RIL,RQ,TF,TC,CS,AF,AB,AT,ST).
window([count(N)|LF],RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    true |
	NN:=N+1,
	RI=[count(NN)|RIL],
	window(LF,RIL,RQ,TF,TC,CS,AF,AB,AT,ST).
window([contents(H)|LF],RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    true |
	vector_element(ST,1,Name,ST2),
	H=[Name|L],
	RI=[contents(L)|RIL],
	window(LF,RIL,RQ,TF,TC,CS,AF,AB,AT,ST2).

%%  Interrupt.
window(LF,RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    wait(AT), vector_element(ST,2,normal) |
	AB=fep#abort,
	CS=reset(AB2,AT2,_,CSL),
	TC=[AT|TCL],
	window(LF,RI,RQ,TF,TCL,CSL,AF,AB2,AT2,ST).
window(LF,RI,RQ,TF,TC,CS,AF,AB,AT,ST):-
    wait(AT), vector_element(ST,2,device) |
	TC=AT,
	window(LF,RI,RQ,TF,_,CS,AF,AB,_,ST).

%%  Abort command.
window(LF,RI,RQ,TF,TC,CS,fep#abort,AB,AT,ST):-
    true |
	AB=fep#abort,
	window(LF,RI,RQ,TF,_,CS,_,_,_,ST).

alternatively.
%%  Termination.
window(LF,RI,RQ,die,TC,CS,AF,AB,AT,ST):-
    true |
	LF=RI, TC=[], CS=[], AB=fep#abort.

alternatively.
%%  From User.
window(LF,RI,[],TF,TC,CS,AF,AB,AT,ST):-
    true |
	LF=RI, TC=[], CS=[].
window(LF,RI,[close(S)|_],TF,TC,CS,AF,AB,AT,ST):-
    true |
	LF=RI, TC=[], CS=close(S).
window(LF,RI,[reset(AF,Int,S)|RQ],TF,_,CS,_,_,_,ST):-
    true |
	CS=reset(AB,AT,S,CSL),
	window(LF,RI,RQ,TF,Int,CSL,AF,AB,AT,ST).
window(LF,RI,[next_attention(Int,S)|RQ],TF,_,CS,AF,AB,_,ST):-
    true |
	CS=next_attention(AT,S,CSL),
	window(LF,RI,RQ,TF,Int,CSL,AF,AB,AT,ST).
window(LF,RI,[getc(Char,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=getc(Char,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[getl(Line,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=getl(Line,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[getb(Buf,Max,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=getb(Max,Buf,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[gettkn(Token,Stt,NV)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=gettkn(Token,NV,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[putc(Char,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=putc(Char,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[putl(Line,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=putl(Line,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[putb(Buf,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=putb(Buf,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[putt(Term,Len,Dep,Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=putt(Term,Len,Dep,Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[show(Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=show(Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[hide(Stt)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	CS=hide(Stt,CSL),
	window(LF,RI,RQ,TF,TC,CSL,AF,AB,AT,ST).
window(LF,RI,[do(L)|RQ],TF,TC,CS,AF,AB,AT,ST):-
    true |
	append(L,RQ,RQ2),
	window(LF,RI,RQ2,TF,TC,CS,AF,AB,AT,ST).

append([X|X1],Y,Z) :- true | Z = [X|Z1], append(X1,Y,Z1).
append([],    Y,Z) :- true | Z = Y.
