aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/xterm/input.c
blob: 2e93686a903ac59a0220bc6d612f3ed6a903cc43 (plain)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
/* $XTermId: input.c,v 1.174 2005/09/18 23:48:12 tom Exp $ */

/*
 *	$Xorg: input.c,v 1.3 2000/08/17 19:55:08 cpqbld Exp $
 */

/* $XFree86: xc/programs/xterm/input.c,v 3.73 2005/09/18 23:48:12 dickey Exp $ */

/*
 * Copyright 1999-2004,2005 by Thomas E. Dickey
 *
 *                         All Rights Reserved
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization.
 *
 *
 * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
 *
 *                         All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Digital Equipment
 * Corporation not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 *
 *
 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/* input.c */

#include <xterm.h>

#include <X11/keysym.h>

#ifdef VMS
#include <X11/keysymdef.h>
#endif

#if HAVE_X11_DECKEYSYM_H
#include <X11/DECkeysym.h>
#endif

#if HAVE_X11_SUNKEYSYM_H
#include <X11/Sunkeysym.h>
#endif

#include <X11/Xutil.h>
#include <ctype.h>

#include <xutf8.h>

#include <data.h>
#include <fontutils.h>

#define MAP(from, to) case from: return(to)

#define KEYSYM_FMT "0x%04lX"	/* simplify matching <X11/keysymdef.h> */

/*                       0123456789 abc def0123456789abcdef0123456789abcdef0123456789abcd */
static char *kypd_num = " XXXXXXXX\tXXX\rXXXxxxxXXXXXXXXXXXXXXXXXXXXX*+,-./0123456789XXX=";

/*                       0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd */
static char *kypd_apl = " ABCDEFGHIJKLMNOPQRSTUVWXYZ??????abcdefghijklmnopqrstuvwxyzXXX";

static char *curfinal = "HDACB  FE";

static int decfuncvalue(KeySym keycode);
static int sunfuncvalue(KeySym keycode);
#if OPT_HP_FUNC_KEYS
static int hpfuncvalue(KeySym keycode);
#endif
#if OPT_SCO_FUNC_KEYS
static int scofuncvalue(KeySym keycode);
#endif

#if OPT_TRACE
static char *
ModifierName(unsigned modifier)
{
    char *s = "";
    if (modifier & ShiftMask)
	s = " Shift";
    else if (modifier & LockMask)
	s = " Lock";
    else if (modifier & ControlMask)
	s = " Control";
    else if (modifier & Mod1Mask)
	s = " Mod1";
    else if (modifier & Mod2Mask)
	s = " Mod2";
    else if (modifier & Mod3Mask)
	s = " Mod3";
    else if (modifier & Mod4Mask)
	s = " Mod4";
    else if (modifier & Mod5Mask)
	s = " Mod5";
    return s;
}
#endif

static void
AdjustAfterInput(TScreen * screen)
{
    if (screen->scrollkey && screen->topline != 0)
	WindowScroll(screen, 0);
    if (screen->marginbell) {
	int col = screen->max_col - screen->nmarginbell;
	if (screen->bellarmed >= 0) {
	    if (screen->bellarmed == screen->cur_row) {
		if (screen->cur_col >= col) {
		    Bell(XkbBI_MarginBell, 0);
		    screen->bellarmed = -1;
		}
	    } else
		screen->bellarmed =
		    screen->cur_col < col ? screen->cur_row : -1;
	} else if (screen->cur_col < col)
	    screen->bellarmed = screen->cur_row;
    }
}

/* returns true if the key is on the editing keypad */
static Bool
IsEditFunctionKey(KeySym keysym)
{
    switch (keysym) {
    case XK_Prior:
    case XK_Next:
    case XK_Insert:
    case XK_Find:
    case XK_Select:
#ifdef DXK_Remove
    case DXK_Remove:
#endif
#ifdef XK_KP_Delete
    case XK_KP_Delete:
    case XK_KP_Insert:
#endif
#ifdef XK_ISO_Left_Tab
    case XK_ISO_Left_Tab:
#endif
	return True;
    default:
	return False;
    }
}

#if OPT_SUNPC_KBD
/*
 * If we have told xterm that our keyboard is really a Sun/PC keyboard, this is
 * enough to make a reasonable approximation to DEC vt220 numeric and editing
 * keypads.
 */
static KeySym
TranslateFromSUNPC(KeySym keysym)
{
    /* *INDENT-OFF* */
    static struct {
	    KeySym before, after;
    } table[] = {
#ifdef DXK_Remove
	{ XK_Delete,       DXK_Remove },
#endif
	{ XK_Home,         XK_Find },
	{ XK_End,          XK_Select },
#ifdef XK_KP_Home
	{ XK_Delete,       XK_KP_Decimal },
	{ XK_KP_Delete,    XK_KP_Decimal },
	{ XK_KP_Insert,    XK_KP_0 },
	{ XK_KP_End,       XK_KP_1 },
	{ XK_KP_Down,      XK_KP_2 },
	{ XK_KP_Next,      XK_KP_3 },
	{ XK_KP_Left,      XK_KP_4 },
	{ XK_KP_Begin,     XK_KP_5 },
	{ XK_KP_Right,     XK_KP_6 },
	{ XK_KP_Home,      XK_KP_7 },
	{ XK_KP_Up,        XK_KP_8 },
	{ XK_KP_Prior,     XK_KP_9 },
#endif
    };
    /* *INDENT-ON* */

    unsigned n;

    for (n = 0; n < sizeof(table) / sizeof(table[0]); n++) {
	if (table[n].before == keysym) {
	    TRACE(("...Input keypad before was " KEYSYM_FMT "\n", keysym));
	    keysym = table[n].after;
	    TRACE(("...Input keypad changed to " KEYSYM_FMT "\n", keysym));
	    break;
	}
    }
    return keysym;
}
#endif

