[PATCH libevdev] Add libevdev_property_from_name()

Peter Hutterer peter.hutterer at who-t.net
Sun Aug 17 21:24:12 PDT 2014


12717d79 "Add libevdev_event_type/code_from_name() resolvers" added the
lookup functions for types and codes, this commit adds the missing ones for
input properties.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 libevdev/libevdev-names.c    | 20 ++++++++++++++++++++
 libevdev/libevdev.h          | 30 ++++++++++++++++++++++++++++++
 libevdev/libevdev.sym        |  2 ++
 libevdev/make-event-names.py |  5 ++++-
 test/test-event-codes.c      | 37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/libevdev/libevdev-names.c b/libevdev/libevdev-names.c
index 10669a3..ad318de 100644
--- a/libevdev/libevdev-names.c
+++ b/libevdev/libevdev-names.c
@@ -141,3 +141,23 @@ libevdev_event_code_from_name_n(unsigned int type, const char *name, size_t len)
 
 	return entry ? (int)entry->value : -1;
 }
+
+LIBEVDEV_EXPORT int
+libevdev_property_from_name(const char *name)
+{
+	return libevdev_property_from_name_n(name, strlen(name));
+}
+
+LIBEVDEV_EXPORT int
+libevdev_property_from_name_n(const char *name, size_t len)
+{
+	struct name_lookup lookup;
+	const struct name_entry *entry;
+
+	lookup.name = name;
+	lookup.len = len;
+
+	entry = lookup_name(prop_names, ARRAY_LENGTH(prop_names), &lookup);
+
+	return entry ? (int)entry->value : -1;
+}
diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h
index 4f33cb7..321f980 100644
--- a/libevdev/libevdev.h
+++ b/libevdev/libevdev.h
@@ -2085,6 +2085,36 @@ int libevdev_event_code_from_name_n(unsigned int type, const char *name,
 				    size_t len);
 
 /**
+ * @ingroup misc
+ *
+ * Look up an input property by its name. Properties start with the fixed
+ * prefix "INPUT_PROP_" followed by their name (eg., "INPUT_PROP_POINTER").
+ * The prefix must be included in the name. It returns the constant assigned
+ * to the property or -1 if not found.
+ *
+ * @param name A non-NULL string describing an input property
+ *
+ * @return The given code constant for the name or -1 if not found.
+ */
+int libevdev_property_from_name(const char *name);
+
+/**
+ * @ingroup misc
+ *
+ * Look up an input property by its name. Properties start with the fixed
+ * prefix "INPUT_PROP_" followed by their name (eg., "INPUT_PROP_POINTER").
+ * The prefix must be included in the name. It returns the constant assigned
+ * to the property or -1 if not found.
+ *
+ * @param name A non-NULL string describing an input property
+ * @param len The length of the string in @p name excluding any terminating 0
+ * character.
+ *
+ * @return The given code constant for the name or -1 if not found.
+ */
+int libevdev_property_from_name_n(const char *name, size_t len);
+
+/**
  * @ingroup bits
  *
  * Get the repeat delay and repeat period values for this device. This
diff --git a/libevdev/libevdev.sym b/libevdev/libevdev.sym
index 228e555..374712f 100644
--- a/libevdev/libevdev.sym
+++ b/libevdev/libevdev.sym
@@ -106,6 +106,8 @@ local:
 LIBEVDEV_1_3 {
 global:
 	libevdev_set_device_log_function;
+	libevdev_property_from_name;
+	libevdev_property_from_name_n;
 
 local:
 	*;
diff --git a/libevdev/make-event-names.py b/libevdev/make-event-names.py
index 149f093..980283f 100755
--- a/libevdev/make-event-names.py
+++ b/libevdev/make-event-names.py
@@ -133,7 +133,10 @@ def print_lookup_table(bits):
 		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 */")
diff --git a/test/test-event-codes.c b/test/test-event-codes.c
index ac2f738..b64940e 100644
--- a/test/test-event-codes.c
+++ b/test/test-event-codes.c
@@ -99,6 +99,38 @@ START_TEST(test_key_invalid)
 }
 END_TEST
 
+START_TEST(test_properties)
+{
+	struct prop {
+		int val;
+		const char *name;
+	} lut[] = {
+		{ INPUT_PROP_DIRECT, "INPUT_PROP_DIRECT" },
+		{ INPUT_PROP_POINTER, "INPUT_PROP_POINTER" },
+		{ INPUT_PROP_MAX, "INPUT_PROP_MAX" },
+		{ -1, NULL}
+	};
+	struct prop *p = lut;
+	while (p->val != -1) {
+		ck_assert_int_eq(libevdev_property_from_name(p->name), p->val);
+		p++;
+	}
+}
+END_TEST
+
+START_TEST(test_properties_invalid)
+{
+	ck_assert_int_eq(libevdev_property_from_name("EV_ABS"), -1);
+	ck_assert_int_eq(libevdev_property_from_name("INPUT_PROP"), -1);
+	ck_assert_int_eq(libevdev_property_from_name("INPUT_PROP_"), -1);
+	ck_assert_int_eq(libevdev_property_from_name("INPUT_PROP_FOO"), -1);
+
+	ck_assert_int_eq(libevdev_property_from_name_n("INPUT_PROP_POINTER", 11), -1);
+	ck_assert_int_eq(libevdev_property_from_name_n("INPUT_PROP_POINTER",
+						strlen("INPUT_PROP_POINTER") - 1), -1);
+}
+END_TEST
+
 Suite *
 event_code_suite(void)
 {
@@ -114,5 +146,10 @@ event_code_suite(void)
 	tcase_add_test(tc, test_key_invalid);
 	suite_add_tcase(s, tc);
 
+	tc = tcase_create("property tests");
+	tcase_add_test(tc, test_properties);
+	tcase_add_test(tc, test_properties_invalid);
+	suite_add_tcase(s, tc);
+
 	return s;
 }
-- 
1.9.3



More information about the Input-tools mailing list