forked from MarkHofmann11/WWIVEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WEKBD.PAS
236 lines (206 loc) · 4.65 KB
/
WEKBD.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
UNIT WEKbd;
{$I WEGLOBAL.PAS}
{ -- This is the Low Level Keyboard Module for WWIVEdit 2.3
-- Last Modified : 4/15/92
-- Written By:
-- Adam Caldwell
--
-- This Code is Limited Public Domain (see WWIVEDIT.PAS for details)
--
-- Purpose : Provides low level, buffered keyboard input routines
--
-- Know Errors: Will lock up a computer if the user hangs up while
-- seperate_local_input is on. This only occurs during chat mode.
--
-- Planned Enhancements : Implementing fix described in WECHAT.PAS
--
-- }
INTERFACE
PROCEDURE Drain;
FUNCTION ReadKey:char;
FUNCTION KeyPressed:boolean;
FUNCTION Empty:boolean;
PROCEDURE SeperateLocalInput;
PROCEDURE MergeLocalInput;
FUNCTION KeyPressedL:boolean;
FUNCTION ReadKeyL:char;
PROCEDURE StuffIn(s:string);
PROCEDURE Macro(ch:char);
PROCEDURE EnableInts; INLINE($FB);
PROCEDURE DisableInts; INLINE($FA);
IMPLEMENTATION
USES DOS,WEVars,WECom,WEFossil;
CONST
BufferSize = 1024;
TYPE
KeyBuffer = RECORD
head : integer;
tail : integer;
Buffer : ARRAY[0..Buffersize-1] OF char;
END;
CONST
LKeyBuffer : KeyBuffer = (head: 0; tail:0);
RKeyBuffer : KeyBuffer = (head: 0; tail:0);
InstalledInt9: Boolean = FALSE;
VAR
KbdHead : WORD ABSOLUTE $40:$1a;
KbdTail : WORD ABSOLUTE $40:$1c;
KbdMin : WORD ABSOLUTE $40:$80;
KbdMax : WORD ABSOLUTE $40:$82;
OldInt9 : pointer;
PROCEDURE Enque(VAR b:KeyBuffer; ch:char);
BEGIN
WITH B DO BEGIN
buffer[tail]:=ch;
tail:=(tail+1) MOD BufferSize;
END
END;
FUNCTION DeQue(VAR b:KeyBuffer):char;
BEGIN
WITH B DO BEGIN
DeQue:=buffer[head];
head:=(head+1) MOD BufferSize
END
END;
FUNCTION NoMoreKeys : boolean;
VAR
empty:boolean;
r : registers;
BEGIN
r.ah:=$06; r.dl:=$FF;
msdos(r);
Empty:=(r.flags AND FZero) >0;
IF not Empty THEN Enque(RKeyBuffer,chr(r.al));
IF Setting.Comport>0 THEN
BEGIN
Empty:=Empty OR NOT AsyncCharWaiting OR NOT FossilCharWaiting;
IF AsyncCharWaiting THEN Enque(RKeyBuffer,AsyncRecieve);
IF FossilCharWaiting THEN Enque(RKeyBuffer,FossilRecieve);
END;
NoMoreKeys := Empty;
END;
FUNCTION Critical(VAR B:KeyBuffer):boolean;
BEGIN
Critical:=((b.tail+1) mod BufferSize) = b.head;
END;
PROCEDURE Drain;
BEGIN
REPEAT
UNTIL Critical(RKeyBuffer) OR NoMoreKeys;
END;
FUNCTION BufferEmpty(VAR B:KeyBuffer):boolean;
BEGIN
BufferEmpty:=b.head=b.tail
END;
FUNCTION KeyPressedL:boolean;
BEGIN
KeyPressedL:=NOT BufferEmpty(LKeyBuffer)
END;
FUNCTION ReadKeyL:char;
BEGIN
REPEAT UNTIL KeyPressedL;
ReadKeyL:=DeQue(LKeyBuffer);
END;
PROCEDURE NewInt9;
INTERRUPT;
BEGIN
inline( $9C/ $FF/ $1E/ OldInt9 ); { Pushf / Call Far [OldInt9] }
IF KbdHead<>KbdTail THEN
BEGIN
WITH LKeyBuffer DO BEGIN
buffer[tail]:=chr(Mem[$40:KbdHead]);
tail:=(tail+1) MOD BufferSize;
END;
IF Mem[$40:KbdHead]=0 THEN
WITH LKeyBuffer DO BEGIN
buffer[tail]:=chr(Mem[$40:KbdHead+1]);
tail:=(tail+1) MOD BufferSize;
END;
KbdHead:=KbdHead+2;
IF KbdHead=KbdMax THEN KbdHead:=KbdMin;
END;
END;
PROCEDURE MergeLocalInput;
BEGIN
IF InstalledInt9 THEN
BEGIN
SetIntVec(9,OldInt9);
InstalledInt9:=FALSE;
END;
END;
PROCEDURE SeperateLocalInput;
BEGIN
IF NOT InstalledInt9 THEN
BEGIN
GetIntVec(9,OldInt9);
SetIntVec(9,@NewInt9);
InstalledInt9:=TRUE;
LKeyBuffer.Head:=LKeyBuffer.Tail;
RKeyBuffer.Head:=RKeyBuffer.Tail;
END;
END;
FUNCTION empty : boolean;
BEGIN
Drain;
empty := BufferEmpty(RKeyBuffer);
END;
FUNCTION KeyPressed : boolean;
BEGIN
keypressed := (not empty);
END;
PROCEDURE StuffIn(s:string);
VAR i:integer;
BEGIN
FOR i:=1 TO length(s) DO
Enque(RKeyBuffer,s[i]);
END;
PROCEDURE WriteKeyBuffer(ch:char; n:byte);
VAR
r:registers;
BEGIN
IF Setting.KeyBIOS THEN BEGIN
r.ah:=5; r.cl:=ord(ch);
r.ch:=n;
intr($16,r);
END
ELSE BEGIN
Mem[$40:KbdTail]:=ord(ch);
Mem[$40:KbdTail+1]:=n;
KbdTail:=KbdTail+2;
IF KbdTail=KbdMax THEN KbdTail:=KbdMin;
END;
END;
PROCEDURE Macro(ch:char);
VAR r:registers;
BEGIN
IF (Setting.BBS=BBS_None) AND (Setting.WWIVEditMacrosOk)
THEN CASE ch OF
^A : WriteKeyBuffer(#0,1);
^D : WriteKeyBuffer(#0,2);
^F : WriteKeyBuffer(#0,3);
END
ELSE IF (Setting.BBS=BBS_WWIV) THEN
BEGIN
WriteKeyBuffer(^P,0);
WriteKeyBuffer(ch,0);
END
END;
FUNCTION ReadKey : char;
VAR
ch : char;
r : registers;
BEGIN
IF BufferEmpty(RKeyBuffer) THEN
BEGIN
IF Setting.Comport>0 THEN
REPEAT UNTIL KeyPressed;
IF FossilCharWaiting THEN Enque(RKeybuffer,FossilRecieve)
ELSE IF AsyncCharWaiting THEN Enque(RKeybuffer,AsyncRecieve)
ELSE BEGIN
r.ah:=$07; msdos(r);
Enque(RKeyBuffer,chr(r.al));
END;
END;
ReadKey:=Deque(RKeyBuffer);
END;
END.