/*
 * Modifiers other than shift, control and numlock should be reserved for the
 * user.  We use the first two explicitly to support VT220 keyboard, and the
 * third is used implicitly in keyboard configuration to make the keypad work.
 */
#define isModified(event) \
    (event->state & \
	(Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask ))

#define VT52_KEYPAD \
	if_OPT_VT52_MODE(screen,{ \
		reply.a_type = ESC; \
		reply.a_pintro = '?'; \
		})

#define VT52_CURSOR_KEYS \
	if_OPT_VT52_MODE(screen,{ \
		reply.a_type = ESC; \
		})

#define APPEND_PARM(number) \
	    reply.a_param[(int) reply.a_nparam] = number, \
	    reply.a_nparam += 1

#if OPT_MOD_FKEYS
#define MODIFIER_PARM \
	if (modify_parm > 1) APPEND_PARM(modify_parm)
#else
#define MODIFIER_PARM		/*nothing */
#endif

/*
 * Determine if we use the \E[3~ sequence for Delete, or the legacy ^?.  We
 * maintain the delete_is_del value as 3 states:  unspecified(2), true and
 * false.  If unspecified, it is handled differently according to whether the
 * legacy keyboard support is enabled, or if xterm emulates a VT220.
 *
 * Once the user (or application) has specified delete_is_del via resource
 * setting, popup menu or escape sequence, it overrides the keyboard type
 * rather than the reverse.
 */
Bool
xtermDeleteIsDEL(void)
{
    TScreen *screen = &term->screen;
    Bool result = True;

    if (term->keyboard.type == keyboardIsDefault
	|| term->keyboard.type == keyboardIsVT220)
	result = (screen->delete_is_del == True);

    if (term->keyboard.type == keyboardIsLegacy)
	result = (screen->delete_is_del != False);

    TRACE(("xtermDeleteIsDEL(%d/%d) = %d\n",
	   term->keyboard.type,
	   screen->delete_is_del,
	   result));

    return result;
}

