SlackProcessImpl.mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Last edited by Pier on October 17, 1986 6:04:57 pm PDT
DIRECTORY
Atom, CedarProcess, IO, MessageWindow, Process, Rope, SlackProcess, TIPUser, Terminal, Interminal, ViewerClasses, ViewerOps;
SlackProcessImpl: CEDAR MONITOR LOCKS handle USING handle: SlackHandle
IMPORTS Atom, CedarProcess, IO, MessageWindow, Process, Terminal, Interminal, ViewerOps
EXPORTS SlackProcess = BEGIN
SlackProcess implements a general queue which can be used to "take up the slack" between user generated input events and system generated responses to these events. Multiple slack processes and their associated queues may be used by many clients; the slack process issues a handle containing MONITORED data for use by the client. Actions may be queued by calls to QueueInputAction or QueueInputActionNoPoint. If the client wishes, calls to QueueOrBashInputAction, QueueOrBashInputActionNoPoint will replace the prior action on the queue with the new action IF the prior action is of the same type as the new action. The type of an action is denoted by an ATOM as the first element of the input action list.
Each SlackProcess provides a logging feature for clients. A client can register a Logging procedure and enable/disable Session logging. With logging enabled, the registered Logging procedure will be called every time an action is dequeued. The client may then save a log or script of the session in progress for later playback to a future SlackProcess.
Point: TYPE = SlackProcess.Point; -- RECORD [x, y: REAL]
SlackHandle: TYPE = REF SlackData;
SlackData: PUBLIC TYPE = SlackProcess.SlackData; -- this is the monitored record type
EventProc: TYPE = SlackProcess.EventProc;
MouseEventProc: TYPE = SlackProcess.MouseEventProc;
LoggingProc: TYPE = SlackProcess.LoggingProc;
AbortProc: TYPE = SlackProcess.AbortProc;
AbortData: TYPE = SlackProcess.AbortData;
Log: TYPE = SlackProcess.Log;
LogData: TYPE = SlackProcess.LogData;
Queue: TYPE = SlackProcess.Queue;
QueueData: TYPE = SlackProcess.QueueData;
ClientDatas: TYPE = SlackProcess.ClientDatas;
ClientDatasData: TYPE = SlackProcess.ClientDatasData;
Actions: TYPE = SlackProcess.Actions;
ActionsData: TYPE = SlackProcess.ActionsData;
Points: TYPE = SlackProcess.Points;
PointsData: TYPE = SlackProcess.PointsData;
MouseProcs: TYPE = SlackProcess.MouseProcs;
MouseProcsData: TYPE = SlackProcess.MouseProcsData;
EventProcs: TYPE = SlackProcess.EventProcs;
EventProcsData: TYPE = SlackProcess.EventProcsData;
ReceivedData: TYPE = SlackProcess.ReceivedData;
BashedData: TYPE = SlackProcess.BashedData;
EventsData: TYPE = SlackProcess.EventsData;
Viewer: TYPE = ViewerClasses.Viewer;
Create: PUBLIC PROC [queueSize: NAT ← 50, logSize: NAT ← 50, loggingProc: LoggingProc ← NIL, abortProc: AbortProc ← NIL, abortData: REF ANYNIL, abortViewer: ViewerClasses.Viewer ← NIL] RETURNS[handle: SlackHandle] = {
handle ← NEW[SlackData ← [slackProcess: NIL, queue: NEW[QueueData ← [] ], log: NEW[LogData ← [] ], abort: NEW[AbortData ← [] ] ] ];
handle.queue.clientDatas ← NEW[ClientDatasData[queueSize]];
handle.queue.actions ← NEW[ActionsData[queueSize]];
handle.queue.points ← NEW[PointsData[queueSize]];
handle.queue.mouseProcs ← NEW[MouseProcsData[queueSize]];
handle.queue.eventProcs ← NEW[EventProcsData[queueSize]];
handle.queue.size ← queueSize;
handle.log.received ← NEW[ReceivedData[logSize]];
handle.log.bashed ← NEW[BashedData[logSize]];
handle.log.events ← NEW[EventsData[logSize]];
handle.log.points ← NEW[PointsData[logSize]];
handle.log.size ← logSize;
handle.log.logger ← loggingProc;
handle.abort^ ← [enabled: FALSE, viewer: abortViewer, proc: abortProc, data: abortData];
};
FlushQueue: PUBLIC ENTRY PROC [handle: SlackHandle] = {
FlushQueueInternal[handle];
};
FlushQueueInternal: INTERNAL PROC [handle: SlackHandle] = {
Removes all unprocessed actions from the slack queue. CAUTION: If the caller is counting on a certain set of actions to be done atomically, this call could destroy its invariants.
queueSize: NAT ← handle.queue.size;
FOR i: NAT IN [0..queueSize-1] DO
handle.queue.clientDatas[i] ← NIL;
handle.queue.actions[i] ← NIL;
handle.queue.points[i] ← [0,0];
handle.queue.mouseProcs[i] ← NIL;
handle.queue.eventProcs[i] ← NIL;
ENDLOOP;
handle.queue.head ← 0;
handle.queue.tail ← 0;
BROADCAST handle.queue.notFull;
};
Restart: PUBLIC ENTRY PROC [handle: SlackHandle] = TRUSTED {
Sometimes the Painter process is aborted because of a bug in one of the dispatched procedures. This procedure allows the user to get things going again.
Process.Detach[handle.slackProcess ← FORK Actor[handle] ];
};
QueueInputAction: PUBLIC ENTRY PROC [handle: SlackHandle, callBack: MouseEventProc, inputAction: LIST OF REF ANY, point: Point, clientData: REF ANY] = {
Add an action to the slack process queue.
InternalQueueInputAction[handle, callBack, inputAction, point, clientData];
};
InternalQueueInputAction: INTERNAL PROC [handle: SlackHandle, callBack: MouseEventProc, inputAction: LIST OF REF ANY, point: Point, clientData: REF ANY] = TRUSTED {
Add an action to the tail of the slack process queue. This action will definitely be performed, in its turn. Process.Detach is an unsafe operation.
queue: Queue ← handle.queue;
qIndex: NAT ← 0;
WHILE (queue.tail+1) MOD queue.size = queue.head DO WAIT queue.notFull ENDLOOP;
qIndex ← queue.tail;
queue.clientDatas[qIndex] ← clientData;
queue.actions[qIndex] ← inputAction;
queue.points[qIndex] ← point;
queue.mouseProcs[qIndex] ← callBack;
queue.eventProcs[qIndex] ← NIL;
queue.tail ← (qIndex + 1) MOD queue.size;
LogReceived[log: handle.log, event: inputAction.first, point: point, bash: FALSE];
IF handle.slackProcess = NIL THEN Process.Detach[handle.slackProcess ← FORK Actor[handle]];
};
IsBashable: INTERNAL PROC [atom: ATOM, bashable: LIST OF ATOM] RETURNS [BOOL] = {
FOR list: LIST OF ATOM ← bashable, list.rest UNTIL list = NIL DO
IF atom = list.first THEN RETURN[TRUE];
REPEAT
FINISHED => RETURN[FALSE];
ENDLOOP;
};
QueueOrBashInputAction: PUBLIC ENTRY PROC [handle: SlackHandle, callBack: MouseEventProc, inputAction: LIST OF REF ANY, bashable: LIST OF ATOM, point: Point, clientData: REF ANY] = TRUSTED {
Add an action to the slack process queue. If the previous action is of the same sort, then overwrite it instead of adding a new one (useful for dragging).
queue: Queue ← handle.queue;
prevAction: ATOM ← PeekActionTail[queue];
qIndex: NAT ← (queue.tail - 1 + queue.size) MOD queue.size;
IF IsBashable[prevAction, bashable] THEN { -- bash
queue.clientDatas[qIndex] ← clientData;
queue.actions[qIndex] ← inputAction;
queue.points[qIndex] ← point;
queue.mouseProcs[qIndex] ← callBack;
queue.eventProcs[qIndex] ← NIL;
LogReceived[log: handle.log, event: inputAction.first, point: point, bash: TRUE];
IF handle.slackProcess = NIL THEN Process.Detach[handle.slackProcess ← FORK Actor[handle]];
}
ELSE InternalQueueInputAction[handle, callBack, inputAction, point, clientData]; -- queue.
};
QueueInputActionNoPoint: PUBLIC ENTRY PROC [handle: SlackHandle, callBack: EventProc, inputAction: LIST OF REF ANY, clientData: REF ANY] = TRUSTED {
Add an action to the slack processes queue. This action will definitely be performed, in its turn.
InternalQueueInputActionNoPoint[handle, callBack, inputAction, clientData];
};
InternalQueueInputActionNoPoint: INTERNAL PROC [handle: SlackHandle, callBack: EventProc, inputAction: LIST OF REF ANY, clientData: REF ANY] = TRUSTED {
Add an action to the tail of the slack process queue. This action will definitely be performed, in its turn.
queue: Queue ← handle.queue;
qIndex: NAT ← 0;
WHILE (queue.tail+1) MOD queue.size = queue.head DO WAIT queue.notFull ENDLOOP;
qIndex ← queue.tail;
queue.clientDatas[qIndex] ← clientData;
queue.actions[qIndex] ← inputAction;
queue.points[qIndex] ← [-9999.0, -9999.0];
queue.mouseProcs[qIndex] ← NIL;
queue.eventProcs[qIndex] ← callBack;
queue.tail ← (qIndex + 1) MOD queue.size;
LogReceived[log: handle.log, event: inputAction.first, point: [-9999.0, -9999.0], bash: FALSE];
IF handle.slackProcess = NIL THEN Process.Detach[handle.slackProcess ← FORK Actor[handle]];
};
QueueOrBashInputActionNoPoint: PUBLIC ENTRY PROC [handle: SlackHandle, callBack: EventProc, inputAction: LIST OF REF ANY, bashable: LIST OF ATOM, clientData: REF ANY] = TRUSTED {
Add an action to the slack processes queue. If the previous action is of the same sort, then overwrite it instead of adding a new one (useful for testing Gravity).
queue: Queue ← handle.queue;
prevAction: ATOM ← PeekActionTail[queue];
qIndex: NAT ← (queue.tail - 1 + queue.size) MOD queue.size;
IF IsBashable[prevAction, bashable] THEN { -- bash
queue.clientDatas[qIndex] ← clientData;
queue.actions[qIndex] ← inputAction;
queue.points[qIndex] ← [-9999.0, -9999.0];
queue.mouseProcs[qIndex] ← NIL;
queue.eventProcs[qIndex] ← callBack;
LogReceived[log: handle.log, event: inputAction.first, point: [-9999.0, -9999.0], bash: TRUE];
IF handle.slackProcess = NIL THEN Process.Detach[handle.slackProcess ← FORK Actor[handle]];
}
ELSE InternalQueueInputActionNoPoint[handle, callBack, inputAction, clientData]; -- queue
};
EmptyQ: INTERNAL PROC [queue: Queue] RETURNS [BOOL] = {
RETURN[queue.tail = queue.head];
};
PeekActionTail: PRIVATE INTERNAL PROC [queue: Queue] RETURNS [whatToDo: ATOM] = {
IF queue.head = queue.tail THEN RETURN[NIL];
whatToDo ← NARROW[queue.actions[(queue.tail - 1 + queue.size) MOD queue.size].first, ATOM];
};
Actor: PROC [handle: SlackHandle] = { -- runs in autonomous process(es) to service queue(s)
May FORK a process that looks to see if SHIFT-SWAT is down and aborts Actor if it is.
IF handle.abort.enabled THEN [] ← CedarProcess.Fork[Killer, handle, [excited, TRUE]];
DoActorsJob[handle ! UNWIND => NilIt[handle]];
NilIt[handle];
};
NilIt: PRIVATE ENTRY PROC [handle: SlackHandle] = TRUSTED {handle.slackProcess ← NIL};
Killer: PROC [data: REF] RETURNS [results: REFNIL] = {
handle: SlackHandle ← NARROW[data];
UNTIL handle.slackProcess = NIL DO
IF MaybeKill[] THEN handle.abort.proc[handle.abortData]; -- WHERE TO DO THIS ??
[] ← MaybeKill[handle];
Process.Pause[ticks: Process.SecondsToTicks[1]];
ENDLOOP;
};
MaybeKill: PRIVATE ENTRY PROC [handle: SlackHandle] RETURNS [killed: BOOLFALSE]= {
ClientWantsKill: PROC RETURNS [killIt: BOOLFALSE] = {
mouse: Interminal.MousePosition;
tsc: TIPUser.TIPScreenCoords ← NEW[TIPUser.TIPScreenCoordsRec];
vt: Terminal.Virtual ← Terminal.Current[];
keyBits: Terminal.KeyBits ← Terminal.GetKeys[vt: vt];
killBits: BOOL ← (keyBits[LeftShift]=down OR keyBits[RightShift]=down) AND keyBits[Spare3]=down;
IF killBits THEN {
IF handle.abort.viewer=NIL THEN RETURN[TRUE]; -- viewer does not matter
mouse ← Interminal.GetMousePosition[];
tsc^ ← [
mouseX: mouse.mouseX,
mouseY: (IF mouse.color THEN vt.colorHeight ELSE vt.bwHeight) - mouse.mouseY,
color: mouse.color
];
RETURN[ViewerOps.MouseInViewer[tsc: tsc].viewer=handle.abort.viewer];
};
};
IF handle.slackProcess#NIL AND ClientWantsKill[] THEN TRUSTED {
Process.Abort[handle.slackProcess]; -- kill the actor slack process
handle.slackProcess ← NIL;
FlushQueueInternal[handle]; -- you have to do this HERE holding the handle lock!
IF handle.abort.proc#NIL THEN handle.abort.proc[handle.abort.data]; -- notify the user that abort happened. Hold the lock.
RETURN[TRUE];
};
};
DoActorsJob: PROC [handle: SlackHandle] = { -- autonomous process(es) to service queue(s)
inputAction: LIST OF REF ANY;
point: Point;
clientData: REF ANY;
mouseEventProc: MouseEventProc;
eventProc: EventProc;
[mouseEventProc, eventProc, inputAction, point, clientData] ← NextAction[handle];
WHILE inputAction # NIL DO
IF mouseEventProc = NIL THEN eventProc[inputAction, clientData]
ELSE mouseEventProc[inputAction, point, clientData];
[mouseEventProc, eventProc, inputAction, point, clientData] ← NextAction[handle];
ENDLOOP;
};
NextAction: ENTRY PROC [handle: SlackHandle] RETURNS [mouseEventProc: MouseEventProc, eventProc: EventProc, inputAction: LIST OF REF ANY, point: Point, clientData: REF ANY] = {
queue: Queue ← handle.queue;
IF EmptyQ[queue] THEN {
handle.slackProcess ← NIL; -- no mouse action. Let process die. Caller will terminate.
inputAction ← NIL;
}
ELSE {
[mouseEventProc, eventProc, inputAction, point, clientData] ← DeQueueAction[handle];
BROADCAST queue.notFull;
};
};
DeQueueAction: INTERNAL PROC [handle: SlackHandle] RETURNS [mouseEventProc: MouseEventProc, eventProc: EventProc, inputAction: LIST OF REF ANY, point: Point, clientData: REF ANY] = {
queue: Queue ← handle.queue;
where: NAT ← queue.head;
IF queue.tail = queue.head THEN ERROR;
mouseEventProc ← queue.mouseProcs[where];
eventProc ← queue.eventProcs[where];
inputAction ← queue.actions[where];
point ← queue.points[where];
clientData ← queue.clientDatas[where];
queue.head ← (queue.head + 1) MOD queue.size;
LogActed[handle.log, inputAction, point, mouseEventProc#NIL, clientData];
};
SlackProcess keeps a ring buffer of recent input actions (called the log) for debugging purposes.
LogRawMouse: PUBLIC ENTRY PROC [handle: SlackHandle, point: Point] = {
log: Log ← handle.log;
where: NAT ← log.head;
log.events[where] ← $RawMouse;
log.points[where] ← point;
log.head ← (log.head + 1) MOD log.size;
};
LogReceived: INTERNAL PROC [log: Log, event: REF ANY, point: Point, bash: BOOL] = {
where: NAT ← log.head;
log.received[where] ← TRUE;
log.bashed[where] ← bash;
log.events[where] ← event;
log.points[where] ← point;
log.head ← (log.head + 1) MOD log.size;
};
LogActed: INTERNAL PROC [log: Log, action: LIST OF REF ANY, point: Point, mouseEvent: BOOL, clientData: REF ANY] = {
where: NAT ← log.head;
log.received[where] ← FALSE;
log.bashed[where] ← FALSE;
log.events[where] ← action.first;
log.points[where] ← point;
log.head ← (log.head + 1) MOD log.size;
IF log.loggerEnabled THEN log.logger[point, action, mouseEvent, clientData];
};
debugLog: BOOLFALSE;
OutputLog: PUBLIC ENTRY PROC [handle: SlackHandle, stream: IO.STREAM] = {
log: Log ← handle.log;
i: NAT ← (log.head - 1 + log.size) MOD log.size; -- back up pointer one slot
Output the "LogSize-1" most recent events to the stream. The most recent event is output first.
IF debugLog THEN stream.PutF["\nLog Head = %g", IO.int[log.head] ];
stream.PutF["\nMost Recent Event: "];
WHILE i#log.head DO
IF log.events[i] = $RawMouse THEN
stream.PutF["\nRAW MOUSE [%g, %g]",
[real[log.points[i].x]],
[real[log.points[i].y]] ]
ELSE IF log.bashed[i] THEN
IF ISTYPE[log.events[i], ATOM] THEN
stream.PutF["\nBashed %g at [%g, %g]",
[rope[Atom.GetPName[NARROW[log.events[i]]]]],
[real[log.points[i].x]],
[real[log.points[i].y]] ]
ELSE stream.PutF["\nBashed %g at [%g, %g]",
[character[NARROW[log.events[i], REF CHAR]^]],
[real[log.points[i].x]],
[real[log.points[i].y]] ]
ELSE IF log.received[i] THEN
IF ISTYPE[log.events[i], ATOM] THEN
stream.PutF["\nReceived %g at [%g, %g]",
[rope[Atom.GetPName[NARROW[log.events[i]]]]],
[real[log.points[i].x]],
[real[log.points[i].y]] ]
ELSE stream.PutF["\nReceived %g at [%g, %g]",
[character[NARROW[log.events[i], REF CHAR]^]],
[real[log.points[i].x]],
[real[log.points[i].y]] ]
ELSE
IF ISTYPE[log.events[i], ATOM] THEN
stream.PutF["\nActed on %g at [%g, %g]",
[rope[Atom.GetPName[NARROW[log.events[i]]]]],
[real[log.points[i].x]],
[real[log.points[i].y]] ]
ELSE stream.PutF["\nActed on %g at [%g, %g]",
[character[NARROW[log.events[i], REF CHAR]^]],
[real[log.points[i].x]],
[real[log.points[i].y]] ];
i ← (i - 1 + log.size) MOD log.size;
ENDLOOP;
stream.PutF["\nLeast Recent Event.\n"];
};
SlackProcess supports reporting of all actions dequeued from the slack queue, which can be recorded and played back by the client to recreate the session. The client must provide a logging procedure to be called whenever an action is dequeued for the application. The registered LoggingProc will be called at every dequeue of an event.
EnableSessionLogging: PUBLIC ENTRY PROC [handle: SlackHandle] = {
IF handle.log.loggerEnabled THEN MessageWindow.Append["Warning: EnableSessionLogging called while already Enabled", TRUE]
ELSE handle.log.loggerEnabled ← TRUE;
};
DisableSessionLogging: PUBLIC ENTRY PROC [handle: SlackHandle] = {
IF NOT handle.log.loggerEnabled THEN MessageWindow.Append["Warning: DisableSessionLogging called while already Disabled", TRUE]
ELSE handle.log.loggerEnabled ← FALSE;
};
RegisterLogger: PUBLIC ENTRY PROC [handle: SlackHandle, loggingProc: LoggingProc] = {
IF handle.log.logger#NIL THEN MessageWindow.Append["Warning: RegisterLogger called while already Registered", TRUE]
ELSE handle.log.logger ← loggingProc;
};
EnableAborts: PUBLIC ENTRY PROC [handle: SlackHandle] = {
IF handle.abort.enabled THEN MessageWindow.Append["Warning: EnableAborts called while already Enabled", TRUE]
ELSE handle.abort.enabled ← TRUE;
};
DisableAborts: PUBLIC ENTRY PROC [handle: SlackHandle] = {
IF NOT handle.abort.enabled THEN MessageWindow.Append["Warning: DisableAborts called while already Disabled", TRUE]
ELSE handle.abort.enabled ← FALSE;
};
RegisterAbortProc: PUBLIC ENTRY PROC [handle: SlackHandle, abortProc: AbortProc ← NIL, abortData: REF ANYNIL, abortViewer: ViewerClasses.Viewer ← NIL] = {
The registered abortProc will be called (if aborts are enabled) with abortData as argument when the slack process is aborted.
IF handle.abort.proc#NIL THEN MessageWindow.Append["Warning: RegisterAbortProc called while already Registered", TRUE]
ELSE {
handle.abort.proc ← abortProc;
handle.abort.data ← abortData;
handle.abort.viewer ← abortViewer;
};
};
END.