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

Peter Hutterer peter.hutterer at who-t.net
Tue Oct 29 01:57:49 CET 2013


On Mon, Oct 28, 2013 at 05:16:45PM +0100, David Herrmann wrote:
> Additionally to type->string mapping tables we now also create
> string->type lookup tables. The lookup tables are sorted by their name
> so binary-search will work.
> 
> We create one lookup table for EV_* types and one for all event-codes.
> 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 | 60 +++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 56 insertions(+), 4 deletions(-)
> 
> diff --git a/libevdev/make-event-names.py b/libevdev/make-event-names.py
> index b1db9bc..e8e8321 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
> @@ -39,12 +39,36 @@ blacklist = [
>  		"BTN_TRIGGER_HAPPY"
>  ]
>  
> +btn_additional = [
> +		[0, "BTN_A"],
> +		[0, "BTN_B"],
> +		[0, "BTN_X"],
> +		[0, "BTN_Y"],
> +]
> +
> +names = [
> +		"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("")
>  
> @@ -55,6 +79,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))
> @@ -95,6 +122,17 @@ def print_python_map(bits):
>  	print("}")
>  	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;
> +
> +	for val, name in sorted(names, key=lambda e: e[1]):
> +		print("	{ .name = \"%s\", .value = %s }," % (name, name))
> +
>  def print_mapping_table(bits):
>  	print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
>  	print("")
> @@ -113,6 +151,22 @@ def print_mapping_table(bits):
>  
>  	print_map(bits)
>  
> +	print("struct name_entry {")
> +	print("	const char *name;")
> +	print("	unsigned int value;")
> +	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(names, key=lambda e: e):
> +		print_lookup(bits, prefix[:-1].lower())
> +	print("};")
> +	print("")
> +

I've moved that into a separate function, left unchanged otherwise.

Cheers,
   Peter

>  	print("#endif /* EVENT_NAMES_H */")
>  
>  def print_python_mapping_table(bits):
> @@ -156,8 +210,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.1
> 


More information about the Input-tools mailing list