void
Input(TKeyboard * keyboard,
      TScreen * screen,
      XKeyEvent * event,
      Bool eightbit)
{

#define STRBUFSIZE 500

    char strbuf[STRBUFSIZE];
    Char *string;
    int key = False;
    int pty = screen->respond;
    int nbytes;
    KeySym keysym = 0;
    ANSI reply;
    int dec_code;
    short modify_parm = 0;
    int keypad_mode = ((keyboard->flags & MODE_DECKPAM) != 0);

    /* Ignore characters typed at the keyboard */
    if (keyboard->flags & MODE_KAM)
	return;

#if OPT_TCAP_QUERY
    if (screen->tc_query >= 0) {
	keysym = screen->tc_query;
	if (keysym != XK_BackSpace) {
	    nbytes = 0;
	    strbuf[0] = 0;
	} else {
	    nbytes = 1;
	    strbuf[0] = 8;
	}
    } else
#endif
#if OPT_I18N_SUPPORT
    if (screen->xic) {
	Status status_return;
#if OPT_WIDE_CHARS
	if (screen->utf8_mode)
	    nbytes = Xutf8LookupString(screen->xic, event,
				       strbuf, sizeof(strbuf),
				       &keysym, &status_return);
	else
#endif
	    nbytes = XmbLookupString(screen->xic, event,
				     strbuf, sizeof(strbuf),
				     &keysym, &status_return);
    } else
#endif
    {
	static XComposeStatus compose_status =
	{NULL, 0};
	nbytes = XLookupString(event, strbuf, sizeof(strbuf),
			       &keysym, &compose_status);
    }

    string = (Char *) & strbuf[0];
    reply.a_pintro = 0;
    reply.a_final = 0;
    reply.a_nparam = 0;
    reply.a_inters = 0;

    TRACE(("Input keysym "
	   KEYSYM_FMT
	   ", %d:'%.*s'%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
	   keysym,
	   nbytes,
	   nbytes > 0 ? nbytes : 1,
	   nbytes > 0 ? strbuf : "",
	   ModifierName(event->state & ShiftMask),
	   ModifierName(event->state & LockMask),
	   ModifierName(event->state & ControlMask),
	   ModifierName(event->state & Mod1Mask),
	   ModifierName(event->state & Mod2Mask),
	   ModifierName(event->state & Mod3Mask),
	   ModifierName(event->state & Mod4Mask),
	   ModifierName(event->state & Mod5Mask),
	   eightbit ? " 8bit" : " 7bit",
	   IsKeypadKey(keysym) ? " KeypadKey" : "",
	   IsCursorKey(keysym) ? " CursorKey" : "",
	   IsPFKey(keysym) ? " PFKey" : "",
	   IsFunctionKey(keysym) ? " FKey" : "",
	   IsMiscFunctionKey(keysym) ? " MiscFKey" : "",
	   IsEditFunctionKey(keysym) ? " EditFkey" : ""));

#if OPT_SUNPC_KBD
    /*
     * DEC keyboards don't have keypad(+), but do have keypad(,) instead.
     * Other (Sun, PC) keyboards commonly have keypad(+), but no keypad(,)
     * - it's a pain for users to work around.
     */
    if (keyboard->type == keyboardIsVT220
	&& (event->state & ShiftMask) == 0) {
	if (keysym == XK_KP_Add) {
	    keysym = XK_KP_Separator;
	    TRACE(("...Input keypad(+), change keysym to "
		   KEYSYM_FMT
		   "\n",
		   keysym));
	}
	if ((event->state & ControlMask) != 0
	    && keysym == XK_KP_Separator) {
	    keysym = XK_KP_Subtract;
	    TRACE(("...Input control/keypad(,), change keysym to "
		   KEYSYM_FMT
		   "\n",
		   keysym));
	}
    }
#endif

    /*
     * The keyboard tables may give us different keypad codes according to
     * whether NumLock is pressed.  Use this check to simplify the process
     * of determining whether we generate an escape sequence for a keypad
     * key, or force it to the value kypd_num[].  There is no fixed
     * modifier for this feature, so we assume that it is the one assigned
     * to the NumLock key.
     *
     * This check used to try to return the contents of strbuf, but that
     * does not work properly when a control modifier is given (trash is
     * returned in the buffer in some cases -- perhaps an X bug).
     */
#if OPT_NUM_LOCK
    if (nbytes == 1
	&& IsKeypadKey(keysym)
	&& term->misc.real_NumLock
	&& (term->misc.num_lock & event->state) != 0) {
	keypad_mode = 0;
	TRACE(("...Input num_lock, force keypad_mode off\n"));
    }
#endif

#if OPT_MOD_FKEYS
    /*
     * If we are in the normal (possibly Sun/PC) keyboard state, allow
     * modifiers to add a parameter to the function-key control sequences.
     */
    if (event->state != 0
	&& !(IsKeypadKey(keysym) && keypad_mode)
#if OPT_SUNPC_KBD
	&& keyboard->type != keyboardIsVT220
#endif
#if OPT_VT52_MODE
	&& screen->vtXX_level != 0
#endif
	) {
/*
* Modifier codes:
*       None                  1
*       Shift                 2 = 1(None)+1(Shift)
*       Alt                   3 = 1(None)+2(Alt)
*       Alt+Shift             4 = 1(None)+1(Shift)+2(Alt)
*       Ctrl                  5 = 1(None)+4(Ctrl)
*       Ctrl+Shift            6 = 1(None)+1(Shift)+4(Ctrl)
*       Ctrl+Alt              7 = 1(None)+2(Alt)+4(Ctrl)
*       Ctrl+Alt+Shift        8 = 1(None)+1(Shift)+2(Alt)+4(Ctrl)
*       Meta                  9 = 1(None)+8(Meta)
*       Meta+Shift           10 = 1(None)+8(Meta)+1(Shift)
*       Meta+Alt             11 = 1(None)+8(Meta)+2(Alt)
*       Meta+Alt+Shift       12 = 1(None)+8(Meta)+1(Shift)+2(Alt)
*       Meta+Ctrl            13 = 1(None)+8(Meta)+4(Ctrl)
*       Meta+Ctrl+Shift      14 = 1(None)+8(Meta)+1(Shift)+4(Ctrl)
*       Meta+Ctrl+Alt        15 = 1(None)+8(Meta)+2(Alt)+4(Ctrl)
*       Meta+Ctrl+Alt+Shift  16 = 1(None)+8(Meta)+1(Shift)+2(Alt)+4(Ctrl)
*/
#define	UNMOD	1
#define	SHIFT	1
#define	ALT	2
#define	CTRL	4
#define	META	8
	modify_parm = UNMOD;
	if (event->state & ShiftMask) {
	    modify_parm += SHIFT;
	}
	if (event->state & ControlMask) {
	    modify_parm += CTRL;
	}
#if OPT_NUM_LOCK
	if ((term->misc.alwaysUseMods
	     || term->misc.real_NumLock)
	    && ((event->state & term->misc.alt_left) != 0
		|| (event->state & term->misc.alt_right)) != 0) {
	    modify_parm += ALT;
	}
	if (term->misc.alwaysUseMods
	    && ((event->state & term->misc.meta_left) != 0
		|| (event->state & term->misc.meta_right)) != 0) {
	    modify_parm += META;
	}
#endif
	TRACE(("...ModifierParm %d\n", modify_parm));
    }
#endif /* OPT_MOD_FKEYS */

    /* VT300 & up: backarrow toggle */
    if ((nbytes == 1)
	&& (((keyboard->flags & MODE_DECBKM) == 0)
	    ^ ((event->state & ControlMask) != 0))
	&& (keysym == XK_BackSpace)) {
	strbuf[0] = '\177';
	TRACE(("...Input backarrow changed to %d\n", *strbuf));
    }
#if OPT_SUNPC_KBD
    /* make an DEC editing-keypad from a Sun or PC editing-keypad */
    if (keyboard->type == keyboardIsVT220
	&& (keysym != XK_Delete || !xtermDeleteIsDEL()))
	keysym = TranslateFromSUNPC(keysym);
    else
#endif
    {
#ifdef XK_KP_Home
	if (keysym >= XK_KP_Home && keysym <= XK_KP_Begin) {
	    TRACE(("...Input keypad before was " KEYSYM_FMT "\n", keysym));
	    keysym += XK_Home - XK_KP_Home;
	    TRACE(("...Input keypad changed to " KEYSYM_FMT "\n", keysym));
	}
#endif
    }

#if OPT_HP_FUNC_KEYS
    if (keyboard->type == keyboardIsHP
	&& (reply.a_final = hpfuncvalue(keysym)) != 0) {
	reply.a_type = ESC;
	MODIFIER_PARM;
	unparseseq(&reply, pty);
    } else
#endif
#if OPT_SCO_FUNC_KEYS
	if (keyboard->type == keyboardIsSCO
	    && (reply.a_final = scofuncvalue(keysym)) != 0) {
	reply.a_type = CSI;
	MODIFIER_PARM;
	unparseseq(&reply, pty);
    } else
#endif
    if (IsPFKey(keysym)) {
	reply.a_type = SS3;
	reply.a_final = keysym - XK_KP_F1 + 'P';
	VT52_CURSOR_KEYS;
	MODIFIER_PARM;
	unparseseq(&reply, pty);
	key = True;
#if 0				/* OPT_SUNPC_KBD should suppress - but only for vt220 compatibility */
    } else if (keyboard->type == keyboardIsVT220
	       && screen->vtXX_level <= 1
	       && IsEditFunctionKey(keysym)) {
	key = False;		/* ignore editing-keypad in vt100 mode */
#endif
    } else if (IsCursorKey(keysym) &&
	       keysym != XK_Prior && keysym != XK_Next) {
	if (keyboard->flags & MODE_DECCKM) {
	    reply.a_type = SS3;
	} else {
	    reply.a_type = CSI;
	}
#if OPT_MOD_FKEYS
	if (modify_parm > 1) {
	    if (keyboard->modify_cursor_keys)
		reply.a_type = CSI;	/* SS3 should not have params */
	    if (keyboard->modify_cursor_keys > 1)
		APPEND_PARM(1);	/* force modifier to 2nd param */
	    if (keyboard->modify_cursor_keys > 2)
		reply.a_pintro = '>';	/* mark this as "private" */
	}
#endif
	reply.a_final = curfinal[keysym - XK_Home];
	VT52_CURSOR_KEYS;
	MODIFIER_PARM;
	unparseseq(&reply, pty);
	key = True;
    } else if (IsFunctionKey(keysym)
	       || IsMiscFunctionKey(keysym)
	       || IsEditFunctionKey(keysym)
#ifdef SunXK_F36
	       || keysym == SunXK_F36
	       || keysym == SunXK_F37
#endif
	       || (keysym == XK_Delete
		   && ((modify_parm > 1)
		       || !xtermDeleteIsDEL()))) {
#if OPT_SUNPC_KBD
	if (keyboard->type == keyboardIsVT220) {
	    if ((event->state & ControlMask)
		&& (keysym >= XK_F1 && keysym <= XK_F12))
		keysym += term->misc.ctrl_fkeys;
	}
#endif

	dec_code = decfuncvalue(keysym);
	if ((event->state & ShiftMask)
#if OPT_SUNPC_KBD
	    && keyboard->type == keyboardIsVT220
#endif
	    && ((string = (Char *) udk_lookup(dec_code, &nbytes)) != 0)) {
	    while (nbytes-- > 0)
		unparseputc(*string++, pty);
	}
#if OPT_VT52_MODE
	/*
	 * Interpret F1-F4 as PF1-PF4 for VT52, VT100
	 */
	else if (keyboard->type != keyboardIsSun
		 && keyboard->type != keyboardIsLegacy
		 && (dec_code >= 11 && dec_code <= 14)) {
	    reply.a_type = SS3;
	    VT52_CURSOR_KEYS;
	    reply.a_final = A2E(dec_code - 11 + E2A('P'));
	    MODIFIER_PARM;
	    unparseseq(&reply, pty);
	}
#endif
	else {
	    reply.a_type = CSI;
	    reply.a_nparam = 1;
	    reply.a_final = 0;
	    MODIFIER_PARM;
	    if (keyboard->type == keyboardIsSun) {
		reply.a_param[0] = sunfuncvalue(keysym);
		reply.a_final = 'z';
#ifdef XK_ISO_Left_Tab
	    } else if (keysym == XK_ISO_Left_Tab) {
		reply.a_nparam = 0;
		reply.a_final = 'Z';
#endif
	    } else {
		reply.a_param[0] = dec_code;
		reply.a_final = '~';
	    }
	    if (reply.a_final != 0
		&& (reply.a_nparam == 0 || reply.a_param[0] >= 0))
		unparseseq(&reply, pty);
	}
	key = True;
    } else if (IsKeypadKey(keysym)) {
	if (keypad_mode) {
	    reply.a_type = SS3;
	    reply.a_final = kypd_apl[keysym - XK_KP_Space];
	    VT52_KEYPAD;
	    MODIFIER_PARM;
	    unparseseq(&reply, pty);
	} else {
	    unparseputc(kypd_num[keysym - XK_KP_Space], pty);
	}
	key = True;
    } else if (nbytes > 0) {
#if OPT_TEK4014
	if (screen->TekGIN) {
	    TekEnqMouse(*string++);
	    TekGINoff();
	    nbytes--;
	}
#endif
	if (nbytes == 1) {
#if OPT_NUM_LOCK
	    /*
	     * Send ESC if we have a META modifier and
	     * metaSendsEcape is true.  Like eightBitInput, except
	     * that it is not associated with terminal settings.
	     */
	    if (screen->meta_sends_esc
		&& ((event->state & term->misc.meta_left) != 0
		    || (event->state & term->misc.meta_right) != 0)) {
		TRACE(("...input-char is modified by META\n"));
		eightbit = False;
		unparseputc(ESC, pty);	/* escape */
	    }
#endif
	    if (eightbit && screen->input_eight_bits) {
		IChar ch = CharOf(*string);
		if (ch < 128) {
		    *string |= 0x80;
		    TRACE(("...input shift from %d to %d (%#x to %#x)\n",
			   ch, CharOf(*string),
			   ch, CharOf(*string)));
#if OPT_WIDE_CHARS
		    if (screen->utf8_mode) {
			/*
			 * We could interpret the incoming code as "in the
			 * current locale", but it's simpler to treat it as
			 * a Unicode value to translate to UTF-8.
			 */
			ch = CharOf(*string);
			nbytes = 2;
			string[0] = 0xc0 | ((ch >> 6) & 0x3);
			string[1] = 0x80 | (ch & 0x3f);
			TRACE(("...encoded %#x in UTF-8 as %#x,%#x\n",
			       ch, string[0], string[1]));
		    }
#endif
		}
		eightbit = False;
	    }
	    /* VT220 & up: National Replacement Characters */
	    if ((term->flags & NATIONAL) != 0) {
		int cmp = xtermCharSetIn(CharOf(*string),
					 screen->keyboard_dialect[0]);
		TRACE(("...input NRC %d, %s %d\n",
		       CharOf(*string),
		       (CharOf(*string) == cmp)
		       ? "unchanged"
		       : "changed to",
		       CharOf(cmp)));
		*string = cmp;
	    } else if (eightbit) {
		unparseputc(ESC, pty);	/* escape */
	    } else if (*string == '?'
		       && (event->state & ControlMask) != 0) {
		*string = 127;
	    }
	}
	while (nbytes-- > 0)
	    unparseputc(*string++, pty);
	key = True;
    }
    if (key && !TEK4014_ACTIVE(screen))
	AdjustAfterInput(screen);
#ifdef ENABLE_PRINT
    if (keysym == XK_F2)
	TekPrint();
#endif
    return;
}

