mouse-codes revision 1.1.1.1 1 1.1 mrg #!/usr/bin/env perl
2 1.1 mrg # $XTermId: mouse-codes,v 1.7 2020/12/13 15:07:02 tom Exp $
3 1.1 mrg # -----------------------------------------------------------------------------
4 1.1 mrg # this file is part of xterm
5 1.1 mrg #
6 1.1 mrg # Copyright 2018-2019,2020 by Thomas E. Dickey
7 1.1 mrg #
8 1.1 mrg # All Rights Reserved
9 1.1 mrg #
10 1.1 mrg # Permission is hereby granted, free of charge, to any person obtaining a
11 1.1 mrg # copy of this software and associated documentation files (the
12 1.1 mrg # "Software"), to deal in the Software without restriction, including
13 1.1 mrg # without limitation the rights to use, copy, modify, merge, publish,
14 1.1 mrg # distribute, sublicense, and/or sell copies of the Software, and to
15 1.1 mrg # permit persons to whom the Software is furnished to do so, subject to
16 1.1 mrg # the following conditions:
17 1.1 mrg #
18 1.1 mrg # The above copyright notice and this permission notice shall be included
19 1.1 mrg # in all copies or substantial portions of the Software.
20 1.1 mrg #
21 1.1 mrg # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 1.1 mrg # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 1.1 mrg # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 1.1 mrg # IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25 1.1 mrg # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 1.1 mrg # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 1.1 mrg # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 1.1 mrg #
29 1.1 mrg # Except as contained in this notice, the name(s) of the above copyright
30 1.1 mrg # holders shall not be used in advertising or otherwise to promote the
31 1.1 mrg # sale, use or other dealings in this Software without prior written
32 1.1 mrg # authorization.
33 1.1 mrg # -----------------------------------------------------------------------------
34 1.1 mrg # Imitate xterm's BtnCode() function, to enumerate the possible inputs (button,
35 1.1 mrg # event state) versus output (xterm mouse-code).
36 1.1 mrg use strict;
37 1.1 mrg use warnings;
38 1.1 mrg
39 1.1 mrg use Getopt::Std;
40 1.1 mrg
41 1.1 mrg our ( $opt_b, $opt_i, $opt_x );
42 1.1 mrg
43 1.1 mrg # Typically,
44 1.1 mrg # - Buttons 1, 2, and 3 are left-to-right assignments on a 3-button mouse.
45 1.1 mrg # - Buttons 4 and 5 are (by convention) assigned to a wheel mouse's
46 1.1 mrg # up/down events.
47 1.1 mrg # - Buttons 6 and 7 may happen to work, e.g., as left/right events from a
48 1.1 mrg # tiltable wheel mouse. There is no explicit support for these (there
49 1.1 mrg # are no related symbols in Xt), so it is not possible to use them in the
50 1.1 mrg # translations resource.
51 1.1 mrg our $maxbutton = 7;
52 1.1 mrg
53 1.1 mrg # xterm uses code 3 internally for release-events.
54 1.1 mrg sub ButtonName($) {
55 1.1 mrg my $code = shift;
56 1.1 mrg my $result = "";
57 1.1 mrg if ( $code < 0 ) {
58 1.1 mrg $result = "release";
59 1.1 mrg }
60 1.1 mrg else {
61 1.1 mrg $result = sprintf "Button%d", ( $code > 3 ) ? $code : ( $code + 1 );
62 1.1 mrg }
63 1.1 mrg return $result;
64 1.1 mrg }
65 1.1 mrg
66 1.1 mrg # Combine the modifier state as bits:
67 1.1 mrg # shift key -> 1
68 1.1 mrg # meta key -> 2 (Mod1 came from X11R1, but was adapted from X10)
69 1.1 mrg # control key -> 4
70 1.1 mrg our $maxstates = 7;
71 1.1 mrg
72 1.1 mrg sub KeyState($) {
73 1.1 mrg my $mask = shift;
74 1.1 mrg my $result = "";
75 1.1 mrg $result .= " + shift" if ( ( $mask & 1 ) != 0 );
76 1.1 mrg $result .= " + meta" if ( ( $mask & 2 ) != 0 );
77 1.1 mrg $result .= " + control" if ( ( $mask & 4 ) != 0 );
78 1.1 mrg return $result;
79 1.1 mrg }
80 1.1 mrg
81 1.1 mrg sub Motion($) {
82 1.1 mrg my $code = shift;
83 1.1 mrg my $result = "";
84 1.1 mrg $result = " + motion" if ( $code != 0 );
85 1.1 mrg return $result;
86 1.1 mrg }
87 1.1 mrg
88 1.1 mrg sub report() {
89 1.1 mrg my $button;
90 1.1 mrg my $states;
91 1.1 mrg my $motion;
92 1.1 mrg my %encoded;
93 1.1 mrg my %reports;
94 1.1 mrg for $states ( 0 .. $maxstates ) {
95 1.1 mrg for $motion ( 0 .. 1 ) {
96 1.1 mrg for $button ( -1 .. $maxbutton ) {
97 1.1 mrg next if ( $button == 3 );
98 1.1 mrg my $result = ( 32 + ( $states << 2 ) );
99 1.1 mrg $result += 32 if ( $motion != 0 );
100 1.1 mrg if ( $button < 0 ) {
101 1.1 mrg $result += 3;
102 1.1 mrg }
103 1.1 mrg else {
104 1.1 mrg $result += $button & 3;
105 1.1 mrg if ( $button & 4 ) {
106 1.1 mrg $result += 64;
107 1.1 mrg }
108 1.1 mrg if ( $button & 8 ) {
109 1.1 mrg $result += 128;
110 1.1 mrg }
111 1.1 mrg }
112 1.1 mrg my $code = sprintf "%3d", $result;
113 1.1 mrg my $report = sprintf "%s%s%s", &ButtonName($button),
114 1.1 mrg &KeyState($states), &Motion($motion);
115 1.1 mrg if ( defined $encoded{$code} ) {
116 1.1 mrg printf "OOPS %s ->%s versus %s\n", $code, $report,
117 1.1 mrg $encoded{$code};
118 1.1 mrg }
119 1.1 mrg elsif ( $result > 255 and not defined $opt_x ) {
120 1.1 mrg printf "OOPS %s ->%s\n", $code, $report;
121 1.1 mrg }
122 1.1 mrg $encoded{$code} = $report;
123 1.1 mrg $reports{$report} = $result;
124 1.1 mrg }
125 1.1 mrg }
126 1.1 mrg }
127 1.1 mrg if ($opt_i) {
128 1.1 mrg foreach my $report ( sort keys %reports ) {
129 1.1 mrg printf "%s = %s\n", $report, $reports{$report};
130 1.1 mrg }
131 1.1 mrg }
132 1.1 mrg else {
133 1.1 mrg foreach my $code ( sort keys %encoded ) {
134 1.1 mrg printf "%s = %s\n", $code, $encoded{$code};
135 1.1 mrg }
136 1.1 mrg }
137 1.1 mrg }
138 1.1 mrg
139 1.1 mrg sub main::HELP_MESSAGE() {
140 1.1 mrg printf STDERR <<EOF
141 1.1 mrg Usage: $0 [options]
142 1.1 mrg
143 1.1 mrg Options:
144 1.1 mrg -b NUM set the number of buttons (default: 7)
145 1.1 mrg -i invert the report to show code for each combination
146 1.1 mrg -x eliminate 1-byte limit for codes
147 1.1 mrg EOF
148 1.1 mrg ;
149 1.1 mrg exit 1;
150 1.1 mrg }
151 1.1 mrg $Getopt::Std::STANDARD_HELP_VERSION = 1;
152 1.1 mrg &getopts('b:ix') || &main::HELP_MESSAGE;
153 1.1 mrg $maxbutton = $opt_b if ( defined $opt_b );
154 1.1 mrg &main::HELP_MESSAGE if ( $maxbutton !~ /^\d+$/ );
155 1.1 mrg $#ARGV < 0 || &main::HELP_MESSAGE;
156 1.1 mrg
157 1.1 mrg &report;
158 1.1 mrg
159 1.1 mrg 1;
160