-
Notifications
You must be signed in to change notification settings - Fork 8
/
upykvm.pas
299 lines (243 loc) · 8.32 KB
/
upykvm.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{$i xpc}{$mode delphiunicode}
unit upykvm;
interface uses
xpc, classes, sysutils,
PythonEngine,
kvm, cw, kbd, lined, keyboard, arrays;
procedure initkvm; cdecl;
procedure PyInit_kvm; cdecl;
type
TPythonKvmIO = class(TPythonInputOutput)
procedure SendData(const data : AnsiString); override;
procedure SendUniData(const Data : UnicodeString ); override;
function ReceiveData : AnsiString; override;
function ReceiveUniData : UnicodeString; override;
end;
TTerms = GArray<ITerm>;
implementation
var
gEng : TPythonEngine;
gMod : TPythonModule;
gTerms : TTerms;
{-- python constants --}
function NoneRef: PPyObject; cdecl;
begin result := gEng.Py_None; gEng.Py_IncRef(result)
end;
function TrueRef: PPyObject; cdecl;
begin result := PPyObject(gEng.Py_True); gEng.Py_IncRef(result)
end;
function FalseRef: PPyObject; cdecl;
begin result := PPyObject(gEng.Py_False); gEng.Py_IncRef(result)
end;
{-- clear screen / line --}
function PyClrScr( self, args : PPyObject ) : PPyObject; cdecl;
begin kvm.ClrScr; result := NoneRef;
end;
function PyClrEol( self, args : PPyObject ) : PPyObject; cdecl;
begin kvm.ClrEol; result := NoneRef;
end;
{-- screen dimensions --}
function PyGetW( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do result := PyInt_FromLong( kvm.width )
end;
function PyGetH( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do result := PyInt_FromLong( kvm.height )
end;
function PyXMax( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do result := PyInt_FromLong( kvm.xMax )
end;
function PyYMax( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do result := PyInt_FromLong( kvm.yMax )
end;
{-- generating text --}
function PyNewLine( self, args : PPyObject ) : PPyObject; cdecl;
begin kvm.NewLine; result := NoneRef;
end;
function PyEmit( self, args : PPyObject ) : PPyObject; cdecl;
var a:PAnsiChar;
begin
if gEng.PyArg_ParseTuple(args, 's:emit', @a) <> 0 then kvm.emit(a);
result := NoneRef;
end;
function PyCWrite( self, args : PPyObject ) : PPyObject; cdecl;
var a:PAnsiChar;
begin
with gEng do begin
// TODO: if PyTuple_Size(args) <> 1 then throw python Exception
if PyArg_ParseTuple(args, 's:cwrite', @a) <> 0 then cw.cwrite(a);
result := NoneRef;
end;
end;
{-- cursor control --}
function PyGotoXY( self, args : PPyObject ) : PPyObject; cdecl;
var x, y: integer;
begin
with gEng do begin
// TODO: if PyTuple_Size(args) <> 1 then throw python Exception
if PyArg_ParseTuple(args, 'ii:gotoXY', @x,@y) <> 0 then
kvm.GotoXY(x,y);
result := NoneRef;
end;
end;
function PyWhereX( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do result := PyInt_FromLong( kvm.whereX )
end;
function PyWhereY( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do result := PyInt_FromLong( kvm.whereY )
end;
function PyShowCursor( self, args : PPyObject ) : PPyObject; cdecl;
begin kvm.ShowCursor; result := NoneRef;
end;
function PyHideCursor( self, args : PPyObject ) : PPyObject; cdecl;
begin kvm.HideCursor; result := NoneRef;
end;
{-- subterms --}
function PyPushSub( self, args : PPyObject ) : PPyObject; cdecl;
var x,y,w,h:integer;
begin with gEng do
begin
if PyArg_ParseTuple(args, 'iiii:pushSub', @x,@y,@w,@h) <> 0 then
kvm.PushSub(x,y,w,h);
result := NoneRef;
end;
end;
function PySubTerm( self, args : PPyObject ) : PPyObject; cdecl;
var x,y,w,h:integer;
begin with gEng do
begin
if PyArg_ParseTuple(args, 'iiii:subTerm', @x,@y,@w,@h) <> 0 then
result := PyInt_FromLong(
gTerms.append( kvm.SubTerm( kvm.asTerm, x,y,w,h )))
else result := NoneRef;
end
end;
function PyPushTerm( self, args : PPyObject ) : PPyObject; cdecl;
var i : integer;
begin
with gEng do begin
if PyArg_ParseTuple(args, 'i:subTerm', @i) <> 0 then
if (i >= 0) and (i < gTerms.length ) then
kvm.PushTerm(gTerms[i])
else ok; // TODO: error message for bad index!
result := NoneRef;
end;
end;
function PyPopTerm( self, args : PPyObject ) : PPyObject; cdecl;
begin
with gEng do begin
kvm.PopTerm; result := NoneRef;
end;
end;
function PyFg( self, args : PPyObject ) : PPyObject; cdecl;
var i:integer; //a:PAnsiChar;
begin with gEng do
begin
//if PyArg_ParseTuple(args, 's:Fg', @a) <> 0 then kvm.fg(a[1]) else
if PyArg_ParseTuple(args, 'i:Fg', @i) <> 0 then kvm.fg(i);
result := NoneRef;
end;
end;
function PyBg( self, args : PPyObject ) : PPyObject; cdecl;
var i:integer; //a:PAnsiChar;
begin with gEng do
begin
//if PyArg_ParseTuple(args, 's:Bg', @a) <> 0 then kvm.fg(a[1]) else
if PyArg_ParseTuple(args, 'i:Bg', @i) <> 0 then kvm.Bg(i);
result := NoneRef;
end;
end;
function PyInitKeyboard( self, args : PPyObject ) : PPyObject; cdecl;
begin keyboard.initkeyboard; result := NoneRef;
end;
function PyDoneKeyboard( self, args : PPyObject ) : PPyObject; cdecl;
begin keyboard.donekeyboard; result := NoneRef;
end;
function PyKeyPressed( self, args : PPyObject ) : PPyObject; cdecl;
begin with gEng do
if kbd.keypressed then result := TrueRef
else result := FalseRef;
end;
function PyReadKey( self, args : PPyObject ) : PPyObject; cdecl;
var c:char;
begin with gEng do
begin
c := u2a(kbd.readkey)[1]; result := Py_BuildValue('u#',@c,1);
end
end;
function PyGetLine( self, args : PPyObject ) : PPyObject; cdecl;
var pa:PAnsiChar; si:TStr='';
begin with gEng do
begin
if PyArg_ParseTuple(args, 's:getline', @pa) <> 0 then begin
lined.prompt(TStr(pa), si);
result := PyString_FromString(PAnsiChar(u2a(si)));
end;
end
end;
procedure TPythonKvmIO.SendData(const data : AnsiString);
begin write(data);
end;
procedure TPythonKvmIO.SendUniData(const Data : UnicodeString );
begin write(data);
end;
function TPythonKvmIO.ReceiveData : AnsiString;
begin readln(result);
end;
function TPythonKvmIO.ReceiveUniData : UnicodeString;
begin readln(result);
end;
procedure initkvm; cdecl;
begin
try gEng := GetPythonEngine
except
on Exception do begin
gEng := TPythonEngine.Create(Nil);
gEng.AutoFinalize := false;
gEng.Initialize;
gEng.IO := TPythonKvmIO.Create(gEng);
end
end;
gMod := TPythonModule.Create(Nil);
gMod.Engine := gEng;
gMod.ModuleName := 'kvm';
gMod.AddMethod('clrScr', @PyClrScr, 'Clear the screen');
gMod.AddMethod('clrEol', @PyClrEol, 'Clear to end of line');
gMod.AddMethod('newLine', @PyNewLine, 'Move to next line');
gMod.AddMethod('emit', @PyEmit, 'Emit a string');
gMod.AddMethod('cwrite', @PyCWrite, 'colorwrite');
gMod.AddMethod('gotoXY', @PyGotoXY, 'move cursor to x,y coordinates');
gMod.AddMethod('subTerm', @PySubTerm, 'get handle to (x,y,w,h) subterminal');
gMod.AddMethod('pushTerm', @PyPushTerm, 'push a handle creaned with subTerm');
gMod.AddMethod('pushSub', @PyPushSub, 'push a (x,y,w,h) subterminal');
gMod.AddMethod('popTerm', @PyPopTerm, 'pop subterminal off stack');
gMod.AddMethod('fg', @PyFg, 'set foreground');
gMod.AddMethod('bg', @PyBg, 'set background');
gMod.AddMethod('keyPressed', @PyKeyPressed, 'is keypressed?');
gMod.AddMethod('readKey', @PyReadKey, 'get a character from the keyboard');
gMod.AddMethod('getW', @PyGetW, 'width of current terminal');
gMod.AddMethod('getH', @PyGetH, 'height of current terminal');
gMod.AddMethod('xMax', @PyXMax, 'max x coordinate');
gMod.AddMethod('yMax', @PyYMax, 'max y coordinate');
gMod.AddMethod('whereX', @PyWhereX, 'cursor x position');
gMod.AddMethod('whereY', @PyWhereY, 'cursor y position');
gMod.AddMethod('showCursor', @PyShowCursor, 'show the cursor');
gMod.AddMethod('hideCursor', @PyHideCursor, 'hide the cursor');
gMod.AddMethod('getLine', @PyGetLine, 'read a line of text, interactively');
gMod.AddMethod('initKeyboard', @PyInitKeyboard, 'initialize keyboard driver');
gMod.AddMethod('doneKeyboard', @PyDoneKeyboard, 'finalize keyboard driver');
gMod.DocString.text := 'Python KVM module';
gMod.Initialize;
end;
{-- python 3.x version --}
procedure PyInit_kvm; cdecl;
begin initkvm
end;
var term : ITerm;
initialization
keyboard.DoneKeyboard;
gTerms := TTerms.Create;
finalization
gTerms.Free;
gEng.Free;
gMod.Free;
end.