void
StringInput(TScreen * screen, Char * string, size_t nbytes)
{
    int pty = screen->respond;

    TRACE(("InputString (%s,%d)\n",
	   visibleChars(PAIRED_CHARS(string, 0), nbytes),
	   nbytes));
#if OPT_TEK4014
    if (nbytes && screen->TekGIN) {
	TekEnqMouse(*string++);
	TekGINoff();
	nbytes--;
    }
#endif
    while (nbytes-- != 0)
	unparseputc(*string++, pty);
    if (!TEK4014_ACTIVE(screen))
	AdjustAfterInput(screen);
}

/* These definitions are DEC-style (e.g., vt320) */
static int
decfuncvalue(KeySym keycode)
{
    switch (keycode) {
	MAP(XK_F1, 11);
	MAP(XK_F2, 12);
	MAP(XK_F3, 13);
	MAP(XK_F4, 14);
	MAP(XK_F5, 15);
	MAP(XK_F6, 17);
	MAP(XK_F7, 18);
	MAP(XK_F8, 19);
	MAP(XK_F9, 20);
	MAP(XK_F10, 21);
	MAP(XK_F11, 23);
	MAP(XK_F12, 24);
	MAP(XK_F13, 25);
	MAP(XK_F14, 26);
	MAP(XK_F15, 28);
	MAP(XK_Help, 28);
	MAP(XK_F16, 29);
	MAP(XK_Menu, 29);
	MAP(XK_F17, 31);
	MAP(XK_F18, 32);
	MAP(XK_F19, 33);
	MAP(XK_F20, 34);
#if defined(XK_F21)
	MAP(XK_F21, 42);
	MAP(XK_F22, 43);
	MAP(XK_F23, 44);
	MAP(XK_F24, 45);
	MAP(XK_F25, 46);
	MAP(XK_F26, 47);
	MAP(XK_F27, 48);
	MAP(XK_F28, 49);
	MAP(XK_F29, 50);
	MAP(XK_F30, 51);
	MAP(XK_F31, 52);
	MAP(XK_F32, 53);
	MAP(XK_F33, 54);
	MAP(XK_F34, 55);
	MAP(XK_F35, 56);
#endif
#ifdef SunXK_F36
	MAP(SunXK_F36, 57);
	MAP(SunXK_F37, 58);
#endif
	MAP(XK_Find, 1);
	MAP(XK_Insert, 2);
	MAP(XK_Delete, 3);
#ifdef XK_KP_Insert
	MAP(XK_KP_Insert, 2);
	MAP(XK_KP_Delete, 3);
#endif
#ifdef DXK_Remove
	MAP(DXK_Remove, 3);
#endif
	MAP(XK_Select, 4);
	MAP(XK_Prior, 5);
	MAP(XK_Next, 6);
#ifdef XK_ISO_Left_Tab
	MAP(XK_ISO_Left_Tab, 'Z');
#endif
    default:
	return (-1);
    }
}

