summaryrefslogtreecommitdiff
path: root/libevdev/make-event-names.py
blob: 716c1d7f50ee79663fe657b0fb49899753ca5bb3 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# Parses linux/input.h scanning for #define KEY_FOO 134
# Prints C header files or Python files that can be used as
# mapping and lookup tables.
#

from __future__ import print_function
import re
import sys

class Bits(object):
	def __init__(self):
		self.max_codes = {}

prefixes = [
		"EV_",
		"REL_",
		"ABS_",
		"KEY_",
		"BTN_",
		"LED_",
		"SND_",
		"MSC_",
		"SW_",
		"FF_",
		"SYN_",
		"REP_",
		"INPUT_PROP_",
		"MT_TOOL_",
]

duplicates = [
		"EV_VERSION",
		"BTN_MISC",
		"BTN_MOUSE",
		"BTN_JOYSTICK",
		"BTN_GAMEPAD",
		"BTN_DIGI",
		"BTN_WHEEL",
		"BTN_TRIGGER_HAPPY",
		"SW_MAX",
		"REP_MAX",
]

btn_additional = [
		[0, "BTN_A"],
		[0, "BTN_B"],
		[0, "BTN_X"],
		[0, "BTN_Y"],
]

code_prefixes = [
		"REL_",
		"ABS_",
		"KEY_",
		"BTN_",
		"LED_",
		"SND_",
		"MSC_",
		"SW_",
		"FF_",
		"SYN_",
		"REP_",
]

def print_bits(bits, prefix):
	if  not hasattr(bits, prefix):
		return
	print("static const char * const %s_map[%s_MAX + 1] = {" % (prefix, prefix.upper()))
	for val, name in list(getattr(bits, prefix).items()):
		print("	[%s] = \"%s\"," % (name, name))
	if prefix == "key":
		for val, name in list(getattr(bits, "btn").items()):
			print("	[%s] = \"%s\"," % (name, name))
	print("};")
	print("")

def print_map(bits):
	print("static const char * const * const event_type_map[EV_MAX + 1] = {")

	for prefix in prefixes:
		if prefix in ["BTN_", "EV_", "INPUT_PROP_", "MT_TOOL_"]:
			continue
		print("	[EV_%s] = %s_map," % (prefix[:-1], prefix[:-1].lower()))

	print("};")
	print("")

	print("#if __clang__")
	print("#pragma clang diagnostic push")
	print("#pragma clang diagnostic ignored \"-Winitializer-overrides\"")
	print("#else")
	print("#pragma GCC diagnostic push")
	print("#pragma GCC diagnostic ignored \"-Woverride-init\"")
	print("#endif")
	print("static const int ev_max[EV_MAX + 1] = {")
	print("	[0 ... EV_MAX] = -1,")
	for prefix in prefixes:
		if prefix in ["BTN_", "EV_", "INPUT_PROP_", "MT_TOOL_"]:
			continue
		print("	[EV_%s] = %s_MAX," % (prefix[:-1], prefix[:-1]))
	print("};")
	print("#if __clang__")
	print("#pragma clang diagnostic pop /* \"-Winitializer-overrides\" */")
	print("#else")
	print("#pragma GCC diagnostic pop /* \"-Woverride-init\" */")
	print("#endif");
	print("")

def print_lookup(bits, prefix):
	if not hasattr(bits, prefix):
		return

	names = list(getattr(bits, prefix).items())
	if prefix == "btn":
		names = names + btn_additional;

	# We need to manually add the _MAX codes because some are
	# duplicates
	maxname = "%s_MAX" % (prefix.upper())
	if maxname in duplicates:
		names.append((bits.max_codes[maxname], maxname))

	for val, name in sorted(names, key=lambda e: e[1]):
		print("	{ .name = \"%s\", .value = %s }," % (name, name))

def print_lookup_table(bits):
	print("struct name_entry {")
	print("	const char *name;")
	print("	unsigned int value;")
	print("};")
	print("")
	print("static const struct name_entry tool_type_names[] = {")
	print_lookup(bits, "mt_tool")
	print("};")
	print("")
	print("static const struct name_entry ev_names[] = {")
	print_lookup(bits, "ev")
	print("};")
	print("")

	print("static const struct name_entry code_names[] = {")
	for prefix in sorted(code_prefixes, key=lambda e: e):
		print_lookup(bits, prefix[:-1].lower())
	print("};")
	print("")
	print("static const struct name_entry prop_names[] = {")
	print_lookup(bits, "input_prop")
	print("};")
	print("")

def print_mapping_table(bits):
	print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
	print("")
	print("#ifndef EVENT_NAMES_H")
	print("#define EVENT_NAMES_H")
	print("")

	for prefix in prefixes:
		if prefix == "BTN_":
			continue
		print_bits(bits, prefix[:-1].lower())

	print_map(bits)
	print_lookup_table(bits)

	print("#endif /* EVENT_NAMES_H */")

def parse_define(bits, line):
	m = re.match(r"^#define\s+(\w+)\s+(\w+)", line)
	if m == None:
		return

	name = m.group(1)

	try:
		value = int(m.group(2), 0)
	except ValueError:
		return

	for prefix in prefixes:
		if not name.startswith(prefix):
			continue

		if name.endswith("_MAX"):
			bits.max_codes[name] = value

		if name in duplicates:
			return

		attrname = prefix[:-1].lower()

		if not hasattr(bits, attrname):
			setattr(bits, attrname, {})
		b = getattr(bits, attrname)
		b[value] = name

def parse(fp):
	bits = Bits()

	lines = fp.readlines()
	for line in lines:
		if not line.startswith("#define"):
			continue
		parse_define(bits, line)

	return bits

def usage(prog):
	print("Usage: cat <files> | %s" % prog)

if __name__ == "__main__":
	if len(sys.argv) != 1:
		usage(sys.argv[0])
		sys.exit(2)

	bits = parse(sys.stdin)
	print_mapping_table(bits)