aboutsummaryrefslogtreecommitdiff
path: root/make-arrays-from-input.h.pl
blob: 710d517c688a477212276b5f664aa2f5298341c7 (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
#!/usr/bin/perl -w

use Switch;

#========================================================================
# FUNCTIONS
#========================================================================

#------------------------------------------------------------------------
# parse out the types and codes from a line from the header 
# usage:  @dataArray getDataFromHeaderLine($lineFromHeader);
sub getDataFromHeaderLine 
{
	 $_ = shift;

	 my @returnArray;

	 if ( m/#define ([A-Z0-9_]*)\s+(0x)?([0-9a-f]+)/) 
	 { 
		  if ($2) { $index = hex($3); } else { $index = $3; }
		  if ($index >=0) 
		  {
				$returnArray[0] = $index;
				$returnArray[1] = lc("$1");
				return @returnArray;
		  }
#		  print "$1 \t\t\t $index   $#returnArray\n "; 
#		  if ($index == 0) { print("index: $index   3: $3  2: $2   1: $1   value: $returnArray[1]\n"); }
	 } 
}

#------------------------------------------------------------------------
# print an array out in C format
#
sub printCArray
{
	 my @arrayToPrint = @_;

#	 print("$arrayToPrint[0] $#arrayToPrint \n");

	 print("int ${arrayToPrint[0]}_total = $#arrayToPrint;  /* # of elements in array */\n");
	 print("char *${arrayToPrint[0]}[$#arrayToPrint] = {");

	 for($i = 1; $i < $#arrayToPrint; $i++)
	 {
		  # format nicely in sets of 6
		  if ( ($i+4)%6 == 5 ) { print("\n       "); }
		  # if the array element's data is null, print NULL
		  if ($arrayToPrint[$i]) { print("\"$arrayToPrint[$i]\","); }
		  else { print("NULL,"); }
	 }

	 print("\"$arrayToPrint[$#arrayToPrint]\"\n };\n\n\n");
}

#------------------------------------------------------------------------
# print an array out in a comment table in Pd
#
sub printPdFile
{
	 my @arrayToPrint = @_;
	 my $x;
	 my $y;
	 my $lineNum = 1;

	 my $PDFILENAME = "$arrayToPrint[0]-list.pd";
	 open(PDFILE, ">$PDFILENAME");

	 print(PDFILE "#N canvas 282 80 210 570 10;\n");
	 if ($arrayToPrint[0] eq "ev") { print(PDFILE "#X text 5 5 Event Types;\n"); }
	 else { print(PDFILE "#X text 5 5 Codes for Type: $arrayToPrint[0];\n"); }
	 print(PDFILE "#X text 5 20 ----------------------------;\n");

	 for($i = 1; $i <= $#arrayToPrint; $i++)
	 {
		  # if the array element's data is null, print NULL
		  if ($arrayToPrint[$i]) 
		  { 
				$x = 5;
				$y = $lineNum * 20 + 20;
				print(PDFILE "#X text $x $y $arrayToPrint[$i];\n"); 
				$lineNum++;
		  }
	 }

	 close(PDFILE);
}

#========================================================================
# MAIN
#========================================================================

# source file
$SOURCEFILENAME = "linux/input.h";
open(INPUT_H, "<$SOURCEFILENAME");

# output files
$ARRAYSFILENAME = "input_arrays.h";
open(ARRAYS, ">$ARRAYSFILENAME");

while (<INPUT_H>)
{
	 if (m/\#define (FF_STATUS|[A-Z_]*?)_/)
	 {
# filter EV_VERSION and *_MAX
		  m/\#define\s+(EV_VERSION|[A-Z_]+_MAX)\s+/;
#		  print "$1 \n";
		  switch ($1) 
		  {
				# types
				case "EV"        { ($index, $value) = getDataFromHeaderLine($_); $EV[$index] = $value; }
				# codes
				case "SYN"       { ($index, $value) = getDataFromHeaderLine($_); $SYN[$index] = $value; }
				case "KEY"       { ($index, $value) = getDataFromHeaderLine($_); $KEY[$index] = $value; }
# BTN codes are actually part of the KEY type
				case "BTN"       { ($index, $value) = getDataFromHeaderLine($_); $KEY[$index] = $value; }
				case "REL"       { ($index, $value) = getDataFromHeaderLine($_); $REL[$index] = $value; }
				case "ABS"       { ($index, $value) = getDataFromHeaderLine($_); $ABS[$index] = $value; }
				case "MSC"       { ($index, $value) = getDataFromHeaderLine($_); $MSC[$index] = $value; }
				case "LED"       { ($index, $value) = getDataFromHeaderLine($_); $LED[$index] = $value; }
				case "SND"       { ($index, $value) = getDataFromHeaderLine($_); $SND[$index] = $value; }
				case "REP"       { ($index, $value) = getDataFromHeaderLine($_); $REP[$index] = $value; }
				case "FF"        { ($index, $value) = getDataFromHeaderLine($_); $FF[$index] = $value; }
# there doesn't seem to be any PWR events yet...
#				case "PWR"       { ($index, $value) = getDataFromHeaderLine($_); $PWR[$index] = $value; }
				case "FF_STATUS" { ($index, $value) = getDataFromHeaderLine($_); $FF_STATUS[$index] = $value; }
#				else { print " none $_"; } 
		  }
	 }
}

# generate a .pd file for each array
printPdFile("ev",@EV);
printPdFile("ev_syn",@SYN);
printPdFile("ev_key",@KEY);
printPdFile("ev_rel",@REL);
printPdFile("ev_abs",@ABS);
printPdFile("ev_msc",@MSC);
printPdFile("ev_led",@LED);
printPdFile("ev_snd",@SND);
printPdFile("ev_rep",@REP);
printPdFile("ev_ff",@FF);
# there doesn't seem to be any PWR events yet...
#printPdFile("pwr",@PWR);
printPdFile("ev_ff_status",@FF_STATUS);

# generate a C array for each array and stick them all in the same file
select ARRAYS;
printCArray("ev",@EV);
printCArray("ev_syn",@SYN);
printCArray("ev_key",@KEY);
printCArray("ev_rel",@REL);
printCArray("ev_abs",@ABS);
printCArray("ev_msc",@MSC);
printCArray("ev_led",@LED);
printCArray("ev_snd",@SND);
printCArray("ev_rep",@REP);
printCArray("ev_ff",@FF);
# there doesn't seem to be any PWR events yet...
#printCArray("pwr",@PWR);
print("char *ev_pwr[1] = { NULL };\n\n");
printCArray("ev_ff_status",@FF_STATUS);

# print array of arrays
print("char **event_names[",$#EV+1,"] = {");
for($i = 0; $i < $#EV; $i++)
{
	 # format nicely in sets of 6
	 if ( ($i+4)%6 == 5 ) { print("\n       "); }

	 # if the array element's data is null, print NULL
	 if ($EV[$i]) 
	 { 
		  $_ = $EV[$i];
		  m/(ev_[a-z_]+)/;
		  print("$1,");  
	 }
	 else { print("NULL,"); }
}
$_ = $EV[$#EV];
m/(ev_[a-z_]+)/;
print("$1\n };\n");


# print "EV: $#EV \n";
# print "SYN: $#SYN \n";
# print "KEY: $#KEY \n";
# print "REL: $#REL \n";
# print "ABS: $#ABS \n";
# print "MSC: $#MSC \n";
# print "LED: $#LED \n";
# print "SND: $#SND \n";
# print "REP: $#REP \n";
# print "FF: $#FF \n";
# #print "PWR: $#PWR \n";
# print "FF_STATUS: $#FF_STATUS \n";

close(ARRAYS);
close(INPUT_H);