#if OPT_HP_FUNC_KEYS
static int
hpfuncvalue(KeySym keycode)
{
    switch (keycode) {
	MAP(XK_Up, 'A');
	MAP(XK_Down, 'B');
	MAP(XK_Right, 'C');
	MAP(XK_Left, 'D');
	MAP(XK_End, 'F');
	MAP(XK_Clear, 'J');
	MAP(XK_Delete, 'P');
	MAP(XK_Insert, 'Q');
	MAP(XK_Next, 'S');
	MAP(XK_Prior, 'T');
	MAP(XK_Home, 'h');
	MAP(XK_F1, 'p');
	MAP(XK_F2, 'q');
	MAP(XK_F3, 'r');
	MAP(XK_F4, 's');
	MAP(XK_F5, 't');
	MAP(XK_F6, 'u');
	MAP(XK_F7, 'v');
	MAP(XK_F8, 'w');
#ifdef XK_KP_Insert
	MAP(XK_KP_Delete, 'P');
	MAP(XK_KP_Insert, 'Q');
#endif
#ifdef DXK_Remove
	MAP(DXK_Remove, 'P');
#endif
	MAP(XK_Select, 'F');
	MAP(XK_Find, 'h');
    default:
	return 0;
    }
}
#endif

#if OPT_SCO_FUNC_KEYS
static int
scofuncvalue(KeySym keycode)
{
    switch (keycode) {
	MAP(XK_Up, 'A');
	MAP(XK_Down, 'B');
	MAP(XK_Right, 'C');
	MAP(XK_Left, 'D');
	MAP(XK_End, 'F');
	MAP(XK_Insert, 'L');
	MAP(XK_Next, 'G');
	MAP(XK_Prior, 'I');
	MAP(XK_Home, 'H');
	MAP(XK_F1, 'M');
	MAP(XK_F2, 'N');
	MAP(XK_F3, 'O');
	MAP(XK_F4, 'P');
	MAP(XK_F5, 'Q');
	MAP(XK_F6, 'R');
	MAP(XK_F7, 'S');
	MAP(XK_F8, 'T');
	MAP(XK_F9, 'U');
	MAP(XK_F10, 'V');
	MAP(XK_F11, 'W');
	MAP(XK_F12, 'X');
	MAP(XK_F13, 'Y');
	MAP(XK_F15, 'a');
	MAP(XK_F16, 'b');
	MAP(XK_F17, 'c');
	MAP(XK_F18, 'd');
	MAP(XK_F19, 'e');
	MAP(XK_F20, 'f');
#if defined(XK_F21)
	MAP(XK_F21, 'g');
	MAP(XK_F22, 'h');
	MAP(XK_F23, 'i');
	MAP(XK_F24, 'j');
	MAP(XK_F25, 'k');
	MAP(XK_F26, 'l');
	MAP(XK_F27, 'm');
	MAP(XK_F28, 'n');
	MAP(XK_F29, 'o');
	MAP(XK_F30, 'p');
	MAP(XK_F31, 'q');
	MAP(XK_F32, 'r');
	MAP(XK_F33, 's');
	MAP(XK_F34, 't');
	MAP(XK_F35, 'u');
#endif
#ifdef XK_KP_Insert
	MAP(XK_KP_Insert, 'L');
#endif
    default:
	return 0;
    }
}
#endif

