[PATCH libevdev 2/5] Create event type/code lookup tables

David Herrmann dh.herrmann at gmail.com
Fri Oct 4 11:37:32 PDT 2013


Additionally to type->string mapping tables we now also create
string->type lookup tables. The lookup tables are sorted
case-insensitively so binary-search will work.

As we need separate lookup tables for KEY_* and BTN_* codes, we first have
to remove the logic that merged both into the same group. Instead, we keep
both in different groups and merge them in the old mapping tables directly
when writing the bits.

Currently, only basic event-code tables are created. More tables (like
bus-names, input-properties, ..) can be added later.

Signed-off-by: David Herrmann <dh.herrmann at gmail.com>
---
 libevdev/make-event-names.py | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/libevdev/make-event-names.py b/libevdev/make-event-names.py
index e321bd7..f4d7a5a 100755
--- a/libevdev/make-event-names.py
+++ b/libevdev/make-event-names.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 # Parses linux/input.h scanning for #define KEY_FOO 134
-# Prints a C header file or a Python file that can be used as
-# mapping table
+# Prints C header files or Python files that can be used as
+# mapping and lookup tables.
 #
 
 from __future__ import print_function
@@ -47,6 +47,18 @@ def print_bits(bits, prefix):
 	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_lookup(bits, prefix):
+	if  not hasattr(bits, prefix):
+		return
+	print("static const struct name_entry %s_names[] = {" % (prefix))
+	for val, name in sorted(list(getattr(bits, prefix).items()), key=lambda e: e[1].lower()):
+		print("	{ .name = \"%s\", .value = %s }," % (name[len(prefix)+1:], name))
 	print("};")
 	print("")
 
@@ -57,6 +69,9 @@ def print_python_bits(bits, prefix):
 	print("%s_map = {" % (prefix))
 	for val, name in list(getattr(bits, prefix).items()):
 		print("	%d : \"%s\"," % (val, name))
+	if prefix == "key":
+		for val, name in list(getattr(bits, "btn").items()):
+			print("	%d : \"%s\"," % (val, name))
 	print("}")
 	print("for k, v in %s_map.items():" % (prefix))
 	print("	%s_map[v] = k" % (prefix))
@@ -115,6 +130,15 @@ def print_mapping_table(bits):
 
 	print_map(bits)
 
+	print("struct name_entry {")
+	print("	const char *name;")
+	print("	unsigned int value;")
+	print("};")
+	print("")
+
+	for prefix in prefixes:
+		print_lookup(bits, prefix[:-1].lower())
+
 	print("#endif /* EVENT_NAMES_H */")
 
 def print_python_mapping_table(bits):
@@ -158,8 +182,6 @@ def parse_define(bits, line):
 			continue
 
 		attrname = prefix[:-1].lower()
-		if attrname == "btn":
-			attrname = "key"
 
 		if not hasattr(bits, attrname):
 			setattr(bits, attrname, {})
-- 
1.8.4



More information about the Input-tools mailing list