tizen 2.4 release
[framework/uifw/libevdev.git] / libevdev / make-event-names.py
1 #!/usr/bin/env python
2 # Parses linux/input.h scanning for #define KEY_FOO 134
3 # Prints C header files or Python files that can be used as
4 # mapping and lookup tables.
5 #
6
7 from __future__ import print_function
8 import re
9 import sys
10
11 class Bits(object):
12         pass
13
14 prefixes = [
15                 "EV_",
16                 "REL_",
17                 "ABS_",
18                 "KEY_",
19                 "BTN_",
20                 "LED_",
21                 "SND_",
22                 "MSC_",
23                 "SW_",
24                 "FF_",
25                 "SYN_",
26                 "REP_",
27                 "INPUT_PROP_",
28 ]
29
30 blacklist = [
31                 "EV_VERSION",
32                 "BTN_MISC",
33                 "BTN_MOUSE",
34                 "BTN_JOYSTICK",
35                 "BTN_GAMEPAD",
36                 "BTN_DIGI",
37                 "BTN_WHEEL",
38                 "BTN_TRIGGER_HAPPY"
39 ]
40
41 btn_additional = [
42                 [0, "BTN_A"],
43                 [0, "BTN_B"],
44                 [0, "BTN_X"],
45                 [0, "BTN_Y"],
46 ]
47
48 names = [
49                 "REL_",
50                 "ABS_",
51                 "KEY_",
52                 "BTN_",
53                 "LED_",
54                 "SND_",
55                 "MSC_",
56                 "SW_",
57                 "FF_",
58                 "SYN_",
59                 "REP_",
60 ]
61
62 def print_bits(bits, prefix):
63         if  not hasattr(bits, prefix):
64                 return
65         print("static const char * const %s_map[%s_MAX + 1] = {" % (prefix, prefix.upper()))
66         for val, name in list(getattr(bits, prefix).items()):
67                 print(" [%s] = \"%s\"," % (name, name))
68         if prefix == "key":
69                 for val, name in list(getattr(bits, "btn").items()):
70                         print(" [%s] = \"%s\"," % (name, name))
71         print("};")
72         print("")
73
74 def print_map(bits):
75         print("static const char * const * const event_type_map[EV_MAX + 1] = {")
76
77         for prefix in prefixes:
78                 if prefix == "BTN_" or prefix == "EV_" or prefix == "INPUT_PROP_":
79                         continue
80                 print(" [EV_%s] = %s_map," % (prefix[:-1], prefix[:-1].lower()))
81
82         print("};")
83         print("")
84
85         print("#pragma GCC diagnostic push")
86         print("#pragma GCC diagnostic ignored \"-Woverride-init\"")
87         print("static const int ev_max[EV_MAX + 1] = {")
88         print(" [0 ... EV_MAX] = -1,")
89         for prefix in prefixes:
90                 if prefix == "BTN_" or prefix == "EV_" or prefix == "INPUT_PROP_":
91                         continue
92                 print(" [EV_%s] = %s_MAX," % (prefix[:-1], prefix[:-1]))
93         print("};")
94         print("#pragma GCC diagnostic pop /* \"-Woverride-init\" */")
95         print("")
96
97 def print_python_map(bits):
98         print("map = {")
99
100         for val, name in list(getattr(bits, "ev").items()):
101                 name = name[3:]
102                 if name == "REP" or name == "PWR"  or name == "FF_STATUS"  or name == "MAX":
103                         continue
104                 print(" %d : %s_map," % (val, name.lower()))
105
106         print("}")
107         print("")
108
109 def print_lookup(bits, prefix):
110         if not hasattr(bits, prefix):
111                 return
112
113         names = list(getattr(bits, prefix).items())
114         if prefix == "btn":
115                 names = names + btn_additional;
116
117         for val, name in sorted(names, key=lambda e: e[1]):
118                 print(" { .name = \"%s\", .value = %s }," % (name, name))
119
120 def print_lookup_table(bits):
121         print("struct name_entry {")
122         print(" const char *name;")
123         print(" unsigned int value;")
124         print("};")
125         print("")
126         print("static const struct name_entry ev_names[] = {")
127         print_lookup(bits, "ev")
128         print("};")
129         print("")
130
131         print("static const struct name_entry code_names[] = {")
132         for prefix in sorted(names, key=lambda e: e):
133                 print_lookup(bits, prefix[:-1].lower())
134         print("};")
135         print("")
136
137
138 def print_mapping_table(bits):
139         print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
140         print("")
141         print("#ifndef EVENT_NAMES_H")
142         print("#define EVENT_NAMES_H")
143         print("")
144
145         for prefix in prefixes:
146                 if prefix == "BTN_":
147                         continue
148                 print_bits(bits, prefix[:-1].lower())
149
150         print_map(bits)
151         print_lookup_table(bits)
152
153         print("#endif /* EVENT_NAMES_H */")
154
155 def parse_define(bits, line):
156         m = re.match(r"^#define\s+(\w+)\s+(\w+)", line)
157         if m == None:
158                 return
159
160         name = m.group(1)
161
162         if name in blacklist:
163                 return
164
165         try:
166                 value = int(m.group(2), 0)
167         except ValueError:
168                 return
169
170         for prefix in prefixes:
171                 if not name.startswith(prefix):
172                         continue
173
174                 attrname = prefix[:-1].lower()
175
176                 if not hasattr(bits, attrname):
177                         setattr(bits, attrname, {})
178                 b = getattr(bits, attrname)
179                 b[value] = name
180
181 def parse(fp):
182         bits = Bits()
183
184         lines = fp.readlines()
185         for line in lines:
186                 if not line.startswith("#define"):
187                         continue
188                 parse_define(bits, line)
189
190         return bits
191
192 def usage(prog):
193         print("Usage: %s /path/to/linux/input.h" % prog)
194
195 if __name__ == "__main__":
196         if len(sys.argv) != 2:
197                 usage(sys.argv[0])
198                 sys.exit(2)
199
200         with open(sys.argv[1]) as f:
201                 bits = parse(f)
202                 print_mapping_table(bits)