static int
sunfuncvalue(KeySym keycode)
{
    switch (keycode) {
	MAP(XK_F1, 224);
	MAP(XK_F2, 225);
	MAP(XK_F3, 226);
	MAP(XK_F4, 227);
	MAP(XK_F5, 228);
	MAP(XK_F6, 229);
	MAP(XK_F7, 230);
	MAP(XK_F8, 231);
	MAP(XK_F9, 232);
	MAP(XK_F10, 233);
	MAP(XK_F11, 192);
	MAP(XK_F12, 193);
	MAP(XK_F13, 194);
	MAP(XK_F14, 195);	/* kund */
	MAP(XK_F15, 196);
	MAP(XK_Help, 196);	/* khlp */
	MAP(XK_F16, 197);	/* kcpy */
	MAP(XK_Menu, 197);
	MAP(XK_F17, 198);
	MAP(XK_F18, 199);
	MAP(XK_F19, 200);	/* kfnd */
	MAP(XK_F20, 201);

	MAP(XK_R1, 208);	/* kf31 */
	MAP(XK_R2, 209);	/* kf32 */
	MAP(XK_R3, 210);	/* kf33 */
	MAP(XK_R4, 211);	/* kf34 */
	MAP(XK_R5, 212);	/* kf35 */
	MAP(XK_R6, 213);	/* kf36 */
	MAP(XK_R7, 214);	/* kf37 */
	MAP(XK_R8, 215);	/* kf38 */
	MAP(XK_R9, 216);	/* kf39=kpp */
	MAP(XK_R10, 217);	/* kf40 */
	MAP(XK_R11, 218);	/* kf41=kb2 */
	MAP(XK_R12, 219);	/* kf42 */
	MAP(XK_R13, 220);	/* kf43=kend */
	MAP(XK_R14, 221);	/* kf44 */
	MAP(XK_R15, 222);	/* kf45 */
#ifdef SunXK_F36
	MAP(SunXK_F36, 234);
	MAP(SunXK_F37, 235);
#endif

	MAP(XK_Find, 1);
	MAP(XK_Insert, 2);	/* kich1 */
	MAP(XK_Delete, 3);
#ifdef XK_KP_Insert
	MAP(XK_KP_Insert, 2);
	MAP(XK_KP_Delete, 3);
#endif
#ifdef DXK_Remove
	MAP(DXK_Remove, 3);
#endif
	MAP(XK_Select, 4);
	MAP(XK_Prior, 5);
	MAP(XK_Next, 6);
    default:
	return (-1);
    }
}

#if OPT_NUM_LOCK
/*
 * Note that this can only retrieve translations that are given as resource
 * values; the default translations in charproc.c for example are not
 * retrievable by any interface to X.
 *
 * Also:  We can retrieve only the most-specified translation resource.  For
 * example, if the resource file specifies both "*translations" and
 * "XTerm*translations", we see only the latter.
 */
