This repository has been archived by the owner on Jul 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
input.c
2092 lines (1949 loc) · 53.9 KB
/
input.c
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
/* $XTermId: input.c,v 1.345 2013/02/06 09:51:33 tom Exp $ */
/*
* Copyright 1999-2011,2012 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
#if HAVE_X11_XF86KEYSYM_H
#include <X11/XF86keysym.h>
#endif
#ifdef HAVE_XKBKEYCODETOKEYSYM
#include <X11/XKBlib.h>
#endif
#include <X11/Xutil.h>
#include <stdio.h>
#include <ctype.h>
#include <xutf8.h>
#include <data.h>
#include <fontutils.h>
#include <xstrings.h>
#include <xtermcap.h>
/*
* Xutil.h has no macro to check for the complete set of function- and
* modifier-keys that might be returned. Fake it.
*/
#ifdef XK_ISO_Lock
#define IsPredefinedKey(n) ((n) >= XK_ISO_Lock && (n) <= XK_Delete)
#else
#define IsPredefinedKey(n) ((n) >= XK_BackSpace && (n) <= XK_Delete)
#endif
#ifdef XK_ISO_Left_Tab
#define IsTabKey(n) ((n) == XK_Tab || (n) == XK_ISO_Left_Tab)
#else
#define IsTabKey(n) ((n) == XK_Tab)
#endif
#ifndef IsPrivateKeypadKey
#define IsPrivateKeypadKey(k) (0)
#endif
#define IsBackarrowToggle(keyboard, keysym, state) \
((((keyboard->flags & MODE_DECBKM) == 0) \
^ ((state & ControlMask) != 0)) \
&& (keysym == XK_BackSpace))
#define MAP(from, to) case from: result = to; break
#define Masked(value,mask) ((value) & (unsigned) (~(mask)))
#define KEYSYM_FMT "0x%04lX" /* simplify matching <X11/keysymdef.h> */
#define TEK4014_GIN(tw) (tw != 0 && TekScreenOf(tw)->TekGIN)
typedef struct {
KeySym keysym;
Bool is_fkey;
int nbytes;
#define STRBUFSIZE 500
char strbuf[STRBUFSIZE];
} KEY_DATA;
static
const char *kypd_num = " XXXXXXXX\tXXX\rXXXxxxxXXXXXXXXXXXXXXXXXXXXX*+,-./0123456789XXX=";
/* 0123456789 abc def0123456789abcdef0123456789abcdef0123456789abcd */
static
const char *kypd_apl = " ABCDEFGHIJKLMNOPQRSTUVWXYZ??????abcdefghijklmnopqrstuvwxyzXXX";
/* 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd */
static
const char *curfinal = "HDACB FE";
static int decfuncvalue(KEY_DATA *);
static void sunfuncvalue(ANSI *, KEY_DATA *);
static void hpfuncvalue(ANSI *, KEY_DATA *);
static void scofuncvalue(ANSI *, KEY_DATA *);
static void
AdjustAfterInput(XtermWidget xw)
{
TScreen *screen = TScreenOf(xw);
if (screen->scrollkey && screen->topline != 0)
WindowScroll(xw, 0, False);
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(xw, 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;
}
}
/*
* Return true if the key is on the editing keypad. This overlaps with
* IsCursorKey() and IsKeypadKey() and must be tested before those macros to
* distinguish it from them.
*
* VT220 emulation uses the VT100 numeric keypad as well as a 6-key
* editing keypad. Here's a picture of the VT220 editing keypad:
* +--------+--------+--------+
* | Find | Insert | Remove |
* +--------+--------+--------+
* | Select | Prev | Next |
* +--------+--------+--------+
*
* and the similar Sun and PC keypads:
* +--------+--------+--------+
* | Insert | Home | PageUp |
* +--------+--------+--------+
* | Delete | End | PageDn |
* +--------+--------+--------+
*/
static Bool
IsEditKeypad(XtermWidget xw, KeySym keysym)
{
Bool result;
switch (keysym) {
case XK_Delete:
result = !xtermDeleteIsDEL(xw);
break;
case XK_Prior:
case XK_Next:
case XK_Insert:
case XK_Find:
case XK_Select:
#ifdef DXK_Remove
case DXK_Remove:
#endif
result = True;
break;
default:
result = False;
break;
}
return result;
}
/*
* Editing-keypad, plus other editing keys which are not included in the
* other macros.
*/
static Bool
IsEditFunctionKey(XtermWidget xw, KeySym keysym)
{
Bool result;
switch (keysym) {
#ifdef XK_KP_Delete
case XK_KP_Delete: /* editing key on numeric keypad */
case XK_KP_Insert: /* editing key on numeric keypad */
#endif
#ifdef XK_ISO_Left_Tab
case XK_ISO_Left_Tab:
#endif
result = True;
break;
default:
result = IsEditKeypad(xw, keysym);
break;
}
return result;
}
#if OPT_MOD_FKEYS
#define IS_CTRL(n) ((n) < ANSI_SPA || ((n) >= 0x7f && (n) <= 0x9f))
/*
* Return true if the keysym corresponds to one of the control characters,
* or one of the common ASCII characters that is combined with control to
* make a control character.
*/
static Bool
IsControlInput(KEY_DATA * kd)
{
return ((kd->keysym) >= 0x40 && (kd->keysym) <= 0x7f);
}
static Bool
IsControlOutput(KEY_DATA * kd)
{
return IS_CTRL(kd->keysym);
}
/*
* X "normally" has some built-in translations, which the user may want to
* suppress when processing the modifyOtherKeys resource. In particular, the
* control modifier applied to some of the keyboard digits gives results for
* control characters.
*
* control 2 0 NUL
* control SPC 0 NUL
* control @ 0 NUL
* control ` 0 NUL
* control 3 0x1b ESC
* control 4 0x1c FS
* control \ 0x1c FS
* control 5 0x1d GS
* control 6 0x1e RS
* control ^ 0x1e RS
* control ~ 0x1e RS
* control 7 0x1f US
* control / 0x1f US
* control _ 0x1f US
* control 8 0x7f DEL
*
* It is possible that some other keyboards do not work for these combinations,
* but they do work with modifyOtherKeys=2 for the US keyboard:
*
* control ` 0 NUL
* control [ 0x1b ESC
* control \ 0x1c FS
* control ] 0x1d GS
* control ? 0x7f DEL
*/
static Bool
IsControlAlias(KEY_DATA * kd)
{
Bool result = False;
if (kd->nbytes == 1) {
result = IS_CTRL(CharOf(kd->strbuf[0]));
}
return result;
}
/*
* If we are in the non-VT220/VT52 keyboard state, allow modifiers to add a
* parameter to the function-key control sequences.
*
* Note that we generally cannot capture the Shift-modifier for the numeric
* keypad since this is commonly used to act as a type of NumLock, e.g.,
* making the keypad send "7" (actually XK_KP_7) where the unshifted code
* would be Home (XK_KP_Home). The other modifiers work, subject to the
* usual window-manager assignments.
*/
#if OPT_SUNPC_KBD
#define LegacyAllows(code) (!is_legacy || (code & xw->keyboard.modify_now.allow_keys) != 0)
#else
#define LegacyAllows(code) True
#endif
static Bool
allowModifierParm(XtermWidget xw, KEY_DATA * kd)
{
TKeyboard *keyboard = &(xw->keyboard);
TScreen *screen = TScreenOf(xw);
int keypad_mode = ((keyboard->flags & MODE_DECKPAM) != 0);
int is_legacy = (keyboard->type == keyboardIsLegacy);
Bool result = False;
#if OPT_SUNPC_KBD
if (keyboard->type == keyboardIsVT220)
is_legacy = True;
#endif
(void) screen;
#if OPT_VT52_MODE
if (screen->vtXX_level != 0)
#endif
{
if (IsCursorKey(kd->keysym) || IsEditFunctionKey(xw, kd->keysym)) {
result = LegacyAllows(2);
} else if (IsKeypadKey(kd->keysym)) {
if (keypad_mode) {
result = LegacyAllows(1);
}
} else if (IsFunctionKey(kd->keysym)) {
result = LegacyAllows(4);
} else if (IsMiscFunctionKey(kd->keysym)) {
result = LegacyAllows(8);
}
}
if (xw->keyboard.modify_now.other_keys != 0) {
result = True;
}
return result;
}
/*
* 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)
*/
unsigned
xtermParamToState(XtermWidget xw, unsigned param)
{
unsigned result = 0;
#if OPT_NUM_LOCK
if (param > MOD_NONE) {
if ((param - MOD_NONE) & MOD_SHIFT)
UIntSet(result, ShiftMask);
if ((param - MOD_NONE) & MOD_CTRL)
UIntSet(result, ControlMask);
if ((param - MOD_NONE) & MOD_ALT)
UIntSet(result, xw->work.alt_mods);
if ((param - MOD_NONE) & MOD_META)
UIntSet(result, xw->work.meta_mods);
}
#else
(void) xw;
(void) param;
#endif
TRACE(("xtermParamToState(%d) %s%s%s%s -> %#x\n", param,
MODIFIER_NAME(param, MOD_SHIFT),
MODIFIER_NAME(param, MOD_ALT),
MODIFIER_NAME(param, MOD_CTRL),
MODIFIER_NAME(param, MOD_META),
result));
return result;
}
unsigned
xtermStateToParam(XtermWidget xw, unsigned state)
{
unsigned modify_parm = MOD_NONE;
TRACE(("xtermStateToParam %#x\n", state));
#if OPT_NUM_LOCK
if (state & ShiftMask) {
modify_parm += MOD_SHIFT;
UIntClr(state, ShiftMask);
}
if (state & ControlMask) {
modify_parm += MOD_CTRL;
UIntClr(state, ControlMask);
}
if ((state & xw->work.alt_mods) != 0) {
modify_parm += MOD_ALT;
UIntClr(state, xw->work.alt_mods);
}
if ((state & xw->work.meta_mods) != 0) {
modify_parm += MOD_META;
UIntClr(state, xw->work.meta_mods);
}
if (modify_parm == MOD_NONE)
modify_parm = 0;
#else
(void) xw;
(void) state;
#endif
TRACE(("...xtermStateToParam %d%s%s%s%s\n", modify_parm,
MODIFIER_NAME(modify_parm, MOD_SHIFT),
MODIFIER_NAME(modify_parm, MOD_ALT),
MODIFIER_NAME(modify_parm, MOD_CTRL),
MODIFIER_NAME(modify_parm, MOD_META)));
return modify_parm;
}
#define computeMaskedModifier(xw, state, mask) \
xtermStateToParam(xw, Masked(state, mask))
#if OPT_NUM_LOCK
static unsigned
filterAltMeta(unsigned result, unsigned mask, Bool enable, KEY_DATA * kd)
{
if ((result & mask) != 0) {
/*
* metaSendsEscape makes the meta key independent of
* modifyOtherKeys.
*/
if (enable) {
result &= ~mask;
}
/*
* A bare meta-modifier is independent of modifyOtherKeys. If it
* is combined with other modifiers, make it depend.
*/
if ((result & ~(mask)) == 0) {
result &= ~mask;
}
/*
* Check for special cases of control+meta which are used by some
* applications, e.g., emacs.
*/
if ((IsControlInput(kd)
|| IsControlOutput(kd))
&& (result & ControlMask) != 0) {
result &= ~(mask | ControlMask);
}
if (kd->keysym == XK_Return || kd->keysym == XK_Tab) {
result &= ~(mask | ControlMask);
}
}
return result;
}
#endif /* OPT_NUM_LOCK */
/*
* Single characters (not function-keys) are allowed fewer modifiers when
* interpreting modifyOtherKeys due to pre-existing associations with some
* modifiers.
*/
static unsigned
allowedCharModifiers(XtermWidget xw, unsigned state, KEY_DATA * kd)
{
#if OPT_NUM_LOCK
unsigned a_or_m = (state & (xw->work.meta_mods | xw->work.alt_mods));
#else
unsigned a_or_m = 0;
#endif
/*
* Start by limiting the result to the modifiers we might want to use.
*/
unsigned result = (state & (ControlMask
| ShiftMask
| a_or_m));
/*
* If modifyOtherKeys is off or medium (0 or 1), moderate its effects by
* excluding the common cases for modifiers.
*/
if (xw->keyboard.modify_now.other_keys <= 1) {
if (IsControlInput(kd)
&& Masked(result, ControlMask) == 0) {
/* These keys are already associated with the control-key */
if (xw->keyboard.modify_now.other_keys == 0) {
UIntClr(result, ControlMask);
}
} else if (kd->keysym == XK_Tab || kd->keysym == XK_Return) {
/* EMPTY */ ;
} else if (IsControlAlias(kd)) {
/* Things like "^_" work here... */
if (Masked(result, (ControlMask | ShiftMask)) == 0) {
result = 0;
}
} else if (!IsControlOutput(kd) && !IsPredefinedKey(kd->keysym)) {
/* Printable keys are already associated with the shift-key */
if (!(result & ControlMask)) {
UIntClr(result, ShiftMask);
}
}
#if OPT_NUM_LOCK
result = filterAltMeta(result,
xw->work.meta_mods,
TScreenOf(xw)->meta_sends_esc, kd);
if (TScreenOf(xw)->alt_is_not_meta) {
result = filterAltMeta(result,
xw->work.alt_mods,
TScreenOf(xw)->alt_sends_esc, kd);
}
#endif
}
TRACE(("...allowedCharModifiers(state=%u" FMT_MODIFIER_NAMES
", ch=" KEYSYM_FMT ") ->"
"%u" FMT_MODIFIER_NAMES "\n",
state, ARG_MODIFIER_NAMES(state), kd->keysym,
result, ARG_MODIFIER_NAMES(result)));
return result;
}
/*
* Decide if we should generate a special escape sequence for "other" keys
* than cursor-, function-keys, etc., as per the modifyOtherKeys resource.
*/
static Bool
ModifyOtherKeys(XtermWidget xw,
unsigned state,
KEY_DATA * kd,
unsigned modify_parm)
{
TKeyboard *keyboard = &(xw->keyboard);
Bool result = False;
/*
* Exclude the keys already covered by a modifier.
*/
if (kd->is_fkey
|| IsEditFunctionKey(xw, kd->keysym)
|| IsKeypadKey(kd->keysym)
|| IsCursorKey(kd->keysym)
|| IsPFKey(kd->keysym)
|| IsMiscFunctionKey(kd->keysym)
|| IsPrivateKeypadKey(kd->keysym)) {
result = False;
} else if (modify_parm != 0) {
if (IsBackarrowToggle(keyboard, kd->keysym, state)) {
kd->keysym = XK_Delete;
UIntClr(state, ControlMask);
}
if (!IsPredefinedKey(kd->keysym)) {
state = allowedCharModifiers(xw, state, kd);
}
if (state != 0) {
switch (keyboard->modify_now.other_keys) {
default:
break;
case 1:
switch (kd->keysym) {
case XK_BackSpace:
case XK_Delete:
result = False;
break;
#ifdef XK_ISO_Left_Tab
case XK_ISO_Left_Tab:
if (computeMaskedModifier(xw, state, ShiftMask))
result = True;
break;
#endif
case XK_Return:
case XK_Tab:
result = (modify_parm != 0);
break;
default:
if (IsControlInput(kd)) {
if (state == ControlMask || state == ShiftMask) {
result = False;
} else {
result = (modify_parm != 0);
}
} else if (IsControlAlias(kd)) {
if (state == ShiftMask)
result = False;
else if (computeMaskedModifier(xw, state, ControlMask)) {
result = True;
}
} else {
result = True;
}
break;
}
break;
case 2:
switch (kd->keysym) {
case XK_BackSpace:
/* strip ControlMask as per IsBackarrowToggle() */
if (computeMaskedModifier(xw, state, ControlMask))
result = True;
break;
case XK_Delete:
result = (xtermStateToParam(xw, state) != 0);
break;
#ifdef XK_ISO_Left_Tab
case XK_ISO_Left_Tab:
if (computeMaskedModifier(xw, state, ShiftMask))
result = True;
break;
#endif
case XK_Return:
case XK_Tab:
result = (modify_parm != 0);
break;
default:
if (IsControlInput(kd)) {
result = True;
} else if (state == ShiftMask) {
result = (kd->keysym == ' ' || kd->keysym == XK_Return);
} else if (computeMaskedModifier(xw, state, ShiftMask)) {
result = True;
}
break;
}
break;
}
}
}
TRACE(("...ModifyOtherKeys(%d,%d) %s\n",
keyboard->modify_now.other_keys,
modify_parm,
BtoS(result)));
return result;
}
#define APPEND_PARM(number) \
reply->a_param[reply->a_nparam] = (ParmType) number; \
reply->a_nparam++
/*
* Function-key code 27 happens to not be used in the vt220-style encoding.
* xterm uses this to represent modified non-function-keys such as control/+ in
* the Sun/PC keyboard layout. See the modifyOtherKeys resource in the manpage
* for more information.
*/
static Bool
modifyOtherKey(ANSI * reply, int input_char, unsigned modify_parm, int format_keys)
{
Bool result = False;
if (input_char >= 0) {
reply->a_type = ANSI_CSI;
if (format_keys) {
APPEND_PARM(input_char);
APPEND_PARM(modify_parm);
reply->a_final = 'u';
} else {
APPEND_PARM(27);
APPEND_PARM(modify_parm);
APPEND_PARM(input_char);
reply->a_final = '~';
}
result = True;
}
return result;
}
static void
modifyCursorKey(ANSI * reply, int modify, unsigned *modify_parm)
{
if (*modify_parm != 0) {
if (modify < 0) {
*modify_parm = 0;
}
if (modify > 0) {
reply->a_type = ANSI_CSI; /* SS3 should not have params */
}
if (modify > 1 && reply->a_nparam == 0) {
APPEND_PARM(1); /* force modifier to 2nd param */
}
if (modify > 2) {
reply->a_pintro = '>'; /* mark this as "private" */
}
}
}
#else
#define modifyCursorKey(reply, modify, parm) /* nothing */
#endif /* OPT_MOD_FKEYS */
#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 /* OPT_SUNPC_KBD */
#define VT52_KEYPAD \
if_OPT_VT52_MODE(screen,{ \
reply.a_type = ANSI_ESC; \
reply.a_pintro = '?'; \
})
#define VT52_CURSOR_KEYS \
if_OPT_VT52_MODE(screen,{ \
reply.a_type = ANSI_ESC; \
})
#undef APPEND_PARM
#define APPEND_PARM(number) \
reply.a_param[reply.a_nparam] = (ParmType) number, \
reply.a_nparam++
#if OPT_MOD_FKEYS
#define MODIFIER_PARM \
if (modify_parm != 0) 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(XtermWidget xw)
{
Bool result = True;
if (xw->keyboard.type == keyboardIsDefault
|| xw->keyboard.type == keyboardIsVT220)
result = (TScreenOf(xw)->delete_is_del == True);
if (xw->keyboard.type == keyboardIsLegacy)
result = (TScreenOf(xw)->delete_is_del != False);
TRACE(("xtermDeleteIsDEL(%d/%d) = %d\n",
xw->keyboard.type,
TScreenOf(xw)->delete_is_del,
result));
return result;
}
static Boolean
lookupKeyData(KEY_DATA * kd, XtermWidget xw, XKeyEvent * event)
{
TScreen *screen = TScreenOf(xw);
TKeyboard *keyboard = &(xw->keyboard);
Boolean result = True;
TRACE(("%s %#x\n", visibleEventType(event->type), event->keycode));
kd->keysym = 0;
kd->is_fkey = False;
#if OPT_TCAP_QUERY
if (screen->tc_query_code >= 0) {
kd->keysym = (KeySym) screen->tc_query_code;
kd->is_fkey = screen->tc_query_fkey;
if (kd->keysym != XK_BackSpace) {
kd->nbytes = 0;
kd->strbuf[0] = 0;
} else {
kd->nbytes = 1;
kd->strbuf[0] = 8;
}
} else
#endif
{
#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
TInput *input = lookupTInput(xw, (Widget) xw);
if (input && input->xic) {
Status status_return;
#if OPT_WIDE_CHARS
if (screen->utf8_mode) {
kd->nbytes = Xutf8LookupString(input->xic, event,
kd->strbuf, (int) sizeof(kd->strbuf),
&(kd->keysym), &status_return);
} else
#endif
{
kd->nbytes = XmbLookupString(input->xic, event,
kd->strbuf, (int) sizeof(kd->strbuf),
&(kd->keysym), &status_return);
}
#if OPT_MOD_FKEYS
/*
* Fill-in some code useful with IsControlAlias():
*/
if (status_return == XLookupBoth
&& kd->nbytes <= 1
&& !IsPredefinedKey(kd->keysym)
&& (keyboard->modify_now.other_keys > 1)
&& !IsControlInput(kd)) {
kd->nbytes = 1;
kd->strbuf[0] = (char) kd->keysym;
}
#endif /* OPT_MOD_FKEYS */
} else
#endif /* OPT_I18N_SUPPORT */
{
static XComposeStatus compose_status =
{NULL, 0};
kd->nbytes = XLookupString(event,
kd->strbuf, (int) sizeof(kd->strbuf),
&(kd->keysym), &compose_status);
}
kd->is_fkey = IsFunctionKey(kd->keysym);
}
return result;
}
void
Input(XtermWidget xw,
XKeyEvent * event,
Bool eightbit)
{
Char *string;
TKeyboard *keyboard = &(xw->keyboard);
TScreen *screen = TScreenOf(xw);
int j;
int key = False;
ANSI reply;
int dec_code;
unsigned modify_parm = 0;
int keypad_mode = ((keyboard->flags & MODE_DECKPAM) != 0);
unsigned evt_state = event->state;
unsigned mod_state;
KEY_DATA kd;
/* Ignore characters typed at the keyboard */
if (keyboard->flags & MODE_KAM)
return;
lookupKeyData(&kd, xw, event);
memset(&reply, 0, sizeof(reply));
TRACE(("Input keysym "
KEYSYM_FMT
", %d:'%s'%s" FMT_MODIFIER_NAMES "%s%s%s%s%s%s\n",
kd.keysym,
kd.nbytes,
visibleChars((Char *) kd.strbuf,
((kd.nbytes > 0)
? (unsigned) kd.nbytes
: 0)),
ARG_MODIFIER_NAMES(evt_state),
eightbit ? " 8bit" : " 7bit",
IsKeypadKey(kd.keysym) ? " KeypadKey" : "",
IsCursorKey(kd.keysym) ? " CursorKey" : "",
IsPFKey(kd.keysym) ? " PFKey" : "",
kd.is_fkey ? " FKey" : "",
IsMiscFunctionKey(kd.keysym) ? " MiscFKey" : "",
IsEditFunctionKey(xw, kd.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
&& (evt_state & ShiftMask) == 0) {
if (kd.keysym == XK_KP_Add) {
kd.keysym = XK_KP_Separator;
UIntClr(evt_state, ShiftMask);
TRACE(("...Input keypad(+), change keysym to "
KEYSYM_FMT
"\n",
kd.keysym));
}
if ((evt_state & ControlMask) != 0
&& kd.keysym == XK_KP_Separator) {
kd.keysym = XK_KP_Subtract;
UIntClr(evt_state, ControlMask);
TRACE(("...Input control/keypad(,), change keysym to "
KEYSYM_FMT
"\n",
kd.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 (kd.nbytes == 1
&& IsKeypadKey(kd.keysym)
&& xw->misc.real_NumLock
&& (xw->work.num_lock & evt_state) != 0) {
keypad_mode = 0;
TRACE(("...Input num_lock, force keypad_mode off\n"));
}
#endif
#if OPT_MOD_FKEYS
if (evt_state != 0
&& allowModifierParm(xw, &kd)) {
modify_parm = xtermStateToParam(xw, evt_state);
}
/*
* Shift-tab is often mapped to XK_ISO_Left_Tab which is classified as
* IsEditFunctionKey(), and the conversion does not produce any bytes.
* Check for this special case so we have data when handling the
* modifyOtherKeys resource.
*/
if (keyboard->modify_now.other_keys > 1) {
if (IsTabKey(kd.keysym) && kd.nbytes == 0) {
kd.nbytes = 1;
kd.strbuf[0] = '\t';
}
}
#ifdef XK_ISO_Left_Tab
else if (IsTabKey(kd.keysym)
&& kd.nbytes <= 1
&& modify_parm == (MOD_NONE + MOD_SHIFT)) {
kd.keysym = XK_ISO_Left_Tab;
}