-
Notifications
You must be signed in to change notification settings - Fork 17
/
modify-keys.pl
executable file
·1539 lines (1410 loc) · 46.5 KB
/
modify-keys.pl
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
#!/usr/bin/env perl
# $XTermId: modify-keys.pl,v 1.92 2022/11/24 12:43:26 tom Exp $
# -----------------------------------------------------------------------------
# this file is part of xterm
#
# Copyright 2019-2020,2022 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.
# -----------------------------------------------------------------------------
# Print a table to illustrate the modifyOtherKeys resource choices.
#
# Some of the key combinations are unavailable unless certain translations
# resource settings are suppressed. This command helped to verify those:
# xterm -xrm '*omitTranslation:fullscreen,scroll-lock,shift-fonts'
#
# Additionally, a test-script was written to exercise xterm when the
# "Allow SendEvents" feature is enabled, in combination with keys sent by
# commands like this:
# xdotool key --window XXX shift 2>/dev/null
#
# A curses application running in the target xterm showed the received data
# in the terminfo-style format used in this script.
# TODO factor in the backspace/delete meta/alt/escape resource-settings
# TODO show keycodes via "xmodmap -pk" as alternative to xkbcomp
# TODO show different sort-order (code, sym, xkb)
# TODO use U+xxxx codepoints in keysymdef.h for rendering plain text
# TODO optionally show 2**N, e.g., 4 (shift+control), 8 (shift+alt+control) or 16 (+meta) modifiers
# TODO optionally show -c (cursor) -e (edit) -f (function-keys) with modifiers
use strict;
use warnings;
use Getopt::Std;
$| = 1;
our ( $opt_d, $opt_h, $opt_k, $opt_K, $opt_l, $opt_m, $opt_o, $opt_u, $opt_v );
our $REPORT;
our @headers;
our @nolinks = ();
our ( $xkb_layout, $xkb_model );
our $keyfile = "/usr/include/X11/keysymdef.h";
our @keyNames; # xkb's notion of key-names (undocumented)
our %keySyms; # all keysyms, hashed by name
our %keyCodes; # all keysyms, hashed by keycode
our %uniCodes; # keysym Unicode values, hashed by keycode
our %uniNames; # keysym Unicode descriptions, hashed by keycode
our @keyTypes; # XkbKeyTypeRec
our @symCache; # keysyms defined in keysymdef.h which might be used
our @symMap; # index into symCache from keyNames
our %keysUsed; # report derived from @symMap, etc.
our %linkUsed; # check for uniqueness of html anchor-names
our $MAXMODS = 8; # maximum for modifier-param
our %Shifted; # map keycode to shifted-keycode seen by xterm
# imitate /usr/include/X11/X.h
our $ShiftMask = 1;
our $LockMask = 2;
our $ControlMask = 4;
our $AltMask = 8; # assume mod1=alt
our $MetaMask = 16; # assume mod2=meta
our %editKeys = qw(
XK_Delete 1
XK_Prior 1
XK_Next 1
XK_Insert 1
XK_Find 1
XK_Select 1
XK_KP_Delete 1
XK_KP_Insert 1
XK_ISO_Left_Tab 1
);
sub failed($) {
printf STDERR "%s\n", $_[0];
exit 1;
}
# prefer hex with 4 digit for hash keys
sub toCode($) {
my $value = shift;
$value = sprintf( "0x%04x", $value ) if ( $value =~ /^\d+$/ );
return $value;
}
sub codeOf($) {
my $value = shift;
my $result = 0;
&failed("missing keysym") unless ( defined $value );
if ( $value =~ /^\d+$/ ) {
$result = $value;
}
elsif ( $value =~ /^0x[[:xdigit:]]+$/i ) {
$result = hex $value;
}
elsif ( $value =~ /^XK_/ ) {
$result = hex $keySyms{$value};
}
else {
&failed("not a keysym: $value");
}
return $result;
}
# macros from <X11/Xutil.h>
sub IsKeypadKey($) {
my $code = &codeOf( $_[0] );
my $result = ( ( $code >= &codeOf("XK_KP_Space") )
and ( $code <= &codeOf("XK_KP_Equal") ) ) ? 1 : 0;
return $result;
}
sub IsPrivateKeypadKey($) {
my $code = &codeOf( $_[0] );
my $result =
( ( $code >= 0x11000000 ) and ( $code <= 0x1100FFFF ) ) ? 1 : 0;
return $result;
}
sub IsCursorKey($) {
my $code = &codeOf( $_[0] );
my $result =
( ( $code >= &codeOf("XK_Home") ) and ( $code < &codeOf("XK_Select") ) )
? 1
: 0;
return $result;
}
sub IsPFKey($) {
my $code = &codeOf( $_[0] );
my $result =
( ( $code >= &codeOf("XK_KP_F1") ) and ( $code <= &codeOf("XK_KP_F4") ) )
? 1
: 0;
return $result;
}
sub IsFunctionKey($) {
my $code = &codeOf( $_[0] );
my $result =
( ( $code >= &codeOf("XK_F1") ) and ( $code <= &codeOf("XK_F35") ) )
? 1
: 0;
return $result;
}
sub IsMiscFunctionKey($) {
my $code = &codeOf( $_[0] );
my $result =
( ( $code >= &codeOf("XK_Select") ) and ( $code <= &codeOf("XK_Break") ) )
? 1
: 0;
return $result;
}
sub IsModifierKey($) {
my $code = &codeOf( $_[0] );
my $result = (
(
( $code >= &codeOf("XK_Shift_L") )
and ( $code <= &codeOf("XK_Hyper_R") )
)
or ( ( $code >= &codeOf("XK_ISO_Lock") )
and ( $code <= &codeOf("XK_ISO_Level5_Lock") ) )
or ( $code == &codeOf("XK_Mode_switch") )
or ( $code == &codeOf("XK_Num_Lock") )
) ? 1 : 0;
return $result;
}
# debugging/reporting
# Xutil.h's macros do not cover the whole range of special keys, which are not
# actually printable.
sub IsSpecialKey($) {
my $code = &codeOf( $_[0] );
my $result =
( ( $code >= 0xff00 ) and ( $code <= 0xffff ) )
? 1
: 0;
return $result;
}
sub VisibleChar($) {
my $ch = shift;
my $ord = ord $ch;
my $result = $ch;
if ( $ord < 32 ) {
if ( $ord == 8 ) {
$result = '\b';
}
elsif ( $ord == 9 ) {
$result = '\t';
}
elsif ( $ord == 10 ) {
$result = '\n';
}
elsif ( $ord == 12 ) {
$result = '\f';
}
elsif ( $ord == 13 ) {
$result = '\r';
}
elsif ( $ord == 27 ) {
$result = '\E';
}
else {
$result = sprintf( "^%c", $ord + 64 );
}
}
elsif ( $ord == 32 ) {
$result = '\s';
}
elsif ( $ord == 94 ) {
$result = '\^';
}
elsif ( $ord == 92 ) {
$result = '\\\\';
}
elsif ( $ord == 127 ) {
$result = '^?';
}
return $result;
}
sub IsShift($$) {
my $code = shift;
my $state = shift; # 0/1=normal, 2=shift
my $result = 0;
if ( ( ( $state - 1 ) & 1 ) != 0 ) {
if ( $Shifted{$code} ) {
return 1 if ( $Shifted{$code} ne $code );
}
}
return 0;
}
sub TypeOf($) {
my $code = &toCode( $_[0] );
my $result = "other";
$result = "special" if ( &IsSpecialKey($code) );
$result = "keypad" if ( &IsKeypadKey($code) );
$result = "*keypad" if ( &IsPrivateKeypadKey($code) );
$result = "cursor" if ( &IsCursorKey($code) );
$result = "pf-key" if ( &IsPFKey($code) );
$result = "func-key" if ( &IsFunctionKey($code) );
$result = "misc-key" if ( &IsMiscFunctionKey($code) );
$result = "edit-key" if ( &IsEditFunctionKey($code) );
$result = "modifier" if ( &IsModifierKey($code) );
return $result;
}
sub KeyToS($$) {
my $code = &codeOf( $_[0] );
my $state = $_[1];
my $result = "";
$code = &codeOf( $Shifted{ $_[0] } ) if ( &IsShift( $_[0], $state ) );
my $type = &TypeOf( &toCode($code) );
if ( $type ne "other" ) {
$result = ( $type eq "special" ) ? "-ignore-" : "?";
}
elsif ($opt_u) {
$result = sprintf( "\\E[%d;%du", $code, $state );
}
else {
$result = sprintf( "\\E[27;%d;%d~", $state, $code );
}
return $result;
}
sub ParamToQ($) {
my $param = shift;
my $result = shift;
$param--;
$result .= ( $param & 1 ) ? 's' : '-';
$result .= ( $param & 2 ) ? 'a' : '-';
$result .= ( $param & 4 ) ? 'c' : '-';
$result .= ( $param & 8 ) ? 'm' : '-';
return $result;
}
sub ParamToS($) {
my $param = shift;
my $result = "";
if ( $param-- > 1 ) {
$result .= "+Shift" if ( $param & 1 );
$result .= "+Alt" if ( $param & 2 );
$result .= "+Ctrl" if ( $param & 4 );
$result .= "+Meta" if ( $param & 8 );
$result =~ s/^\+//;
}
return $result;
}
sub StateToS($) {
my $state = shift;
my $result = "";
$result .= "+Shift" if ( $state & $ShiftMask );
$result .= "+Lock" if ( $state & $LockMask );
$result .= "+Ctrl" if ( $state & $ControlMask );
$result .= "+Alt" if ( $state & $AltMask );
$result .= "+Meta" if ( $state & $MetaMask );
$result =~ s/^\+//;
return $result;
}
# macros/functions in xterm's input.c
sub Masked($$) {
my $value = shift;
my $mask = shift;
my $result = ( ($value) & ( ~($mask) ) );
return $result;
}
sub IsPredefinedKey($) {
my $code = &codeOf( $_[0] );
my $result = 0;
if ( $keySyms{"XK_ISO_Lock"} ) {
$result =
( $code >= &codeOf("XK_ISO_Lock") and $code <= &codeOf("XK_Delete") )
? 1
: 0;
}
else {
$result =
( $code >= &codeOf("XK_BackSpace") and $code <= &codeOf("XK_Delete") )
? 1
: 0;
}
return $result;
}
sub IsTabKey($) {
my $code = &codeOf( $_[0] );
my $result = 0;
if ( $keySyms{"XK_ISO_Left_Tab"} ) {
$result =
( $code == &codeOf("XK_Tab") || $code == &codeOf("XK_ISO_Left_Tab") );
}
else {
$result = ( $code == &codeOf("XK_Tab") ) ? 1 : 0;
}
return $result;
}
sub IsEditFunctionKey($) {
my $code = shift;
my $result = 0;
if ( $keyCodes{$code} ) {
my $name = $keyCodes{$code};
$result = 1 if ( $editKeys{$name} );
}
return $result;
}
sub IS_CTRL($) {
my $code = &codeOf( $_[0] );
my $result = ( $code < 32 || ( $code >= 0x7f && $code <= 0x9f ) );
return $result;
}
sub IsControlInput($) {
my $code = &codeOf( $_[0] );
my $result = 0;
$result = 1 if ( $code >= 0x40 && $code <= 0x7f );
return $result;
}
sub IsControlOutput($) {
my $code = shift;
my $result = 0;
$result = 1 if &IS_CTRL($code);
return $result;
}
sub IsControlAlias($$) {
my $code = shift;
my $state = shift;
my $result = 0;
$code = &toCode($code);
$code = &toCode( &AliasedKey($code) );
if ( hex $code < 256 ) {
$result = &IS_CTRL($code);
# In xterm, this function does not directly test evt_state, but relies
# upon kd.strbuf converted by Xutf8LookupString or XmbLookupString
# (ultimately in _XTranslateKeysym).
#
# See https://www.mail-archive.com/xorg@lists.x.org/msg04434.html
#
# xterm does its own special cases for XK_BackSpace
if ( $state & $ControlMask ) {
my $ch = chr &codeOf($code);
$result = 1 if ( &IsTabKey($code) );
$result = 1 if ( &IsControlInput($code) );
$result = 1 if ( $ch =~ /^[\/ 2-8]$/ );
}
}
return $result;
}
sub computeMaskedModifier($$) {
my $state = shift;
my $mask = shift;
my $result = &xtermStateToParam( &Masked( $state, $mask ) );
return $result;
}
sub xtermStateToParam($) {
my $state = shift;
my $modify_parm = 1;
$modify_parm += 1 if ( $state & $ShiftMask );
$modify_parm += 2 if ( $state & $AltMask );
$modify_parm += 4 if ( $state & $ControlMask );
$modify_parm += 8 if ( $state & $MetaMask );
$modify_parm = 0 if ( $modify_parm == 1 );
return $modify_parm;
}
sub ParamToState($) {
my $modify_parm = shift;
my $state = 0;
$modify_parm-- if ( $modify_parm > 0 );
$state |= $ShiftMask if ( $modify_parm & 1 );
$state |= $AltMask if ( $modify_parm & 2 );
$state |= $ControlMask if ( $modify_parm & 4 );
$state |= $MetaMask if ( $modify_parm & 8 );
return $state;
}
sub allowedCharModifiers($$) {
my $other_key = shift;
my $state = shift;
my $code = shift;
my $result = $state & ( $ShiftMask | $AltMask | $ControlMask | $MetaMask );
# If modifyOtherKeys is off or medium (0 or 1), moderate its effects by
# excluding the common cases for modifiers.
if ( $other_key <= 1 ) {
my $sym = $keyCodes{$code};
if ( &IsControlInput($code)
and &Masked( $result, $ControlMask ) == 0 )
{
# These keys are already associated with the control-key
if ( $other_key == 0 ) {
$result &= ~$ControlMask;
}
}
elsif ( $sym eq "XK_Tab" || $sym eq "XK_Return" ) {
#
}
elsif ( &IsControlAlias( $code, $state ) ) {
# Things like "^_" work here...
if ( &Masked( $result, ( $ControlMask | $ShiftMask ) ) == 0 ) {
if ( $sym =~ /^XK_[34578]$/ or $sym eq "XK_slash" ) {
$result = 0 if ( $state == $ControlMask );
}
else {
$result = 0;
}
}
}
elsif ( !&IsControlOutput($code) && !&IsPredefinedKey($code) ) {
# Printable keys are already associated with the shift-key
if ( !( $result & $ControlMask ) ) {
$result &= ~$ShiftMask;
}
}
# TODO:
# 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);
# }
}
return $result;
}
# Some details are omitted (e.g., the backspace/delete toggle), but this gives
# the general sense of the corresponding function in xterm's input.c
sub ModifyOtherKeys($$$$) {
my $code = shift; # the keycode to test
my $other_key = shift; # "modifyOtherKeys" resource
my $modify_parm = shift; # 0=unmodified, 2=shift, etc
my $state = shift; # mask of modifiers, e.g., ControlMask
my $result = 0;
$modify_parm = 0 if ( $modify_parm == 1 );
if ( &IsModifierKey($code) ) {
# xterm filters out bare modifiers (ignore)
}
elsif (&IsFunctionKey($code)
or &IsEditFunctionKey($code)
or &IsKeypadKey($code)
or &IsCursorKey($code)
or &IsPFKey($code)
or &IsMiscFunctionKey($code)
or &IsPrivateKeypadKey($code) )
{
# Exclude the keys already covered by a modifier.
}
elsif ( $state > 0 ) {
my $sym = "";
$sym = $keyCodes{$code} if ( $keyCodes{$code} );
# TODO:
#if (IsBackarrowToggle(keyboard, kd->keysym, state)) {
# kd->keysym = XK_Delete;
# UIntClr(state, ControlMask);
#}
if ( !&IsPredefinedKey($code) ) {
$state = &allowedCharModifiers( $other_key, $state, $code );
}
if ( $state != 0 ) {
if ( $other_key == 1 ) {
if ( $sym eq "XK_BackSpace"
or $sym eq "XK_Delete" )
{
}
elsif ( $sym eq "XK_ISO_Left_Tab" ) {
$result = 1
if ( &computeMaskedModifier( $state, $ShiftMask ) );
}
elsif ($sym eq "XK_Return"
or $sym eq "XK_Tab" )
{
$result = ( $modify_parm != 0 );
}
else {
if ( &IsControlInput($code) ) {
if ( $state == $ControlMask or $state == $ShiftMask ) {
$result = 0;
}
else {
$result = ( $modify_parm != 0 );
}
}
elsif ( &IsControlAlias( $code, $state ) ) {
if ( $state == $ShiftMask ) {
$result = 0;
}
elsif ( &computeMaskedModifier( $state, $ControlMask ) )
{
$result = 1;
}
}
else {
$result = 1;
}
}
if ($result) { # second case in xterm's Input()
$result = 0
if ( &allowedCharModifiers( $other_key, $state, $code ) ==
0 );
}
}
elsif ( $other_key == 2 ) {
if ( $sym eq "XK_BackSpace" ) {
# strip ControlMask as per IsBackarrowToggle()
$result = 1
if ( &computeMaskedModifier( $state, $ControlMask ) );
}
elsif ( $sym eq "XK_Delete" ) {
$result = ( &xtermStateToParam($state) != 0 );
}
elsif ( $sym eq "XK_ISO_Left_Tab" ) {
$result = 1
if ( &computeMaskedModifier( $state, $ShiftMask ) );
}
elsif ($sym eq "XK_Escape"
or $sym eq "XK_Return"
or $sym eq "XK_Tab" )
{
$result = ( $modify_parm != 0 );
}
else {
if ( &IsControlInput($code) ) {
$result = 1;
}
elsif ( $state == $ShiftMask and $sym eq "XK_space" ) {
$result = 1;
}
elsif ( &computeMaskedModifier( $state, $ShiftMask ) ) {
$result = 1;
}
}
}
}
}
return $result;
}
# See IsControlAlias. This handles some of the special cases where the keycode
# seen or used by xterm is not the same as the actual keycode.
sub AliasedKey($) {
my $code = &toCode( $_[0] );
my $result = &codeOf($code);
my $sym = $keyCodes{$code};
if ($sym) {
$result = 8 if ( $sym eq "XK_BackSpace" );
$result = 9 if ( $sym eq "XK_Tab" );
$result = 13 if ( $sym eq "XK_Return" );
$result = 27 if ( $sym eq "XK_Escape" );
}
return $result;
}
# Returns a short display for shift/control/alt modifiers applied to the
# keycode to show which are affected by "modifyOtherKeys" at the given level in
# $other_key
sub CheckOtherKey($$) {
my $code = shift;
my $other_key = shift;
my $modified = 0;
my $result = "";
for my $modify_parm ( 1 .. $MAXMODS ) {
my $state = &ParamToState($modify_parm);
if ( &ModifyOtherKeys( $code, $other_key, $modify_parm, $state ) ) {
$modified++;
$result .= "*";
}
else {
$result .= "-";
}
}
return $modified ? $result : "-(skip)-";
}
# Use the return-string from CheckOtherKeys as a template for deciding which
# keys to render as escape-sequences.
sub ShowOtherKeys($$$) {
my $code = &AliasedKey( $_[0] );
my $mode = $_[1]; # modifyOtherKeys: 0, 1 or 2
my $show = $_[2];
my $type = &TypeOf( $_[0] );
my @result;
# index for $show[] can be tested with a bit-mask:
# 1 = shift
# 2 = alt
# 4 = ctrl
# 8 = meta
for my $c ( 0 .. length($show) - 1 ) {
my $rc = substr( $show, $c, 1 );
if ( $rc eq "*" ) {
$result[$c] = &KeyToS( &toCode($code), $c + 1 );
}
elsif ( $type eq "other" or ( $type eq "special" and $code < 256 ) ) {
my $map = $code;
my $tmp = &toCode($code);
my $chr = chr hex $tmp;
my $shift = ( $c & 1 );
my $cntrl = ( $c & 4 );
# TODO - can this be simplified using xkb groups?
if ( $chr =~ /^[`345678]$/ and ( $c & 4 ) != 0 ) {
if ($shift) {
$map = 30 if ( $chr eq "`" );
$map = ord "#" if ( $chr eq "3" );
$map = ord '$' if ( $chr eq "4" );
$map = ord "%" if ( $chr eq "5" );
$map = 30 if ( $chr eq "6" );
$map = ord "&" if ( $chr eq "7" );
$map = ord "*" if ( $chr eq "8" );
}
else {
$map = 0 if ( $chr eq "`" );
$map = 27 if ( $chr eq "3" );
$map = 28 if ( $chr eq "4" );
$map = 29 if ( $chr eq "5" );
$map = 30 if ( $chr eq "6" );
$map = 31 if ( $chr eq "7" );
$map = 127 if ( $chr eq "8" );
}
}
else {
$map = &codeOf( $Shifted{$tmp} )
if ( defined( $Shifted{$tmp} ) and $shift );
if ($cntrl) {
if ( $chr =~ /^[190:<=>.,+*()'&%\$#"!]$/ ) {
# ignore
}
elsif ( $chr =~ /^[2]$/ ) {
$map = 0;
}
elsif ( $chr =~ /^[:;]$/ ) {
$map = 27 if ( $mode > 0 );
}
elsif ( $chr eq '-' ) {
$map = 31 if ($shift);
}
elsif ( $chr eq '/' ) {
$map = $shift ? 127 : 31 if ( $mode == 0 );
$map = 31 if ( not $shift and $mode == 1 );
}
elsif ( $chr eq '?' ) {
$map = 127;
}
else {
$map = ( $code & 0x1f ) if ( $code < 128 );
}
}
}
$result[$c] = &VisibleChar( chr $map );
}
elsif ( $type eq "special" ) {
$result[$c] = "-ignore-";
}
else {
$result[$c] = sprintf( "%d:%s", $c + 1, $type );
}
}
return @result;
}
sub readfile($) {
my $data = shift;
my @data;
if ( open my $fp, $data ) {
@data = <$fp>;
close $fp;
chomp @data;
}
return @data;
}
sub readpipe($) {
my $cmd = shift;
return &readfile("$cmd 2>/dev/null |");
}
sub trim($) {
my $text = shift;
$text =~ s/^\s+//;
$text =~ s/\s+$//;
$text =~ s/\s+/ /g;
return $text;
}
sub html_ref($) {
my $header = shift;
my $anchor = lc &trim($header);
$anchor =~ s/\s/_/g;
return $anchor;
}
sub rightarrow() {
return $opt_h ? "→" : "->";
}
sub safe_html($) {
my $text = shift;
if ($opt_h) {
$text =~ s/\&/\&/g;
$text =~ s/\</\</g;
$text =~ s/\</\>/g;
if ( length($text) == 1 ) {
my $s = "";
for my $n ( 0 .. length($text) - 1 ) {
my $ch = substr( $text, $n, 1 );
my $ord = ord($ch);
$s .= sprintf( "&#%d;", $ord ) if ( $ord >= 128 );
$s .= $ch if ( $ord < 128 );
}
$text = $s;
}
}
return $text;
}
sub begin_report() {
if ($opt_o) {
open( $REPORT, '>', $opt_o ) or &failed("cannot open $opt_o");
select $REPORT;
}
if ($opt_h) {
printf <<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta name="generator" content="$0">
<title>XTERM - Modified "Other" Keys ($xkb_layout-$xkb_model)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="xterm, special keys">
<meta name="description" content="This is an example of xterm's modifyOtherKeys feature">
</head>
<body>
EOF
;
}
}
sub end_report() {
if ($opt_h) {
my $output = "output.html";
$output = $opt_o if ($opt_o);
printf <<EOF
<div class="nav">
<ul>
<li class="nav-top"><a href="$output">(top)</a></li>
EOF
;
for my $h ( 0 .. $#headers ) {
printf "<li><a href=\"#%s\">%s</a></li>\n",
&html_ref( $headers[$h] ), $headers[$h];
}
printf <<EOF
</ul>
</div>
EOF
;
}
if ($opt_o) {
select STDOUT;
close $REPORT;
}
}
sub begin_section($) {
my $header = shift;
$headers[ $#headers + 1 ] = $header;
if ($opt_h) {
printf "<h2><a name=\"%s\">%s</a></h2>\n", &html_ref($header), $header;
}
else {
printf "\n";
printf "%s:\n", $header;
}
printf STDERR "** %s\n", $header if ($opt_o);
}
sub begin_table() {
my $title = shift;
&begin_section($title);
if ($opt_h) {
printf "<table border=\"1\" summary=\"$title\">\n";
}
}
sub end_table() {
if ($opt_h) {
printf "</table>\n";
}
}
sub tt_cell($) {
my $text = shift;
return sprintf "<tt>%s</tt>", $text;
}
sub td_any($) {
my $text = shift;
return sprintf "<td>%s</td>", &tt_cell($text);
}
sub td_left($) {
my $text = shift;
return sprintf "<td align=\"left\">%s</td>", &tt_cell($text);
}
sub td_right($) {
my $text = shift;
return sprintf "<td align=\"right\">%s</td>", &tt_cell($text);
}
sub padded($$) {
my $size = shift;
my $text = shift;
$text = sprintf( "%*s", $size, $text ) if ( $size > 0 );
$text = sprintf( "%-*s", $size, $text ) if ( $size < 0 );
$text =~ s/ / /g if ($opt_h);
return $text;
}
sub print_head() {
my $argc = $#_;
if ($opt_h) {
printf "<tr>";
for ( my $n = 0 ; $n <= $argc ; $n += 2 ) {
my $size = $_[$n];
my $text = &padded( $size, $_[ $n + 1 ] );
printf "<th>%s</th>", $text;
}
printf "</tr>\n";
}
else {
for ( my $n = 0 ; $n <= $argc ; $n += 2 ) {
my $size = $_[$n];
my $text = &padded( $size, $_[ $n + 1 ] );
printf "%s", $text;
printf " " if ( $n < $argc );
}
printf "\n";
}
}
sub link_data($$) {
my $thisis = shift;
my $thatis = shift;
my $column = shift;
my $symbol = shift;
my %result;
$result{THISIS} = $thisis; # current table name
$result{THATIS} = $thatis; # name of target table for link
$result{COLUMN} = $column; # column counting from 0
$result{SYMBOL} = $symbol;
return \%result;
}
sub unique_link($$) {
my $thisis = shift;
my $symbol = shift;
my $unique = 0;
for my $n ( 0 .. length($symbol) - 1 ) {
$unique += ord substr( $symbol, $n, 1 );
}
return sprintf( "%s:%s.%x", $thisis, $symbol, $unique );
}
# print a row in the table, using pairs of lengths and strings:
# + Right-align lengths greater than zero and pad;
# + Left-align lengths less than zero, pad.
# + For the special case of zero, just left align without padding.
sub print_data() {
my $argc = $#_;
if ($opt_h) {
my @links = @{ $_[0] };
printf "<tr>";
my $col = 0;
for ( my $n = 1 ; $n <= $argc ; $n += 2 ) {
my $size = $_[$n];
my $text = &padded( $size, $_[ $n + 1 ] );
if ( $#links >= 0 ) {
for my $l ( 0 .. $#links ) {
my %obj = %{ $links[$l] }; # link_data
if ( $obj{COLUMN} == $col ) {
my $props = "";
my $value = &unique_link( $obj{THISIS}, $obj{SYMBOL} );
# The symbol-map from xkbcomp has duplicates because
# different modifier combinations can produce the same
# keysym. Since it appears that the slots that the
# user would expect are filled in first, just ignoring
# the duplicate works well enough.
if ( not $linkUsed{$value} ) {
$props .= " name=\"$value\"";
$linkUsed{$value} = 1;
}
$value = &unique_link( $obj{THATIS}, $obj{SYMBOL} );
$props .= " href=\"#$value\"";