static Bool
TranslationsUseKeyword(Widget w, const char *keyword)
{
    static String data;
    static XtResource key_resources[] =
    {
	{XtNtranslations, XtCTranslations, XtRString,
	 sizeof(data), 0, XtRString, (XtPointer) NULL}
    };
    Bool result = False;

    XtGetSubresources(w,
		      (XtPointer) &data,
		      "vt100",
		      "VT100",
		      key_resources,
		      XtNumber(key_resources),
		      NULL,
		      (Cardinal) 0);

    if (data != 0) {
	char *p = data;
	int state = 0;
	int now = ' ', prv;
	TRACE(("TranslationsUseKeyword(%p):%s\n", w, p));
	while (*p != 0) {
	    prv = now;
	    now = char2lower(*p++);
	    if (now == ':'
		|| now == '!') {
		state = -1;
	    } else if (now == '\n') {
		state = 0;
	    } else if (state >= 0) {
		if (isgraph(now)
		    && now == keyword[state]) {
		    if ((state != 0
			 || !isalnum(prv))
			&& ((keyword[++state] == 0)
			    && !isalnum(CharOf(*p)))) {
			result = True;
			break;
		    }
		} else {
		    state = 0;
		}
	    }
	}
    }
    TRACE(("TranslationsUseKeyword(%p, %s) = %d\n", w, keyword, result));
    return result;
}

#define SaveMask(name)	term->misc.name |= mask;\
			TRACE(("%s mask %#lx is%s modifier\n", \
				#name, \
				term->misc.name, \
				ModifierName(term->misc.name)));
/*
 * Determine which modifier mask (if any) applies to the Num_Lock keysym.
 *
 * Also, determine which modifiers are associated with the ALT keys, so we can
 * send that information as a parameter for special keys in Sun/PC keyboard
 * mode.  However, if the ALT modifier is used in translations, we do not want
 * to confuse things by sending the parameter.
 */
void
VTInitModifiers(void)
{
    int i, j, k;
    Display *dpy = XtDisplay(term);
    XModifierKeymap *keymap = XGetModifierMapping(dpy);
    unsigned long mask;
    int min_keycode, max_keycode, keysyms_per_keycode = 0;

    if (keymap != 0) {
	KeySym *theMap;

	TRACE(("VTInitModifiers\n"));

	XDisplayKeycodes(dpy, &min_keycode, &max_keycode);
	theMap = XGetKeyboardMapping(dpy,
				     min_keycode,
				     (max_keycode - min_keycode + 1),
				     &keysyms_per_keycode);

	if (theMap != 0) {
	    for (i = k = 0, mask = 1; i < 8; i++, mask <<= 1) {
		for (j = 0; j < keymap->max_keypermod; j++) {
		    KeyCode code = keymap->modifiermap[k];
		    if (code != 0) {
			KeySym keysym;
			int l = 0;
			do {
			    keysym = XKeycodeToKeysym(dpy, code, l);
			    l++;
			} while (!keysym && l < keysyms_per_keycode);
			if (keysym == XK_Num_Lock) {
			    SaveMask(num_lock);
			} else if (keysym == XK_Alt_L) {
			    SaveMask(alt_left);
			} else if (keysym == XK_Alt_R) {
			    SaveMask(alt_right);
			} else if (keysym == XK_Meta_L) {
			    SaveMask(meta_left);
			} else if (keysym == XK_Meta_R) {
			    SaveMask(meta_right);
			}
		    }
		    k++;
		}
	    }
	    XFree(theMap);
	}

	/* Don't disable any mods if "alwaysUseMods" is true. */
	if (!term->misc.alwaysUseMods) {
	    /*
	     * If the Alt modifier is used in translations, we would rather not
	     * use it to modify function-keys when NumLock is active.
	     */
	    if ((term->misc.alt_left != 0
		 || term->misc.alt_right != 0)
		&& (TranslationsUseKeyword(toplevel, "alt")
		    || TranslationsUseKeyword((Widget) term, "alt"))) {
		TRACE(("ALT is used as a modifier in translations (ignore mask)\n"));
		term->misc.alt_left = 0;
		term->misc.alt_right = 0;
	    }

	    /*
	     * If the Meta modifier is used in translations, we would rather not
	     * use it to modify function-keys.
	     */
	    if ((term->misc.meta_left != 0
		 || term->misc.meta_right != 0)
		&& (TranslationsUseKeyword(toplevel, "meta")
		    || TranslationsUseKeyword((Widget) term, "meta"))) {
		TRACE(("META is used as a modifier in translations\n"));
		term->misc.meta_trans = True;
	    }
	}

	XFreeModifiermap(keymap);
    }
}
#endif /* OPT_NUM_LOCK */

#if OPT_TCAP_QUERY
static int
hex2int(int c)
{
    if (c >= '0' && c <= '9')
	return c - '0';
    if (c >= 'a' && c <= 'f')
	return c - 'a' + 10;
    if (c >= 'A' && c <= 'F')
	return c - 'A' + 10;
    return -1;
}

/*
 * Parse the termcap/terminfo name from the string, returning a positive number
 * (the keysym) if found, otherwise -1.  Update the string pointer.
 * Returns the (shift, control) state in *state.
 */
