-
Notifications
You must be signed in to change notification settings - Fork 10
/
GlkWrapper.cs
440 lines (368 loc) · 12.6 KB
/
GlkWrapper.cs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright © 2008, Textfyre, Inc. - All Rights Reserved
* Please read the accompanying COPYRIGHT file for licensing resstrictions.
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace FyreVM
{
partial class Engine
{
private const int MAX_GLK_ARGS = 8;
private uint[] glkArgs = new uint[MAX_GLK_ARGS];
private Dictionary<uint, Func<uint[], uint>> glkHandlers;
private bool glkWindowOpen, glkWantLineInput, glkWantCharInput;
private uint glkLineInputBuffer, glkLineInputBufSize;
private Dictionary<uint, GlkStream> glkStreams;
private uint glkNextStreamID = 100;
private GlkStream glkCurrentStream;
private static class GlkConst
{
public const uint wintype_TextBuffer = 3;
public const uint evtype_None = 0;
public const uint evtype_CharInput = 2;
public const uint evtype_LineInput = 3;
public const uint gestalt_CharInput = 1;
public const uint gestalt_CharOutput = 3;
public const uint gestalt_CharOutput_ApproxPrint = 1;
public const uint gestalt_CharOutput_CannotPrint = 0;
public const uint gestalt_CharOutput_ExactPrint = 2;
public const uint gestalt_LineInput = 2;
public const uint gestalt_Version = 0;
}
private abstract class GlkStream
{
public readonly uint ID;
public GlkStream(uint id)
{
this.ID = id;
}
public abstract void PutChar(uint c);
public abstract bool Close(out uint read, out uint written);
}
private class GlkWindowStream : GlkStream
{
private readonly Engine engine;
public GlkWindowStream(uint id, Engine engine)
: base(id)
{
this.engine = engine;
}
public override void PutChar(uint c)
{
if (c > 0xffff)
c = '?';
engine.outputBuffer.Write((char)c);
}
public override bool Close(out uint read, out uint written)
{
written = 0;
read = 0;
return false;
}
}
private class GlkMemoryStream : GlkStream
{
private readonly Engine engine;
private readonly uint address;
private readonly byte[] buffer;
private uint position, written, read;
public GlkMemoryStream(uint id, Engine engine, uint address, uint size)
: base(id)
{
this.engine = engine;
this.address = address;
if (address != 0 && size != 0)
buffer = new byte[size];
position = written = read = 0;
}
public override void PutChar(uint c)
{
if (c > 0xff)
c = '?';
written++;
if (position < buffer.Length)
buffer[position++] = (byte)c;
}
public override bool Close(out uint read, out uint written)
{
written = this.written;
read = this.read;
if (buffer != null)
{
uint max = (uint)Math.Min(written, buffer.Length);
for (uint i = 0; i < max; i++)
engine.image.WriteByte(address + i, buffer[i]);
}
return true;
}
}
private class GlkMemoryUniStream : GlkStream
{
private readonly Engine engine;
private readonly uint address;
private readonly uint[] buffer;
private uint position, written, read;
public GlkMemoryUniStream(uint id, Engine engine, uint address, uint size)
: base(id)
{
this.engine = engine;
this.address = address;
if (address != 0 && size != 0)
buffer = new uint[size];
position = written = read = 0;
}
public override void PutChar(uint c)
{
written++;
if (position < buffer.Length)
buffer[position++] = c;
}
public override bool Close(out uint read, out uint written)
{
written = this.written;
read = this.read;
if (buffer != null)
{
uint max = (uint)Math.Min(written, buffer.Length);
for (uint i = 0; i < max; i++)
engine.image.WriteInt32(address + i * 4, buffer[i]);
}
return true;
}
}
private void GlkWrapperCall(uint[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format("glk(0x{0:X4}, {1})", args[0], args[1]));
if (glkHandlers == null)
InitGlkHandlers();
int gargc = (int)args[1];
if (gargc > MAX_GLK_ARGS)
throw new ArgumentException("Too many stack arguments for @glk");
for (int i = 0; i < gargc; i++)
glkArgs[i] = Pop();
Func<uint[], uint> handler;
if (glkHandlers.TryGetValue(args[0], out handler))
{
System.Diagnostics.Debug.WriteLine(" // " + handler.Target.GetType().Name);
args[2] = handler(glkArgs);
}
else
{
System.Diagnostics.Debug.WriteLine(" // unimplemented");
args[2] = 0;
}
}
private void GlkWrapperWrite(uint ch)
{
if (glkCurrentStream != null)
glkCurrentStream.PutChar(ch);
}
private void GlkWrapperWrite(string str)
{
if (glkCurrentStream != null)
foreach (char c in str)
glkCurrentStream.PutChar(c);
}
private uint GlkReadReference(uint reference)
{
if (reference == 0xffffffff)
return Pop();
return image.ReadInt32(reference);
}
private void GlkWriteReference(uint reference, uint value)
{
if (reference == 0xffffffff)
Push(value);
else
image.WriteInt32(reference, value);
}
private void GlkWriteReference(uint reference, params uint[] values)
{
if (reference == 0xffffffff)
{
foreach (uint v in values)
Push(v);
}
else
{
foreach (uint v in values)
{
image.WriteInt32(reference, v);
reference += 4;
}
}
}
private void InitGlkHandlers()
{
glkHandlers = new Dictionary<uint, Func<uint[], uint>>();
glkHandlers.Add(0x0040, glk_stream_iterate);
glkHandlers.Add(0x0020, glk_window_iterate);
glkHandlers.Add(0x0064, glk_fileref_iterate);
glkHandlers.Add(0x0023, glk_window_open);
glkHandlers.Add(0x002F, glk_set_window);
glkHandlers.Add(0x0086, glk_set_style);
glkHandlers.Add(0x00D0, glk_request_line_event);
glkHandlers.Add(0x00C0, glk_select);
glkHandlers.Add(0x00A0, glk_char_to_lower);
glkHandlers.Add(0x00A1, glk_char_to_upper);
glkHandlers.Add(0x0043, glk_stream_open_memory);
glkHandlers.Add(0x0048, glk_stream_get_current);
glkHandlers.Add(0x0139, glk_stream_open_memory_uni);
glkHandlers.Add(0x0047, glk_stream_set_current);
glkHandlers.Add(0x0044, glk_stream_close);
glkStreams = new Dictionary<uint, GlkStream>();
}
private uint glk_stream_iterate(uint[] args)
{
return 0;
}
private uint glk_window_iterate(uint[] args)
{
if (glkWindowOpen && args[0] == 0)
return 1;
return 0;
}
private uint glk_fileref_iterate(uint[] args)
{
return 0;
}
private uint glk_window_open(uint[] args)
{
if (glkWindowOpen)
return 0;
glkWindowOpen = true;
glkStreams[1] = new GlkWindowStream(1, this);
return 1;
}
private uint glk_set_window(uint[] args)
{
if (glkWindowOpen)
glkStreams.TryGetValue(1, out glkCurrentStream);
return 0;
}
private uint glk_set_style(uint[] args)
{
return 0;
}
private uint glk_request_line_event(uint[] args)
{
glkWantLineInput = true;
glkLineInputBuffer = args[1];
glkLineInputBufSize = args[2];
return 0;
}
private uint glk_select(uint[] args)
{
DeliverOutput();
if (glkWantLineInput)
{
string line;
if (LineWanted == null)
{
line = "";
}
else
{
LineWantedEventArgs e = new LineWantedEventArgs();
LineWanted(this, e);
line = e.Line;
}
byte[] bytes = StringToLatin1(line);
uint max = Math.Min(glkLineInputBufSize, (uint)bytes.Length);
for (uint i = 0; i < max; i++)
image.WriteByte(glkLineInputBuffer + i, bytes[i]);
// return event
GlkWriteReference(
args[0],
GlkConst.evtype_LineInput, 1, max, 0);
glkWantLineInput = false;
}
else if (glkWantCharInput)
{
char ch;
if (KeyWanted == null)
{
ch = '\0';
}
else
{
KeyWantedEventArgs e = new KeyWantedEventArgs();
KeyWanted(this, e);
ch = e.Char;
}
// return event
GlkWriteReference(
args[0],
GlkConst.evtype_CharInput, 1, ch, 0);
glkWantCharInput = false;
}
else
{
// no event
GlkWriteReference(
args[0],
GlkConst.evtype_None, 0, 0, 0);
}
return 0;
}
private uint glk_char_to_lower(uint[] args)
{
char ch = (char)args[0];
return (uint)char.ToLower(ch);
}
private uint glk_char_to_upper(uint[] args)
{
char ch = (char)args[0];
return (uint)char.ToUpper(ch);
}
private uint glk_stream_open_memory(uint[] args)
{
uint id = glkNextStreamID++;
GlkStream stream = new GlkMemoryStream(id, this, args[0], args[1]);
glkStreams[id] = stream;
return id;
}
private uint glk_stream_open_memory_uni(uint[] args)
{
uint id = glkNextStreamID++;
GlkStream stream = new GlkMemoryUniStream(id, this, args[0], args[1]);
glkStreams[id] = stream;
return id;
}
private uint glk_stream_get_current(uint[] args)
{
if (glkCurrentStream == null)
return 0;
return glkCurrentStream.ID;
}
private uint glk_stream_set_current(uint[] args)
{
glkStreams.TryGetValue(args[0], out glkCurrentStream);
return 0;
}
private uint glk_stream_close(uint[] args)
{
GlkStream stream;
if (glkStreams.TryGetValue(args[0], out stream))
{
uint read, written;
bool closed = stream.Close(out read, out written);
if (args[1] != 0)
{
GlkWriteReference(
args[1],
read, written);
}
if (closed)
{
glkStreams.Remove(args[0]);
if (glkCurrentStream == stream)
glkCurrentStream = null;
}
}
return 0;
}
}
}