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
|
###
### This data and information is not to be used as the basis of manufacture,
### or be reproduced or copied, or be distributed to another party, in whole
### or in part, without the prior written consent of Lucent Technologies.
###
### (C) Copyright 1997 Lucent Technologies
###
### Written by N. W. Knauft
###
#
# Revisions Copyright © 1998 Vita Nuova Limited.
implement Keyboard;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "swkeyb.m";
#Icon path
ICPATH: con "keybd/";
#Font
FONT: con "/fonts/lucidasans/latin1.7.font";
SPECFONT: con "/fonts/lucidasans/latin1.6.font";
# Dimension constants
KBDWIDTH: con 360;
KBDHEIGHT: con 120;
KEYSIZE: con "19";
KEYSPACE: con 5;
KEYBORDER: con 1;
KEYGAP: con KEYSPACE - (2 * KEYBORDER);
ENDGAP: con 2 - KEYBORDER;
# Row size constants (cumulative)
ROW1: con 14;
ROW2: con 28;
ROW3: con 41;
ROW4: con 53;
NKEYS: con 63;
#Special key number constants
DELKEY: con 13;
TABKEY: con 14;
BACKSLASHKEY: con 27;
CAPSLOCKKEY: con 28 ;
RETURNKEY: con 40;
LSHIFTKEY: con 41;
RSHIFTKEY: con 52;
ESCKEY: con 53;
CTRLKEY: con 54;
METAKEY: con 55;
ALTKEY: con 56;
SPACEKEY: con 57;
ENTERKEY: con 58;
LEFTKEY: con 59;
RIGHTKEY: con 60;
DOWNKEY: con 61;
UPKEY: con 62;
#Special key code constants
CAPSLOCK: con -1 ;
SHIFT: con -2;
CTRL: con -3;
ALT: con -4;
META: con -5;
MAGIC_PREFIX: con 256;
ARROW_OFFSET: con 57344;
ARROW_PREFIX: con ARROW_OFFSET + 18;
#Special key width constants
DELSIZE: con 44;
TABSIZE: con 32;
BACKSLASHSIZE: con 31;
CAPSLOCKSIZE: con 44;
RETURNSIZE: con 43;
LSHIFTSIZE: con 56;
RSHIFTSIZE: con 55;
ESCSIZE: con 21;
CTRLSIZE: con 23;
METASIZE: con 38;
ALTSIZE: con 22;
SPACESIZE: con 100;
ENTERSIZE: con 31;
#Arrow key code constants
UP: con ARROW_PREFIX;
DOWN: con ARROW_PREFIX + 1;
LEFT: con ARROW_PREFIX + 2;
RIGHT: con ARROW_PREFIX + 3;
direction:= array[] of {"up", "down", "left", "right"};
row_dimensions:= array[] of {0, ROW1, ROW2, ROW3, ROW4, NKEYS};
special_keys:= array[] of {
(DELKEY, DELSIZE),
(TABKEY, TABSIZE),
(BACKSLASHKEY, BACKSLASHSIZE),
(CAPSLOCKKEY, CAPSLOCKSIZE),
(RETURNKEY, RETURNSIZE),
(LSHIFTKEY, LSHIFTSIZE),
(RSHIFTKEY, RSHIFTSIZE),
(ESCKEY, ESCSIZE),
(CTRLKEY, CTRLSIZE),
(METAKEY, METASIZE),
(ALTKEY, ALTSIZE),
(SPACEKEY, SPACESIZE),
(ENTERKEY, ENTERSIZE),
};
keys:= array[] of {
# Unshifted
"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Delete",
"Tab", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\\\",
"CapLoc", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "\'", "Return",
"Shift", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "Shift",
"Esc", "Ctrl", " ", "Alt", " ", "Enter", "<-", "->", "v", "^",
# Shifted
"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "Delete",
"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "\\{", "\\}", "|",
"CapLoc", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "Return",
"Shift", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", "Shift",
"Esc", "Ctrl", " ", "Alt", " ", "Enter", "<-", "->", "v", "^",
};
keyvals:= array[] of {
# Unshifted
'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\',
CAPSLOCK, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '\n',
SHIFT, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', SHIFT,
27, CTRL, META, ALT, 32, '\n', LEFT, RIGHT, DOWN, UP,
# Shifted
'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b',
'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '|',
CAPSLOCK, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '\n',
SHIFT, 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', SHIFT,
27, CTRL, META, ALT, 32, '\n', LEFT, RIGHT, DOWN, UP,
};
rowlayout := array[] of {
"frame .f1",
"frame .f2",
"frame .f3",
"frame .f4",
"frame .f5",
"frame .dummy0 -height " + string (ENDGAP),
"frame .dummy1 -height " + string KEYGAP,
"frame .dummy2 -height " + string KEYGAP,
"frame .dummy3 -height " + string KEYGAP,
"frame .dummy4 -height " + string KEYGAP,
"frame .dummy5 -height " + string (ENDGAP + 1),
};
# Move key flags
move_key_enabled := 0;
meta_active := 0;
# Create keyboard widget, spawn keystroke handler
initialize(t: ref Tk->Toplevel, ctxt : ref Draw->Context, dot: string): chan of string
{
return chaninit(t, ctxt, dot, chan of string);
}
chaninit(t: ref Tk->Toplevel, ctxt : ref Draw->Context, dot: string, rc: chan of string): chan of string
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
tkclient->init();
tk->cmd(t, "frame " + dot + " -bd 2 -relief raised -width " + string KBDWIDTH
+ " -height " + string KBDHEIGHT);
tkcmds(t, rowlayout);
for(i := 0; i < NKEYS; i++) {
tk->cmd(t, "button .b" + string i + " -font " + FONT + " -width " + KEYSIZE
+ " -height " + KEYSIZE + " -bd " + string KEYBORDER);
tk->cmd(t, ".b" + string i + " configure -text {" + keys[i] +
"} -command 'send keypress " + string keyvals[i]);
}
for(i = 0; i < len special_keys; i++) {
(keynum, keysize) := special_keys[i];
tk->cmd(t, ".b" + string keynum + " configure -font " + SPECFONT + " -width " + string keysize);
}
tk->cmd(t, "image create bitmap Capslock_on -file " + ICPATH + "capson.bit -maskfile " + ICPATH + "capson.bit");
tk->cmd(t, "image create bitmap Capslock_off -file " + ICPATH + "capsoff.bit -maskfile " + ICPATH + "capsoff.bit");
tk->cmd(t, "image create bitmap Left_arrow -file " + ICPATH + "larrow.bit -maskfile " + ICPATH + "larrow.bit");
tk->cmd(t, "image create bitmap Right_arrow -file " + ICPATH + "rarrow.bit -maskfile " + ICPATH + "rarrow.bit");
tk->cmd(t, "image create bitmap Down_arrow -file " + ICPATH + "darrow.bit -maskfile " + ICPATH + "darrow.bit");
tk->cmd(t, "image create bitmap Up_arrow -file " + ICPATH + "uarrow.bit -maskfile " + ICPATH + "uarrow.bit");
tk->cmd(t, "image create bitmap Move_on -file " + ICPATH + "moveon.bit -maskfile " + ICPATH + "moveon.bit");
tk->cmd(t, "image create bitmap Move_off -file " + ICPATH + "moveoff.bit -maskfile " + ICPATH + "moveoff.bit");
tk->cmd(t, "image create bitmap None -file " + ICPATH + "none.bit -maskfile " + ICPATH + "none.bit");
tk->cmd(t, ".b" + string CAPSLOCKKEY + " configure -image Capslock_off");
tk->cmd(t, ".b" + string LEFTKEY + " configure -image Left_arrow");
tk->cmd(t, ".b" + string RIGHTKEY + " configure -image Right_arrow");
tk->cmd(t, ".b" + string DOWNKEY + " configure -image Down_arrow");
tk->cmd(t, ".b" + string UPKEY + " configure -image Up_arrow");
for(j:=1; j < len row_dimensions; j++) {
rowstart := row_dimensions[j-1];
rowend := row_dimensions[j];
for(i=rowstart; i<rowend; i++) {
if (i == rowstart) {
tk->cmd(t, "frame .f" + string j + ".dummy -width " + string ENDGAP);
tk->cmd(t, "pack .f" + string j + ".dummy -side left");
}
tk->cmd(t, "pack .b" + string i + " -in .f" + string j + " -side left");
if (i == rowend-1)
tk->cmd(t, "frame .f" + string j + ".dummy" + string i + " -width " + string ENDGAP);
else
tk->cmd(t, "frame .f" + string j + ".dummy" + string i + " -width " + string KEYGAP);
tk->cmd(t, "pack .f" + string j + ".dummy" + string i + " -side left");
}
}
tk->cmd(t, "pack .dummy0 .f1 .dummy1 .f2 .dummy2 .f3 .dummy3 .f4 .dummy4 .f5 .dummy5 -in " + dot);
tk->cmd(t,"update");
key := chan of string;
spawn handle_keyclicks(t, ctxt, key, rc);
return key;
}
tkcmds(t: ref Tk->Toplevel, cmds: array of string)
{
for(i := 0; i < len cmds; i++)
tk->cmd(t, cmds[i]);
}
# Process key clicks and hand keycodes off to Tk
handle_keyclicks(t: ref Tk->Toplevel, ctxt : ref Draw->Context, sc, rc: chan of string)
{
keypress := chan of string;
tk->namechan(t, keypress, "keypress");
minitel := 0;
caps_locked := 0;
shifted := 0;
ctrl_active := 0;
alt_active := 0;
Work:
for(;;){
alt {
k := <-keypress =>
(n, cmdstr) := sys->tokenize(k, " \t\n");
keycode := int hd cmdstr;
case keycode {
CAPSLOCK =>
redisplay_keyboard(t, minitel, caps_locked ^= 1, caps_locked);
shifted = 0;
ctrl_active = 0;
alt_active = 0;
SHIFT =>
redisplay_keyboard(t, minitel, (shifted ^= 1) ^ caps_locked, caps_locked);
CTRL =>
ctrl_active ^= 1;
if (shifted) {
redisplay_keyboard(t, minitel, caps_locked, caps_locked);
shifted = 0;
}
alt_active = 0;
ALT =>
alt_active ^= 1;
if (shifted) {
redisplay_keyboard(t, minitel, caps_locked, caps_locked);
shifted = 0;
}
ctrl_active = 0;
META =>
if (move_key_enabled) {
if (meta_active ^= 1)
tk->cmd(t, ".b" + string METAKEY + " configure -image Move_on");
else
tk->cmd(t, ".b" + string METAKEY + " configure -image Move_off");
}
redisplay_keyboard(t, minitel, caps_locked, caps_locked);
shifted = 0;
ctrl_active = 0;
alt_active = 0;
* =>
if (ctrl_active) {
keycode &= 16r1F;
ctrl_active = 0;
} else if (alt_active) {
keycode += MAGIC_PREFIX;
alt_active = 0;
}
if (meta_active && UP <= keycode && keycode <= RIGHT) {
spawn send_move_msg(direction[keycode - ARROW_PREFIX], sc);
} else
tk->keyboard(t, keycode);
if (shifted) {
redisplay_keyboard(t, minitel, caps_locked, caps_locked);
shifted = 0;
}
}
s := <-rc =>
case s {
"kill" =>
break Work;
"minitel" =>
if (!minitel) {
minitel = 1;
redisplay_keyboard(t, minitel, shifted, caps_locked);
}
"standard" =>
if (minitel) {
minitel = 0;
redisplay_keyboard(t, minitel, shifted, caps_locked);
}
}
}
}
}
send_move_msg(dir: string, ch: chan of string)
{
ch <-= dir;
}
# Redisplay keyboard to reflect current state (shifted or unshifted)
redisplay_keyboard(t: ref Tk->Toplevel, minitel, shifted, caps_locked: int)
{
base: int;
if (shifted)
base = NKEYS;
else
base = 0;
for(i:=0; i<NKEYS; i++) {
n := base + i;
val := keyvals[n];
key := keys[n];
if (minitel) {
if (val >= int 'A' && val <= int 'Z') {
key = keys[n-NKEYS];
} else if (val >= int 'a' && val <= int 'z') {
key = keys[n+NKEYS];
}
}
tk->cmd(t, ".b" + string i + " configure -text {" + key
+ "} -command 'send keypress " + string val);
}
if (caps_locked)
tk->cmd(t, ".b" + string CAPSLOCKKEY + " configure -image Capslock_on");
else
tk->cmd(t, ".b" + string CAPSLOCKKEY + " configure -image Capslock_off");
tk->cmd(t, "update");
}
|