int
xtermcapKeycode(char **params, unsigned *state)
{
    /* *INDENT-OFF* */
#define DATA(tc,ti,x,y) { tc, ti, x, y }
    static struct {
	char *tc;
	char *ti;
	int code;
	unsigned state;
    } table[] = {
	/*	tcap	terminfo	keycode		masks */
	DATA(	"#2",	"kHOM",		XK_Home,	ShiftMask	),
	DATA(	"#4",	"kLFT",		XK_Left,	ShiftMask	),
	DATA(	"%1",	"khlp",		XK_Help,	0		),
	DATA(	"%i",	"kRIT",		XK_Right,	ShiftMask	),
	DATA(	"*6",	"kslt",		XK_Select,	0		),
	DATA(	"*7",	"kEND",		XK_End,		ShiftMask	),
	DATA(	"@0",	"kfnd",		XK_Find,	0		),
	DATA(	"@7",	"kend",		XK_End,		0		),
	DATA(	"F1",	"kf11",		XK_F11,		0		),
	DATA(	"F2",	"kf12",		XK_F12,		0		),
	DATA(	"F3",	"kf13",		XK_F13,		0		),
	DATA(	"F4",	"kf14",		XK_F14,		0		),
	DATA(	"F5",	"kf15",		XK_F15,		0		),
	DATA(	"F6",	"kf16",		XK_F16,		0		),
	DATA(	"F7",	"kf17",		XK_F17,		0		),
	DATA(	"F8",	"kf18",		XK_F18,		0		),
	DATA(	"F9",	"kf19",		XK_F19,		0		),
	DATA(	"FA",	"kf20",		XK_F20,		0		),
	DATA(	"FB",	"kf21",		XK_F21,		0		),
	DATA(	"FC",	"kf22",		XK_F22,		0		),
	DATA(	"FD",	"kf23",		XK_F23,		0		),
	DATA(	"FE",	"kf24",		XK_F24,		0		),
	DATA(	"FF",	"kf25",		XK_F25,		0		),
	DATA(	"FG",	"kf26",		XK_F26,		0		),
	DATA(	"FH",	"kf27",		XK_F27,		0		),
	DATA(	"FI",	"kf28",		XK_F28,		0		),
	DATA(	"FJ",	"kf29",		XK_F29,		0		),
	DATA(	"FK",	"kf30",		XK_F30,		0		),
	DATA(	"FL",	"kf31",		XK_F31,		0		),
	DATA(	"FM",	"kf32",		XK_F32,		0		),
	DATA(	"FN",	"kf33",		XK_F33,		0		),
	DATA(	"FO",	"kf34",		XK_F34,		0		),
	DATA(	"FP",	"kf35",		XK_F35,		0		),
#ifdef SunXK_F36
	DATA(	"FQ",	"kf36",		SunXK_F36,	0		),
	DATA(	"FR",	"kf37",		SunXK_F37,	0		),
#endif
	DATA(	"K1",	"ka1",		XK_KP_Home,	0		),
	DATA(	"K4",	"kc1",		XK_KP_End,	0		),
	DATA(	"k1",	"kf1",		XK_F1,		0		),
	DATA(	"k2",	"kf2",		XK_F2,		0		),
	DATA(	"k3",	"kf3",		XK_F3,		0		),
	DATA(	"k4",	"kf4",		XK_F4,		0		),
	DATA(	"k5",	"kf5",		XK_F5,		0		),
	DATA(	"k6",	"kf6",		XK_F6,		0		),
	DATA(	"k7",	"kf7",		XK_F7,		0		),
	DATA(	"k8",	"kf8",		XK_F8,		0		),
	DATA(	"k9",	"kf9",		XK_F9,		0		),
	DATA(	"k;",	"kf10",		XK_F10,		0		),
#ifdef XK_ISO_Left_Tab
	DATA(	"kB",	"kcbt",		XK_ISO_Left_Tab, 0		),
#endif
	DATA(	"kC",	"kclr",		XK_Clear,	0		),
	DATA(	"kD",	"kdch1",	XK_Delete,	0		),
	DATA(	"kI",	"kich1",	XK_Insert,	0		),
	DATA(	"kN",	"knp",		XK_Next,	0		),
	DATA(	"kP",	"kpp",		XK_Prior,	0		),
	DATA(	"kb",	"kbs",		XK_BackSpace,	0		),
	DATA(	"kd",	"kcud1",	XK_Down,	0		),
	DATA(	"kh",	"khome",	XK_Home,	0		),
	DATA(	"kl",	"kcub1",	XK_Left,	0		),
	DATA(	"kr",	"kcuf1",	XK_Right,	0		),
	DATA(	"ku",	"kcuu1",	XK_Up,		0		),
# if OPT_ISO_COLORS
	/* XK_COLORS is a fake code. */
	DATA(	"Co",	"colors",	XK_COLORS,	0		),
# endif
    };
    /* *INDENT-ON* */

    Cardinal n;
    unsigned len = 0;
    int code = -1;
#define MAX_TNAME_LEN 6
    char name[MAX_TNAME_LEN + 1];
    char *p;

    TRACE(("xtermcapKeycode(%s)\n", *params));

    /* Convert hex encoded name to ascii */
    for (p = *params; hex2int(p[0]) >= 0 && hex2int(p[1]) >= 0; p += 2) {
	if (len >= MAX_TNAME_LEN)
	    break;
	name[len++] = (hex2int(p[0]) << 4) + hex2int(p[1]);
    }
    name[len] = 0;
    *params = p;

    if (*p == 0 || *p == ';') {
	for (n = 0; n < XtNumber(table); n++) {
	    if (!strcmp(table[n].ti, name) || !strcmp(table[n].tc, name)) {
		code = table[n].code;
		*state = table[n].state;
		break;
	    }
	}
    }

    TRACE(("... xtermcapKeycode(%s, %u) -> %#06x\n", name, *state, code));
    return code;
}
#endif