chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Steel / c / keyString
1 /*
2  * keyString
3  *  Converts keypresses to strings
4  *
5  * © 1993-1998 Straylight
6  */
7
8 /*----- Licensing note ----------------------------------------------------*
9  *
10  * This file is part of Straylight's Steel library.
11  *
12  * Steel is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * Steel is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Steel.  If not, write to the Free Software Foundation,
24  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #include "akbd.h"
28 #include "msgs.h"
29 #include "keyString.h"
30 #include "buffer.h"
31 #include <string.h>
32 #include <stdio.h>
33
34 /*
35  * char *keyString(int key)
36  *
37  * Use
38  *  Converts a keypress as returned by the WIMP into a string suitable for
39  *  displaying to the user, for example as a keyboard shortcut.  The routine
40  *  handles the following cases:
41  *
42  *  * Function keys and Print, possibly with <Shift> and/or <Ctrl>
43  *  * Alphabetic keys with <Ctrl>
44  *
45  *  Note: f12 is not trapped; neither is <Ctrl> M, since this is <Return>.
46  *
47  * Parameters
48  *  int key == the key pressed
49  *
50  * Returns
51  *  A pointer to a READ-ONLY string, or 0 if the key was invalid.
52  */
53
54 char *keyString(int key)
55 {
56   return (keyString_convert(key,FALSE));
57 }
58
59 typedef struct
60 {
61   int c : 8;
62   int shift : 1;
63   int ctrl : 1;
64   int keypad : 1;
65   int func : 1;
66 }
67 keyString__keyEntry;
68
69 static keyString__keyEntry keyString__lower[]=
70 {
71   /* --- 0x000 - 0x00F --- */
72
73   ' ',          0,1,0,0,
74   'A',          0,1,0,0,
75   'B',          0,1,0,0,
76   'C',          0,1,0,0,
77   'D',          0,1,0,0,
78   'E',          0,1,0,0,
79   'F',          0,1,0,0,
80   'G',          0,1,0,0,
81   'H',          0,1,0,0,
82   'I',          0,1,0,0,
83   'J',          0,1,0,0,
84   'K',          0,1,0,0,
85   'L',          0,1,0,0,
86   'M',          0,1,0,0,
87   'N',          0,1,0,0,
88   'O',          0,1,0,0,
89
90   /* --- 0x010 - 0x01F --- */
91
92   'P',          0,1,0,0,
93   'Q',          0,1,0,0,
94   'R',          0,1,0,0,
95   'S',          0,1,0,0,
96   'T',          0,1,0,0,
97   'U',          0,1,0,0,
98   'V',          0,1,0,0,
99   'W',          0,1,0,0,
100   'X',          0,1,0,0,
101   'Y',          0,1,0,0,
102   'Z',          0,1,0,0,
103   0x01B,        0,0,0,0,
104   0x01C,        0,0,0,0,
105   0x01D,        0,0,0,0,
106   0x01E,        0,0,0,0,
107   0x07F,        0,1,0,0,
108 };
109
110 static keyString__keyEntry keyString__upper[]=
111 {
112   /* --- 0x100 - 0x10F --- */
113
114   ' ',          1,1,0,0,
115   'A',          1,1,0,0,
116   'B',          1,1,0,0,
117   'C',          1,1,0,0,
118   'D',          1,1,0,0,
119   'E',          1,1,0,0,
120   'F',          1,1,0,0,
121   'G',          1,1,0,0,
122   'H',          1,1,0,0,
123   'I',          1,1,0,0,
124   'J',          1,1,0,0,
125   'K',          1,1,0,0,
126   'L',          1,1,0,0,
127   'M',          1,1,0,0,
128   'N',          1,1,0,0,
129   'O',          1,1,0,0,
130
131   /* --- 0x110 - 0x11F --- */
132
133   'P',          1,1,0,0,
134   'Q',          1,1,0,0,
135   'R',          1,1,0,0,
136   'S',          1,1,0,0,
137   'T',          1,1,0,0,
138   'U',          1,1,0,0,
139   'V',          1,1,0,0,
140   'W',          1,1,0,0,
141   'X',          1,1,0,0,
142   'Y',          1,1,0,0,
143   'Z',          1,1,0,0,
144   0x01B,        1,0,0,0,
145   0x01C,        1,0,0,0,
146   0x01D,        1,0,0,0,
147   0x01E,        1,0,0,0,
148   0x07F,        1,1,0,0,
149
150   /* --- 0x120 - 0x12F --- */
151
152   ' ',          1,0,0,0,
153   '/',          0,1,1,0,
154   '*',          0,1,1,0,
155   '#',          0,1,1,0,
156   '-',          0,1,1,0,
157   '+',          0,1,1,0,
158   0x01D,        0,1,1,0,
159   '.',          0,1,1,0,
160   0,            0,0,0,0,
161   0,            0,0,0,0,
162   0,            0,0,0,0,
163   '[',          0,1,0,0,
164   '\\',         0,1,0,0,
165   ']',          0,1,0,0,
166   0,            0,0,0,0,
167   '-',          0,1,0,0,
168
169   /* --- 0x130 - 0x13F --- */
170
171   '0',          0,1,0,0,
172   '1',          0,1,0,0,
173   '2',          0,1,0,0,
174   '3',          0,1,0,0,
175   '4',          0,1,0,0,
176   '5',          0,1,0,0,
177   '6',          0,1,0,0,
178   '7',          0,1,0,0,
179   '8',          0,1,0,0,
180   '9',          0,1,0,0,
181   0,            0,0,0,0,
182   0x01B,        0,1,0,0,
183   0x01C,        0,1,0,0,
184   0x01D,        0,1,0,0,
185   0x01E,        0,1,0,0,
186   0,            0,0,0,0,
187
188   /* --- 0x140 - 0x14F --- */
189
190   0,            0,0,0,0,
191   '/',          1,1,1,0,
192   '*',          1,1,1,0,
193   '#',          1,1,1,0,
194   '-',          1,1,1,0,
195   '+',          1,1,1,0,
196   0x01D,        1,1,1,0,
197   '.',          1,1,1,0,
198   0,            0,0,0,0,
199   0,            0,0,0,0,
200   0,            0,0,0,0,
201   '[',          1,1,0,0,
202   '\\',         1,1,0,0,
203   ']',          1,1,0,0,
204   0,            0,0,0,0,
205   '-',          1,1,0,0,
206
207   /* --- 0x150 - 0x15F --- */
208
209   '0',          1,1,0,0,
210   '1',          1,1,0,0,
211   '2',          1,1,0,0,
212   '3',          1,1,0,0,
213   '4',          1,1,0,0,
214   '5',          1,1,0,0,
215   '6',          1,1,0,0,
216   '7',          1,1,0,0,
217   '8',          1,1,0,0,
218   '9',          1,1,0,0,
219   0,            0,0,0,0,
220   0x01B,        1,1,0,0,
221   0x01C,        1,1,0,0,
222   0x01D,        1,1,0,0,
223   0x01E,        1,1,0,0,
224   0,            0,0,0,0,
225
226   /* --- 0x160 - 0x16F --- */
227
228   0,            0,0,0,0,
229   '/',          0,0,1,0,
230   '*',          0,0,1,0,
231   '#',          0,0,1,0,
232   '-',          0,0,1,0,
233   '+',          0,0,1,0,
234   0x01D,        0,0,1,0,
235   '.',          0,0,1,0,
236   0,            0,0,0,0,
237   0,            0,0,0,0,
238   0,            0,0,0,0,
239   0,            0,0,0,0,
240   0,            0,0,0,0,
241   0,            0,0,0,0,
242   0,            0,0,0,0,
243   0,            0,0,0,0,
244
245   /* --- 0x170 - 0x17F --- */
246
247   0,            0,0,0,0,
248   '/',          1,0,1,0,
249   '*',          1,0,1,0,
250   '#',          1,0,1,0,
251   '-',          1,0,1,0,
252   '+',          1,0,1,0,
253   0x01D,        1,0,1,0,
254   '.',          1,0,1,0,
255   0,            0,0,0,0,
256   0,            0,0,0,0,
257   0,            0,0,0,0,
258   0,            0,0,0,0,
259   0,            0,0,0,0,
260   0,            0,0,0,0,
261   0,            0,0,0,0,
262   0x07F,        1,0,0,0,
263
264   /* --- 0x180 - 0x18F --- */
265
266   0,            0,0,0,1,
267   1,            0,0,0,1,
268   2,            0,0,0,1,
269   3,            0,0,0,1,
270   4,            0,0,0,1,
271   5,            0,0,0,1,
272   6,            0,0,0,1,
273   7,            0,0,0,1,
274   8,            0,0,0,1,
275   9,            0,0,0,1,
276   13,           0,0,0,1,
277   14,           0,0,0,1,
278   15,           0,0,0,1,
279   16,           0,0,0,1,
280   17,           0,0,0,1,
281   18,           0,0,0,1,
282
283   /* --- 0x190 - 0x19F --- */
284
285   0,            1,0,0,1,
286   1,            1,0,0,1,
287   2,            1,0,0,1,
288   3,            1,0,0,1,
289   4,            1,0,0,1,
290   5,            1,0,0,1,
291   6,            1,0,0,1,
292   7,            1,0,0,1,
293   8,            1,0,0,1,
294   9,            1,0,0,1,
295   13,           1,0,0,1,
296   14,           1,0,0,1,
297   15,           1,0,0,1,
298   16,           1,0,0,1,
299   17,           1,0,0,1,
300   18,           1,0,0,1,
301
302   /* --- 0x1A0 - 0x1AF --- */
303
304   0,            0,1,0,1,
305   1,            0,1,0,1,
306   2,            0,1,0,1,
307   3,            0,1,0,1,
308   4,            0,1,0,1,
309   5,            0,1,0,1,
310   6,            0,1,0,1,
311   7,            0,1,0,1,
312   8,            0,1,0,1,
313   9,            0,1,0,1,
314   13,           0,1,0,1,
315   14,           0,1,0,1,
316   15,           0,1,0,1,
317   16,           0,1,0,1,
318   17,           0,1,0,1,
319   18,           0,1,0,1,
320
321   /* --- 0x1B0 - 0x1BF --- */
322
323   0,            1,1,0,1,
324   1,            1,1,0,1,
325   2,            1,1,0,1,
326   3,            1,1,0,1,
327   4,            1,1,0,1,
328   5,            1,1,0,1,
329   6,            1,1,0,1,
330   7,            1,1,0,1,
331   8,            1,1,0,1,
332   9,            1,1,0,1,
333   13,           1,1,0,1,
334   14,           1,1,0,1,
335   15,           1,1,0,1,
336   16,           1,1,0,1,
337   17,           1,1,0,1,
338   18,           1,1,0,1,
339
340   /* --- 0x1C0 - 0x1CF --- */
341
342   '0',          0,0,1,0,
343   '1',          0,0,1,0,
344   '2',          0,0,1,0,
345   '3',          0,0,1,0,
346   '4',          0,0,1,0,
347   '5',          0,0,1,0,
348   '6',          0,0,1,0,
349   '7',          0,0,1,0,
350   '8',          0,0,1,0,
351   '9',          0,0,1,0,
352   10,           0,0,0,1,
353   11,           0,0,0,1,
354   12,           0,0,0,1,
355   19,           0,0,0,1,
356   0,            0,0,0,0,
357   0,            0,0,0,0,
358
359   /* --- 0x1D0 - 0x1DF --- */
360
361   '0',          1,0,1,0,
362   '1',          1,0,1,0,
363   '2',          1,0,1,0,
364   '3',          1,0,1,0,
365   '4',          1,0,1,0,
366   '5',          1,0,1,0,
367   '6',          1,0,1,0,
368   '7',          1,0,1,0,
369   '8',          1,0,1,0,
370   '9',          1,0,1,0,
371   10,           1,0,0,1,
372   11,           1,0,0,1,
373   12,           1,0,0,1,
374   19,           1,0,0,1,
375   0,            0,0,0,0,
376   0,            0,0,0,0,
377
378   /* --- 0x1E0 - 0x1EF --- */
379
380   '0',          0,1,1,0,
381   '1',          0,1,1,0,
382   '2',          0,1,1,0,
383   '3',          0,1,1,0,
384   '4',          0,1,1,0,
385   '5',          0,1,1,0,
386   '6',          0,1,1,0,
387   '7',          0,1,1,0,
388   '8',          0,1,1,0,
389   '9',          0,1,1,0,
390   10,           0,1,0,1,
391   11,           0,1,0,1,
392   12,           0,1,0,1,
393   19,           0,1,0,1,
394   0,            0,0,0,0,
395   0,            0,0,0,0,
396
397   /* --- 0x1F0 - 0x1FF --- */
398
399   '0',          1,1,1,0,
400   '1',          1,1,1,0,
401   '2',          1,1,1,0,
402   '3',          1,1,1,0,
403   '4',          1,1,1,0,
404   '5',          1,1,1,0,
405   '6',          1,1,1,0,
406   '7',          1,1,1,0,
407   '8',          1,1,1,0,
408   '9',          1,1,1,0,
409   10,           1,1,0,1,
410   11,           1,1,0,1,
411   12,           1,1,0,1,
412   19,           1,1,0,1,
413   0,            0,0,0,0,
414   0,            0,0,0,0,
415 };
416
417 static char *keyString__fkeys[]=
418 {
419   "kstrPRT:Print",
420   0,
421   0,
422   0,
423   0,
424   0,
425   0,
426   0,
427   0,
428   0,
429   0,
430   0,
431   0,
432   "kstrF13:Tab",
433   "kstrF14:Copy",
434   "kstrF15:Left",
435   "kstrF16:Right",
436   "kstrF17:Down",
437   "kstrF18:Up",
438   "kstrF19:Insert",
439 };
440
441 static char *keyString__lowctl[]=
442 {
443   "kstrESC:Escape",
444   "kstrBSP:Backspace",
445   "kstrRET:Return",
446   "kstrHOME:Home",
447 };
448
449 /*
450  * char *keyString_convert(int key,BOOL mnu)
451  *
452  * Use
453  *  Converts a STEEL extended keypress into a string suitable for displaying
454  *  either as a short-cut string in a dialogue box or in a menu
455  *
456  * Parameters
457  *  int key == the keypress to translate
458  *  BOOL mnu == TRUE to create menu shortcut string, FALSE for dbox
459  *
460  * Returns
461  *  A pointer to the string, or 0
462  */
463
464 char *keyString_convert(int key,BOOL mnu)
465 {
466   keyString__keyEntry ke;
467   char base[20];
468   char *bp;
469   char *buffer=buffer_find();
470
471   /* --- Make sure it's not a printable character --- */
472
473   if (key<' ')
474     ke=keyString__lower[key];
475   else if (key>0x0FF)
476     ke=keyString__upper[key-0x100];
477   else if (key==0x07F)
478   {
479     ke.c=0x07F;
480     ke.shift=ke.ctrl=ke.keypad=ke.func=0;
481   }
482   else
483     return (0);
484   if (!ke.c && !ke.func)
485     return (0);
486
487   /* --- Get the name of the base key --- */
488
489   if (ke.func)
490   {
491     if (ke.c==12)
492       return (0);  /* Always let F12 through */
493     bp=keyString__fkeys[ke.c];
494     if (!bp)
495     {
496       sprintf(base,"%s%i",msgs_lookup("kstrF:F"),ke.c);
497       bp=base;
498     }
499     else
500       bp=msgs_lookup(bp);
501   }
502   else if (ke.c==0x01D && ke.keypad)
503   {
504     ke.keypad=0;
505     bp=msgs_lookup("kstrENT:Enter");
506   }
507   else if (ke.c>0x01A && ke.c<0x01F)
508     bp=msgs_lookup(keyString__lowctl[ke.c-0x01B]);
509   else if (ke.c==0x07f)
510     bp=msgs_lookup("kstrDEL:Delete");
511   else if (ke.c==0x020)
512     bp=msgs_lookup("kstrSPC:Space");
513   else
514   {
515     base[0]=ke.c;
516     base[1]=0;
517     bp=base;
518   }
519
520   /* --- Build the keyname in the buffer --- */
521
522   buffer[0]=0;
523   if (ke.shift)
524     strcat(buffer,mnu ? "\x8B" : msgs_lookup("kstrSH:Shift "));
525   if (ke.ctrl)
526     strcat(buffer,mnu ? "^" : msgs_lookup("kstrCTL:Ctrl "));
527   if (ke.keypad)
528     strcat(buffer,mnu ? "~" : msgs_lookup("kstrKPD:Keypad "));
529   strcat(buffer,bp);
530
531   /* --- Well, that's it at last --- */
532
533   return (buffer);
534 }