Debian Codegraph @

utils/keytable/gen_keytables.pl

raw
1 #!/usr/bin/perl
2 use strict;
3 use File::Find;
4
5 my @ir_files = (
6 "drivers/media/usb/dvb-usb/af9005-remote.c",
7 "drivers/media/usb/dvb-usb/az6027.c",
8 "drivers/media/usb/dvb-usb/cinergyT2-core.c",
9 "drivers/media/usb/dvb-usb/dibusb-common.c",
10 "drivers/media/usb/dvb-usb/digitv.c",
11 "drivers/media/usb/dvb-usb/m920x.c",
12 "drivers/media/usb/dvb-usb/nova-t-usb2.c",
13 "drivers/media/usb/dvb-usb/opera1.c",
14 "drivers/media/usb/dvb-usb/vp702x.c",
15 );
16
17 my $debug = 1;
18 my $dir="rc_keymaps";
19 my $deftype = "unknown";
20
21 my $keyname="";
22 my $out;
23 my $read=0;
24 my $type = $deftype;
25 my $variant = $deftype;
26 my $check_type = 0;
27 my $name;
28 my $warn;
29 my $warn_all;
30 my %rc_map_names;
31
32 my $kernel_dir = shift or die "Need a file name to proceed.";
33
34 sub flush($$)
35 {
36 my $filename = shift;
37 my $legacy = shift;
38 my $defined;
39 my $relative_filename = $filename;
40
41 return if (!$keyname || !$out);
42 $relative_filename =~ s/^$kernel_dir//;
43 $relative_filename =~ s/^\///;
44 print "Creating $dir/$keyname.toml from $relative_filename\n";
45 open OUT, ">$dir/$keyname.toml";
46 print OUT "# Generated with gen_keytables.pl from $relative_filename\n";
47 print OUT "[[protocols]]\n";
48 print OUT "name = \"$keyname\"\n";
49 print OUT "protocol = \"$type\"\n";
50 if ($type eq "nec" || $type eq "rc5" || $type eq "rc6" || $type eq "sony") {
51 print OUT "variant = \"$variant\"\n";
52 }
53 print OUT "[protocols.scancodes]\n";
54 print OUT $out;
55 close OUT;
56
57 if (!$name && !$legacy) {
58 $warn++;
59 } else {
60 $defined = 1 if ($rc_map_names{$name});
61 }
62
63 if ($defined) {
64 printf OUT_MAP "*\t%-24s %s.toml\n", $rc_map_names{$name} , $keyname;
65 } else {
66 my $fname = $filename;
67 $fname =~ s,.*/,,;
68 printf OUT_MAP "# *\t*\t\t\t %-20s # found in %s\n", "$keyname.toml", $fname;
69 }
70
71 $keyname = "";
72 $out = "";
73 $type = $deftype;
74 $name = "";
75 }
76
77 sub parse_file($$)
78 {
79 my $filename = shift;
80 my $legacy = shift;
81
82 my $num_tables = 0;
83 my %scancodes = ();
84 $warn = 0;
85
86 next if ($filename =~ m/\.mod.c/);
87
88 printf "processing file $filename\n" if ($debug);
89 open IN, "<$filename" or die "couldn't find $filename";
90 # read the entire file
91 my $file = do { local $/ = undef; <IN> };
92 close IN;
93
94 # remove comments
95 $file =~ s,/\*.*?\*/,,sg;
96 $file =~ s,//[^\n]*,,sg;
97
98 my @lines = split /\n/, $file;
99
100 foreach (@lines) {
101 if (m/struct\s+rc_map_table\s+(\w[\w\d_]+)/) {
102 flush($filename, $legacy);
103
104 $keyname = $1;
105 $keyname =~ s/^rc_map_//;
106 $keyname =~ s/_table$//;
107 $read = 1;
108 $num_tables++;
109 %scancodes = ();
110 next;
111 }
112 if (m/struct\s+rc_map_list.*=\s+{/) {
113 $check_type = 1;
114 next;
115 }
116 if ($check_type) {
117 if (m/\.name\s*=\s*(RC_MAP_[^\s\,]+)/) {
118 $name = $1;
119 $keyname = $1;
120 $keyname =~ s/^RC_MAP_//;
121 $keyname =~ tr/A-Z/a-z/;
122 $keyname =~ s/_table$//;
123 }
124
125 if (m/^\s*}/) {
126 $check_type = 0;
127 next;
128 }
129 if (m/RC_PROTO_([\w\d_]+)/) {
130 $variant = lc $1;
131 $type = $1;
132
133 # Proper name the RC6 protocol
134 $type =~ s/^RC6_MCE$/RC6/;
135
136 # Proper name the RC-5-SZ protocol
137 $type =~ s/^RC5_SZ$/RC-5-SZ/;
138
139 # NECX protocol variant uses nec decoder
140 $type =~ s/^NECX$/NEC/;
141
142 # NEC32 protocol variant uses nec decoder
143 $type =~ s/^NEC32$/NEC/;
144 $type = lc $type;
145 }
146 next;
147 }
148
149 if ($read) {
150 if (m/(0x[\dA-Fa-f]+)[\s\,]+(KEY|BTN)(\_[^\s\,\}]+)/) {
151 my $scancode = hex($1);
152 my $keycode = "$2$3";
153
154 if (exists($scancodes{$scancode})) {
155 printf STDERR "WARNING: duplicate scancode $1 in file $filename, set to $keycode and $scancodes{$scancode}\n";
156 } else {
157 $out .= "$1 = \"$keycode\"\n";
158 $scancodes{$scancode} = $keycode;
159 }
160 next;
161 }
162 if (m/\}/) {
163 $read = 0;
164 }
165 }
166 }
167
168 flush($filename, $legacy);
169
170 printf STDERR "WARNING: keyboard name not found on %d tables at file $filename\n", $warn if ($warn);
171 print STDERR "WARNING: no tables found at $filename\n" if (!$num_tables);
172
173 $warn_all += $warn;
174 }
175
176 sub parse_dir()
177 {
178 my $file = $File::Find::name;
179
180 return if ($file =~ m/^\./);
181
182 return if (! ($file =~ m/\.c$/));
183
184 parse_file $file, 0;
185 }
186
187 sub sort_dir()
188 {
189 sort @_;
190 }
191
192 sub parse_rc_map_names($)
193 {
194 my $filename = shift;
195
196 $warn = 0;
197
198 printf "processing file $filename\n" if ($debug);
199 open IN, "<$filename" or die "couldn't find $filename";
200 while (<IN>) {
201 if (m/^\s*\#define\s+(RC_MAP[^\s]+)\s+\"(.*)\"/) {
202 $rc_map_names{$1} = $2;
203 }
204 }
205 }
206
207 # Main logic
208 #
209
210 parse_rc_map_names "$kernel_dir/include/media/rc-map.h";
211
212 open OUT_MAP, ">rc_maps.cfg";
213 print OUT_MAP << "EOF";
214 #
215 # Keymaps table
216 #
217 # This table creates an association between a keycode file and a kernel
218 # driver. It can be used to automatically override a keycode definition.
219 #
220 # Although not yet tested, it is mented to be added at udev.
221 #
222 # To use, you just need to run:
223 # ./ir-keytable -a
224 #
225 # Or, if the remote is not the first device:
226 # ./ir-keytable -a -s rc1 # for RC at rc1
227 #
228
229 # Format:
230 # driver - name of the driver provided via uevent - use * for any driver
231 # table - RC keymap table, provided via uevent - use * for any table
232 # file - file name. If directory is not specified, it will default to
233 # /etc/rc_keymaps.
234 # For example:
235 # driver table file
236 # cx8800 * ./keycodes/rc5_hauppauge_new.toml
237 # * rc-avermedia-m135a-rm-jx ./keycodes/kworld_315u.toml
238 # saa7134 rc-avermedia-m135a-rm-jx ./keycodes/keycodes/nec_terratec_cinergy_xs.toml
239 # em28xx * ./keycodes/kworld_315u.toml
240 # * * ./keycodes/rc5_hauppauge_new.toml
241
242 # Table to automatically load the rc maps for the bundled IR's provided with the
243 # devices supported by the linux kernel
244
245 #driver table file
246 EOF
247
248 find({wanted => \&parse_dir, preprocess => \&sort_dir, no_chdir => 1}, "$kernel_dir/drivers/media/rc/keymaps");
249
250 foreach my $file (@ir_files) {
251 parse_file "$kernel_dir/$file", 1;
252 }
253
254 printf STDERR "WARNING: there are %d tables not defined at rc_maps.h\n", $warn_all if ($warn_all);
255 close OUT_MAP;